github.com/spinnaker/spin@v1.30.0/cmd/pipeline-template/list_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" 21 "net/http/httptest" 22 "strings" 23 "testing" 24 25 "github.com/spinnaker/spin/cmd" 26 "github.com/spinnaker/spin/util" 27 ) 28 29 func TestPipelineTemplateList_basic(t *testing.T) { 30 ts := testGatePipelineTemplateListSuccess() 31 defer ts.Close() 32 33 rootCmd, rootOpts := cmd.NewCmdRoot(ioutil.Discard, ioutil.Discard) 34 rootCmd.AddCommand(NewPipelineTemplateCmd(rootOpts)) 35 36 args := []string{"pipeline-template", "list", "--gate-endpoint", ts.URL} 37 rootCmd.SetArgs(args) 38 err := rootCmd.Execute() 39 if err != nil { 40 t.Fatalf("Command failed with: %s", err) 41 } 42 } 43 44 func TestPipelineTemplateList_scope(t *testing.T) { 45 ts := testGateScopedPipelineTemplateListSuccess() 46 defer ts.Close() 47 48 rootCmd, rootOpts := cmd.NewCmdRoot(ioutil.Discard, ioutil.Discard) 49 rootCmd.AddCommand(NewPipelineTemplateCmd(rootOpts)) 50 51 args := []string{"pipeline-template", "list", "--scopes", "specific", "--gate-endpoint", ts.URL} 52 rootCmd.SetArgs(args) 53 err := rootCmd.Execute() 54 if err != nil { 55 t.Fatalf("Command failed with: %s", err) 56 } 57 } 58 59 func TestPipelineTemplateList_fail(t *testing.T) { 60 ts := testGateFail() 61 defer ts.Close() 62 63 rootCmd, rootOpts := cmd.NewCmdRoot(ioutil.Discard, ioutil.Discard) 64 rootCmd.AddCommand(NewPipelineTemplateCmd(rootOpts)) 65 66 args := []string{"pipeline-template", "list", "--gate-endpoint", ts.URL} 67 rootCmd.SetArgs(args) 68 err := rootCmd.Execute() 69 if err == nil { 70 t.Fatalf("Command failed with: %s", err) 71 } 72 } 73 74 // testGatePipelineTemplateListSuccess spins up a local http server that we will configure the GateClient 75 // to direct requests to. Responds with a 200 and a well-formed pipelineTemplate list. 76 func testGatePipelineTemplateListSuccess() *httptest.Server { 77 mux := util.TestGateMuxWithVersionHandler() 78 mux.Handle("/v2/pipelineTemplates/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 79 fmt.Fprintln(w, strings.TrimSpace(pipelineTemplateListJson)) 80 })) 81 return httptest.NewServer(mux) 82 } 83 84 // testGateScopedPipelineTemplateListSuccess spins up a local http server that we will configure the GateClient 85 // to direct requests to. Responds with a 200 and a well-formed pipelineTemplate list. 86 func testGateScopedPipelineTemplateListSuccess() *httptest.Server { 87 mux := util.TestGateMuxWithVersionHandler() 88 mux.Handle("/v2/pipelineTemplates/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 89 fmt.Fprintln(w, strings.TrimSpace(scopedPipelineTemplateListJson)) 90 })) 91 return httptest.NewServer(mux) 92 } 93 94 const pipelineTemplateListJson = ` 95 [ 96 { 97 "id": "newSpelTemplate", 98 "lastModifiedBy": "anonymous", 99 "metadata": { 100 "description": "A generic application wait.", 101 "name": "Default Wait", 102 "owner": "example@example.com", 103 "scopes": [ 104 "specific" 105 ] 106 }, 107 "pipeline": { 108 "description": "", 109 "keepWaitingPipelines": false, 110 "lastModifiedBy": "anonymous", 111 "limitConcurrent": true, 112 "notifications": [], 113 "parameterConfig": [], 114 "stages": [ 115 { 116 "name": "My Wait Stage", 117 "refId": "wait1", 118 "requisiteStageRefIds": [], 119 "type": "wait", 120 "waitTime": "${ templateVariables.waitTime }" 121 } 122 ], 123 "triggers": [ 124 { 125 "attributeConstraints": {}, 126 "enabled": true, 127 "payloadConstraints": {}, 128 "pubsubSystem": "google", 129 "source": "jake", 130 "subscription": "super-why", 131 "subscriptionName": "super-why", 132 "type": "pubsub" 133 } 134 ], 135 "updateTs": "1543509523663" 136 }, 137 "protect": false, 138 "schema": "v2", 139 "updateTs": "1543860678988", 140 "variables": [ 141 { 142 "defaultValue": 42, 143 "description": "The time a wait stage shall pauseth", 144 "name": "waitTime", 145 "type": "int" 146 } 147 ] 148 }, 149 { 150 "id": "newSpelTemplate", 151 "lastModifiedBy": "anonymous", 152 "metadata": { 153 "description": "A generic application wait.", 154 "name": "Default Wait", 155 "owner": "example@example.com", 156 "scopes": [ 157 "global" 158 ] 159 }, 160 "pipeline": { 161 "description": "", 162 "keepWaitingPipelines": false, 163 "lastModifiedBy": "anonymous", 164 "limitConcurrent": true, 165 "notifications": [], 166 "parameterConfig": [], 167 "stages": [ 168 { 169 "name": "My Wait Stage", 170 "refId": "wait1", 171 "requisiteStageRefIds": [], 172 "type": "wait", 173 "waitTime": "${ templateVariables.waitTime }" 174 } 175 ], 176 "triggers": [ 177 { 178 "attributeConstraints": {}, 179 "enabled": true, 180 "payloadConstraints": {}, 181 "pubsubSystem": "google", 182 "source": "jake", 183 "subscription": "super-why", 184 "subscriptionName": "super-why", 185 "type": "pubsub" 186 } 187 ], 188 "updateTs": "1543509523663" 189 }, 190 "protect": false, 191 "schema": "v2", 192 "updateTs": "1543860678988", 193 "variables": [ 194 { 195 "defaultValue": 42, 196 "description": "The time a wait stage shall pauseth", 197 "name": "waitTime", 198 "type": "int" 199 } 200 ] 201 } 202 ] 203 ` 204 205 const scopedPipelineTemplateListJson = ` 206 [ 207 { 208 "id": "newSpelTemplate", 209 "lastModifiedBy": "anonymous", 210 "metadata": { 211 "description": "A generic application wait.", 212 "name": "Default Wait", 213 "owner": "example@example.com", 214 "scopes": [ 215 "specific" 216 ] 217 }, 218 "pipeline": { 219 "description": "", 220 "keepWaitingPipelines": false, 221 "lastModifiedBy": "anonymous", 222 "limitConcurrent": true, 223 "notifications": [], 224 "parameterConfig": [], 225 "stages": [ 226 { 227 "name": "My Wait Stage", 228 "refId": "wait1", 229 "requisiteStageRefIds": [], 230 "type": "wait", 231 "waitTime": "${ templateVariables.waitTime }" 232 } 233 ], 234 "triggers": [ 235 { 236 "attributeConstraints": {}, 237 "enabled": true, 238 "payloadConstraints": {}, 239 "pubsubSystem": "google", 240 "source": "jake", 241 "subscription": "super-why", 242 "subscriptionName": "super-why", 243 "type": "pubsub" 244 } 245 ], 246 "updateTs": "1543509523663" 247 }, 248 "protect": false, 249 "schema": "v2", 250 "updateTs": "1543860678988", 251 "variables": [ 252 { 253 "defaultValue": 42, 254 "description": "The time a wait stage shall pauseth", 255 "name": "waitTime", 256 "type": "int" 257 } 258 ] 259 } 260 ] 261 `