Skip to content

Commit

Permalink
test: add v3 curl test to cover all maintainence REST API
Browse files Browse the repository at this point in the history
Signed-off-by: Benjamin Wang <[email protected]>
  • Loading branch information
ahrtr committed Aug 28, 2023
1 parent d09b8d4 commit 94d052b
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 0 deletions.
16 changes: 16 additions & 0 deletions tests/e2e/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package e2e

import (
"context"
"encoding/json"
"fmt"
"strings"
"testing"
Expand Down Expand Up @@ -108,3 +109,18 @@ func curl(endpoint string, method string, curlReq e2e.CURLReq, connType e2e.Clie
}
return strings.Join(lines, "\n"), nil
}

func runCommandAndReadJsonOutput(args []string) (map[string]interface{}, error) {
lines, err := e2e.RunUtilCompletion(args, nil)
if err != nil {
return nil, err
}

var resp map[string]interface{}
err = json.Unmarshal([]byte(strings.Join(lines, "\n")), &resp)
if err != nil {
return nil, err
}

return resp, nil
}
115 changes: 115 additions & 0 deletions tests/e2e/v3_curl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"go.etcd.io/etcd/api/v3/authpb"
pb "go.etcd.io/etcd/api/v3/etcdserverpb"
"go.etcd.io/etcd/api/v3/v3rpc/rpctypes"
"go.etcd.io/etcd/api/v3/version"
"go.etcd.io/etcd/client/pkg/v3/testutil"
"go.etcd.io/etcd/pkg/v3/expect"
epb "go.etcd.io/etcd/server/v3/etcdserver/api/v3election/v3electionpb"
Expand Down Expand Up @@ -385,6 +386,120 @@ func testV3CurlMaintenanceAlarmMissiongAlarm(cx ctlCtx) {
}
}

func TestV3CurlMaintenanceStatus(t *testing.T) {
testCtl(t, testV3CurlMaintenanceStatus, withCfg(*e2e.NewConfigNoTLS()))
}

func testV3CurlMaintenanceStatus(cx ctlCtx) {
clus := cx.epc
args := e2e.CURLPrefixArgsCluster(clus.Cfg, clus.Procs[rand.Intn(clus.Cfg.ClusterSize)], "POST", e2e.CURLReq{
Endpoint: "/v3/maintenance/status",
Value: "{}",
})
resp, err := runCommandAndReadJsonOutput(args)
require.NoError(cx.t, err)

requiredFields := []string{"version", "dbSize", "leader", "raftIndex", "raftTerm", "raftAppliedIndex", "dbSizeInUse", "storageVersion"}
for _, field := range requiredFields {
if _, ok := resp[field]; !ok {
cx.t.Fatalf("Field %q not found in (%v)", field, resp)
}
}

actualVersion, _ := resp["version"]
require.Equal(cx.t, version.Version, actualVersion)
}

func TestV3CurlMaintenanceDefragment(t *testing.T) {
testCtl(t, testV3CurlMaintenanceDefragment, withCfg(*e2e.NewConfigNoTLS()))
}

func testV3CurlMaintenanceDefragment(cx ctlCtx) {
if err := e2e.CURLPost(cx.epc, e2e.CURLReq{
Endpoint: "/v3/maintenance/defragment",
Value: "{}",
Expected: expect.ExpectedResponse{
Value: "{}",
},
}); err != nil {
cx.t.Fatalf("failed post maintenance defragment request: (%v)", err)
}
}

func TestV3CurlMaintenanceHash(t *testing.T) {
testCtl(t, testV3CurlMaintenanceHash, withCfg(*e2e.NewConfigNoTLS()))
}

func testV3CurlMaintenanceHash(cx ctlCtx) {
clus := cx.epc
args := e2e.CURLPrefixArgsCluster(clus.Cfg, clus.Procs[rand.Intn(clus.Cfg.ClusterSize)], "POST", e2e.CURLReq{
Endpoint: "/v3/maintenance/hash",
Value: "{}",
})
resp, err := runCommandAndReadJsonOutput(args)
require.NoError(cx.t, err)

requiredFields := []string{"header", "hash"}
for _, field := range requiredFields {
if _, ok := resp[field]; !ok {
cx.t.Fatalf("Field %q not found in (%v)", field, resp)
}
}
}

func TestV3CurlMaintenanceHashKV(t *testing.T) {
testCtl(t, testV3CurlMaintenanceHashKV, withCfg(*e2e.NewConfigNoTLS()))
}

func testV3CurlMaintenanceHashKV(cx ctlCtx) {
clus := cx.epc
args := e2e.CURLPrefixArgsCluster(clus.Cfg, clus.Procs[rand.Intn(clus.Cfg.ClusterSize)], "POST", e2e.CURLReq{
Endpoint: "/v3/maintenance/hashkv",
Value: "{}",
})
resp, err := runCommandAndReadJsonOutput(args)
require.NoError(cx.t, err)

requiredFields := []string{"header", "hash", "compact_revision", "hash_revision"}
for _, field := range requiredFields {
if _, ok := resp[field]; !ok {
cx.t.Fatalf("Field %q not found in (%v)", field, resp)
}
}
}

func TestV3CurlMaintenanceMoveleader(t *testing.T) {
testCtl(t, testV3CurlMaintenanceMoveleader, withCfg(*e2e.NewConfigNoTLS()))
}

func testV3CurlMaintenanceMoveleader(cx ctlCtx) {
if err := e2e.CURLPost(cx.epc, e2e.CURLReq{
Endpoint: "/v3/maintenance/transfer-leadership",
Value: `{"targetID": 123}`,
Expected: expect.ExpectedResponse{
Value: `"message":"etcdserver: bad leader transferee"`,
},
}); err != nil {
cx.t.Fatalf("failed post maintenance moveleader request: (%v)", err)
}
}

func TestV3CurlMaintenanceDowngrade(t *testing.T) {
testCtl(t, testV3CurlMaintenanceDowngrade, withCfg(*e2e.NewConfigNoTLS()))
}

func testV3CurlMaintenanceDowngrade(cx ctlCtx) {
if err := e2e.CURLPost(cx.epc, e2e.CURLReq{
Endpoint: "/v3/maintenance/downgrade",
Value: `{"action": 0, "version": "3.0"}`,
Expected: expect.ExpectedResponse{
Value: `"message":"etcdserver: invalid downgrade target version"`,
},
}); err != nil {
cx.t.Fatalf("failed post maintenance downgrade request: (%v)", err)
}
}

// to manually decode; JSON marshals integer fields with
// string types, so can't unmarshal with epb.CampaignResponse
type campaignResponse struct {
Expand Down

0 comments on commit 94d052b

Please sign in to comment.