Merge pull request #13820 from kkkkun/defrag-test

tests: Migrate defrag tests to common framework
This commit is contained in:
Marek Siarkowicz 2022-03-18 16:50:39 +01:00 committed by GitHub
commit 34f0ab4f4f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 94 additions and 96 deletions

View File

@ -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)
}
})
}

View File

@ -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"
)

View File

@ -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 <nil>")
}
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 <nil>")
}
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 <nil>")
}
}
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)
}

View File

@ -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)
}

View File

@ -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}))
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}

View File

@ -41,4 +41,5 @@ type Client interface {
Status() ([]*clientv3.StatusResponse, error)
HashKV(rev int64) ([]*clientv3.HashKVResponse, error)
Health() error
Defragment(opts config.DefragOption) error
}