github.com/spinnaker/spin@v1.30.0/cmd/pipeline/execution/cancel_test.go (about) 1 // Copyright (c) 2019, 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 execution 16 17 import ( 18 "encoding/json" 19 "fmt" 20 "io/ioutil" 21 "net/http" 22 "net/http/httptest" 23 "strings" 24 "testing" 25 26 "github.com/spinnaker/spin/cmd" 27 "github.com/spinnaker/spin/cmd/pipeline" 28 ) 29 30 func TestExecutionCancel_basic(t *testing.T) { 31 ts := testGateExecutionCancelSuccess() 32 defer ts.Close() 33 34 rootCmd, rootOpts := cmd.NewCmdRoot(ioutil.Discard, ioutil.Discard) 35 pipelineCmd, pipelineOpts := pipeline.NewPipelineCmd(rootOpts) 36 pipelineCmd.AddCommand(NewExecutionCmd(pipelineOpts)) 37 rootCmd.AddCommand(pipelineCmd) 38 39 args := []string{"pipeline", "ex", "cancel", "someId", "--gate-endpoint", ts.URL} 40 rootCmd.SetArgs(args) 41 err := rootCmd.Execute() 42 if err != nil { 43 t.Fatalf("Command failed with: %s", err) 44 } 45 } 46 47 func TestExecutionCancel_noinput(t *testing.T) { 48 ts := testGateExecutionCancelSuccess() 49 defer ts.Close() 50 51 rootCmd, rootOpts := cmd.NewCmdRoot(ioutil.Discard, ioutil.Discard) 52 pipelineCmd, pipelineOpts := pipeline.NewPipelineCmd(rootOpts) 53 pipelineCmd.AddCommand(NewExecutionCmd(pipelineOpts)) 54 rootCmd.AddCommand(pipelineCmd) 55 56 // Exclude 'pipeline' since we are testing only the 'execution' subcommand. 57 args := []string{"pipeline", "ex", "cancel", "--gate-endpoint", ts.URL} 58 rootCmd.SetArgs(args) 59 err := rootCmd.Execute() 60 if err == nil { 61 t.Fatalf("Command failed with: %v", err) 62 } 63 } 64 65 func TestExecutionCancel_failure(t *testing.T) { 66 ts := testGateFail() 67 defer ts.Close() 68 69 rootCmd, rootOpts := cmd.NewCmdRoot(ioutil.Discard, ioutil.Discard) 70 pipelineCmd, pipelineOpts := pipeline.NewPipelineCmd(rootOpts) 71 pipelineCmd.AddCommand(NewExecutionCmd(pipelineOpts)) 72 rootCmd.AddCommand(pipelineCmd) 73 74 args := []string{"pipeline", "ex", "cancel", "someId", "--gate-endpoint", ts.URL} 75 rootCmd.SetArgs(args) 76 err := rootCmd.Execute() 77 if err == nil { 78 t.Fatalf("Command failed with: %v", err) 79 } 80 } 81 82 // testGateExecutionCancelSuccess spins up a local http server that we will configure the GateClient 83 // to direct requests to. Responds with a 200 and a well-formed pipeline get response. 84 func testGateExecutionCancelSuccess() *httptest.Server { 85 return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 86 if strings.Contains(r.URL.String(), "/version") { 87 payload := map[string]string{ 88 "version": "Unknown", 89 } 90 b, _ := json.Marshal(&payload) 91 fmt.Fprintln(w, string(b)) 92 } else { 93 fmt.Fprintln(w, "") 94 } 95 })) 96 }