mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
tests: Implement remaining GetOptions fields
This commit is contained in:
parent
fd5cd9fd98
commit
f7ee30cc41
@ -33,7 +33,7 @@ func TestKVPut(t *testing.T) {
|
|||||||
if err := cc.Put(key, value); err != nil {
|
if err := cc.Put(key, value); err != nil {
|
||||||
t.Fatalf("count not put key %q, err: %s", key, err)
|
t.Fatalf("count not put key %q, err: %s", key, err)
|
||||||
}
|
}
|
||||||
resp, err := cc.Get(key, testutils.WithSerializable())
|
resp, err := cc.Get(key, testutils.GetOptions{Serializable: true})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("count not get key %q, err: %s", key, err)
|
t.Fatalf("count not get key %q, err: %s", key, err)
|
||||||
}
|
}
|
||||||
|
@ -19,9 +19,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"go.etcd.io/etcd/client/pkg/v3/testutil"
|
"go.etcd.io/etcd/client/pkg/v3/testutil"
|
||||||
clientv3 "go.etcd.io/etcd/client/v3"
|
|
||||||
"go.etcd.io/etcd/tests/v3/framework/e2e"
|
"go.etcd.io/etcd/tests/v3/framework/e2e"
|
||||||
"go.etcd.io/etcd/tests/v3/framework/testutils"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type e2eRunner struct{}
|
type e2eRunner struct{}
|
||||||
@ -58,11 +56,3 @@ func (c *e2eCluster) Client() Client {
|
|||||||
type e2eClient struct {
|
type e2eClient struct {
|
||||||
*e2e.EtcdctlV3
|
*e2e.EtcdctlV3
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c e2eClient) Get(key string, opts ...testutils.GetOption) (*clientv3.GetResponse, error) {
|
|
||||||
o := testutils.GetOptions{}
|
|
||||||
for _, opt := range opts {
|
|
||||||
opt(&o)
|
|
||||||
}
|
|
||||||
return c.EtcdctlV3.Get(key, o)
|
|
||||||
}
|
|
||||||
|
@ -44,15 +44,64 @@ func (ctl *EtcdctlV3) Get(key string, o testutils.GetOptions) (*clientv3.GetResp
|
|||||||
if o.Serializable {
|
if o.Serializable {
|
||||||
args = append(args, "--consistency", "s")
|
args = append(args, "--consistency", "s")
|
||||||
}
|
}
|
||||||
cmd, err := SpawnCmd(append(args, "get", key, "-w", "json"), nil)
|
args = append(args, "get", key, "-w", "json")
|
||||||
|
if o.End != "" {
|
||||||
|
args = append(args, o.End)
|
||||||
|
}
|
||||||
|
if o.Revision != 0 {
|
||||||
|
args = append(args, fmt.Sprintf("--rev=%d", o.Revision))
|
||||||
|
}
|
||||||
|
if o.Prefix {
|
||||||
|
args = append(args, "--prefix")
|
||||||
|
}
|
||||||
|
if o.Limit != 0 {
|
||||||
|
args = append(args, fmt.Sprintf("--limit=%d", o.Limit))
|
||||||
|
}
|
||||||
|
if o.FromKey {
|
||||||
|
args = append(args, "--from-key")
|
||||||
|
}
|
||||||
|
if o.CountOnly {
|
||||||
|
args = append(args, "-w", "fields", "--count-only")
|
||||||
|
} else {
|
||||||
|
args = append(args, "-w", "json")
|
||||||
|
}
|
||||||
|
switch o.SortBy {
|
||||||
|
case clientv3.SortByCreateRevision:
|
||||||
|
args = append(args, "--sort-by=CREATE")
|
||||||
|
case clientv3.SortByModRevision:
|
||||||
|
args = append(args, "--sort-by=MODIFY")
|
||||||
|
case clientv3.SortByValue:
|
||||||
|
args = append(args, "--sort-by=VALUE")
|
||||||
|
case clientv3.SortByVersion:
|
||||||
|
args = append(args, "--sort-by=VERSION")
|
||||||
|
case clientv3.SortByKey:
|
||||||
|
// nothing
|
||||||
|
default:
|
||||||
|
return nil, fmt.Errorf("bad sort target %v", o.SortBy)
|
||||||
|
}
|
||||||
|
switch o.Order {
|
||||||
|
case clientv3.SortAscend:
|
||||||
|
args = append(args, "--order=ASCEND")
|
||||||
|
case clientv3.SortDescend:
|
||||||
|
args = append(args, "--order=DESCEND")
|
||||||
|
case clientv3.SortNone:
|
||||||
|
// nothing
|
||||||
|
default:
|
||||||
|
return nil, fmt.Errorf("bad sort order %v", o.Order)
|
||||||
|
}
|
||||||
|
cmd, err := SpawnCmd(args, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
var resp clientv3.GetResponse
|
||||||
|
if o.CountOnly {
|
||||||
|
_, err := cmd.Expect("Count")
|
||||||
|
return &resp, err
|
||||||
|
}
|
||||||
line, err := cmd.Expect("kvs")
|
line, err := cmd.Expect("kvs")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
var resp clientv3.GetResponse
|
|
||||||
err = json.Unmarshal([]byte(line), &resp)
|
err = json.Unmarshal([]byte(line), &resp)
|
||||||
return &resp, err
|
return &resp, err
|
||||||
}
|
}
|
||||||
|
@ -63,15 +63,32 @@ type integrationClient struct {
|
|||||||
*clientv3.Client
|
*clientv3.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c integrationClient) Get(key string, opts ...testutils.GetOption) (*clientv3.GetResponse, error) {
|
func (c integrationClient) Get(key string, o testutils.GetOptions) (*clientv3.GetResponse, error) {
|
||||||
o := testutils.GetOptions{}
|
|
||||||
for _, opt := range opts {
|
|
||||||
opt(&o)
|
|
||||||
}
|
|
||||||
clientOpts := []clientv3.OpOption{}
|
clientOpts := []clientv3.OpOption{}
|
||||||
|
if o.Revision != 0 {
|
||||||
|
clientOpts = append(clientOpts, clientv3.WithRev(int64(o.Revision)))
|
||||||
|
}
|
||||||
|
if o.End != "" {
|
||||||
|
clientOpts = append(clientOpts, clientv3.WithRange(o.End))
|
||||||
|
}
|
||||||
if o.Serializable {
|
if o.Serializable {
|
||||||
clientOpts = append(clientOpts, clientv3.WithSerializable())
|
clientOpts = append(clientOpts, clientv3.WithSerializable())
|
||||||
}
|
}
|
||||||
|
if o.Prefix {
|
||||||
|
clientOpts = append(clientOpts, clientv3.WithPrefix())
|
||||||
|
}
|
||||||
|
if o.Limit != 0 {
|
||||||
|
clientOpts = append(clientOpts, clientv3.WithLimit(int64(o.Limit)))
|
||||||
|
}
|
||||||
|
if o.FromKey {
|
||||||
|
clientOpts = append(clientOpts, clientv3.WithFromKey())
|
||||||
|
}
|
||||||
|
if o.CountOnly {
|
||||||
|
clientOpts = append(clientOpts, clientv3.WithCountOnly())
|
||||||
|
}
|
||||||
|
if o.SortBy != clientv3.SortByKey || o.Order != clientv3.SortNone {
|
||||||
|
clientOpts = append(clientOpts, clientv3.WithSort(o.SortBy, o.Order))
|
||||||
|
}
|
||||||
return c.Client.Get(context.Background(), key, clientOpts...)
|
return c.Client.Get(context.Background(), key, clientOpts...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -34,5 +34,5 @@ type Cluster interface {
|
|||||||
|
|
||||||
type Client interface {
|
type Client interface {
|
||||||
Put(key, value string) error
|
Put(key, value string) error
|
||||||
Get(key string, opts ...testutils.GetOption) (*clientv3.GetResponse, error)
|
Get(key string, opts testutils.GetOptions) (*clientv3.GetResponse, error)
|
||||||
}
|
}
|
||||||
|
@ -14,14 +14,16 @@
|
|||||||
|
|
||||||
package testutils
|
package testutils
|
||||||
|
|
||||||
|
import clientv3 "go.etcd.io/etcd/client/v3"
|
||||||
|
|
||||||
type GetOptions struct {
|
type GetOptions struct {
|
||||||
|
Revision int
|
||||||
|
End string
|
||||||
|
CountOnly bool
|
||||||
Serializable bool
|
Serializable bool
|
||||||
}
|
Prefix bool
|
||||||
|
FromKey bool
|
||||||
type GetOption func(*GetOptions)
|
Limit int
|
||||||
|
Order clientv3.SortOrder
|
||||||
func WithSerializable() GetOption {
|
SortBy clientv3.SortTarget
|
||||||
return func(options *GetOptions) {
|
|
||||||
options.Serializable = true
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user