From 407459c88ec918ae8c6ffc1e700668bd7a75c1dc Mon Sep 17 00:00:00 2001 From: Benjamin Wang Date: Thu, 24 Aug 2023 17:10:01 +0100 Subject: [PATCH] Update test case to accept random spaces in case TestV3CurlTxn Signed-off-by: Benjamin Wang --- pkg/expect/expect.go | 20 +++++++++++++++++++- tests/e2e/v3_curl_test.go | 10 +++++----- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/pkg/expect/expect.go b/pkg/expect/expect.go index b7e2978af5c..86d659c8a92 100644 --- a/pkg/expect/expect.go +++ b/pkg/expect/expect.go @@ -23,6 +23,7 @@ import ( "io" "os" "os/exec" + "regexp" "strings" "sync" "syscall" @@ -224,7 +225,24 @@ func (ep *ExpectProcess) ExpectFunc(ctx context.Context, f func(string) bool) (s // ExpectWithContext returns the first line containing the given string. func (ep *ExpectProcess) ExpectWithContext(ctx context.Context, s string) (string, error) { - return ep.ExpectFunc(ctx, func(txt string) bool { return strings.Contains(txt, s) }) + return ep.ExpectFunc(ctx, func(txt string) bool { + if strings.Contains(txt, s) { + return true + } + + s1 := s + if strings.HasPrefix(s1, "EXPR") { + s1 = s1[4:] + } else { + return false + } + + r, err := regexp.Compile(s1) + if err != nil { + return false + } + return r.MatchString(txt) + }) } // Expect returns the first line containing the given string. diff --git a/tests/e2e/v3_curl_test.go b/tests/e2e/v3_curl_test.go index ddd1da103dc..0c19188c986 100644 --- a/tests/e2e/v3_curl_test.go +++ b/tests/e2e/v3_curl_test.go @@ -180,7 +180,7 @@ func testV3CurlTxn(cx ctlCtx) { if jerr != nil { cx.t.Fatal(jerr) } - expected := `"succeeded":true,"responses":[{"response_put":{"header":{"revision":"2"}}}]` + expected := `EXPR.*"succeeded":true,\s*"responses":\[{"response_put":{"header":{"revision":"2"}}}].*` p := cx.apiPrefix if err := e2e.CURLPost(cx.epc, e2e.CURLReq{Endpoint: path.Join(p, "/kv/txn"), Value: string(jsonDat), Expected: expected}); err != nil { cx.t.Fatalf("failed testV3CurlTxn txn with curl using prefix (%s) (%v)", p, err) @@ -188,7 +188,7 @@ func testV3CurlTxn(cx ctlCtx) { // was crashing etcd server malformed := `{"compare":[{"result":0,"target":1,"key":"Zm9v","TargetUnion":null}],"success":[{"Request":{"RequestPut":{"key":"Zm9v","value":"YmFy"}}}]}` - if err := e2e.CURLPost(cx.epc, e2e.CURLReq{Endpoint: path.Join(p, "/kv/txn"), Value: malformed, Expected: `"code":3,"message":"etcdserver: key not found"`}); err != nil { + if err := e2e.CURLPost(cx.epc, e2e.CURLReq{Endpoint: path.Join(p, "/kv/txn"), Value: malformed, Expected: `EXPR.*"code":3,\s*"message":"etcdserver: key not found".*`}); err != nil { cx.t.Fatalf("failed testV3CurlTxn put with curl using prefix (%s) (%v)", p, err) } @@ -239,7 +239,7 @@ func testV3CurlAuth(cx ctlCtx) { testutil.AssertNil(cx.t, err) // fail put no auth - if err = e2e.CURLPost(cx.epc, e2e.CURLReq{Endpoint: path.Join(p, "/kv/put"), Value: string(putreq), Expected: `"code":3,"message":"etcdserver: user name is empty"`}); err != nil { + if err = e2e.CURLPost(cx.epc, e2e.CURLReq{Endpoint: path.Join(p, "/kv/put"), Value: string(putreq), Expected: `EXPR.*"code":3,\s*"message":"etcdserver: user name is empty".*`}); err != nil { cx.t.Fatalf("failed testV3CurlAuth no auth put with curl using prefix (%s) (%v)", p, err) } @@ -354,7 +354,7 @@ func testV3CurlProclaimMissiongLeaderKey(cx ctlCtx) { if err = e2e.CURLPost(cx.epc, e2e.CURLReq{ Endpoint: path.Join(cx.apiPrefix, "/election/proclaim"), Value: string(pdata), - Expected: `{"code":2,"message":"\"leader\" field must be provided"}`, + Expected: `EXPR.*{"code":2,\s*"message":".*leader.* field must be provided"}.*`, }); err != nil { cx.t.Fatalf("failed post proclaim request (%s) (%v)", cx.apiPrefix, err) } @@ -370,7 +370,7 @@ func testV3CurlResignMissiongLeaderKey(cx ctlCtx) { if err := e2e.CURLPost(cx.epc, e2e.CURLReq{ Endpoint: path.Join(cx.apiPrefix, "/election/resign"), Value: `{}`, - Expected: `{"code":2,"message":"\"leader\" field must be provided"}`, + Expected: `EXPR.*{"code":2,\s*"message":".*leader.* field must be provided"}.*`, }); err != nil { cx.t.Fatalf("failed post resign request (%s) (%v)", cx.apiPrefix, err) }