Skip to content

Commit

Permalink
Add test for providers which don't support namespaces
Browse files Browse the repository at this point in the history
A failing unit test was added to reproduce issue #39, the test
was then made to pass by handling the 404 error we know to be
thrown when the /system/namespaces endpoint is missing.

Fixes: #39

Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
  • Loading branch information
alexellis committed Oct 19, 2019
1 parent 752212c commit 7560324
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
16 changes: 9 additions & 7 deletions types/function_list_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,16 @@ func (s *FunctionLookupBuilder) getNamespaces() ([]string, error) {
defer res.Body.Close()
}

bytesOut, err := ioutil.ReadAll(res.Body)
if err != nil {
return namespaces, err
}
if res.StatusCode != http.StatusNotFound {
bytesOut, err := ioutil.ReadAll(res.Body)
if err != nil {
return namespaces, err
}

err = json.Unmarshal(bytesOut, &namespaces)
if err != nil {
return namespaces, err
err = json.Unmarshal(bytesOut, &namespaces)
if err != nil {
return namespaces, err
}
}

return namespaces, err
Expand Down
21 changes: 21 additions & 0 deletions types/function_list_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,27 @@ func Test_GetNamespaces(t *testing.T) {
}
}

func Test_GetNamespaces_ProviderGives404(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("Not available"))
}))

client := srv.Client()
builder := FunctionLookupBuilder{
Client: client,
GatewayURL: srv.URL,
}

namespaces, err := builder.getNamespaces()
if err != nil {
t.Errorf("%s", err.Error())
}
if len(namespaces) != 0 {
t.Errorf("Namespaces - want: %d, got: %d", 2, len(namespaces))
}
}

func Test_GetFunctions(t *testing.T) {

srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down

0 comments on commit 7560324

Please sign in to comment.