github.com/mmcquillan/packer@v1.1.1-0.20171009221028-c85cf0483a5d/builder/azure/arm/template_factory_test.go (about) 1 package arm 2 3 import ( 4 "encoding/base64" 5 "encoding/json" 6 "testing" 7 8 "github.com/Azure/azure-sdk-for-go/arm/resources/resources" 9 "github.com/approvals/go-approval-tests" 10 "github.com/hashicorp/packer/builder/azure/common/constants" 11 "github.com/hashicorp/packer/builder/azure/common/template" 12 ) 13 14 // Ensure the link values are not set, and the concrete values are set. 15 func TestVirtualMachineDeployment00(t *testing.T) { 16 c, _, _ := newConfig(getArmBuilderConfiguration(), getPackerConfiguration()) 17 deployment, err := GetVirtualMachineDeployment(c) 18 if err != nil { 19 t.Fatal(err) 20 } 21 22 if deployment.Properties.Mode != resources.Incremental { 23 t.Errorf("Expected deployment.Properties.Mode to be %s, but got %s", resources.Incremental, deployment.Properties.Mode) 24 } 25 26 if deployment.Properties.ParametersLink != nil { 27 t.Error("Expected the ParametersLink to be nil!") 28 } 29 30 if deployment.Properties.TemplateLink != nil { 31 t.Error("Expected the TemplateLink to be nil!") 32 } 33 34 if deployment.Properties.Parameters == nil { 35 t.Error("Expected the Parameters to not be nil!") 36 } 37 38 if deployment.Properties.Template == nil { 39 t.Error("Expected the Template to not be nil!") 40 } 41 } 42 43 // Ensure the Virtual Machine template is a valid JSON document. 44 func TestVirtualMachineDeployment01(t *testing.T) { 45 c, _, _ := newConfig(getArmBuilderConfiguration(), getPackerConfiguration()) 46 deployment, err := GetVirtualMachineDeployment(c) 47 if err != nil { 48 t.Fatal(err) 49 } 50 51 _, err = json.Marshal(deployment.Properties.Template) 52 if err != nil { 53 t.Fatal(err) 54 } 55 } 56 57 // Ensure the Virtual Machine template parameters are correct. 58 func TestVirtualMachineDeployment02(t *testing.T) { 59 c, _, _ := newConfig(getArmBuilderConfiguration(), getPackerConfiguration()) 60 deployment, err := GetVirtualMachineDeployment(c) 61 if err != nil { 62 t.Fatal(err) 63 } 64 65 bs, err := json.Marshal(deployment.Properties.Parameters) 66 if err != nil { 67 t.Fatal(err) 68 } 69 70 var params template.TemplateParameters 71 err = json.Unmarshal(bs, ¶ms) 72 if err != nil { 73 t.Fatal(err) 74 } 75 76 if params.AdminUsername.Value != c.UserName { 77 t.Errorf("Expected template parameter 'AdminUsername' to be %s, but got %s.", params.AdminUsername.Value, c.UserName) 78 } 79 if params.AdminPassword.Value != c.tmpAdminPassword { 80 t.Errorf("Expected template parameter 'AdminPassword' to be %s, but got %s.", params.AdminPassword.Value, c.tmpAdminPassword) 81 } 82 if params.DnsNameForPublicIP.Value != c.tmpComputeName { 83 t.Errorf("Expected template parameter 'DnsNameForPublicIP' to be %s, but got %s.", params.DnsNameForPublicIP.Value, c.tmpComputeName) 84 } 85 if params.OSDiskName.Value != c.tmpOSDiskName { 86 t.Errorf("Expected template parameter 'OSDiskName' to be %s, but got %s.", params.OSDiskName.Value, c.tmpOSDiskName) 87 } 88 if params.StorageAccountBlobEndpoint.Value != c.storageAccountBlobEndpoint { 89 t.Errorf("Expected template parameter 'StorageAccountBlobEndpoint' to be %s, but got %s.", params.StorageAccountBlobEndpoint.Value, c.storageAccountBlobEndpoint) 90 } 91 if params.VMSize.Value != c.VMSize { 92 t.Errorf("Expected template parameter 'VMSize' to be %s, but got %s.", params.VMSize.Value, c.VMSize) 93 } 94 if params.VMName.Value != c.tmpComputeName { 95 t.Errorf("Expected template parameter 'VMName' to be %s, but got %s.", params.VMName.Value, c.tmpComputeName) 96 } 97 } 98 99 // Ensure the VM template is correct when using a market place image. 100 func TestVirtualMachineDeployment03(t *testing.T) { 101 m := getArmBuilderConfiguration() 102 m["image_publisher"] = "ImagePublisher" 103 m["image_offer"] = "ImageOffer" 104 m["image_sku"] = "ImageSku" 105 m["image_version"] = "ImageVersion" 106 107 c, _, _ := newConfig(m, getPackerConfiguration()) 108 deployment, err := GetVirtualMachineDeployment(c) 109 if err != nil { 110 t.Fatal(err) 111 } 112 113 err = approvaltests.VerifyJSONStruct(t, deployment.Properties.Template) 114 if err != nil { 115 t.Fatal(err) 116 } 117 } 118 119 // Ensure the VM template is correct when using a custom image. 120 func TestVirtualMachineDeployment04(t *testing.T) { 121 config := map[string]string{ 122 "capture_name_prefix": "ignore", 123 "capture_container_name": "ignore", 124 "location": "ignore", 125 "image_url": "https://localhost/custom.vhd", 126 "resource_group_name": "ignore", 127 "storage_account": "ignore", 128 "subscription_id": "ignore", 129 "os_type": constants.Target_Linux, 130 "communicator": "none", 131 } 132 133 c, _, err := newConfig(config, getPackerConfiguration()) 134 if err != nil { 135 t.Fatal(err) 136 } 137 138 deployment, err := GetVirtualMachineDeployment(c) 139 if err != nil { 140 t.Fatal(err) 141 } 142 143 err = approvaltests.VerifyJSONStruct(t, deployment.Properties.Template) 144 if err != nil { 145 t.Fatal(err) 146 } 147 } 148 149 func TestVirtualMachineDeployment05(t *testing.T) { 150 config := map[string]string{ 151 "capture_name_prefix": "ignore", 152 "capture_container_name": "ignore", 153 "location": "ignore", 154 "image_url": "https://localhost/custom.vhd", 155 "resource_group_name": "ignore", 156 "storage_account": "ignore", 157 "subscription_id": "ignore", 158 "os_type": constants.Target_Linux, 159 "communicator": "none", 160 "virtual_network_name": "virtualNetworkName", 161 "virtual_network_resource_group_name": "virtualNetworkResourceGroupName", 162 "virtual_network_subnet_name": "virtualNetworkSubnetName", 163 } 164 165 c, _, err := newConfig(config, getPackerConfiguration()) 166 if err != nil { 167 t.Fatal(err) 168 } 169 170 deployment, err := GetVirtualMachineDeployment(c) 171 if err != nil { 172 t.Fatal(err) 173 } 174 175 err = approvaltests.VerifyJSONStruct(t, deployment.Properties.Template) 176 if err != nil { 177 t.Fatal(err) 178 } 179 } 180 181 // Verify that tags are properly applied to every resource 182 func TestVirtualMachineDeployment06(t *testing.T) { 183 config := map[string]interface{}{ 184 "capture_name_prefix": "ignore", 185 "capture_container_name": "ignore", 186 "location": "ignore", 187 "image_url": "https://localhost/custom.vhd", 188 "resource_group_name": "ignore", 189 "storage_account": "ignore", 190 "subscription_id": "ignore", 191 "os_type": constants.Target_Linux, 192 "communicator": "none", 193 "azure_tags": map[string]string{ 194 "tag01": "value01", 195 "tag02": "value02", 196 "tag03": "value03", 197 }, 198 } 199 200 c, _, err := newConfig(config, getPackerConfiguration()) 201 if err != nil { 202 t.Fatal(err) 203 } 204 205 deployment, err := GetVirtualMachineDeployment(c) 206 if err != nil { 207 t.Fatal(err) 208 } 209 210 err = approvaltests.VerifyJSONStruct(t, deployment.Properties.Template) 211 if err != nil { 212 t.Fatal(err) 213 } 214 } 215 216 // Verify that custom data are properly inserted 217 func TestVirtualMachineDeployment07(t *testing.T) { 218 config := map[string]interface{}{ 219 "capture_name_prefix": "ignore", 220 "capture_container_name": "ignore", 221 "location": "ignore", 222 "image_url": "https://localhost/custom.vhd", 223 "resource_group_name": "ignore", 224 "storage_account": "ignore", 225 "subscription_id": "ignore", 226 "os_type": constants.Target_Linux, 227 "communicator": "none", 228 } 229 230 c, _, err := newConfig(config, getPackerConfiguration()) 231 if err != nil { 232 t.Fatal(err) 233 } 234 235 // The user specifies a configuration value for the setting custom_data_file. 236 // The config type will read that file, and base64 encode it. The encoded 237 // contents are then assigned to Config's customData property, which are directly 238 // injected into the template. 239 // 240 // I am not aware of an easy to mimic this situation in a test without having 241 // a file on disk, which I am loathe to do. The alternative is to inject base64 242 // encoded data myself, which is what I am doing here. 243 customData := `#cloud-config 244 growpart: 245 mode: off 246 ` 247 base64CustomData := base64.StdEncoding.EncodeToString([]byte(customData)) 248 c.customData = base64CustomData 249 250 deployment, err := GetVirtualMachineDeployment(c) 251 if err != nil { 252 t.Fatal(err) 253 } 254 255 err = approvaltests.VerifyJSONStruct(t, deployment.Properties.Template) 256 if err != nil { 257 t.Fatal(err) 258 } 259 } 260 261 // Ensure the VM template is correct when building from a custom managed image. 262 func TestVirtualMachineDeployment08(t *testing.T) { 263 config := map[string]interface{}{ 264 "location": "ignore", 265 "subscription_id": "ignore", 266 "os_type": constants.Target_Linux, 267 "communicator": "none", 268 "custom_managed_image_resource_group_name": "CustomManagedImageResourceGroupName", 269 "custom_managed_image_name": "CustomManagedImageName", 270 "managed_image_name": "ManagedImageName", 271 "managed_image_resource_group_name": "ManagedImageResourceGroupName", 272 } 273 274 c, _, err := newConfig(config, getPackerConfiguration()) 275 if err != nil { 276 t.Fatal(err) 277 } 278 279 deployment, err := GetVirtualMachineDeployment(c) 280 if err != nil { 281 t.Fatal(err) 282 } 283 284 err = approvaltests.VerifyJSONStruct(t, deployment.Properties.Template) 285 if err != nil { 286 t.Fatal(err) 287 } 288 } 289 290 // Ensure the VM template is correct when building from a platform managed image. 291 func TestVirtualMachineDeployment09(t *testing.T) { 292 config := map[string]interface{}{ 293 "location": "ignore", 294 "subscription_id": "ignore", 295 "os_type": constants.Target_Linux, 296 "communicator": "none", 297 "image_publisher": "--image-publisher--", 298 "image_offer": "--image-offer--", 299 "image_sku": "--image-sku--", 300 "image_version": "--version--", 301 "managed_image_name": "ManagedImageName", 302 "managed_image_resource_group_name": "ManagedImageResourceGroupName", 303 } 304 305 c, _, err := newConfig(config, getPackerConfiguration()) 306 if err != nil { 307 t.Fatal(err) 308 } 309 310 deployment, err := GetVirtualMachineDeployment(c) 311 if err != nil { 312 t.Fatal(err) 313 } 314 315 err = approvaltests.VerifyJSONStruct(t, deployment.Properties.Template) 316 if err != nil { 317 t.Fatal(err) 318 } 319 } 320 321 // Ensure the VM template is correct when building with PublicIp and connect to Private Network 322 func TestVirtualMachineDeployment10(t *testing.T) { 323 config := map[string]interface{}{ 324 "location": "ignore", 325 "subscription_id": "ignore", 326 "os_type": constants.Target_Linux, 327 "communicator": "none", 328 "image_publisher": "--image-publisher--", 329 "image_offer": "--image-offer--", 330 "image_sku": "--image-sku--", 331 "image_version": "--version--", 332 333 "virtual_network_resource_group_name": "--virtual_network_resource_group_name--", 334 "virtual_network_name": "--virtual_network_name--", 335 "virtual_network_subnet_name": "--virtual_network_subnet_name--", 336 "private_virtual_network_with_public_ip": true, 337 338 "managed_image_name": "ManagedImageName", 339 "managed_image_resource_group_name": "ManagedImageResourceGroupName", 340 } 341 342 c, _, err := newConfig(config, getPackerConfiguration()) 343 if err != nil { 344 t.Fatal(err) 345 } 346 347 deployment, err := GetVirtualMachineDeployment(c) 348 if err != nil { 349 t.Fatal(err) 350 } 351 352 err = approvaltests.VerifyJSONStruct(t, deployment.Properties.Template) 353 if err != nil { 354 t.Fatal(err) 355 } 356 } 357 358 // Ensure the link values are not set, and the concrete values are set. 359 func TestKeyVaultDeployment00(t *testing.T) { 360 c, _, _ := newConfig(getArmBuilderConfiguration(), getPackerConfiguration()) 361 deployment, err := GetKeyVaultDeployment(c) 362 if err != nil { 363 t.Fatal(err) 364 } 365 366 if deployment.Properties.Mode != resources.Incremental { 367 t.Errorf("Expected deployment.Properties.Mode to be %s, but got %s", resources.Incremental, deployment.Properties.Mode) 368 } 369 370 if deployment.Properties.ParametersLink != nil { 371 t.Error("Expected the ParametersLink to be nil!") 372 } 373 374 if deployment.Properties.TemplateLink != nil { 375 t.Error("Expected the TemplateLink to be nil!") 376 } 377 378 if deployment.Properties.Parameters == nil { 379 t.Error("Expected the Parameters to not be nil!") 380 } 381 382 if deployment.Properties.Template == nil { 383 t.Error("Expected the Template to not be nil!") 384 } 385 } 386 387 // Ensure the KeyVault template is a valid JSON document. 388 func TestKeyVaultDeployment01(t *testing.T) { 389 c, _, _ := newConfig(getArmBuilderConfiguration(), getPackerConfiguration()) 390 deployment, err := GetKeyVaultDeployment(c) 391 if err != nil { 392 t.Fatal(err) 393 } 394 395 _, err = json.Marshal(deployment.Properties.Template) 396 if err != nil { 397 t.Fatal(err) 398 } 399 } 400 401 // Ensure the KeyVault template parameters are correct. 402 func TestKeyVaultDeployment02(t *testing.T) { 403 c, _, _ := newConfig(getArmBuilderConfigurationWithWindows(), getPackerConfiguration()) 404 405 deployment, err := GetKeyVaultDeployment(c) 406 if err != nil { 407 t.Fatal(err) 408 } 409 410 bs, err := json.Marshal(deployment.Properties.Parameters) 411 if err != nil { 412 t.Fatal(err) 413 } 414 415 var params template.TemplateParameters 416 err = json.Unmarshal(bs, ¶ms) 417 if err != nil { 418 t.Fatal(err) 419 } 420 421 if params.ObjectId.Value != c.ObjectID { 422 t.Errorf("Expected template parameter 'ObjectId' to be %s, but got %s.", params.ObjectId.Value, c.ObjectID) 423 } 424 if params.TenantId.Value != c.TenantID { 425 t.Errorf("Expected template parameter 'TenantId' to be %s, but got %s.", params.TenantId.Value, c.TenantID) 426 } 427 if params.KeyVaultName.Value != c.tmpKeyVaultName { 428 t.Errorf("Expected template parameter 'KeyVaultName' to be %s, but got %s.", params.KeyVaultName.Value, c.tmpKeyVaultName) 429 } 430 if params.KeyVaultSecretValue.Value != c.winrmCertificate { 431 t.Errorf("Expected template parameter 'KeyVaultSecretValue' to be %s, but got %s.", params.KeyVaultSecretValue.Value, c.winrmCertificate) 432 } 433 } 434 435 // Ensure the KeyVault template is correct when tags are supplied. 436 func TestKeyVaultDeployment03(t *testing.T) { 437 tags := map[string]interface{}{ 438 "azure_tags": map[string]string{ 439 "tag01": "value01", 440 "tag02": "value02", 441 "tag03": "value03", 442 }, 443 } 444 445 c, _, _ := newConfig(tags, getArmBuilderConfigurationWithWindows(), getPackerConfiguration()) 446 deployment, err := GetKeyVaultDeployment(c) 447 if err != nil { 448 t.Fatal(err) 449 } 450 451 err = approvaltests.VerifyJSONStruct(t, deployment.Properties.Template) 452 if err != nil { 453 t.Fatal(err) 454 } 455 }