github.com/spinnaker/spin@v1.30.0/cmd/canary/canary-config/get_test.go (about) 1 // Copyright (c) 2019, Waze, 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 canary_config 16 17 import ( 18 "bytes" 19 "fmt" 20 "io/ioutil" 21 "net/http" 22 "net/http/httptest" 23 "strings" 24 "testing" 25 26 "github.com/andreyvit/diff" 27 28 "github.com/spinnaker/spin/cmd" 29 "github.com/spinnaker/spin/cmd/canary" 30 "github.com/spinnaker/spin/util" 31 ) 32 33 func TestCanaryConfigGet_json(t *testing.T) { 34 ts := testGateCanaryConfigGetSuccess() 35 defer ts.Close() 36 37 buffer := new(bytes.Buffer) 38 rootCmd, rootOpts := cmd.NewCmdRoot(buffer, buffer) 39 canaryCmd, canaryOpts := canary.NewCanaryCmd(rootOpts) 40 canaryCmd.AddCommand(NewCanaryConfigCmd(canaryOpts)) 41 rootCmd.AddCommand(canaryCmd) 42 43 args := []string{"canary", "canary-config", "get", "--id", "3f3dbcc1", "--gate-endpoint", ts.URL} 44 45 rootCmd.SetArgs(args) 46 err := rootCmd.Execute() 47 if err != nil { 48 t.Fatalf("Command failed with: %s", err) 49 } 50 51 expected := strings.TrimSpace(canaryConfigGetJson) 52 recieved := strings.TrimSpace(buffer.String()) 53 if expected != recieved { 54 t.Fatalf("Unexpected command output:\n%s", diff.LineDiff(expected, recieved)) 55 } 56 } 57 58 func TestCanaryConfigGet_yaml(t *testing.T) { 59 ts := testGateCanaryConfigGetSuccess() 60 defer ts.Close() 61 62 buffer := new(bytes.Buffer) 63 rootCmd, rootOpts := cmd.NewCmdRoot(buffer, buffer) 64 canaryCmd, canaryOpts := canary.NewCanaryCmd(rootOpts) 65 canaryCmd.AddCommand(NewCanaryConfigCmd(canaryOpts)) 66 rootCmd.AddCommand(canaryCmd) 67 68 args := []string{"canary", "canary-config", "get", "--id", "3f3dbcc1", "--output", "yaml", "--gate-endpoint", ts.URL} 69 70 rootCmd.SetArgs(args) 71 err := rootCmd.Execute() 72 if err != nil { 73 t.Fatalf("Command failed with: %s", err) 74 } 75 76 expected := strings.TrimSpace(canaryConfigGetYaml) 77 recieved := strings.TrimSpace(buffer.String()) 78 if expected != recieved { 79 t.Fatalf("Unexpected command output:\n%s", diff.LineDiff(expected, recieved)) 80 } 81 } 82 83 func TestCanaryConfigGet_args(t *testing.T) { 84 ts := testGateCanaryConfigGetSuccess() 85 defer ts.Close() 86 87 rootCmd, rootOpts := cmd.NewCmdRoot(ioutil.Discard, ioutil.Discard) 88 canaryCmd, canaryOpts := canary.NewCanaryCmd(rootOpts) 89 canaryCmd.AddCommand(NewCanaryConfigCmd(canaryOpts)) 90 rootCmd.AddCommand(canaryCmd) 91 92 // Missing 'id' argument. 93 args := []string{"canary", "canary-config", "get", "--gate-endpoint", ts.URL} 94 rootCmd.SetArgs(args) 95 err := rootCmd.Execute() 96 if err == nil { 97 t.Fatalf("Command failed with: %s", err) 98 } 99 } 100 101 func TestCanaryConfigGet_fail(t *testing.T) { 102 ts := testGateFail() 103 defer ts.Close() 104 105 rootCmd, rootOpts := cmd.NewCmdRoot(ioutil.Discard, ioutil.Discard) 106 canaryCmd, canaryOpts := canary.NewCanaryCmd(rootOpts) 107 canaryCmd.AddCommand(NewCanaryConfigCmd(canaryOpts)) 108 rootCmd.AddCommand(canaryCmd) 109 110 args := []string{"canary", "canary-config", "get", "--id", "3f3dbcc1", "--gate-endpoint", ts.URL} 111 rootCmd.SetArgs(args) 112 err := rootCmd.Execute() 113 if err == nil { 114 t.Fatalf("Command failed with: %s", err) 115 } 116 } 117 118 func TestPipelineGet_notfound(t *testing.T) { 119 ts := testGateCanaryConfigGetMissing() 120 defer ts.Close() 121 122 rootCmd, rootOpts := cmd.NewCmdRoot(ioutil.Discard, ioutil.Discard) 123 canaryCmd, canaryOpts := canary.NewCanaryCmd(rootOpts) 124 canaryCmd.AddCommand(NewCanaryConfigCmd(canaryOpts)) 125 rootCmd.AddCommand(canaryCmd) 126 127 args := []string{"canary", "canary-config", "get", "--id", "3f3dbcc1", "--gate-endpoint", ts.URL} 128 rootCmd.SetArgs(args) 129 130 err := rootCmd.Execute() 131 if err == nil { 132 t.Fatalf("Command failed with: %s", err) 133 } 134 } 135 136 // testGateCanaryConfigGetSuccess spins up a local http server that we will configure the GateClient 137 // to direct requests to. Responds with a 200 and a well-formed canaryConfig get. 138 func testGateCanaryConfigGetSuccess() *httptest.Server { 139 mux := util.TestGateMuxWithVersionHandler() 140 mux.Handle( 141 "/v2/canaryConfig/", 142 http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 143 w.Header().Add("content-type", "application/json") 144 fmt.Fprintln(w, strings.TrimSpace(canaryConfigGetJson)) 145 })) 146 return httptest.NewServer(mux) 147 } 148 149 // testGatePipelineGetMissing returns a 404 Not Found for an errant pipeline name|application pair. 150 func testGateCanaryConfigGetMissing() *httptest.Server { 151 mux := util.TestGateMuxWithVersionHandler() 152 mux.Handle("/v2/canaryConfig/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 153 http.NotFound(w, r) 154 })) 155 return httptest.NewServer(mux) 156 } 157 158 const canaryConfigGetJson = ` 159 { 160 "applications": [ 161 "canaryconfigs" 162 ], 163 "id": "3f3dbcc1-002d-458c-b181-be4aa809922a", 164 "name": "exampleCanary", 165 "updatedTimestamp": 1568131247595, 166 "updatedTimestampIso": "2019-09-10T16:00:47.595Z" 167 } 168 ` 169 170 const canaryConfigGetYaml = ` 171 applications: 172 - canaryconfigs 173 id: 3f3dbcc1-002d-458c-b181-be4aa809922a 174 name: exampleCanary 175 updatedTimestamp: 1568131247595 176 updatedTimestampIso: "2019-09-10T16:00:47.595Z" 177 `