github.com/spinnaker/spin@v1.30.0/cmd/application/list_test.go (about) 1 // Copyright (c) 2018, Google, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package application 16 17 import ( 18 "fmt" 19 "io/ioutil" 20 "net/http" 21 "net/http/httptest" 22 "strings" 23 "testing" 24 25 "github.com/spinnaker/spin/cmd" 26 "github.com/spinnaker/spin/util" 27 ) 28 29 func TestApplicationList_basic(t *testing.T) { 30 ts := testGateApplicationList() 31 defer ts.Close() 32 33 rootCmd, options := cmd.NewCmdRoot(ioutil.Discard, ioutil.Discard) 34 rootCmd.AddCommand(NewApplicationCmd(options)) 35 36 args := []string{"application", "list", "--gate-endpoint=" + ts.URL} 37 rootCmd.SetArgs(args) 38 err := rootCmd.Execute() 39 if err != nil { 40 t.Fatalf("Command failed with: %s", err) 41 } 42 } 43 44 func TestApplicationList_fail(t *testing.T) { 45 ts := testGateFail() 46 defer ts.Close() 47 48 rootCmd, options := cmd.NewCmdRoot(ioutil.Discard, ioutil.Discard) 49 rootCmd.AddCommand(NewApplicationCmd(options)) 50 51 args := []string{"application", "list", "--gate-endpoint=" + ts.URL} 52 rootCmd.SetArgs(args) 53 err := rootCmd.Execute() 54 if err == nil { 55 t.Fatalf("Command failed with: %s", err) 56 } 57 } 58 59 // testGateApplicationList spins up a local http server that we will configure the GateClient 60 // to direct requests to. When 'returnMalformed' is false, responds with a 200 and a well-formed application list. 61 // Returns a malformed list of application configs when 'returnMalformed' is true 62 func testGateApplicationList() *httptest.Server { 63 mux := util.TestGateMuxWithVersionHandler() 64 mux.Handle("/applications", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 65 w.Header().Add("content-type", "application/json") 66 fmt.Fprintln(w, strings.TrimSpace(applicationListJson)) 67 })) 68 69 return httptest.NewServer(mux) 70 } 71 72 const applicationListJson = ` 73 [ 74 { 75 "accounts": "account1", 76 "cloudproviders": [ 77 "gce", 78 "kubernetes" 79 ], 80 "createTs": "1527261941734", 81 "email": "app", 82 "instancePort": 80, 83 "lastModifiedBy": "anonymous", 84 "name": "app", 85 "updateTs": "1527261941735", 86 "user": "anonymous" 87 } 88 ] 89 `