Skip to content

Commit

Permalink
Revert to individual tests and correct async response code
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
johnmccabe authored and alexellis committed Mar 2, 2018
1 parent 017665c commit 7997a46
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 77 deletions.
90 changes: 47 additions & 43 deletions commands/invoke_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

}
69 changes: 35 additions & 34 deletions proxy/invoke_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 7997a46

Please sign in to comment.