github.com/mitchellh/packer@v1.3.2/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/services/resources/mgmt/2018-02-01/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, &params)
    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 VM template is correct when building with additional unmanaged disks
   359  func TestVirtualMachineDeployment11(t *testing.T) {
   360  	config := map[string]interface{}{
   361  		"location":        "ignore",
   362  		"subscription_id": "ignore",
   363  		"os_type":         constants.Target_Linux,
   364  		"communicator":    "none",
   365  		"image_publisher": "--image-publisher--",
   366  		"image_offer":     "--image-offer--",
   367  		"image_sku":       "--image-sku--",
   368  		"image_version":   "--version--",
   369  
   370  		"disk_additional_size": []uint{32},
   371  
   372  		"resource_group_name":    "packergroup",
   373  		"storage_account":        "packerartifacts",
   374  		"capture_name_prefix":    "packer",
   375  		"capture_container_name": "packerimages",
   376  	}
   377  
   378  	c, _, err := newConfig(config, getPackerConfiguration())
   379  	if err != nil {
   380  		t.Fatal(err)
   381  	}
   382  
   383  	deployment, err := GetVirtualMachineDeployment(c)
   384  	if err != nil {
   385  		t.Fatal(err)
   386  	}
   387  
   388  	err = approvaltests.VerifyJSONStruct(t, deployment.Properties.Template)
   389  	if err != nil {
   390  		t.Fatal(err)
   391  	}
   392  }
   393  
   394  // Ensure the VM template is correct when building with additional managed disks
   395  func TestVirtualMachineDeployment12(t *testing.T) {
   396  	config := map[string]interface{}{
   397  		"location":        "ignore",
   398  		"subscription_id": "ignore",
   399  		"os_type":         constants.Target_Linux,
   400  		"communicator":    "none",
   401  		"image_publisher": "--image-publisher--",
   402  		"image_offer":     "--image-offer--",
   403  		"image_sku":       "--image-sku--",
   404  		"image_version":   "--version--",
   405  
   406  		"disk_additional_size": []uint{32},
   407  
   408  		"managed_image_name":                "ManagedImageName",
   409  		"managed_image_resource_group_name": "ManagedImageResourceGroupName",
   410  	}
   411  
   412  	c, _, err := newConfig(config, getPackerConfiguration())
   413  	if err != nil {
   414  		t.Fatal(err)
   415  	}
   416  
   417  	deployment, err := GetVirtualMachineDeployment(c)
   418  	if err != nil {
   419  		t.Fatal(err)
   420  	}
   421  
   422  	err = approvaltests.VerifyJSONStruct(t, deployment.Properties.Template)
   423  	if err != nil {
   424  		t.Fatal(err)
   425  	}
   426  }
   427  
   428  // Ensure the link values are not set, and the concrete values are set.
   429  func TestKeyVaultDeployment00(t *testing.T) {
   430  	c, _, _ := newConfig(getArmBuilderConfiguration(), getPackerConfiguration())
   431  	deployment, err := GetKeyVaultDeployment(c)
   432  	if err != nil {
   433  		t.Fatal(err)
   434  	}
   435  
   436  	if deployment.Properties.Mode != resources.Incremental {
   437  		t.Errorf("Expected deployment.Properties.Mode to be %s, but got %s", resources.Incremental, deployment.Properties.Mode)
   438  	}
   439  
   440  	if deployment.Properties.ParametersLink != nil {
   441  		t.Error("Expected the ParametersLink to be nil!")
   442  	}
   443  
   444  	if deployment.Properties.TemplateLink != nil {
   445  		t.Error("Expected the TemplateLink to be nil!")
   446  	}
   447  
   448  	if deployment.Properties.Parameters == nil {
   449  		t.Error("Expected the Parameters to not be nil!")
   450  	}
   451  
   452  	if deployment.Properties.Template == nil {
   453  		t.Error("Expected the Template to not be nil!")
   454  	}
   455  }
   456  
   457  // Ensure the KeyVault template is a valid JSON document.
   458  func TestKeyVaultDeployment01(t *testing.T) {
   459  	c, _, _ := newConfig(getArmBuilderConfiguration(), getPackerConfiguration())
   460  	deployment, err := GetKeyVaultDeployment(c)
   461  	if err != nil {
   462  		t.Fatal(err)
   463  	}
   464  
   465  	_, err = json.Marshal(deployment.Properties.Template)
   466  	if err != nil {
   467  		t.Fatal(err)
   468  	}
   469  }
   470  
   471  // Ensure the KeyVault template parameters are correct.
   472  func TestKeyVaultDeployment02(t *testing.T) {
   473  	c, _, _ := newConfig(getArmBuilderConfigurationWithWindows(), getPackerConfiguration())
   474  
   475  	deployment, err := GetKeyVaultDeployment(c)
   476  	if err != nil {
   477  		t.Fatal(err)
   478  	}
   479  
   480  	bs, err := json.Marshal(deployment.Properties.Parameters)
   481  	if err != nil {
   482  		t.Fatal(err)
   483  	}
   484  
   485  	var params template.TemplateParameters
   486  	err = json.Unmarshal(bs, &params)
   487  	if err != nil {
   488  		t.Fatal(err)
   489  	}
   490  
   491  	if params.ObjectId.Value != c.ObjectID {
   492  		t.Errorf("Expected template parameter 'ObjectId' to be %s, but got %s.", params.ObjectId.Value, c.ObjectID)
   493  	}
   494  	if params.TenantId.Value != c.TenantID {
   495  		t.Errorf("Expected template parameter 'TenantId' to be %s, but got %s.", params.TenantId.Value, c.TenantID)
   496  	}
   497  	if params.KeyVaultName.Value != c.tmpKeyVaultName {
   498  		t.Errorf("Expected template parameter 'KeyVaultName' to be %s, but got %s.", params.KeyVaultName.Value, c.tmpKeyVaultName)
   499  	}
   500  	if params.KeyVaultSecretValue.Value != c.winrmCertificate {
   501  		t.Errorf("Expected template parameter 'KeyVaultSecretValue' to be %s, but got %s.", params.KeyVaultSecretValue.Value, c.winrmCertificate)
   502  	}
   503  }
   504  
   505  // Ensure the KeyVault template is correct when tags are supplied.
   506  func TestKeyVaultDeployment03(t *testing.T) {
   507  	tags := map[string]interface{}{
   508  		"azure_tags": map[string]string{
   509  			"tag01": "value01",
   510  			"tag02": "value02",
   511  			"tag03": "value03",
   512  		},
   513  	}
   514  
   515  	c, _, _ := newConfig(tags, getArmBuilderConfigurationWithWindows(), getPackerConfiguration())
   516  	deployment, err := GetKeyVaultDeployment(c)
   517  	if err != nil {
   518  		t.Fatal(err)
   519  	}
   520  
   521  	err = approvaltests.VerifyJSONStruct(t, deployment.Properties.Template)
   522  	if err != nil {
   523  		t.Fatal(err)
   524  	}
   525  }
   526  
   527  func TestPlanInfo01(t *testing.T) {
   528  	planInfo := map[string]interface{}{
   529  		"plan_info": map[string]string{
   530  			"plan_name":      "planName00",
   531  			"plan_product":   "planProduct00",
   532  			"plan_publisher": "planPublisher00",
   533  		},
   534  	}
   535  
   536  	c, _, _ := newConfig(planInfo, getArmBuilderConfiguration(), getPackerConfiguration())
   537  	deployment, err := GetVirtualMachineDeployment(c)
   538  	if err != nil {
   539  		t.Fatal(err)
   540  	}
   541  
   542  	err = approvaltests.VerifyJSONStruct(t, deployment.Properties.Template)
   543  	if err != nil {
   544  		t.Fatal(err)
   545  	}
   546  }
   547  
   548  func TestPlanInfo02(t *testing.T) {
   549  	planInfo := map[string]interface{}{
   550  		"azure_tags": map[string]string{
   551  			"dept": "engineering",
   552  		},
   553  		"plan_info": map[string]string{
   554  			"plan_name":           "planName00",
   555  			"plan_product":        "planProduct00",
   556  			"plan_publisher":      "planPublisher00",
   557  			"plan_promotion_code": "planPromotionCode00",
   558  		},
   559  	}
   560  
   561  	c, _, _ := newConfig(planInfo, getArmBuilderConfiguration(), getPackerConfiguration())
   562  	deployment, err := GetVirtualMachineDeployment(c)
   563  	if err != nil {
   564  		t.Fatal(err)
   565  	}
   566  
   567  	err = approvaltests.VerifyJSONStruct(t, deployment.Properties.Template)
   568  	if err != nil {
   569  		t.Fatal(err)
   570  	}
   571  }