Skip to content

Commit 838df85

Browse files
committed
e2e: add "Election" grpc-gateway test cases
Signed-off-by: Gyuho Lee <gyuhox@gmail.com>
1 parent 4b22f27 commit 838df85

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed

e2e/v3_curl_test.go

+115
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@
1515
package e2e
1616

1717
import (
18+
"encoding/base64"
1819
"encoding/json"
1920
"path"
21+
"strconv"
2022
"testing"
2123

24+
epb "github.com/coreos/etcd/etcdserver/api/v3election/v3electionpb"
2225
pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
2326
"github.com/coreos/etcd/pkg/testutil"
2427

@@ -245,3 +248,115 @@ func testV3CurlAuth(cx ctlCtx) {
245248
cx.t.Fatalf("failed testV3CurlAuth auth put with curl using prefix (%s) (%v)", p, err)
246249
}
247250
}
251+
252+
func TestV3CurlCampaignNoTLS(t *testing.T) {
253+
for _, p := range apiPrefix {
254+
testCtl(t, testV3CurlCampaign, withApiPrefix(p), withCfg(configNoTLS))
255+
}
256+
}
257+
258+
func testV3CurlCampaign(cx ctlCtx) {
259+
cdata, err := json.Marshal(&epb.CampaignRequest{
260+
Name: []byte("/election-prefix"),
261+
Value: []byte("v1"),
262+
})
263+
if err != nil {
264+
cx.t.Fatal(err)
265+
}
266+
cargs := cURLPrefixArgs(cx.epc, "POST", cURLReq{
267+
endpoint: path.Join(cx.apiPrefix, "/election/campaign"),
268+
value: string(cdata),
269+
})
270+
lines, err := spawnWithExpectLines(cargs, `"leader":{"name":"`)
271+
if err != nil {
272+
cx.t.Fatalf("failed post campaign request (%s) (%v)", cx.apiPrefix, err)
273+
}
274+
if len(lines) != 1 {
275+
cx.t.Fatalf("len(lines) expected 1, got %+v", lines)
276+
}
277+
278+
var cresp campaignResponse
279+
if err = json.Unmarshal([]byte(lines[0]), &cresp); err != nil {
280+
cx.t.Fatalf("failed to unmarshal campaign response %v", err)
281+
}
282+
ndata, err := base64.StdEncoding.DecodeString(cresp.Leader.Name)
283+
if err != nil {
284+
cx.t.Fatalf("failed to decode leader key %v", err)
285+
}
286+
kdata, err := base64.StdEncoding.DecodeString(cresp.Leader.Key)
287+
if err != nil {
288+
cx.t.Fatalf("failed to decode leader key %v", err)
289+
}
290+
291+
rev, _ := strconv.ParseInt(cresp.Leader.Rev, 10, 64)
292+
lease, _ := strconv.ParseInt(cresp.Leader.Lease, 10, 64)
293+
pdata, err := json.Marshal(&epb.ProclaimRequest{
294+
Leader: &epb.LeaderKey{
295+
Name: ndata,
296+
Key: kdata,
297+
Rev: rev,
298+
Lease: lease,
299+
},
300+
Value: []byte("v2"),
301+
})
302+
if err != nil {
303+
cx.t.Fatal(err)
304+
}
305+
if err = cURLPost(cx.epc, cURLReq{
306+
endpoint: path.Join(cx.apiPrefix, "/election/proclaim"),
307+
value: string(pdata),
308+
expected: `"revision":`,
309+
}); err != nil {
310+
cx.t.Fatalf("failed post proclaim request (%s) (%v)", cx.apiPrefix, err)
311+
}
312+
}
313+
314+
func TestV3CurlProclaimMissiongLeaderKeyNoTLS(t *testing.T) {
315+
for _, p := range apiPrefix {
316+
testCtl(t, testV3CurlProclaimMissiongLeaderKey, withApiPrefix(p), withCfg(configNoTLS))
317+
}
318+
}
319+
320+
func testV3CurlProclaimMissiongLeaderKey(cx ctlCtx) {
321+
pdata, err := json.Marshal(&epb.ProclaimRequest{Value: []byte("v2")})
322+
if err != nil {
323+
cx.t.Fatal(err)
324+
}
325+
if err != nil {
326+
cx.t.Fatal(err)
327+
}
328+
if err = cURLPost(cx.epc, cURLReq{
329+
endpoint: path.Join(cx.apiPrefix, "/election/proclaim"),
330+
value: string(pdata),
331+
expected: `{"error":"missing \"leader\" field","code":2}`,
332+
}); err != nil {
333+
cx.t.Fatalf("failed post proclaim request (%s) (%v)", cx.apiPrefix, err)
334+
}
335+
}
336+
337+
func TestV3CurlResignMissiongLeaderKeyNoTLS(t *testing.T) {
338+
for _, p := range apiPrefix {
339+
testCtl(t, testV3CurlResignMissiongLeaderKey, withApiPrefix(p), withCfg(configNoTLS))
340+
}
341+
}
342+
343+
func testV3CurlResignMissiongLeaderKey(cx ctlCtx) {
344+
if err := cURLPost(cx.epc, cURLReq{
345+
endpoint: path.Join(cx.apiPrefix, "/election/resign"),
346+
value: `{}`,
347+
expected: `{"error":"missing \"leader\" field","code":2}`,
348+
}); err != nil {
349+
cx.t.Fatalf("failed post resign request (%s) (%v)", cx.apiPrefix, err)
350+
}
351+
}
352+
353+
// to manually decode; JSON marshals integer fields with
354+
// string types, so can't unmarshal with epb.CampaignResponse
355+
type campaignResponse struct {
356+
Leader struct {
357+
Name string `json:"name,omitempty"`
358+
Key string `json:"key,omitempty"`
359+
Rev string `json:"rev,omitempty"`
360+
Lease string `json:"lease,omitempty"`
361+
} `json:"leader,omitempty"`
362+
}

0 commit comments

Comments
 (0)