diff --git a/tests/framework/e2e/etcdctl.go b/tests/framework/e2e/etcdctl.go index 802de6d59..97de1f21f 100644 --- a/tests/framework/e2e/etcdctl.go +++ b/tests/framework/e2e/etcdctl.go @@ -44,7 +44,8 @@ func (ctl *EtcdctlV3) DowngradeEnable(version string) error { } func (ctl *EtcdctlV3) Get(key string, o config.GetOptions) (*clientv3.GetResponse, error) { - args := ctl.cmdArgs() + resp := clientv3.GetResponse{} + var args []string if o.Timeout != 0 { args = append(args, fmt.Sprintf("--command-timeout=%s", o.Timeout)) } @@ -96,20 +97,15 @@ func (ctl *EtcdctlV3) Get(key string, o config.GetOptions) (*clientv3.GetRespons default: return nil, fmt.Errorf("bad sort order %v", o.Order) } - cmd, err := SpawnCmd(args, nil) - if err != nil { - return nil, err - } - var resp clientv3.GetResponse if o.CountOnly { - _, err := cmd.Expect("Count") + cmd, err := SpawnCmd(ctl.cmdArgs(args...), nil) + if err != nil { + return nil, err + } + _, err = cmd.Expect("Count") return &resp, err } - line, err := cmd.Expect("header") - if err != nil { - return nil, err - } - err = json.Unmarshal([]byte(line), &resp) + err := ctl.spawnJsonCmd(&resp, args...) return &resp, err } @@ -123,8 +119,7 @@ func (ctl *EtcdctlV3) Put(key, value string, opts config.PutOptions) error { } func (ctl *EtcdctlV3) Delete(key string, o config.DeleteOptions) (*clientv3.DeleteResponse, error) { - args := ctl.cmdArgs() - args = append(args, "del", key, "-w", "json") + args := []string{"del", key} if o.End != "" { args = append(args, o.End) } @@ -134,16 +129,8 @@ func (ctl *EtcdctlV3) Delete(key string, o config.DeleteOptions) (*clientv3.Dele if o.FromKey { args = append(args, "--from-key") } - cmd, err := SpawnCmd(args, nil) - if err != nil { - return nil, err - } var resp clientv3.DeleteResponse - line, err := cmd.Expect("header") - if err != nil { - return nil, err - } - err = json.Unmarshal([]byte(line), &resp) + err := ctl.spawnJsonCmd(&resp, args...) return &resp, err } @@ -243,44 +230,20 @@ func AddTxnResponse(resp *clientv3.TxnResponse, jsonData string) { } } func (ctl *EtcdctlV3) MemberList() (*clientv3.MemberListResponse, error) { - cmd, err := SpawnCmd(ctl.cmdArgs("member", "list", "-w", "json"), nil) - if err != nil { - return nil, err - } var resp clientv3.MemberListResponse - line, err := cmd.Expect("header") - if err != nil { - return nil, err - } - err = json.Unmarshal([]byte(line), &resp) + err := ctl.spawnJsonCmd(&resp, "member", "list") return &resp, err } func (ctl *EtcdctlV3) MemberAddAsLearner(name string, peerAddrs []string) (*clientv3.MemberAddResponse, error) { - cmd, err := SpawnCmd(ctl.cmdArgs("member", "add", name, "--learner", "--peer-urls", strings.Join(peerAddrs, ","), "-w", "json"), nil) - if err != nil { - return nil, err - } var resp clientv3.MemberAddResponse - line, err := cmd.Expect("header") - if err != nil { - return nil, err - } - err = json.Unmarshal([]byte(line), &resp) + err := ctl.spawnJsonCmd(&resp, "member", "add", name, "--learner", "--peer-urls", strings.Join(peerAddrs, ",")) return &resp, err } func (ctl *EtcdctlV3) MemberRemove(id uint64) (*clientv3.MemberRemoveResponse, error) { - cmd, err := SpawnCmd(ctl.cmdArgs("member", "remove", fmt.Sprintf("%x", id), "-w", "json"), nil) - if err != nil { - return nil, err - } var resp clientv3.MemberRemoveResponse - line, err := cmd.Expect("header") - if err != nil { - return nil, err - } - err = json.Unmarshal([]byte(line), &resp) + err := ctl.spawnJsonCmd(&resp, "member", "remove", fmt.Sprintf("%x", id)) return &resp, err } @@ -313,9 +276,7 @@ func (ctl *EtcdctlV3) flags() map[string]string { } func (ctl *EtcdctlV3) Compact(rev int64, o config.CompactOption) (*clientv3.CompactResponse, error) { - args := ctl.cmdArgs() - args = append(args, "compact", fmt.Sprint(rev)) - + args := ctl.cmdArgs("compact", fmt.Sprint(rev)) if o.Timeout != 0 { args = append(args, fmt.Sprintf("--command-timeout=%s", o.Timeout)) } @@ -327,22 +288,11 @@ func (ctl *EtcdctlV3) Compact(rev int64, o config.CompactOption) (*clientv3.Comp } func (ctl *EtcdctlV3) Status() ([]*clientv3.StatusResponse, error) { - args := ctl.cmdArgs() - args = append(args, "endpoint", "status", "-w", "json") - args = append(args, "--endpoints", strings.Join(ctl.endpoints, ",")) - cmd, err := SpawnCmd(args, nil) - if err != nil { - return nil, err - } var epStatus []*struct { Endpoint string Status *clientv3.StatusResponse } - line, err := cmd.Expect("header") - if err != nil { - return nil, err - } - err = json.Unmarshal([]byte(line), &epStatus) + err := ctl.spawnJsonCmd(&epStatus, "endpoint", "status", "--endpoints", strings.Join(ctl.endpoints, ",")) if err != nil { return nil, err } @@ -354,23 +304,11 @@ func (ctl *EtcdctlV3) Status() ([]*clientv3.StatusResponse, error) { } func (ctl *EtcdctlV3) HashKV(rev int64) ([]*clientv3.HashKVResponse, error) { - args := ctl.cmdArgs() - args = append(args, "endpoint", "hashkv", "-w", "json") - args = append(args, "--endpoints", strings.Join(ctl.endpoints, ",")) - args = append(args, "--rev", fmt.Sprint(rev)) - cmd, err := SpawnCmd(args, nil) - if err != nil { - return nil, err - } var epHashKVs []*struct { Endpoint string HashKV *clientv3.HashKVResponse } - line, err := cmd.Expect("header") - if err != nil { - return nil, err - } - err = json.Unmarshal([]byte(line), &epHashKVs) + err := ctl.spawnJsonCmd(&epHashKVs, "endpoint", "hashkv", "--endpoints", strings.Join(ctl.endpoints, ","), "--rev", fmt.Sprint(rev)) if err != nil { return nil, err } @@ -440,8 +378,7 @@ func (ctl *EtcdctlV3) Defragment(o config.DefragOption) error { } func (ctl *EtcdctlV3) LeaseList() (*clientv3.LeaseLeasesResponse, error) { - args := ctl.cmdArgs() - args = append(args, "lease", "list", "-w", "json") + args := ctl.cmdArgs("lease", "list", "-w", "json") cmd, err := SpawnCmd(args, nil) if err != nil { return nil, err @@ -456,8 +393,7 @@ func (ctl *EtcdctlV3) LeaseList() (*clientv3.LeaseLeasesResponse, error) { } func (ctl *EtcdctlV3) LeaseKeepAliveOnce(id clientv3.LeaseID) (*clientv3.LeaseKeepAliveResponse, error) { - args := ctl.cmdArgs() - args = append(args, "lease", "keep-alive", strconv.FormatInt(int64(id), 16), "--once", "-w", "json") + args := ctl.cmdArgs("lease", "keep-alive", strconv.FormatInt(int64(id), 16), "--once", "-w", "json") cmd, err := SpawnCmd(args, nil) if err != nil { return nil, err @@ -472,34 +408,14 @@ func (ctl *EtcdctlV3) LeaseKeepAliveOnce(id clientv3.LeaseID) (*clientv3.LeaseKe } func (ctl *EtcdctlV3) LeaseRevoke(id clientv3.LeaseID) (*clientv3.LeaseRevokeResponse, error) { - args := ctl.cmdArgs() - args = append(args, "lease", "revoke", strconv.FormatInt(int64(id), 16), "-w", "json") - cmd, err := SpawnCmd(args, nil) - if err != nil { - return nil, err - } var resp clientv3.LeaseRevokeResponse - line, err := cmd.Expect("header") - if err != nil { - return nil, err - } - err = json.Unmarshal([]byte(line), &resp) + err := ctl.spawnJsonCmd(&resp, "lease", "revoke", strconv.FormatInt(int64(id), 16)) return &resp, err } func (ctl *EtcdctlV3) AlarmList() (*clientv3.AlarmResponse, error) { - args := ctl.cmdArgs() - args = append(args, "alarm", "list", "-w", "json") - ep, err := SpawnCmd(args, nil) - if err != nil { - return nil, err - } var resp clientv3.AlarmResponse - line, err := ep.Expect("alarm") - if err != nil { - return nil, err - } - err = json.Unmarshal([]byte(line), &resp) + err := ctl.spawnJsonCmd(&resp, "alarm", "list") return &resp, err } @@ -558,34 +474,14 @@ func (ctl *EtcdctlV3) UserAdd(name, password string, opts config.UserAddOptions) } func (ctl *EtcdctlV3) UserList() (*clientv3.AuthUserListResponse, error) { - args := ctl.cmdArgs() - args = append(args, "user", "list", "-w", "json") - cmd, err := SpawnCmd(args, nil) - if err != nil { - return nil, err - } var resp clientv3.AuthUserListResponse - line, err := cmd.Expect("header") - if err != nil { - return nil, err - } - err = json.Unmarshal([]byte(line), &resp) + err := ctl.spawnJsonCmd(&resp, "user", "list") return &resp, err } func (ctl *EtcdctlV3) UserDelete(name string) (*clientv3.AuthUserDeleteResponse, error) { - args := ctl.cmdArgs() - args = append(args, "user", "delete", name, "-w", "json") - cmd, err := SpawnCmd(args, nil) - if err != nil { - return nil, err - } var resp clientv3.AuthUserDeleteResponse - line, err := cmd.Expect("header") - if err != nil { - return nil, err - } - err = json.Unmarshal([]byte(line), &resp) + err := ctl.spawnJsonCmd(&resp, "user", "delete", name) return &resp, err }