github.com/spinnaker/spin@v1.30.0/cmd/pipeline-template/use_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 "fmt" 19 "io/ioutil" 20 "net/http/httptest" 21 "os" 22 "strings" 23 "testing" 24 25 "github.com/spinnaker/spin/cmd" 26 "github.com/spinnaker/spin/util" 27 ) 28 29 var ( 30 testAppName = "test-application" 31 testPipelineName = "test-pipeline" 32 testDescription = "test-description" 33 testVariables = "one=1,two=2,three=3,four=4" 34 ) 35 36 func TestPipelineTemplateUse_basic(t *testing.T) { 37 ts := testGateVersionSuccess() 38 defer ts.Close() 39 40 rootCmd, rootOpts := cmd.NewCmdRoot(ioutil.Discard, ioutil.Discard) 41 rootCmd.AddCommand(NewPipelineTemplateCmd(rootOpts)) 42 43 args := []string{ 44 "pipeline-template", "use", "test-template-id", "--application", testAppName, 45 "--name", testPipelineName, 46 "--description", testDescription, 47 fmt.Sprintf("--set=%s", testVariables), 48 "--gate-endpoint", ts.URL, 49 } 50 51 rootCmd.SetArgs(args) 52 err := rootCmd.Execute() 53 if err != nil { 54 t.Fatalf("Command failed with: %s", err) 55 } 56 } 57 58 func TestPipelineTemplateUse_basicShort(t *testing.T) { 59 ts := testGateVersionSuccess() 60 defer ts.Close() 61 62 rootCmd, rootOpts := cmd.NewCmdRoot(ioutil.Discard, ioutil.Discard) 63 rootCmd.AddCommand(NewPipelineTemplateCmd(rootOpts)) 64 65 args := []string{ 66 "pipeline-template", "use", "test-template-id", "-a", testAppName, 67 "-n", testPipelineName, 68 "-d", testDescription, 69 fmt.Sprintf("--set=%s", testVariables), 70 "--gate-endpoint", ts.URL, 71 } 72 73 rootCmd.SetArgs(args) 74 err := rootCmd.Execute() 75 if err != nil { 76 t.Fatalf("Command failed with: %s", err) 77 } 78 } 79 80 func TestPipelineTemplateUse_missingFlags(t *testing.T) { 81 ts := testGateVersionSuccess() 82 defer ts.Close() 83 84 rootCmd, rootOpts := cmd.NewCmdRoot(ioutil.Discard, ioutil.Discard) 85 rootCmd.AddCommand(NewPipelineTemplateCmd(rootOpts)) 86 87 // Missing id, application, name 88 args := []string{ 89 "pipeline-template", "use", 90 "--gate-endpoint", ts.URL, 91 } 92 rootCmd.SetArgs(args) 93 err := rootCmd.Execute() 94 if err == nil { 95 t.Fatalf("Expected failure but command succeeded") 96 } 97 } 98 99 func TestPipelineTemplateUse_templateVariables(t *testing.T) { 100 ts := testGateVersionSuccess() 101 defer ts.Close() 102 103 tempDir, tempFiles := createTestValuesFiles() 104 defer os.RemoveAll(tempDir) // Remove all files in the test directory for use_test.go 105 106 if tempFiles == nil || tempDir == "" { 107 t.Fatal("Could not create temp pipeline template file.") 108 } 109 110 rootCmd, rootOpts := cmd.NewCmdRoot(ioutil.Discard, ioutil.Discard) 111 rootCmd.AddCommand(NewPipelineTemplateCmd(rootOpts)) 112 113 args := []string{ 114 "pipeline-template", "use", "test-template-id", "-a", testAppName, 115 "-n", testPipelineName, 116 "-d", testDescription, 117 "--set", testVariables, 118 "--values", strings.Join(tempFiles, ","), 119 fmt.Sprintf("--set=%s", testVariables), 120 "--gate-endpoint", ts.URL, 121 } 122 123 rootCmd.SetArgs(args) 124 err := rootCmd.Execute() 125 if err != nil { 126 t.Fatalf("Command failed with: %s", err) 127 } 128 } 129 130 // testGateVersionSuccess spins up a local http server that we will configure the GateClient 131 // to direct requests to. Responds healthy to the version endpoint only. 132 func testGateVersionSuccess() *httptest.Server { 133 mux := util.TestGateMuxWithVersionHandler() 134 return httptest.NewServer(mux) 135 } 136 137 func createTestValuesFiles() (string, []string) { 138 // Create temp dir for the multiple values files to sit in 139 tempDir, err := ioutil.TempDir("", "use-template-tests") 140 if err != nil { 141 fmt.Println("Could not create temp directory") 142 return "", nil 143 } 144 // First file content is json, second is a basic yaml file. Yaml file should overwrite json file (where they have matching variables) 145 fileContents := []string{"{\"one\":\"1\",\"two\":\"2\",\"overwrite\": false}", "overwrite: true"} 146 fileNames := make([]string, len(fileContents)) 147 for i, valuesFile := range fileContents { 148 tempFile, _ := ioutil.TempFile(tempDir /* /tmp dir. */, "template-use-values") 149 bytes, err := tempFile.Write([]byte(valuesFile)) 150 if err != nil || bytes == 0 { 151 fmt.Println("Could not write temp file.") 152 return "", nil 153 } 154 fileNames[i] = tempFile.Name() 155 } 156 157 return tempDir, fileNames 158 }