From 9beb29916e66034fbbce2a34aabe20f006c628f7 Mon Sep 17 00:00:00 2001 From: Marek Siarkowicz Date: Fri, 13 May 2022 14:28:04 +0200 Subject: [PATCH] tests: Extract cluster test cases --- tests/common/kv_test.go | 81 +--------------- tests/common/lease_test.go | 108 +--------------------- tests/common/main_test.go | 28 ++++++ tests/common/role_test.go | 27 +----- tests/common/txn_test.go | 58 +----------- tests/common/user_test.go | 185 ++++++------------------------------- 6 files changed, 70 insertions(+), 417 deletions(-) diff --git a/tests/common/kv_test.go b/tests/common/kv_test.go index 16a254b47..aab344fc3 100644 --- a/tests/common/kv_test.go +++ b/tests/common/kv_test.go @@ -26,32 +26,7 @@ import ( func TestKVPut(t *testing.T) { testRunner.BeforeTest(t) - tcs := []struct { - name string - config config.ClusterConfig - }{ - { - name: "NoTLS", - config: config.ClusterConfig{ClusterSize: 1}, - }, - { - name: "PeerTLS", - config: config.ClusterConfig{ClusterSize: 3, PeerTLS: config.ManualTLS}, - }, - { - name: "PeerAutoTLS", - config: config.ClusterConfig{ClusterSize: 3, PeerTLS: config.AutoTLS}, - }, - { - name: "ClientTLS", - config: config.ClusterConfig{ClusterSize: 1, ClientTLS: config.ManualTLS}, - }, - { - name: "ClientAutoTLS", - config: config.ClusterConfig{ClusterSize: 1, ClientTLS: config.AutoTLS}, - }, - } - for _, tc := range tcs { + for _, tc := range clusterTestCases { t.Run(tc.name, func(t *testing.T) { clus := testRunner.NewCluster(t, tc.config) defer clus.Close() @@ -83,32 +58,7 @@ func TestKVPut(t *testing.T) { func TestKVGet(t *testing.T) { testRunner.BeforeTest(t) - tcs := []struct { - name string - config config.ClusterConfig - }{ - { - name: "NoTLS", - config: config.ClusterConfig{ClusterSize: 1}, - }, - { - name: "PeerTLS", - config: config.ClusterConfig{ClusterSize: 3, PeerTLS: config.ManualTLS}, - }, - { - name: "PeerAutoTLS", - config: config.ClusterConfig{ClusterSize: 3, PeerTLS: config.AutoTLS}, - }, - { - name: "ClientTLS", - config: config.ClusterConfig{ClusterSize: 1, ClientTLS: config.ManualTLS}, - }, - { - name: "ClientAutoTLS", - config: config.ClusterConfig{ClusterSize: 1, ClientTLS: config.AutoTLS}, - }, - } - for _, tc := range tcs { + for _, tc := range clusterTestCases { t.Run(tc.name, func(t *testing.T) { clus := testRunner.NewCluster(t, tc.config) defer clus.Close() @@ -166,32 +116,7 @@ func TestKVGet(t *testing.T) { func TestKVDelete(t *testing.T) { testRunner.BeforeTest(t) - tcs := []struct { - name string - config config.ClusterConfig - }{ - { - name: "NoTLS", - config: config.ClusterConfig{ClusterSize: 1}, - }, - { - name: "PeerTLS", - config: config.ClusterConfig{ClusterSize: 3, PeerTLS: config.ManualTLS}, - }, - { - name: "PeerAutoTLS", - config: config.ClusterConfig{ClusterSize: 3, PeerTLS: config.AutoTLS}, - }, - { - name: "ClientTLS", - config: config.ClusterConfig{ClusterSize: 1, ClientTLS: config.ManualTLS}, - }, - { - name: "ClientAutoTLS", - config: config.ClusterConfig{ClusterSize: 1, ClientTLS: config.AutoTLS}, - }, - } - for _, tc := range tcs { + for _, tc := range clusterTestCases { t.Run(tc.name, func(t *testing.T) { clus := testRunner.NewCluster(t, tc.config) defer clus.Close() diff --git a/tests/common/lease_test.go b/tests/common/lease_test.go index 24805fb96..e3fd4d6ec 100644 --- a/tests/common/lease_test.go +++ b/tests/common/lease_test.go @@ -74,32 +74,7 @@ func TestLeaseGrantTimeToLive(t *testing.T) { func TestLeaseGrantAndList(t *testing.T) { testRunner.BeforeTest(t) - tcs := []struct { - name string - config config.ClusterConfig - }{ - { - name: "NoTLS", - config: config.ClusterConfig{ClusterSize: 1}, - }, - { - name: "PeerTLS", - config: config.ClusterConfig{ClusterSize: 3, PeerTLS: config.ManualTLS}, - }, - { - name: "PeerAutoTLS", - config: config.ClusterConfig{ClusterSize: 3, PeerTLS: config.AutoTLS}, - }, - { - name: "ClientTLS", - config: config.ClusterConfig{ClusterSize: 1, ClientTLS: config.ManualTLS}, - }, - { - name: "ClientAutoTLS", - config: config.ClusterConfig{ClusterSize: 1, ClientTLS: config.AutoTLS}, - }, - } - for _, tc := range tcs { + for _, tc := range clusterTestCases { nestedCases := []struct { name string leaseCount int @@ -164,32 +139,7 @@ func TestLeaseGrantAndList(t *testing.T) { func TestLeaseGrantTimeToLiveExpired(t *testing.T) { testRunner.BeforeTest(t) - tcs := []struct { - name string - config config.ClusterConfig - }{ - { - name: "NoTLS", - config: config.ClusterConfig{ClusterSize: 1}, - }, - { - name: "PeerTLS", - config: config.ClusterConfig{ClusterSize: 3, PeerTLS: config.ManualTLS}, - }, - { - name: "PeerAutoTLS", - config: config.ClusterConfig{ClusterSize: 3, PeerTLS: config.AutoTLS}, - }, - { - name: "ClientTLS", - config: config.ClusterConfig{ClusterSize: 1, ClientTLS: config.ManualTLS}, - }, - { - name: "ClientAutoTLS", - config: config.ClusterConfig{ClusterSize: 1, ClientTLS: config.AutoTLS}, - }, - } - for _, tc := range tcs { + for _, tc := range clusterTestCases { t.Run(tc.name, func(t *testing.T) { clus := testRunner.NewCluster(t, tc.config) defer clus.Close() @@ -224,32 +174,7 @@ func TestLeaseGrantTimeToLiveExpired(t *testing.T) { func TestLeaseGrantKeepAliveOnce(t *testing.T) { testRunner.BeforeTest(t) - tcs := []struct { - name string - config config.ClusterConfig - }{ - { - name: "NoTLS", - config: config.ClusterConfig{ClusterSize: 1}, - }, - { - name: "PeerTLS", - config: config.ClusterConfig{ClusterSize: 3, PeerTLS: config.ManualTLS}, - }, - { - name: "PeerAutoTLS", - config: config.ClusterConfig{ClusterSize: 3, PeerTLS: config.AutoTLS}, - }, - { - name: "ClientTLS", - config: config.ClusterConfig{ClusterSize: 1, ClientTLS: config.ManualTLS}, - }, - { - name: "ClientAutoTLS", - config: config.ClusterConfig{ClusterSize: 1, ClientTLS: config.AutoTLS}, - }, - } - for _, tc := range tcs { + for _, tc := range clusterTestCases { t.Run(tc.name, func(t *testing.T) { clus := testRunner.NewCluster(t, tc.config) defer clus.Close() @@ -276,32 +201,7 @@ func TestLeaseGrantKeepAliveOnce(t *testing.T) { func TestLeaseGrantRevoke(t *testing.T) { testRunner.BeforeTest(t) - tcs := []struct { - name string - config config.ClusterConfig - }{ - { - name: "NoTLS", - config: config.ClusterConfig{ClusterSize: 1}, - }, - { - name: "PeerTLS", - config: config.ClusterConfig{ClusterSize: 3, PeerTLS: config.ManualTLS}, - }, - { - name: "PeerAutoTLS", - config: config.ClusterConfig{ClusterSize: 3, PeerTLS: config.AutoTLS}, - }, - { - name: "ClientTLS", - config: config.ClusterConfig{ClusterSize: 1, ClientTLS: config.ManualTLS}, - }, - { - name: "ClientAutoTLS", - config: config.ClusterConfig{ClusterSize: 1, ClientTLS: config.AutoTLS}, - }, - } - for _, tc := range tcs { + for _, tc := range clusterTestCases { t.Run(tc.name, func(t *testing.T) { clus := testRunner.NewCluster(t, tc.config) defer clus.Close() diff --git a/tests/common/main_test.go b/tests/common/main_test.go index 73dbae69d..d03f120e0 100644 --- a/tests/common/main_test.go +++ b/tests/common/main_test.go @@ -18,10 +18,38 @@ import ( "testing" "go.etcd.io/etcd/tests/v3/framework" + "go.etcd.io/etcd/tests/v3/framework/config" ) var testRunner = framework.UnitTestRunner +var clusterTestCases = []testCase{ + { + name: "NoTLS", + config: config.ClusterConfig{ClusterSize: 1}, + }, + { + name: "PeerTLS", + config: config.ClusterConfig{ClusterSize: 3, PeerTLS: config.ManualTLS}, + }, + { + name: "PeerAutoTLS", + config: config.ClusterConfig{ClusterSize: 3, PeerTLS: config.AutoTLS}, + }, + { + name: "ClientTLS", + config: config.ClusterConfig{ClusterSize: 1, ClientTLS: config.ManualTLS}, + }, + { + name: "ClientAutoTLS", + config: config.ClusterConfig{ClusterSize: 1, ClientTLS: config.AutoTLS}, + }, +} func TestMain(m *testing.M) { testRunner.TestMain(m) } + +type testCase struct { + name string + config config.ClusterConfig +} diff --git a/tests/common/role_test.go b/tests/common/role_test.go index 3cd8cad1e..d2e7ada24 100644 --- a/tests/common/role_test.go +++ b/tests/common/role_test.go @@ -27,32 +27,7 @@ import ( func TestRoleAdd_Simple(t *testing.T) { testRunner.BeforeTest(t) - tcs := []struct { - name string - config config.ClusterConfig - }{ - { - name: "NoTLS", - config: config.ClusterConfig{ClusterSize: 1}, - }, - { - name: "PeerTLS", - config: config.ClusterConfig{ClusterSize: 3, PeerTLS: config.ManualTLS}, - }, - { - name: "PeerAutoTLS", - config: config.ClusterConfig{ClusterSize: 3, PeerTLS: config.AutoTLS}, - }, - { - name: "ClientTLS", - config: config.ClusterConfig{ClusterSize: 1, ClientTLS: config.ManualTLS}, - }, - { - name: "ClientAutoTLS", - config: config.ClusterConfig{ClusterSize: 1, ClientTLS: config.AutoTLS}, - }, - } - for _, tc := range tcs { + for _, tc := range clusterTestCases { t.Run(tc.name, func(t *testing.T) { clus := testRunner.NewCluster(t, tc.config) defer clus.Close() diff --git a/tests/common/txn_test.go b/tests/common/txn_test.go index 3e982b89c..5003ac5f6 100644 --- a/tests/common/txn_test.go +++ b/tests/common/txn_test.go @@ -34,31 +34,7 @@ type txnReq struct { } func TestTxnSucc(t *testing.T) { - tcs := []struct { - name string - config config.ClusterConfig - }{ - { - name: "NoTLS", - config: config.ClusterConfig{ClusterSize: 1}, - }, - { - name: "PeerTLS", - config: config.ClusterConfig{ClusterSize: 3, PeerTLS: config.ManualTLS}, - }, - { - name: "PeerAutoTLS", - config: config.ClusterConfig{ClusterSize: 3, PeerTLS: config.AutoTLS}, - }, - { - name: "ClientTLS", - config: config.ClusterConfig{ClusterSize: 1, ClientTLS: config.ManualTLS}, - }, - { - name: "ClientAutoTLS", - config: config.ClusterConfig{ClusterSize: 1, ClientTLS: config.AutoTLS}, - }, - } + testRunner.BeforeTest(t) reqs := []txnReq{ { compare: []string{`value("key1") != "value2"`, `value("key2") != "value1"`}, @@ -77,8 +53,7 @@ func TestTxnSucc(t *testing.T) { results: []string{"SUCCESS", `key "with" space`, "value \x23"}, }, } - testRunner.BeforeTest(t) - for _, cfg := range tcs { + for _, cfg := range clusterTestCases { t.Run(cfg.name, func(t *testing.T) { clus := testRunner.NewCluster(t, cfg.config) defer clus.Close() @@ -105,31 +80,7 @@ func TestTxnSucc(t *testing.T) { } func TestTxnFail(t *testing.T) { - tcs := []struct { - name string - config config.ClusterConfig - }{ - { - name: "NoTLS", - config: config.ClusterConfig{ClusterSize: 1}, - }, - { - name: "PeerTLS", - config: config.ClusterConfig{ClusterSize: 3, PeerTLS: config.ManualTLS}, - }, - { - name: "PeerAutoTLS", - config: config.ClusterConfig{ClusterSize: 3, PeerTLS: config.AutoTLS}, - }, - { - name: "ClientTLS", - config: config.ClusterConfig{ClusterSize: 1, ClientTLS: config.ManualTLS}, - }, - { - name: "ClientAutoTLS", - config: config.ClusterConfig{ClusterSize: 1, ClientTLS: config.AutoTLS}, - }, - } + testRunner.BeforeTest(t) reqs := []txnReq{ { compare: []string{`version("key") < "0"`}, @@ -144,8 +95,7 @@ func TestTxnFail(t *testing.T) { results: []string{"FAILURE", "OK"}, }, } - testRunner.BeforeTest(t) - for _, cfg := range tcs { + for _, cfg := range clusterTestCases { t.Run(cfg.name, func(t *testing.T) { clus := testRunner.NewCluster(t, cfg.config) defer clus.Close() diff --git a/tests/common/user_test.go b/tests/common/user_test.go index a1f7dbb24..49d8ea5fe 100644 --- a/tests/common/user_test.go +++ b/tests/common/user_test.go @@ -26,67 +26,42 @@ import ( func TestUserAdd_Simple(t *testing.T) { testRunner.BeforeTest(t) tcs := []struct { - name string - config config.ClusterConfig + name string + username string + password string + noPassword bool + expectedError string }{ { - name: "NoTLS", - config: config.ClusterConfig{ClusterSize: 1}, + name: "empty_username_not_allowed", + username: "", + password: "foobar", + // Very Vague error expectation because the CLI and the API return very + // different error structures. + expectedError: "user name", }, { - name: "PeerTLS", - config: config.ClusterConfig{ClusterSize: 3, PeerTLS: config.ManualTLS}, + // Can create a user with no password, restricted to CN auth + name: "no_password_with_noPassword_set", + username: "foo", + password: "", + noPassword: true, }, { - name: "PeerAutoTLS", - config: config.ClusterConfig{ClusterSize: 3, PeerTLS: config.AutoTLS}, + // Can create a user with no password, but not restricted to CN auth + name: "no_password_without_noPassword_set", + username: "foo", + password: "", + noPassword: false, }, { - name: "ClientTLS", - config: config.ClusterConfig{ClusterSize: 1, ClientTLS: config.ManualTLS}, - }, - { - name: "ClientAutoTLS", - config: config.ClusterConfig{ClusterSize: 1, ClientTLS: config.AutoTLS}, + name: "regular_user_with_password", + username: "foo", + password: "bar", }, } - for _, tc := range tcs { - nestedCases := []struct { - name string - username string - password string - noPassword bool - expectedError string - }{ - { - name: "empty_username_not_allowed", - username: "", - password: "foobar", - // Very Vague error expectation because the CLI and the API return very - // different error structures. - expectedError: "user name", - }, - { - // Can create a user with no password, restricted to CN auth - name: "no_password_with_noPassword_set", - username: "foo", - password: "", - noPassword: true, - }, - { - // Can create a user with no password, but not restricted to CN auth - name: "no_password_without_noPassword_set", - username: "foo", - password: "", - noPassword: false, - }, - { - name: "regular_user_with_password", - username: "foo", - password: "bar", - }, - } - for _, nc := range nestedCases { + for _, tc := range clusterTestCases { + for _, nc := range tcs { t.Run(tc.name+"/"+nc.name, func(t *testing.T) { clus := testRunner.NewCluster(t, tc.config) defer clus.Close() @@ -118,32 +93,7 @@ func TestUserAdd_Simple(t *testing.T) { func TestUserAdd_DuplicateUserNotAllowed(t *testing.T) { testRunner.BeforeTest(t) - tcs := []struct { - name string - config config.ClusterConfig - }{ - { - name: "NoTLS", - config: config.ClusterConfig{ClusterSize: 1}, - }, - { - name: "PeerTLS", - config: config.ClusterConfig{ClusterSize: 3, PeerTLS: config.ManualTLS}, - }, - { - name: "PeerAutoTLS", - config: config.ClusterConfig{ClusterSize: 3, PeerTLS: config.AutoTLS}, - }, - { - name: "ClientTLS", - config: config.ClusterConfig{ClusterSize: 1, ClientTLS: config.ManualTLS}, - }, - { - name: "ClientAutoTLS", - config: config.ClusterConfig{ClusterSize: 1, ClientTLS: config.AutoTLS}, - }, - } - for _, tc := range tcs { + for _, tc := range clusterTestCases { t.Run(tc.name, func(t *testing.T) { clus := testRunner.NewCluster(t, tc.config) defer clus.Close() @@ -170,32 +120,7 @@ func TestUserAdd_DuplicateUserNotAllowed(t *testing.T) { func TestUserList(t *testing.T) { testRunner.BeforeTest(t) - tcs := []struct { - name string - config config.ClusterConfig - }{ - { - name: "NoTLS", - config: config.ClusterConfig{ClusterSize: 1}, - }, - { - name: "PeerTLS", - config: config.ClusterConfig{ClusterSize: 3, PeerTLS: config.ManualTLS}, - }, - { - name: "PeerAutoTLS", - config: config.ClusterConfig{ClusterSize: 3, PeerTLS: config.AutoTLS}, - }, - { - name: "ClientTLS", - config: config.ClusterConfig{ClusterSize: 1, ClientTLS: config.ManualTLS}, - }, - { - name: "ClientAutoTLS", - config: config.ClusterConfig{ClusterSize: 1, ClientTLS: config.AutoTLS}, - }, - } - for _, tc := range tcs { + for _, tc := range clusterTestCases { t.Run(tc.name, func(t *testing.T) { clus := testRunner.NewCluster(t, tc.config) defer clus.Close() @@ -234,32 +159,7 @@ func TestUserList(t *testing.T) { func TestUserDelete(t *testing.T) { testRunner.BeforeTest(t) - tcs := []struct { - name string - config config.ClusterConfig - }{ - { - name: "NoTLS", - config: config.ClusterConfig{ClusterSize: 1}, - }, - { - name: "PeerTLS", - config: config.ClusterConfig{ClusterSize: 3, PeerTLS: config.ManualTLS}, - }, - { - name: "PeerAutoTLS", - config: config.ClusterConfig{ClusterSize: 3, PeerTLS: config.AutoTLS}, - }, - { - name: "ClientTLS", - config: config.ClusterConfig{ClusterSize: 1, ClientTLS: config.ManualTLS}, - }, - { - name: "ClientAutoTLS", - config: config.ClusterConfig{ClusterSize: 1, ClientTLS: config.AutoTLS}, - }, - } - for _, tc := range tcs { + for _, tc := range clusterTestCases { t.Run(tc.name, func(t *testing.T) { clus := testRunner.NewCluster(t, tc.config) defer clus.Close() @@ -309,32 +209,7 @@ func TestUserDelete(t *testing.T) { func TestUserChangePassword(t *testing.T) { testRunner.BeforeTest(t) - tcs := []struct { - name string - config config.ClusterConfig - }{ - { - name: "NoTLS", - config: config.ClusterConfig{ClusterSize: 1}, - }, - { - name: "PeerTLS", - config: config.ClusterConfig{ClusterSize: 3, PeerTLS: config.ManualTLS}, - }, - { - name: "PeerAutoTLS", - config: config.ClusterConfig{ClusterSize: 3, PeerTLS: config.AutoTLS}, - }, - { - name: "ClientTLS", - config: config.ClusterConfig{ClusterSize: 1, ClientTLS: config.ManualTLS}, - }, - { - name: "ClientAutoTLS", - config: config.ClusterConfig{ClusterSize: 1, ClientTLS: config.AutoTLS}, - }, - } - for _, tc := range tcs { + for _, tc := range clusterTestCases { t.Run(tc.name, func(t *testing.T) { clus := testRunner.NewCluster(t, tc.config) defer clus.Close()