github.com/spinnaker/spin@v1.30.0/cmd/output/output_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 output 16 17 import ( 18 "strings" 19 "testing" 20 21 "github.com/andreyvit/diff" 22 ) 23 24 func TestOutputMarshalToJson(t *testing.T) { 25 jsonBytes, err := MarshalToJson(testMap) 26 if err != nil { 27 t.Fatalf("Failed to format: %s", err) 28 } 29 30 expected := strings.TrimSpace(testJsonStr) 31 recieved := strings.TrimSpace(string(jsonBytes)) 32 if expected != recieved { 33 t.Fatalf("Unexpected formatted yaml output (want- get+):\n%s", diff.LineDiff(expected, recieved)) 34 } 35 } 36 37 func TestOutputMarshalToYaml(t *testing.T) { 38 yamlBytes, err := MarshalToYaml(testMap) 39 if err != nil { 40 t.Fatalf("Failed to format: %s", err) 41 } 42 43 expected := strings.TrimSpace(testYamlStr) 44 recieved := strings.TrimSpace(string(yamlBytes)) 45 if expected != recieved { 46 t.Fatalf("Unexpected formatted yaml output (want- get+):\n%s", diff.LineDiff(expected, recieved)) 47 } 48 } 49 50 func TestOutputMarshalToJsonPath_string(t *testing.T) { 51 formatFunc := MarshalToJsonPathWrapper("{.parameterConfig[0].name}") 52 jsonBytes, err := formatFunc(testMap) 53 if err != nil { 54 t.Fatalf("Failed to format: %s", err) 55 } 56 57 expected := "foo" 58 recieved := string(jsonBytes) 59 if recieved != expected { 60 t.Fatalf("Unexpected formatted jsonpath output: want=\"%s\" got=\"%s\"", expected, recieved) 61 } 62 } 63 64 func TestOutputMarshalToJsonPath_globString(t *testing.T) { 65 formatFunc := MarshalToJsonPathWrapper("{.stages[*].name}") 66 jsonBytes, err := formatFunc(testMap) 67 if err != nil { 68 t.Fatalf("Failed to format: %s", err) 69 } 70 71 // Expected as the current implementation only returns the first match when using a glob expression. 72 expected := "Wait" 73 recieved := string(jsonBytes) 74 if recieved != expected { 75 t.Fatalf("Unexpected formatted jsonpath output: want=\"%s\" got=\"%s\"", expected, recieved) 76 } 77 } 78 79 func TestOutputMarshalToJsonPath_nonPrimitive(t *testing.T) { 80 formatFunc := MarshalToJsonPathWrapper("{.stages}") 81 jsonBytes, err := formatFunc(testMap) 82 if err != nil { 83 t.Fatalf("Failed to format: %s", err) 84 } 85 86 expected := strings.TrimSpace(testJsonpathNonPrimStr) 87 recieved := string(jsonBytes) 88 if recieved != expected { 89 t.Fatalf("Unexpected formatted jsonpath output: want=\"%s\" got=\"%s\"", expected, recieved) 90 } 91 } 92 93 var testMap = map[string]interface{}{ 94 "application": "app", 95 "id": "pipeline1", 96 "keepWaitingPipelines": false, 97 "lastModifiedBy": "anonymous", 98 "limitConcurrent": true, 99 "name": "pipeline1", 100 "parameterConfig": []map[string]interface{}{ 101 { 102 "default": "bar", 103 "description": "A foo.", 104 "name": "foo", 105 "required": true, 106 }, 107 }, 108 "stages": []map[string]interface{}{ 109 { 110 "comments": "${ parameters.derp }", 111 "name": "Wait", 112 "refId": "1", 113 "requisiteStageRefIds": []string{}, 114 "type": "wait", 115 "waitTime": 30, 116 }, 117 { 118 "comments": "${ parameters.derp }", 119 "name": "Wait Again", 120 "refId": "2", 121 "requisiteStageRefIds": []string{}, 122 "type": "wait", 123 "waitTime": 30, 124 }, 125 }, 126 "triggers": []string{}, 127 "updateTs": "1520879791608", 128 } 129 130 const testJsonPathArrayOfMapsStr = ` 131 [ 132 { 133 "default": "bar", 134 "description": "A foo.", 135 "name": "foo", 136 "required": true 137 } 138 ] 139 ` 140 141 const testJsonStr = ` 142 { 143 "application": "app", 144 "id": "pipeline1", 145 "keepWaitingPipelines": false, 146 "lastModifiedBy": "anonymous", 147 "limitConcurrent": true, 148 "name": "pipeline1", 149 "parameterConfig": [ 150 { 151 "default": "bar", 152 "description": "A foo.", 153 "name": "foo", 154 "required": true 155 } 156 ], 157 "stages": [ 158 { 159 "comments": "${ parameters.derp }", 160 "name": "Wait", 161 "refId": "1", 162 "requisiteStageRefIds": [], 163 "type": "wait", 164 "waitTime": 30 165 }, 166 { 167 "comments": "${ parameters.derp }", 168 "name": "Wait Again", 169 "refId": "2", 170 "requisiteStageRefIds": [], 171 "type": "wait", 172 "waitTime": 30 173 } 174 ], 175 "triggers": [], 176 "updateTs": "1520879791608" 177 } 178 ` 179 180 const testYamlStr = ` 181 application: app 182 id: pipeline1 183 keepWaitingPipelines: false 184 lastModifiedBy: anonymous 185 limitConcurrent: true 186 name: pipeline1 187 parameterConfig: 188 - default: bar 189 description: A foo. 190 name: foo 191 required: true 192 stages: 193 - comments: ${ parameters.derp } 194 name: Wait 195 refId: "1" 196 requisiteStageRefIds: [] 197 type: wait 198 waitTime: 30 199 - comments: ${ parameters.derp } 200 name: Wait Again 201 refId: "2" 202 requisiteStageRefIds: [] 203 type: wait 204 waitTime: 30 205 triggers: [] 206 updateTs: "1520879791608" 207 ` 208 209 const testJsonpathNonPrimStr = ` 210 [ 211 { 212 "comments": "${ parameters.derp }", 213 "name": "Wait", 214 "refId": "1", 215 "requisiteStageRefIds": [], 216 "type": "wait", 217 "waitTime": 30 218 }, 219 { 220 "comments": "${ parameters.derp }", 221 "name": "Wait Again", 222 "refId": "2", 223 "requisiteStageRefIds": [], 224 "type": "wait", 225 "waitTime": 30 226 } 227 ] 228 `