Skip to content

Commit

Permalink
Merge pull request #14632 from clarkfw/functional-options-pattern-of-…
Browse files Browse the repository at this point in the history
…cluster-config

tests: add a ClusterOption interface for ClusterSize in config.Cluster
  • Loading branch information
serathius authored Oct 27, 2022
2 parents b9c0684 + e28b66f commit c45f338
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 24 deletions.
8 changes: 6 additions & 2 deletions tests/common/alarm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ func TestAlarm(t *testing.T) {
testRunner.BeforeTest(t)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
clus := testRunner.NewCluster(ctx, t, config.NewClusterConfig(1, config.WithQuotaBackendBytes(int64(13*os.Getpagesize()))))
cfg := config.NewClusterConfig(
config.WithClusterSize(1),
config.WithQuotaBackendBytes(int64(13*os.Getpagesize())),
)
clus := testRunner.NewCluster(ctx, t, cfg)
defer clus.Close()
cc := framework.MustClient(clus.Client())
testutils.ExecuteUntil(ctx, t, func() {
Expand Down Expand Up @@ -112,7 +116,7 @@ func TestAlarmlistOnMemberRestart(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
clus := testRunner.NewCluster(ctx, t, config.NewClusterConfig(
1,
config.WithClusterSize(1),
config.WithQuotaBackendBytes(int64(13*os.Getpagesize())),
config.WithSnapshotCount(5),
))
Expand Down
2 changes: 1 addition & 1 deletion tests/common/compact_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestCompact(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
clus := testRunner.NewCluster(ctx, t, config.NewClusterConfig(3))
clus := testRunner.NewCluster(ctx, t, config.DefaultClusterConfig())
defer clus.Close()
cc := framework.MustClient(clus.Client())
testutils.ExecuteUntil(ctx, t, func() {
Expand Down
2 changes: 1 addition & 1 deletion tests/common/defrag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestDefragOnline(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
options := config.DefragOption{Timeout: 10 * time.Second}
clus := testRunner.NewCluster(ctx, t, config.NewClusterConfig(3))
clus := testRunner.NewCluster(ctx, t, config.DefaultClusterConfig())
cc := framework.MustClient(clus.Client())
testutils.ExecuteUntil(ctx, t, func() {
defer clus.Close()
Expand Down
6 changes: 3 additions & 3 deletions tests/common/endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestEndpointStatus(t *testing.T) {
testRunner.BeforeTest(t)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
clus := testRunner.NewCluster(ctx, t, config.NewClusterConfig(3))
clus := testRunner.NewCluster(ctx, t, config.DefaultClusterConfig())
defer clus.Close()
cc := framework.MustClient(clus.Client())
testutils.ExecuteUntil(ctx, t, func() {
Expand All @@ -43,7 +43,7 @@ func TestEndpointHashKV(t *testing.T) {
testRunner.BeforeTest(t)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
clus := testRunner.NewCluster(ctx, t, config.NewClusterConfig(3))
clus := testRunner.NewCluster(ctx, t, config.DefaultClusterConfig())
defer clus.Close()
cc := framework.MustClient(clus.Client())
testutils.ExecuteUntil(ctx, t, func() {
Expand All @@ -58,7 +58,7 @@ func TestEndpointHealth(t *testing.T) {
testRunner.BeforeTest(t)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
clus := testRunner.NewCluster(ctx, t, config.NewClusterConfig(3))
clus := testRunner.NewCluster(ctx, t, config.DefaultClusterConfig())
defer clus.Close()
cc := framework.MustClient(clus.Client())
testutils.ExecuteUntil(ctx, t, func() {
Expand Down
2 changes: 1 addition & 1 deletion tests/common/kv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ func TestKVGetNoQuorum(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
clus := testRunner.NewCluster(ctx, t, config.NewClusterConfig(3))
clus := testRunner.NewCluster(ctx, t, config.DefaultClusterConfig())
defer clus.Close()

clus.Members()[0].Stop()
Expand Down
10 changes: 5 additions & 5 deletions tests/common/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,23 @@ var testRunner framework.TestRunner
var clusterTestCases = []testCase{
{
name: "NoTLS",
config: config.NewClusterConfig(1),
config: config.NewClusterConfig(config.WithClusterSize(1)),
},
{
name: "PeerTLS",
config: config.NewClusterConfig(3, config.WithPeerTLS(config.ManualTLS)),
config: config.NewClusterConfig(config.WithPeerTLS(config.ManualTLS)),
},
{
name: "PeerAutoTLS",
config: config.NewClusterConfig(3, config.WithPeerTLS(config.AutoTLS)),
config: config.NewClusterConfig(config.WithPeerTLS(config.AutoTLS)),
},
{
name: "ClientTLS",
config: config.NewClusterConfig(1, config.WithClientTLS(config.ManualTLS)),
config: config.NewClusterConfig(config.WithClusterSize(1), config.WithClientTLS(config.ManualTLS)),
},
{
name: "ClientAutoTLS",
config: config.NewClusterConfig(1, config.WithClientTLS(config.AutoTLS)),
config: config.NewClusterConfig(config.WithClusterSize(1), config.WithClientTLS(config.AutoTLS)),
},
}

Expand Down
8 changes: 4 additions & 4 deletions tests/common/role_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestRoleAdd_Error(t *testing.T) {
testRunner.BeforeTest(t)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
clus := testRunner.NewCluster(ctx, t, config.NewClusterConfig(1))
clus := testRunner.NewCluster(ctx, t, config.NewClusterConfig(config.WithClusterSize(1)))
defer clus.Close()
cc := framework.MustClient(clus.Client())
testutils.ExecuteUntil(ctx, t, func() {
Expand All @@ -74,7 +74,7 @@ func TestRootRole(t *testing.T) {
testRunner.BeforeTest(t)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
clus := testRunner.NewCluster(ctx, t, config.NewClusterConfig(1))
clus := testRunner.NewCluster(ctx, t, config.NewClusterConfig(config.WithClusterSize(1)))
defer clus.Close()
cc := framework.MustClient(clus.Client())
testutils.ExecuteUntil(ctx, t, func() {
Expand Down Expand Up @@ -104,7 +104,7 @@ func TestRoleGrantRevokePermission(t *testing.T) {
testRunner.BeforeTest(t)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
clus := testRunner.NewCluster(ctx, t, config.NewClusterConfig(1))
clus := testRunner.NewCluster(ctx, t, config.NewClusterConfig(config.WithClusterSize(1)))
defer clus.Close()
cc := framework.MustClient(clus.Client())
testutils.ExecuteUntil(ctx, t, func() {
Expand Down Expand Up @@ -139,7 +139,7 @@ func TestRoleDelete(t *testing.T) {
testRunner.BeforeTest(t)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
clus := testRunner.NewCluster(ctx, t, config.NewClusterConfig(1))
clus := testRunner.NewCluster(ctx, t, config.NewClusterConfig(config.WithClusterSize(1)))
defer clus.Close()
cc := framework.MustClient(clus.Client())
testutils.ExecuteUntil(ctx, t, func() {
Expand Down
4 changes: 2 additions & 2 deletions tests/common/wait_leader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ func TestWaitLeader_MemberStop(t *testing.T) {
tcs := []testCase{
{
name: "PeerTLS",
config: config.NewClusterConfig(3, config.WithPeerTLS(config.ManualTLS)),
config: config.NewClusterConfig(config.WithPeerTLS(config.ManualTLS)),
},
{
name: "PeerAutoTLS",
config: config.NewClusterConfig(3, config.WithPeerTLS(config.AutoTLS)),
config: config.NewClusterConfig(config.WithPeerTLS(config.AutoTLS)),
},
}

Expand Down
16 changes: 11 additions & 5 deletions tests/framework/config/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@ type ClusterConfig struct {
SnapshotCount int
}

func defaultClusterConfig() ClusterConfig {
return ClusterConfig{StrictReconfigCheck: true}
func DefaultClusterConfig() ClusterConfig {
return ClusterConfig{
ClusterSize: 3,
StrictReconfigCheck: true,
}
}

func NewClusterConfig(clusterSize int, opts ...ClusterOption) ClusterConfig {
c := defaultClusterConfig()
c.ClusterSize = clusterSize
func NewClusterConfig(opts ...ClusterOption) ClusterConfig {
c := DefaultClusterConfig()
for _, opt := range opts {
opt(&c)
}
Expand All @@ -51,6 +53,10 @@ func NewClusterConfig(clusterSize int, opts ...ClusterOption) ClusterConfig {

type ClusterOption func(*ClusterConfig)

func WithClusterSize(size int) ClusterOption {
return func(c *ClusterConfig) { c.ClusterSize = size }
}

func WithPeerTLS(tls TLSConfig) ClusterOption {
return func(c *ClusterConfig) { c.PeerTLS = tls }
}
Expand Down

0 comments on commit c45f338

Please sign in to comment.