github.com/spinnaker/spin@v1.30.0/cmd/account/list_test.go (about) 1 // Copyright 2019 New Relic Corporation. All rights reserved. 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 account 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 TestAccountList_basic(t *testing.T) { 30 ts := testGateAccountListSuccess() 31 defer ts.Close() 32 33 rootCmd, options := cmd.NewCmdRoot(ioutil.Discard, ioutil.Discard) 34 rootCmd.AddCommand(NewAccountCmd(options)) 35 36 args := []string{"account", "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 TestAccountList_fail(t *testing.T) { 45 ts := testGateFail() 46 defer ts.Close() 47 48 rootCmd, options := cmd.NewCmdRoot(ioutil.Discard, ioutil.Discard) 49 rootCmd.AddCommand(NewAccountCmd(options)) 50 51 args := []string{"account", "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 func testGateAccountListSuccess() *httptest.Server { 60 mux := util.TestGateMuxWithVersionHandler() 61 mux.Handle("/credentials/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 62 w.Header().Add("content-type", "application/json") 63 fmt.Fprintln(w, strings.TrimSpace(accountListJson)) 64 })) 65 return httptest.NewServer(mux) 66 } 67 68 const accountListJson = `[ 69 { 70 "type": "kubernetes", 71 "skin": "v2", 72 "providerVersion": "v2", 73 "name": "foobar" 74 }, 75 { 76 "type": "dockerRegistry", 77 "skin": "v1", 78 "providerVersion": "v1", 79 "name": "dockerhub" 80 } 81 ] 82 `