From 66eb3dbbdcad156c8a122940486f6ad194aa6dfe Mon Sep 17 00:00:00 2001 From: kkkkun Date: Fri, 18 Mar 2022 19:17:48 +0800 Subject: [PATCH] tests: Migrate defrag tests to common framework --- tests/common/defrag_test.go | 46 ++++++++++++++++++ tests/e2e/ctl_v3_alarm_test.go | 2 +- tests/e2e/ctl_v3_compact_test.go | 77 ------------------------------- tests/e2e/ctl_v3_defrag_test.go | 24 ++++------ tests/e2e/ctl_v3_snapshot_test.go | 3 -- tests/framework/config/client.go | 7 ++- tests/framework/e2e/etcdctl.go | 14 ++++++ tests/framework/integration.go | 16 +++++++ tests/framework/interface.go | 1 + 9 files changed, 94 insertions(+), 96 deletions(-) create mode 100644 tests/common/defrag_test.go delete mode 100644 tests/e2e/ctl_v3_compact_test.go diff --git a/tests/common/defrag_test.go b/tests/common/defrag_test.go new file mode 100644 index 000000000..6cfce4c68 --- /dev/null +++ b/tests/common/defrag_test.go @@ -0,0 +1,46 @@ +// Copyright 2022 The etcd Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package common + +import ( + "testing" + "time" + + "go.etcd.io/etcd/tests/v3/framework/config" + "go.etcd.io/etcd/tests/v3/framework/testutils" +) + +func TestDefragOnline(t *testing.T) { + testRunner.BeforeTest(t) + options := config.DefragOption{Timeout: 10 * time.Second} + clus := testRunner.NewCluster(t, config.ClusterConfig{ClusterSize: 3}) + testutils.ExecuteWithTimeout(t, 10*time.Second, func() { + defer clus.Close() + var kvs = []testutils.KV{{Key: "key", Val: "val1"}, {Key: "key", Val: "val2"}, {Key: "key", Val: "val3"}} + for i := range kvs { + if err := clus.Client().Put(kvs[i].Key, kvs[i].Val); err != nil { + t.Fatalf("compactTest #%d: put kv error (%v)", i, err) + } + } + _, err := clus.Client().Compact(4, config.CompactOption{Physical: true, Timeout: 10 * time.Second}) + if err != nil { + t.Fatalf("defrag_test: compact with revision error (%v)", err) + } + + if err = clus.Client().Defragment(options); err != nil { + t.Fatalf("defrag_test: defrag error (%v)", err) + } + }) +} diff --git a/tests/e2e/ctl_v3_alarm_test.go b/tests/e2e/ctl_v3_alarm_test.go index f33654f00..b99d3a6a3 100644 --- a/tests/e2e/ctl_v3_alarm_test.go +++ b/tests/e2e/ctl_v3_alarm_test.go @@ -21,7 +21,7 @@ import ( "testing" "time" - "go.etcd.io/etcd/client/v3" + clientv3 "go.etcd.io/etcd/client/v3" "go.etcd.io/etcd/tests/v3/framework/e2e" ) diff --git a/tests/e2e/ctl_v3_compact_test.go b/tests/e2e/ctl_v3_compact_test.go deleted file mode 100644 index f29e580d9..000000000 --- a/tests/e2e/ctl_v3_compact_test.go +++ /dev/null @@ -1,77 +0,0 @@ -// Copyright 2016 The etcd Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package e2e - -import ( - "strconv" - "strings" - "testing" - - "go.etcd.io/etcd/tests/v3/framework/e2e" -) - -func TestCtlV3Compact(t *testing.T) { testCtl(t, compactTest) } -func TestCtlV3CompactPhysical(t *testing.T) { testCtl(t, compactTest, withCompactPhysical()) } - -func compactTest(cx ctlCtx) { - compactPhysical := cx.compactPhysical - if err := ctlV3Compact(cx, 2, compactPhysical); err != nil { - if !strings.Contains(err.Error(), "required revision is a future revision") { - cx.t.Fatal(err) - } - } else { - cx.t.Fatalf("expected '...future revision' error, got ") - } - - var kvs = []kv{{"key", "val1"}, {"key", "val2"}, {"key", "val3"}} - for i := range kvs { - if err := ctlV3Put(cx, kvs[i].key, kvs[i].val, ""); err != nil { - cx.t.Fatalf("compactTest #%d: ctlV3Put error (%v)", i, err) - } - } - - if err := ctlV3Get(cx, []string{"key", "--rev", "3"}, kvs[1:2]...); err != nil { - cx.t.Errorf("compactTest: ctlV3Get error (%v)", err) - } - - if err := ctlV3Compact(cx, 4, compactPhysical); err != nil { - cx.t.Fatal(err) - } - - if err := ctlV3Get(cx, []string{"key", "--rev", "3"}, kvs[1:2]...); err != nil { - if !strings.Contains(err.Error(), "required revision has been compacted") { - cx.t.Errorf("compactTest: ctlV3Get error (%v)", err) - } - } else { - cx.t.Fatalf("expected '...has been compacted' error, got ") - } - - if err := ctlV3Compact(cx, 2, compactPhysical); err != nil { - if !strings.Contains(err.Error(), "required revision has been compacted") { - cx.t.Fatal(err) - } - } else { - cx.t.Fatalf("expected '...has been compacted' error, got ") - } -} - -func ctlV3Compact(cx ctlCtx, rev int64, physical bool) error { - rs := strconv.FormatInt(rev, 10) - cmdArgs := append(cx.PrefixArgs(), "compact", rs) - if physical { - cmdArgs = append(cmdArgs, "--physical") - } - return e2e.SpawnWithExpectWithEnv(cmdArgs, cx.envMap, "compacted revision "+rs) -} diff --git a/tests/e2e/ctl_v3_defrag_test.go b/tests/e2e/ctl_v3_defrag_test.go index c47ed3f4c..889bbbd07 100644 --- a/tests/e2e/ctl_v3_defrag_test.go +++ b/tests/e2e/ctl_v3_defrag_test.go @@ -15,13 +15,12 @@ package e2e import ( + "strconv" "testing" "go.etcd.io/etcd/tests/v3/framework/e2e" ) -func TestCtlV3DefragOnline(t *testing.T) { testCtl(t, defragOnlineTest) } - func TestCtlV3DefragOfflineEtcdutl(t *testing.T) { testCtlWithOffline(t, maintenanceInitKeys, defragOfflineTest, withEtcdutl()) } @@ -35,18 +34,6 @@ func maintenanceInitKeys(cx ctlCtx) { } } -func defragOnlineTest(cx ctlCtx) { - maintenanceInitKeys(cx) - - if err := ctlV3Compact(cx, 4, cx.compactPhysical); err != nil { - cx.t.Fatal(err) - } - - if err := ctlV3OnlineDefrag(cx); err != nil { - cx.t.Fatalf("defragTest ctlV3Defrag error (%v)", err) - } -} - func ctlV3OnlineDefrag(cx ctlCtx) error { cmdArgs := append(cx.PrefixArgs(), "defrag") lines := make([]string, cx.epc.Cfg.ClusterSize) @@ -67,3 +54,12 @@ func defragOfflineTest(cx ctlCtx) { cx.t.Fatalf("defragTest ctlV3Defrag error (%v)", err) } } + +func ctlV3Compact(cx ctlCtx, rev int64, physical bool) error { + rs := strconv.FormatInt(rev, 10) + cmdArgs := append(cx.PrefixArgs(), "compact", rs) + if physical { + cmdArgs = append(cmdArgs, "--physical") + } + return e2e.SpawnWithExpectWithEnv(cmdArgs, cx.envMap, "compacted revision "+rs) +} diff --git a/tests/e2e/ctl_v3_snapshot_test.go b/tests/e2e/ctl_v3_snapshot_test.go index 59e1dc770..c952b3aae 100644 --- a/tests/e2e/ctl_v3_snapshot_test.go +++ b/tests/e2e/ctl_v3_snapshot_test.go @@ -294,9 +294,6 @@ func testIssue6361(t *testing.T, etcdutl bool) { // For storageVersion to be stored, all fields expected 3.6 fields need to be set. This happens after first WAL snapshot. // In this test we lower SnapshotCount to 1 to ensure WAL snapshot is triggered. -func TestCtlV3SnapshotVersion(t *testing.T) { - testCtl(t, snapshotVersionTest, withCfg(e2e.EtcdProcessClusterConfig{SnapshotCount: 1})) -} func TestCtlV3SnapshotVersionEtcdutl(t *testing.T) { testCtl(t, snapshotVersionTest, withEtcdutl(), withCfg(e2e.EtcdProcessClusterConfig{SnapshotCount: 1})) } diff --git a/tests/framework/config/client.go b/tests/framework/config/client.go index c41565b47..6160d841d 100644 --- a/tests/framework/config/client.go +++ b/tests/framework/config/client.go @@ -15,8 +15,9 @@ package config import ( - clientv3 "go.etcd.io/etcd/client/v3" "time" + + clientv3 "go.etcd.io/etcd/client/v3" ) type GetOptions struct { @@ -41,3 +42,7 @@ type CompactOption struct { Physical bool Timeout time.Duration } + +type DefragOption struct { + Timeout time.Duration +} diff --git a/tests/framework/e2e/etcdctl.go b/tests/framework/e2e/etcdctl.go index 18c436748..f11bdcead 100644 --- a/tests/framework/e2e/etcdctl.go +++ b/tests/framework/e2e/etcdctl.go @@ -243,3 +243,17 @@ func (ctl *EtcdctlV3) Health() error { return SpawnWithExpects(args, map[string]string{}, lines...) } + +func (ctl *EtcdctlV3) Defragment(o config.DefragOption) error { + + args := append(ctl.cmdArgs(), "defrag") + if o.Timeout != 0 { + args = append(args, fmt.Sprintf("--command-timeout=%s", o.Timeout)) + } + lines := make([]string, len(ctl.endpoints)) + for i := range lines { + lines[i] = "Finished defragmenting etcd member" + } + _, err := SpawnWithExpectLines(args, map[string]string{}, lines...) + return err +} diff --git a/tests/framework/integration.go b/tests/framework/integration.go index 73e15f130..94c1925b7 100644 --- a/tests/framework/integration.go +++ b/tests/framework/integration.go @@ -196,3 +196,19 @@ func (c integrationClient) Health() error { } return nil } + +func (c integrationClient) Defragment(o config.DefragOption) error { + ctx := context.Background() + if o.Timeout != 0 { + var cancel context.CancelFunc + ctx, cancel = context.WithTimeout(ctx, o.Timeout) + defer cancel() + } + for _, ep := range c.Endpoints() { + _, err := c.Client.Defragment(ctx, ep) + if err != nil { + return err + } + } + return nil +} diff --git a/tests/framework/interface.go b/tests/framework/interface.go index f6982f321..db4b3b980 100644 --- a/tests/framework/interface.go +++ b/tests/framework/interface.go @@ -41,4 +41,5 @@ type Client interface { Status() ([]*clientv3.StatusResponse, error) HashKV(rev int64) ([]*clientv3.HashKVResponse, error) Health() error + Defragment(opts config.DefragOption) error }