github.com/spinnaker/spin@v1.30.0/cmd/pipeline-template/delete_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_template 16 17 import ( 18 "encoding/json" 19 "fmt" 20 "io/ioutil" 21 "net/http" 22 "net/http/httptest" 23 "testing" 24 25 "github.com/spinnaker/spin/cmd" 26 gate "github.com/spinnaker/spin/gateapi" 27 "github.com/spinnaker/spin/util" 28 ) 29 30 func TestPipelineTemplateDelete_basic(t *testing.T) { 31 ts := testGateDeleteSuccess() 32 defer ts.Close() 33 34 rootCmd, rootOpts := cmd.NewCmdRoot(ioutil.Discard, ioutil.Discard) 35 rootCmd.AddCommand(NewPipelineTemplateCmd(rootOpts)) 36 37 args := []string{"pipeline-template", "delete", "myTemplate", "--gate-endpoint", ts.URL} 38 rootCmd.SetArgs(args) 39 err := rootCmd.Execute() 40 if err != nil { 41 t.Fatalf("Command failed with: %s", err) 42 } 43 } 44 45 func TestPipelineTemplateDelete_tag(t *testing.T) { 46 ts := testGateDeleteSuccess() 47 defer ts.Close() 48 49 rootCmd, rootOpts := cmd.NewCmdRoot(ioutil.Discard, ioutil.Discard) 50 rootCmd.AddCommand(NewPipelineTemplateCmd(rootOpts)) 51 52 args := []string{"pipeline-template", "delete", "myTemplate", "--tag", "stable", "--gate-endpoint", ts.URL} 53 rootCmd.SetArgs(args) 54 err := rootCmd.Execute() 55 if err != nil { 56 t.Fatalf("Command failed with: %s", err) 57 } 58 } 59 60 func TestPipelineTemplateDelete_fail(t *testing.T) { 61 ts := testGateFail() 62 defer ts.Close() 63 64 rootCmd, rootOpts := cmd.NewCmdRoot(ioutil.Discard, ioutil.Discard) 65 rootCmd.AddCommand(NewPipelineTemplateCmd(rootOpts)) 66 67 args := []string{"pipeline-template", "delete", "myTemplate", "--gate-endpoint", ts.URL} 68 rootCmd.SetArgs(args) 69 err := rootCmd.Execute() 70 if err == nil { 71 t.Fatalf("Command failed with: %s", err) 72 } 73 } 74 75 func TestPipelineTemplateDelete_flags(t *testing.T) { 76 ts := testGateDeleteSuccess() 77 defer ts.Close() 78 79 rootCmd, rootOpts := cmd.NewCmdRoot(ioutil.Discard, ioutil.Discard) 80 rootCmd.AddCommand(NewPipelineTemplateCmd(rootOpts)) 81 82 args := []string{"pipeline-template", "delete", "--id", "myTemplate", "--gate-endpoint", ts.URL} // Extra --id flag. 83 rootCmd.SetArgs(args) 84 err := rootCmd.Execute() 85 if err == nil { 86 t.Fatalf("Command failed with: %s", err) 87 } 88 } 89 90 func TestPipelineTemplateDelete_missingid(t *testing.T) { 91 ts := testGateDeleteSuccess() 92 defer ts.Close() 93 94 rootCmd, rootOpts := cmd.NewCmdRoot(ioutil.Discard, ioutil.Discard) 95 rootCmd.AddCommand(NewPipelineTemplateCmd(rootOpts)) 96 97 args := []string{"pipeline-template", "delete", "--gate-endpoint", ts.URL} // Missing pipeline id. 98 rootCmd.SetArgs(args) 99 err := rootCmd.Execute() 100 if err == nil { 101 t.Fatalf("Command errantly succeeded. %s", err) 102 } 103 } 104 105 // testGateDeleteSuccess spins up a local http server that we will configure the GateClient 106 // to direct requests to. Responds with OK to indicate a pipeline template exists, 107 // and Accepts POST calls. 108 func testGateDeleteSuccess() *httptest.Server { 109 mux := util.TestGateMuxWithVersionHandler() 110 mux.Handle("/v2/pipelineTemplates/myTemplate", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 111 if r.Method == http.MethodDelete { 112 resp := gate.ResponseEntity{StatusCode: "201 Accepted", StatusCodeValue: 201} 113 b, _ := json.Marshal(&resp) 114 115 w.WriteHeader(http.StatusAccepted) 116 fmt.Fprintln(w, string(b)) // Write empty 201. 117 } else { 118 w.WriteHeader(http.StatusOK) 119 } 120 })) 121 return httptest.NewServer(mux) 122 }