Skip to content

Commit

Permalink
test: add test case to cover all election REST APIs
Browse files Browse the repository at this point in the history
Signed-off-by: Benjamin Wang <[email protected]>
  • Loading branch information
ahrtr committed Sep 10, 2023
1 parent 9e7f837 commit 18f70c2
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions tests/e2e/v3_curl_election_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"strconv"
"testing"

"github.com/stretchr/testify/require"

"go.etcd.io/etcd/pkg/v3/expect"
epb "go.etcd.io/etcd/server/v3/etcdserver/api/v3election/v3electionpb"
"go.etcd.io/etcd/tests/v3/framework/e2e"
Expand All @@ -33,6 +35,7 @@ func TestCurlV3CampaignNoTLS(t *testing.T) {
}

func testCurlV3Campaign(cx ctlCtx) {
// campaign
cdata, err := json.Marshal(&epb.CampaignRequest{
Name: []byte("/election-prefix"),
Value: []byte("v1"),
Expand Down Expand Up @@ -65,6 +68,27 @@ func testCurlV3Campaign(cx ctlCtx) {
cx.t.Fatalf("failed to decode leader key %v", err)
}

// observe
observeReq, err := json.Marshal(&epb.LeaderRequest{
Name: []byte("/election-prefix"),
})
require.NoError(cx.t, err)

clus := cx.epc
args := e2e.CURLPrefixArgsCluster(clus.Cfg, clus.Procs[0], "POST", e2e.CURLReq{
Endpoint: "/v3/election/observe",
Value: string(observeReq),
})
proc, err := e2e.SpawnCmd(args, nil)
require.NoError(cx.t, err)

proc.ExpectWithContext(context.TODO(), expect.ExpectedResponse{
Value: fmt.Sprintf(`"key":"%s"`, cresp.Leader.Key),
})
err = proc.Stop()
require.NoError(cx.t, err)

// proclaim
rev, _ := strconv.ParseInt(cresp.Leader.Rev, 10, 64)
lease, _ := strconv.ParseInt(cresp.Leader.Lease, 10, 64)
pdata, err := json.Marshal(&epb.ProclaimRequest{
Expand Down Expand Up @@ -120,6 +144,20 @@ func testCurlV3ResignMissiongLeaderKey(cx ctlCtx) {
}
}

func TestCurlV3ElectionLeader(t *testing.T) {
testCtl(t, testCurlV3ElectionLeader, withCfg(*e2e.NewConfigNoTLS()))
}

func testCurlV3ElectionLeader(cx ctlCtx) {
if err := e2e.CURLPost(cx.epc, e2e.CURLReq{
Endpoint: "/v3/election/leader",
Value: `{"name": "aGVsbG8="}`, // base64 encoded string "hello"
Expected: expect.ExpectedResponse{Value: `election: no leader`},
}); err != nil {
cx.t.Fatalf("testCurlV3ElectionLeader failed to get leader (%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 18f70c2

Please sign in to comment.