Skip to content

Commit

Permalink
tests(gateapi): gateapi generated code expects valid JSON and headers
Browse files Browse the repository at this point in the history
This also removes "*malformed" test cases since 2.14.4 doesn't return parsing errors

swagger-api/swagger-codegen#10434 reports the issue.
hopefully swagger-api/swagger-codegen#10429 be merged will add that ability back in.
  • Loading branch information
kevinawoo committed Aug 18, 2020
1 parent b395bc1 commit dae229f
Show file tree
Hide file tree
Showing 16 changed files with 24 additions and 524 deletions.
36 changes: 1 addition & 35 deletions cmd/account/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,21 +107,6 @@ func TestAccountGet_flags(t *testing.T) {
}
}

func TestAccountGet_malformed(t *testing.T) {
ts := testGateAccountGetMalformed()
defer ts.Close()

rootCmd, rootOpts := cmd.NewCmdRoot(ioutil.Discard, ioutil.Discard)
rootCmd.AddCommand(NewAccountCmd(rootOpts))

args := []string{"account", "get", ACCOUNT, "--gate-endpoint=" + ts.URL}
rootCmd.SetArgs(args)
err := rootCmd.Execute()
if err == nil { // Success is actually failure here, return payload is malformed.
t.Fatalf("Command failed with: %d", err)
}
}

func TestAccountGet_fail(t *testing.T) {
ts := testGateFail()
defer ts.Close()
Expand All @@ -142,31 +127,12 @@ func TestAccountGet_fail(t *testing.T) {
func testGateAccountGetSuccess() *httptest.Server {
mux := util.TestGateMuxWithVersionHandler()
mux.Handle("/credentials/"+ACCOUNT, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("content-type", "application/json")
fmt.Fprintln(w, strings.TrimSpace(accountJson))
}))
return httptest.NewServer(mux)
}

// testGateAccountGetMalformed returns a malformed list of pipeline configs.
func testGateAccountGetMalformed() *httptest.Server {
mux := util.TestGateMuxWithVersionHandler()
mux.Handle("/credentials/"+ACCOUNT, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, strings.TrimSpace(malformedAccountGetJson))
}))
return httptest.NewServer(mux)
}

const malformedAccountGetJson = `
"type": "kubernetes",
"providerVersion": "v2",
"environment": "self",
"skin": "v2",
"name": "self",
"cloudProvider": "kubernetes",
"accountType": "self"
}
`

const accountJson = `
{
"type": "kubernetes",
Expand Down
40 changes: 1 addition & 39 deletions cmd/account/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,6 @@ func TestAccountList_basic(t *testing.T) {
}
}

func TestAccountList_malformed(t *testing.T) {
ts := testGateAccountListMalformed()
defer ts.Close()

rootCmd, options := cmd.NewCmdRoot(ioutil.Discard, ioutil.Discard)
rootCmd.AddCommand(NewAccountCmd(options))

args := []string{"account", "list", "--gate-endpoint=" + ts.URL}
rootCmd.SetArgs(args)
err := rootCmd.Execute()
if err == nil {
t.Fatalf("Command failed with: %s", err)
}
}

func TestAccountList_fail(t *testing.T) {
ts := testGateFail()
defer ts.Close()
Expand All @@ -74,35 +59,12 @@ func TestAccountList_fail(t *testing.T) {
func testGateAccountListSuccess() *httptest.Server {
mux := util.TestGateMuxWithVersionHandler()
mux.Handle("/credentials/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("content-type", "application/json")
fmt.Fprintln(w, strings.TrimSpace(accountListJson))
}))
return httptest.NewServer(mux)
}

func testGateAccountListMalformed() *httptest.Server {
mux := util.TestGateMuxWithVersionHandler()
mux.Handle("/credentials/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, strings.TrimSpace(malformedAccountListJson))
}))
return httptest.NewServer(mux)
}

const malformedAccountListJson = `
{
"type": "kubernetes",
"skin": "v2",
"providerVersion": "v2",
"name": "foobar"
},
{
"type": "dockerRegistry",
"skin": "v1",
"providerVersion": "v1",
"name": "dockerhub"
}
]
`

const accountListJson = `[
{
"type": "kubernetes",
Expand Down
3 changes: 3 additions & 0 deletions cmd/application/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,20 +78,23 @@ func testGateApplicationDeleteSuccess() *httptest.Server {
mux.Handle("/applications/"+APP, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
payload := map[string]string{} // We don't use the payload, we are just checking if the target app exists.
b, _ := json.Marshal(&payload)
w.Header().Add("content-type", "application/json")
fmt.Fprintln(w, string(b))
}))
mux.Handle("/tasks", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
payload := map[string]string{
"ref": "/tasks/id",
}
b, _ := json.Marshal(&payload)
w.Header().Add("content-type", "application/json")
fmt.Fprintln(w, string(b))
}))
mux.Handle("/tasks/id", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
payload := map[string]string{
"status": "SUCCEEDED",
}
b, _ := json.Marshal(&payload)
w.Header().Add("content-type", "application/json")
fmt.Fprintln(w, string(b))
}))
return httptest.NewServer(mux)
Expand Down
53 changes: 1 addition & 52 deletions cmd/application/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,21 +114,6 @@ func TestApplicationGet_flags(t *testing.T) {
}
}

func TestApplicationGet_malformed(t *testing.T) {
ts := testGateApplicationGetMalformed()
defer ts.Close()

rootCmd, rootOpts := cmd.NewCmdRoot(ioutil.Discard, ioutil.Discard)
rootCmd.AddCommand(NewApplicationCmd(rootOpts))

args := []string{"application", "get", APP, "--gate-endpoint=" + ts.URL}
rootCmd.SetArgs(args)
err := rootCmd.Execute()
if err == nil { // Success is actually failure here, return payload is malformed.
t.Fatalf("Command failed with: %d", err)
}
}

func TestApplicationGet_fail(t *testing.T) {
ts := testGateFail()
defer ts.Close()
Expand All @@ -149,48 +134,12 @@ func TestApplicationGet_fail(t *testing.T) {
func testGateApplicationGetSuccess() *httptest.Server {
mux := util.TestGateMuxWithVersionHandler()
mux.Handle("/applications/"+APP, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("content-type", "application/json")
fmt.Fprintln(w, strings.TrimSpace(applicationJsonExpanded))
}))
return httptest.NewServer(mux)
}

// testGateApplicationGetMalformed returns a malformed list of pipeline configs.
func testGateApplicationGetMalformed() *httptest.Server {
mux := util.TestGateMuxWithVersionHandler()
mux.Handle("/applications/"+APP, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, strings.TrimSpace(malformedApplicationGetJson))
}))
return httptest.NewServer(mux)
}

const malformedApplicationGetJson = `
"accounts": "account1",
"cloudproviders": [
"gce",
"kubernetes"
],
"createTs": "1527261941734",
"email": "app",
"instancePort": 80,
"lastModifiedBy": "anonymous",
"name": "app",
"permissions": {
"EXECUTE": [
"admin-group"
],
"READ": [
"admin-group",
"user-group"
],
"WRITE": [
"admin-group"
]
},
"updateTs": "1527261941735",
"user": "anonymous"
}
`

// GET /applications/{app} returns an envelope with 'attributes' and 'clusters'.
const applicationJsonExpanded = `
{
Expand Down
44 changes: 4 additions & 40 deletions cmd/application/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
)

func TestApplicationList_basic(t *testing.T) {
ts := testGateApplicationList(false)
ts := testGateApplicationList()
defer ts.Close()

rootCmd, options := cmd.NewCmdRoot(ioutil.Discard, ioutil.Discard)
Expand All @@ -41,21 +41,6 @@ func TestApplicationList_basic(t *testing.T) {
}
}

func TestApplicationList_malformed(t *testing.T) {
ts := testGateApplicationList(true)
defer ts.Close()

rootCmd, options := cmd.NewCmdRoot(ioutil.Discard, ioutil.Discard)
rootCmd.AddCommand(NewApplicationCmd(options))

args := []string{"application", "list", "--gate-endpoint=" + ts.URL}
rootCmd.SetArgs(args)
err := rootCmd.Execute()
if err == nil {
t.Fatalf("Command failed with: %s", err)
}
}

func TestApplicationList_fail(t *testing.T) {
ts := testGateFail()
defer ts.Close()
Expand All @@ -74,37 +59,16 @@ func TestApplicationList_fail(t *testing.T) {
// testGateApplicationList spins up a local http server that we will configure the GateClient
// to direct requests to. When 'returnMalformed' is false, responds with a 200 and a well-formed application list.
// Returns a malformed list of application configs when 'returnMalformed' is true
func testGateApplicationList(returnMalformed bool) *httptest.Server {
func testGateApplicationList() *httptest.Server {
mux := util.TestGateMuxWithVersionHandler()
mux.Handle("/applications", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if returnMalformed {
fmt.Fprintln(w, strings.TrimSpace(malformedApplicationListJson))
} else {
fmt.Fprintln(w, strings.TrimSpace(applicationListJson))
}
w.Header().Add("content-type", "application/json")
fmt.Fprintln(w, strings.TrimSpace(applicationListJson))
}))

return httptest.NewServer(mux)
}

const malformedApplicationListJson = `
{
"accounts": "account1",
"cloudproviders": [
"gce",
"kubernetes"
],
"createTs": "1527261941734",
"email": "app",
"instancePort": 80,
"lastModifiedBy": "anonymous",
"name": "app",
"updateTs": "1527261941735",
"user": "anonymous",
}
]
`

const applicationListJson = `
[
{
Expand Down
1 change: 1 addition & 0 deletions cmd/application/save_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ func testGateAppSaveSuccess(buffer io.Writer) *httptest.Server {
util.NewTestBufferHandlerFunc(http.MethodPost, buffer, http.StatusOK, strings.TrimSpace(testAppTaskRefJsonStr)),
)
mux.Handle("/tasks/id", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("content-type", "application/json")
fmt.Fprintln(w, strings.TrimSpace(testAppTaskStatusJsonStr))
}))
return httptest.NewServer(mux)
Expand Down
41 changes: 1 addition & 40 deletions cmd/canary/canary-config/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,23 +98,6 @@ func TestCanaryConfigGet_args(t *testing.T) {
}
}

func TestCanaryConfigGet_malformed(t *testing.T) {
ts := testGateCanaryConfigGetMalformed()
defer ts.Close()

rootCmd, rootOpts := cmd.NewCmdRoot(ioutil.Discard, ioutil.Discard)
canaryCmd, canaryOpts := canary.NewCanaryCmd(rootOpts)
canaryCmd.AddCommand(NewCanaryConfigCmd(canaryOpts))
rootCmd.AddCommand(canaryCmd)

args := []string{"canary", "canary-config", "get", "--id", "3f3dbcc1", "--gate-endpoint", ts.URL}
rootCmd.SetArgs(args)
err := rootCmd.Execute()
if err == nil {
t.Fatalf("Command failed with: %s", err)
}
}

func TestCanaryConfigGet_fail(t *testing.T) {
ts := testGateFail()
defer ts.Close()
Expand Down Expand Up @@ -157,22 +140,12 @@ func testGateCanaryConfigGetSuccess() *httptest.Server {
mux.Handle(
"/v2/canaryConfig/",
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("content-type", "application/json")
fmt.Fprintln(w, strings.TrimSpace(canaryConfigGetJson))
}))
return httptest.NewServer(mux)
}

// testGateCanaryConfigGetMalformed returns a malformed get of canaryConfig configs.
func testGateCanaryConfigGetMalformed() *httptest.Server {
mux := util.TestGateMuxWithVersionHandler()
mux.Handle(
"/v2/canaryConfig/",
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, strings.TrimSpace(malformedCanaryConfigGetJson))
}))
return httptest.NewServer(mux)
}

// testGatePipelineGetMissing returns a 404 Not Found for an errant pipeline name|application pair.
func testGateCanaryConfigGetMissing() *httptest.Server {
mux := util.TestGateMuxWithVersionHandler()
Expand All @@ -182,18 +155,6 @@ func testGateCanaryConfigGetMissing() *httptest.Server {
return httptest.NewServer(mux)
}

const malformedCanaryConfigGetJson = `
{{
"applications": [
"canaryconfigs"
],
"id": "3f3dbcc1-002d-458c-b181-be4aa809922a",
"name": "exampleCanary",
"updatedTimestamp": 1568131247595,
"updatedTimestampIso": "2019-09-10T16:00:47.595Z"
}
`

const canaryConfigGetJson = `
{
"applications": [
Expand Down
Loading

0 comments on commit dae229f

Please sign in to comment.