Skip to content

Commit

Permalink
tests: Migrate endpoint tests to common framework
Browse files Browse the repository at this point in the history
  • Loading branch information
kkkkun committed Mar 10, 2022
1 parent 8ac44ff commit ddac11c
Show file tree
Hide file tree
Showing 6 changed files with 236 additions and 93 deletions.
117 changes: 117 additions & 0 deletions tests/common/endpoint_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// 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"

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

func TestEndpointStatus(t *testing.T) {
testRunner.BeforeTest(t)
tcs := []struct {
name string
clusterSize int
}{
{
name: "Standalone",
clusterSize: 1,
},
{
name: "Cluster",
clusterSize: 3,
},
}
for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
clus := testRunner.NewCluster(t, config.ClusterConfig{ClusterSize: tc.clusterSize})
defer clus.Close()
testutils.ExecuteWithTimeout(t, 10*time.Second, func() {
status, err := clus.Client().Status()
if err != nil {
t.Fatalf("get endpoint status error: %v", err)
}
assert.Equal(t, tc.clusterSize, len(status), "get endpoint status is not equal ")
})
})
}
}

func TestEndpointHashKV(t *testing.T) {
testRunner.BeforeTest(t)
tcs := []struct {
name string
clusterSize int
}{
{
name: "Standalone",
clusterSize: 1,
},
{
name: "Cluster",
clusterSize: 3,
},
}
for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
clus := testRunner.NewCluster(t, config.ClusterConfig{ClusterSize: tc.clusterSize})
defer clus.Close()
testutils.ExecuteWithTimeout(t, 10*time.Second, func() {
hashKVs, err := clus.Client().HashKV(0)
if err != nil {
t.Fatalf("get endpoint hashkv error: %v", err)
}
assert.Equal(t, tc.clusterSize, len(hashKVs), "get hashkv is not equal ")
})
})
}
}

func TestEndpointHealth(t *testing.T) {
testRunner.BeforeTest(t)
tcs := []struct {
name string
clusterSize int
}{
{
name: "Standalone",
clusterSize: 1,
},
{
name: "Cluster",
clusterSize: 3,
},
}
for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
clus := testRunner.NewCluster(t, config.ClusterConfig{ClusterSize: tc.clusterSize})
defer clus.Close()
testutils.ExecuteWithTimeout(t, 10*time.Second, func() {
epHealths, err := clus.Client().Health()
if err != nil {
t.Fatalf("get endpoint health error: %v", err)
}
assert.Equal(t, tc.clusterSize, len(epHealths), "get health is not equal ")
for _, health := range epHealths {
assert.Equal(t, true, health.Health)
}
})
})
}
}
93 changes: 0 additions & 93 deletions tests/e2e/ctl_v3_endpoint_test.go

This file was deleted.

17 changes: 17 additions & 0 deletions tests/framework/config/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,20 @@ type DeleteOptions struct {
FromKey bool
End string
}

type EpStatus struct {
Endpoint string
Status *clientv3.StatusResponse
}

type EpHashKV struct {
Endpoint string
HashKV *clientv3.HashKVResponse
}

type EpHealth struct {
Ep string `json:"endpoint"`
Health bool `json:"health"`
Took string `json:"took"`
Error string `json:"error,omitempty"`
}
52 changes: 52 additions & 0 deletions tests/framework/e2e/etcdctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,55 @@ func (ctl *EtcdctlV3) flags() map[string]string {
fmap["endpoints"] = strings.Join(ctl.endpoints, ",")
return fmap
}

func (ctl *EtcdctlV3) Status() ([]*config.EpStatus, 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 resp []*config.EpStatus
line, err := cmd.Expect("header")
if err != nil {
return nil, err
}
err = json.Unmarshal([]byte(line), &resp)
return resp, err
}

func (ctl *EtcdctlV3) HashKV(rev int64) ([]*config.EpHashKV, 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 resp []*config.EpHashKV
line, err := cmd.Expect("header")
if err != nil {
return nil, err
}
err = json.Unmarshal([]byte(line), &resp)
return resp, err
}

func (ctl *EtcdctlV3) Health() ([]*config.EpHealth, error) {
args := ctl.cmdArgs()
args = append(args, "endpoint", "health", "-w json")
args = append(args, "--endpoints", strings.Join(ctl.endpoints, ","))
cmd, err := SpawnCmd(args, nil)
if err != nil {
return nil, err
}
var resp []*config.EpHealth
line, err := cmd.Expect("health")
if err != nil {
return nil, err
}
err = json.Unmarshal([]byte(line), &resp)
return resp, err
}
46 changes: 46 additions & 0 deletions tests/framework/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ import (
"context"
"fmt"
"testing"
"time"

"go.etcd.io/etcd/api/v3/v3rpc/rpctypes"
"go.etcd.io/etcd/client/pkg/v3/testutil"
"go.etcd.io/etcd/client/pkg/v3/transport"
clientv3 "go.etcd.io/etcd/client/v3"
Expand Down Expand Up @@ -141,3 +143,47 @@ func (c integrationClient) Delete(key string, o config.DeleteOptions) (*clientv3
}
return c.Client.Delete(context.Background(), key, clientOpts...)
}

func (c integrationClient) Status() ([]*config.EpStatus, error) {
endpoints := c.Client.Endpoints()
var resp []*config.EpStatus
for _, ep := range endpoints {
status, err := c.Client.Status(context.Background(), ep)
if err != nil {
return nil, err
}
resp = append(resp, &config.EpStatus{Endpoint: ep, Status: status})
}
return resp, nil
}

func (c integrationClient) HashKV(rev int64) ([]*config.EpHashKV, error) {
endpoints := c.Client.Endpoints()
var resp []*config.EpHashKV
for _, ep := range endpoints {
hashKV, err := c.Client.HashKV(context.Background(), ep, rev)
if err != nil {
return nil, err
}
resp = append(resp, &config.EpHashKV{Endpoint: ep, HashKV: hashKV})
}
return resp, nil
}

func (c integrationClient) Health() ([]*config.EpHealth, error) {
endpoints := c.Client.Endpoints()
var resp []*config.EpHealth
for _, ep := range endpoints {
st := time.Now()
_, err := c.Client.Get(context.Background(), "health")
eh := config.EpHealth{Ep: ep, Health: false, Took: time.Since(st).String()}
// permission denied is OK since proposal goes through consensus to get it
if err == nil || err == rpctypes.ErrPermissionDenied {
eh.Health = true
} else {
eh.Error = err.Error()
}
resp = append(resp, &eh)
}
return resp, nil
}
4 changes: 4 additions & 0 deletions tests/framework/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,8 @@ 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)

Status() ([]*config.EpStatus, error)
HashKV(rev int64) ([]*config.EpHashKV, error)
Health() ([]*config.EpHealth, error)
}

0 comments on commit ddac11c

Please sign in to comment.