From 7997a467e0d1a49eae5417b8190ae0f5f5e87bac Mon Sep 17 00:00:00 2001 From: John McCabe Date: Fri, 2 Mar 2018 00:52:17 +0000 Subject: [PATCH] Revert to individual tests and correct async response code This commit reverts to using individual tests rather than t.Run subtests as requested. It also corrects the statuscode returned when submitting an invoke request asynchronously (Accepted - 202). Signed-off-by: John McCabe --- commands/invoke_test.go | 90 +++++++++++++++++++++-------------------- proxy/invoke_test.go | 69 +++++++++++++++---------------- 2 files changed, 82 insertions(+), 77 deletions(-) diff --git a/commands/invoke_test.go b/commands/invoke_test.go index 4e9771299..087a6b601 100644 --- a/commands/invoke_test.go +++ b/commands/invoke_test.go @@ -25,58 +25,62 @@ func Test_invoke(t *testing.T) { ResponseStatusCode: http.StatusOK, ResponseBody: expected_invoke_response, }, + }) + defer s.Close() + + os.Stdin, _ = ioutil.TempFile("", "stdin") + os.Stdin.WriteString("test-data") + os.Stdin.Seek(0, 0) + defer func() { + os.Remove(os.Stdin.Name()) + }() + + stdOut := test.CaptureStdout(func() { + faasCmd.SetArgs([]string{ + "invoke", + "--gateway=" + s.URL, + funcName, + }) + faasCmd.Execute() + }) + + if found, err := regexp.MatchString(`(?m:`+expected_invoke_response+`)`, stdOut); err != nil || !found { + t.Fatalf("Output is not as expected:\nExpected:\n%s\n Got:\n%s", `(?m:`+expected_invoke_response+`)`, stdOut) + } + +} + +func Test_async_invoke(t *testing.T) { + funcName := "test-1" + + s := test.MockHttpServer(t, []test.Request{ { Method: http.MethodPost, Uri: "/async-function/" + funcName, - ResponseStatusCode: http.StatusOK, - ResponseBody: expected_invoke_response, + ResponseStatusCode: http.StatusAccepted, }, }) defer s.Close() - t.Run("Sync", func(t *testing.T) { - os.Stdin, _ = ioutil.TempFile("", "stdin") - os.Stdin.WriteString("test-data") - os.Stdin.Seek(0, 0) - defer func() { - os.Remove(os.Stdin.Name()) - }() - - stdOut := test.CaptureStdout(func() { - faasCmd.SetArgs([]string{ - "invoke", - "--gateway=" + s.URL, - funcName, - }) - faasCmd.Execute() - }) - - if found, err := regexp.MatchString(`(?m:`+expected_invoke_response+`)`, stdOut); err != nil || !found { - t.Fatalf("Output is not as expected:\n%s", stdOut) - } - }) + os.Stdin, _ = ioutil.TempFile("", "stdin") + os.Stdin.WriteString("test-data") + os.Stdin.Seek(0, 0) + defer func() { + os.Remove(os.Stdin.Name()) + }() - t.Run("Async", func(t *testing.T) { - os.Stdin, _ = ioutil.TempFile("", "stdin") - os.Stdin.WriteString("test-data") - os.Stdin.Seek(0, 0) - defer func() { - os.Remove(os.Stdin.Name()) - }() - - stdOut := test.CaptureStdout(func() { - faasCmd.SetArgs([]string{ - "invoke", - "--gateway=" + s.URL, - "--async", - funcName, - }) - faasCmd.Execute() + stdOut := test.CaptureStdout(func() { + faasCmd.SetArgs([]string{ + "invoke", + "--gateway=" + s.URL, + "--async", + funcName, }) - - if found, err := regexp.MatchString(`(?m:`+expected_invoke_response+`)`, stdOut); err != nil || !found { - t.Fatalf("Async output is not as expected:\n%s", stdOut) - } + faasCmd.Execute() }) + if found, err := regexp.MatchString(`(?m:)`, stdOut); err != nil || !found { + t.Fatalf("Async output is not as expected:\nExpected:\n%s\n Got:\n%s", `(?m:)`, stdOut) + } + } diff --git a/proxy/invoke_test.go b/proxy/invoke_test.go index f553f81c4..658ff3210 100644 --- a/proxy/invoke_test.go +++ b/proxy/invoke_test.go @@ -15,42 +15,43 @@ import ( ) func Test_InvokeFunction(t *testing.T) { - s := test.MockHttpServerStatus(t, http.StatusOK, http.StatusOK) + s := test.MockHttpServerStatus(t, http.StatusOK) defer s.Close() - t.Run("Sync", func(t *testing.T) { - bytesIn := []byte("test data") - _, err := InvokeFunction( - s.URL, - "function", - &bytesIn, - "text/plain", - []string{}, - []string{}, - false, - ) - - if err != nil { - t.Fatalf("Error returned: %s", err) - } - }) - - t.Run("Async", func(t *testing.T) { - bytesIn := []byte("test data") - _, err := InvokeFunction( - s.URL, - "function", - &bytesIn, - "text/plain", - []string{}, - []string{}, - true, - ) - - if err != nil { - t.Fatalf("Error returned: %s", err) - } - }) + bytesIn := []byte("test data") + _, err := InvokeFunction( + s.URL, + "function", + &bytesIn, + "text/plain", + []string{}, + []string{}, + false, + ) + + if err != nil { + t.Fatalf("Error returned: %s", err) + } +} + +func Test_InvokeFunction_Async(t *testing.T) { + s := test.MockHttpServerStatus(t, http.StatusAccepted) + defer s.Close() + + bytesIn := []byte("test data") + _, err := InvokeFunction( + s.URL, + "function", + &bytesIn, + "text/plain", + []string{}, + []string{}, + true, + ) + + if err != nil { + t.Fatalf("Error returned: %s", err) + } } func Test_InvokeFunction_Not2xx(t *testing.T) {