Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: Migrate compact tests to common framework #13770

Merged
merged 1 commit into from
Mar 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor comment: It isn't necessary to define a PackageName ("clientv3") here. You can still use clientv3 in the following code without the PackageName here.

Copy link
Member

@ahrtr ahrtr Mar 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not asking for change on this, because it isn't a big deal. And it looks even clear to clearly define a PackageName clientv3 here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I commit other tests, i will fixed this.

)

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
}