Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add test case to cover all election REST APIs #16570

Merged
merged 1 commit into from
Sep 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading