github.com/spinnaker/spin@v1.30.0/cmd/pipeline/execute_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 pipeline 16 17 import ( 18 "encoding/json" 19 "fmt" 20 "io/ioutil" 21 "net/http" 22 "net/http/httptest" 23 "os" 24 "strings" 25 "testing" 26 27 "github.com/spinnaker/spin/cmd" 28 gate "github.com/spinnaker/spin/gateapi" 29 "github.com/spinnaker/spin/util" 30 ) 31 32 // TODO(jacobkiefer): This test overlaps heavily with pipeline_save_test.go, 33 // consider factoring common testing code out. 34 func TestPipelineExecute_basic(t *testing.T) { 35 ts := testGatePipelineExecuteSuccess() 36 defer ts.Close() 37 38 tempFile := tempPipelineFile(testPipelineJsonStr) 39 if tempFile == nil { 40 t.Fatal("Could not create temp pipeline file.") 41 } 42 defer os.Remove(tempFile.Name()) 43 44 rootCmd, rootOpts := cmd.NewCmdRoot(ioutil.Discard, ioutil.Discard) 45 pipelineCmd, _ := NewPipelineCmd(rootOpts) 46 rootCmd.AddCommand(pipelineCmd) 47 48 args := []string{"pipeline", "execute", "--application", "app", "--name", "one", "--gate-endpoint", ts.URL} 49 rootCmd.SetArgs(args) 50 err := rootCmd.Execute() 51 if err != nil { 52 t.Fatalf("Command failed with: %s", err) 53 } 54 } 55 56 func TestPipelineExecute_fail(t *testing.T) { 57 ts := testGateFail() 58 defer ts.Close() 59 60 tempFile := tempPipelineFile(testPipelineJsonStr) 61 if tempFile == nil { 62 t.Fatal("Could not create temp pipeline file.") 63 } 64 defer os.Remove(tempFile.Name()) 65 66 rootCmd, rootOpts := cmd.NewCmdRoot(ioutil.Discard, ioutil.Discard) 67 pipelineCmd, _ := NewPipelineCmd(rootOpts) 68 rootCmd.AddCommand(pipelineCmd) 69 70 args := []string{"pipeline", "execute", "--application", "app", "--name", "one", "--gate-endpoint", ts.URL} 71 rootCmd.SetArgs(args) 72 err := rootCmd.Execute() 73 if err == nil { 74 t.Fatalf("Command failed with: %s", err) 75 } 76 } 77 78 func TestPipelineExecute_flags(t *testing.T) { 79 ts := testGateSuccess() 80 defer ts.Close() 81 82 rootCmd, rootOpts := cmd.NewCmdRoot(ioutil.Discard, ioutil.Discard) 83 pipelineCmd, _ := NewPipelineCmd(rootOpts) 84 rootCmd.AddCommand(pipelineCmd) 85 86 args := []string{"pipeline", "execute", "--gate-endpoint", ts.URL} // Missing pipeline app and name. 87 rootCmd.SetArgs(args) 88 err := rootCmd.Execute() 89 if err == nil { 90 t.Fatalf("Command failed with: %s", err) 91 } 92 } 93 94 func TestPipelineExecute_missingname(t *testing.T) { 95 ts := testGateSuccess() 96 defer ts.Close() 97 98 tempFile := tempPipelineFile(missingNameJsonStr) 99 if tempFile == nil { 100 t.Fatal("Could not create temp pipeline file.") 101 } 102 defer os.Remove(tempFile.Name()) 103 104 rootCmd, rootOpts := cmd.NewCmdRoot(ioutil.Discard, ioutil.Discard) 105 pipelineCmd, _ := NewPipelineCmd(rootOpts) 106 rootCmd.AddCommand(pipelineCmd) 107 108 args := []string{"pipeline", "execute", "--application", "app", "--gate-endpoint", ts.URL} 109 rootCmd.SetArgs(args) 110 err := rootCmd.Execute() 111 if err == nil { 112 t.Fatalf("Command failed with: %s", err) 113 } 114 } 115 116 func TestPipelineExecute_missingapp(t *testing.T) { 117 ts := testGateSuccess() 118 defer ts.Close() 119 120 tempFile := tempPipelineFile(missingAppJsonStr) 121 if tempFile == nil { 122 t.Fatal("Could not create temp pipeline file.") 123 } 124 defer os.Remove(tempFile.Name()) 125 126 rootCmd, rootOpts := cmd.NewCmdRoot(ioutil.Discard, ioutil.Discard) 127 pipelineCmd, _ := NewPipelineCmd(rootOpts) 128 rootCmd.AddCommand(pipelineCmd) 129 130 args := []string{"pipeline", "execute", "--name", "one", "--gate-endpoint", ts.URL} 131 rootCmd.SetArgs(args) 132 err := rootCmd.Execute() 133 if err == nil { 134 t.Fatalf("Command failed with: %s", err) 135 } 136 } 137 138 // testGatePipelineExecuteSuccess spins up a local http server that we will configure the GateClient 139 // to direct requests to. Responds with successful responses to pipeline execute API calls. 140 func testGatePipelineExecuteSuccess() *httptest.Server { 141 mux := util.TestGateMuxWithVersionHandler() 142 mux.Handle("/pipelines/app/one", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 143 resp := gate.ResponseEntity{StatusCode: "201 Accepted", StatusCodeValue: 201} 144 b, _ := json.Marshal(&resp) 145 146 w.WriteHeader(http.StatusAccepted) 147 fmt.Fprintln(w, string(b)) // Write empty 201. 148 })) 149 mux.Handle("/applications/app/executions/search", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 150 w.WriteHeader(http.StatusOK) 151 fmt.Fprintln(w, strings.TrimSpace(executions)) 152 })) 153 return httptest.NewServer(mux) 154 } 155 156 const executions = ` 157 [ 158 { 159 "id": "asdflkj" 160 } 161 ] 162 `