Skip to content

Commit

Permalink
Merge pull request #13770 from kkkkun/add-compact-test
Browse files Browse the repository at this point in the history
 tests: Migrate compact tests to common framework
  • Loading branch information
spzala authored Mar 11, 2022
2 parents 91f9ce9 + 34cd8ae commit 4e97271
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 2 deletions.
87 changes: 87 additions & 0 deletions tests/common/compact_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// 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 (
"strings"
"testing"
"time"

"github.com/stretchr/testify/assert"
"go.etcd.io/etcd/tests/v3/framework/config"
"go.etcd.io/etcd/tests/v3/framework/testutils"
)

func TestCompact(t *testing.T) {

testRunner.BeforeTest(t)
tcs := []struct {
name string
options config.CompactOption
}{
{
name: "NoPhysical",
options: config.CompactOption{Physical: false, Timeout: 10 * time.Second},
},
{
name: "Physical",
options: config.CompactOption{Physical: true, Timeout: 10 * time.Second},
},
}
for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
clus := testRunner.NewCluster(t, config.ClusterConfig{ClusterSize: 3})
defer clus.Close()
testutils.ExecuteWithTimeout(t, 10*time.Second, func() {
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)
}
}
get, err := clus.Client().Get("key", config.GetOptions{Revision: 3})
if err != nil {
t.Fatalf("compactTest: Get kv by revision error (%v)", err)
}

getkvs := testutils.KeyValuesFromGetResponse(get)
assert.Equal(t, kvs[1:2], getkvs)

_, err = clus.Client().Compact(4, tc.options)
if err != nil {
t.Fatalf("compactTest: Compact error (%v)", err)
}

get, err = clus.Client().Get("key", config.GetOptions{Revision: 3})
if err != nil {
if !strings.Contains(err.Error(), "required revision has been compacted") {
t.Fatalf("compactTest: Get compact key error (%v)", err)
}
} else {
t.Fatalf("expected '...has been compacted' error, got <nil>")
}

_, err = clus.Client().Compact(2, tc.options)
if err != nil {
if !strings.Contains(err.Error(), "required revision has been compacted") {
t.Fatal(err)
}
} else {
t.Fatalf("expected '...has been compacted' error, got <nil>")
}
})
})
}
}
10 changes: 9 additions & 1 deletion tests/framework/config/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@

package config

import clientv3 "go.etcd.io/etcd/client/v3"
import (
clientv3 "go.etcd.io/etcd/client/v3"
"time"
)

type GetOptions struct {
Revision int
Expand All @@ -33,3 +36,8 @@ type DeleteOptions struct {
FromKey bool
End string
}

type CompactOption struct {
Physical bool
Timeout time.Duration
}
14 changes: 14 additions & 0 deletions tests/framework/e2e/etcdctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,17 @@ func (ctl *EtcdctlV3) flags() map[string]string {
fmap["endpoints"] = strings.Join(ctl.endpoints, ",")
return fmap
}

func (ctl *EtcdctlV3) Compact(rev int64, o config.CompactOption) (*clientv3.CompactResponse, error) {
args := ctl.cmdArgs()
args = append(args, "compact", fmt.Sprint(rev))

if o.Timeout != 0 {
args = append(args, fmt.Sprintf("--command-timeout=%s", o.Timeout))
}
if o.Physical {
args = append(args, "--physical")
}

return nil, SpawnWithExpect(args, fmt.Sprintf("compacted revision %v", rev))
}
14 changes: 14 additions & 0 deletions tests/framework/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,17 @@ func (c integrationClient) Delete(key string, o config.DeleteOptions) (*clientv3
}
return c.Client.Delete(context.Background(), key, clientOpts...)
}

func (c integrationClient) Compact(rev int64, o config.CompactOption) (*clientv3.CompactResponse, error) {
ctx := context.Background()
if o.Timeout != 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, o.Timeout)
defer cancel()
}
clientOpts := []clientv3.CompactOption{}
if o.Physical {
clientOpts = append(clientOpts, clientv3.WithCompactPhysical())
}
return c.Client.Compact(ctx, rev, clientOpts...)
}
1 change: 1 addition & 0 deletions tests/framework/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ type Client interface {
Put(key, value string) error
Get(key string, opts config.GetOptions) (*clientv3.GetResponse, error)
Delete(key string, opts config.DeleteOptions) (*clientv3.DeleteResponse, error)
Compact(rev int64, opts config.CompactOption) (*clientv3.CompactResponse, error)
}
15 changes: 14 additions & 1 deletion tests/framework/testutils/helpters.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,24 @@

package testutils

import clientv3 "go.etcd.io/etcd/client/v3"
import (
clientv3 "go.etcd.io/etcd/client/v3"
)

type KV struct {
Key, Val string
}

func KeysFromGetResponse(resp *clientv3.GetResponse) (kvs []string) {
for _, kv := range resp.Kvs {
kvs = append(kvs, string(kv.Key))
}
return kvs
}

func KeyValuesFromGetResponse(resp *clientv3.GetResponse) (kvs []KV) {
for _, kv := range resp.Kvs {
kvs = append(kvs, KV{Key: string(kv.Key), Val: string(kv.Value)})
}
return kvs
}

0 comments on commit 4e97271

Please sign in to comment.