github.com/GoogleCloudPlatform/terraformer@v0.8.18/terraformutils/tfstate_converter_test.go (about) 1 // Copyright 2018 The Terraformer Authors. 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 terraformutils 16 17 /* 18 import ( 19 "testing" 20 21 "github.com/stretchr/testify/assert" 22 ) 23 24 type convertTest struct { 25 name string 26 dataFilePath string 27 expect []TerraformResource 28 metaData map[string]ResourceMetaData 29 } 30 31 func TestBasicConvert(t *testing.T) { 32 runConvert(convertTest{ 33 dataFilePath: "test1.json", 34 name: "basic tfstate", 35 expect: []TerraformResource{ 36 { 37 ServiceName: "resource-id", 38 ResourceType: "google_compute_firewall", 39 ID: "resource-id", 40 Item: map[string]interface{}{ 41 "direction": "INGRESS", 42 "enable_logging": false, 43 "id": "resource-id", 44 "name": "resource-name", 45 }, 46 Provider: "google", 47 }, 48 }, 49 metaData: map[string]ResourceMetaData{ 50 "resource-id": { 51 Provider: "google", 52 }, 53 }, 54 }, t) 55 } 56 57 func TestBasicIgnoreKeyConvert(t *testing.T) { 58 runConvert(convertTest{ 59 dataFilePath: "test7.json", 60 name: "basic tfstate", 61 expect: []TerraformResource{ 62 { 63 ServiceName: "resource-id", 64 ResourceType: "google_compute_firewall", 65 ID: "resource-id", 66 Item: map[string]interface{}{ 67 "direction": "INGRESS", 68 "enable_logging": false, 69 "id": "resource-id", 70 "name": "resource-name", 71 }, 72 Provider: "google", 73 }, 74 }, 75 metaData: map[string]ResourceMetaData{ 76 "resource-id": { 77 Provider: "google", 78 IgnoreKeys: map[string]bool{ 79 "ignored": true, 80 "more.[0-9].regex": true, 81 }, 82 }, 83 }, 84 }, t) 85 } 86 87 func TestBasicAllowEmptyConvert(t *testing.T) { 88 runConvert(convertTest{ 89 dataFilePath: "test8.json", 90 name: "basic tfstate", 91 expect: []TerraformResource{ 92 { 93 ServiceName: "resource-id", 94 ResourceType: "google_compute_firewall", 95 ID: "resource-id", 96 Item: map[string]interface{}{ 97 "direction": "INGRESS", 98 "enable_logging": false, 99 "id": "resource-id", 100 "name": "resource-name", 101 "allow_empty": "", 102 "boolval": false, 103 "intval": "124", 104 }, 105 Provider: "google", 106 }, 107 }, 108 metaData: map[string]ResourceMetaData{ 109 "resource-id": { 110 Provider: "google", 111 AllowEmptyValue: map[string]bool{"^allow_empty$": true}, 112 }, 113 }, 114 }, t) 115 } 116 117 func TestBasicAdditionalFieldsConvert(t *testing.T) { 118 runConvert(convertTest{ 119 dataFilePath: "test9.json", 120 name: "basic tfstate", 121 expect: []TerraformResource{ 122 { 123 ServiceName: "resource-id", 124 ResourceType: "google_compute_firewall", 125 ID: "resource-id", 126 Item: map[string]interface{}{ 127 "direction": "INGRESS", 128 "enable_logging": false, 129 "id": "resource-id", 130 "name": "resource-name", 131 "boolval": false, 132 "intval": "124", 133 "add_me": "value", 134 }, 135 Provider: "google", 136 }, 137 }, 138 metaData: map[string]ResourceMetaData{ 139 "resource-id": { 140 Provider: "google", 141 AdditionalFields: map[string]string{"add_me": "value"}, 142 }, 143 }, 144 }, t) 145 } 146 147 func TestBasicTfstate2(t *testing.T) { 148 runConvert(convertTest{ 149 dataFilePath: "test2.json", 150 name: "basic tfstate 2", 151 expect: []TerraformResource{ 152 { 153 ServiceName: "resource-idA", 154 ResourceType: "google_compute_firewall", 155 ID: "resource-idA", 156 Item: map[string]interface{}{ 157 "direction": "INGRESS", 158 "enable_logging": false, 159 "id": "resource-idA", 160 "name": "resource-nameA", 161 }, 162 Provider: "google", 163 }, 164 { 165 ServiceName: "resource-idB", 166 ResourceType: "google_compute_firewall", 167 ID: "resource-idB", 168 Item: map[string]interface{}{ 169 "direction": "INGRESS", 170 "enable_logging": false, 171 "id": "resource-idB", 172 "name": "resource-nameB", 173 }, 174 Provider: "google", 175 }, 176 }, 177 metaData: map[string]ResourceMetaData{ 178 "resource-idB": { 179 Provider: "google", 180 }, 181 "resource-idA": { 182 Provider: "google", 183 }, 184 }, 185 }, t) 186 } 187 188 func TestBasicArray(t *testing.T) { 189 runConvert(convertTest{ 190 dataFilePath: "test3.json", 191 name: "basic array", 192 expect: []TerraformResource{ 193 { 194 ServiceName: "resource-id", 195 ResourceType: "google_compute_firewall", 196 ID: "resource-id", 197 Item: map[string]interface{}{ 198 "direction": "INGRESS", 199 "enable_logging": false, 200 "id": "resource-id", 201 "name": "resource-name", 202 "myarray": []interface{}{ 203 map[string]interface{}{ 204 "value1": "value1", 205 "value2": "value2", 206 }, 207 map[string]interface{}{ 208 "value3": "value3", 209 "value4": "value4", 210 }, 211 }, 212 }, 213 Provider: "google", 214 }, 215 }, 216 metaData: map[string]ResourceMetaData{ 217 "resource-id": { 218 Provider: "google", 219 }, 220 }, 221 }, t) 222 } 223 224 func TestBasicArray2(t *testing.T) { 225 runConvert(convertTest{ 226 dataFilePath: "test4.json", 227 name: "basic array 2", 228 expect: []TerraformResource{ 229 { 230 ServiceName: "resource-id", 231 ResourceType: "google_compute_firewall", 232 ID: "resource-id", 233 Item: map[string]interface{}{ 234 "direction": "INGRESS", 235 "enable_logging": false, 236 "id": "resource-id", 237 "name": "resource-name", 238 "myarray": []interface{}{ 239 map[string]interface{}{ 240 "subarray1": map[string]interface{}{ 241 "1245": "value1", 242 "12454": "value2", 243 }, 244 }, 245 map[string]interface{}{ 246 "subarray3": map[string]interface{}{"123456": "value3"}, 247 "subarray4": "value4", 248 }, 249 }, 250 }, 251 Provider: "google", 252 }, 253 }, 254 metaData: map[string]ResourceMetaData{ 255 "resource-id": { 256 Provider: "google", 257 }, 258 }, 259 }, t) 260 } 261 262 func TestBasicArray3(t *testing.T) { 263 runConvert(convertTest{ 264 dataFilePath: "test5.json", 265 name: "basic array 3", 266 expect: []TerraformResource{ 267 { 268 ServiceName: "resource-id", 269 ResourceType: "google_compute_firewall", 270 ID: "resource-id", 271 Item: map[string]interface{}{ 272 "direction": "INGRESS", 273 "enable_logging": false, 274 "id": "resource-id", 275 "name": "resource-name", 276 "myarray": []interface{}{"value1", "value2", "value3"}, 277 "myarray2": []interface{}{ 278 map[string]interface{}{ 279 "subarray3": map[string]interface{}{ 280 "123456": map[string]interface{}{ 281 "subsubarray": "value3", 282 }, 283 }, 284 }, 285 map[string]interface{}{ 286 "subarray4": "value4", 287 }, 288 }, 289 }, 290 Provider: "google", 291 }, 292 }, 293 metaData: map[string]ResourceMetaData{ 294 "resource-id": { 295 Provider: "google", 296 }, 297 }, 298 }, t) 299 } 300 301 func TestBasicArray4(t *testing.T) { 302 runConvert(convertTest{ 303 dataFilePath: "test6.json", 304 name: "basic array 4", 305 expect: []TerraformResource{ 306 { 307 ServiceName: "resource-id", 308 ResourceType: "google_compute_firewall", 309 ID: "resource-id", 310 Item: map[string]interface{}{ 311 "direction": "INGRESS", 312 "enable_logging": false, 313 "id": "resource-id", 314 "name": "resource-name", 315 "lifecycle_rule": []interface{}{ 316 map[string]interface{}{ 317 "action": []interface{}{ 318 map[string]interface{}{ 319 "storage_class": "", 320 "type": "Delete", 321 }, 322 }, 323 "condition": []interface{}{ 324 map[string]interface{}{ 325 "age": "1", 326 "created_before": "", 327 "is_live": false, 328 "num_newer_versions": "0", 329 }, 330 }, 331 }, 332 }, 333 }, 334 Provider: "google", 335 }, 336 }, 337 metaData: map[string]ResourceMetaData{ 338 "resource-id": { 339 Provider: "google", 340 AllowEmptyValue: map[string]bool{ 341 "storage_class": true, 342 "created_before": true, 343 }, 344 }, 345 }, 346 }, t) 347 } 348 349 func runConvert(testCase convertTest, t *testing.T) { 350 c := TfstateConverter{} 351 actual, err := c.Convert("test_data/"+testCase.dataFilePath, testCase.metaData) 352 if err != nil { 353 t.Error(err) 354 } 355 if !assert.ObjectsAreEqual(testCase.expect, actual) { 356 assert.Equal(t, testCase.expect, actual, "Convert error "+testCase.name) 357 } 358 } 359 */