github.com/rahart/packer@v0.12.2-0.20161229105310-282bb6ad370f/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/mitchellh/packer/builder/azure/common/constants"
    11  	"github.com/mitchellh/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 link values are not set, and the concrete values are set.
   262  func TestKeyVaultDeployment00(t *testing.T) {
   263  	c, _, _ := newConfig(getArmBuilderConfiguration(), getPackerConfiguration())
   264  	deployment, err := GetKeyVaultDeployment(c)
   265  	if err != nil {
   266  		t.Fatal(err)
   267  	}
   268  
   269  	if deployment.Properties.Mode != resources.Incremental {
   270  		t.Errorf("Expected deployment.Properties.Mode to be %s, but got %s", resources.Incremental, deployment.Properties.Mode)
   271  	}
   272  
   273  	if deployment.Properties.ParametersLink != nil {
   274  		t.Error("Expected the ParametersLink to be nil!")
   275  	}
   276  
   277  	if deployment.Properties.TemplateLink != nil {
   278  		t.Error("Expected the TemplateLink to be nil!")
   279  	}
   280  
   281  	if deployment.Properties.Parameters == nil {
   282  		t.Error("Expected the Parameters to not be nil!")
   283  	}
   284  
   285  	if deployment.Properties.Template == nil {
   286  		t.Error("Expected the Template to not be nil!")
   287  	}
   288  }
   289  
   290  // Ensure the KeyVault template is a valid JSON document.
   291  func TestKeyVaultDeployment01(t *testing.T) {
   292  	c, _, _ := newConfig(getArmBuilderConfiguration(), getPackerConfiguration())
   293  	deployment, err := GetKeyVaultDeployment(c)
   294  	if err != nil {
   295  		t.Fatal(err)
   296  	}
   297  
   298  	_, err = json.Marshal(deployment.Properties.Template)
   299  	if err != nil {
   300  		t.Fatal(err)
   301  	}
   302  }
   303  
   304  // Ensure the KeyVault template parameters are correct.
   305  func TestKeyVaultDeployment02(t *testing.T) {
   306  	c, _, _ := newConfig(getArmBuilderConfigurationWithWindows(), getPackerConfiguration())
   307  
   308  	deployment, err := GetKeyVaultDeployment(c)
   309  	if err != nil {
   310  		t.Fatal(err)
   311  	}
   312  
   313  	bs, err := json.Marshal(deployment.Properties.Parameters)
   314  	if err != nil {
   315  		t.Fatal(err)
   316  	}
   317  
   318  	var params template.TemplateParameters
   319  	err = json.Unmarshal(bs, &params)
   320  	if err != nil {
   321  		t.Fatal(err)
   322  	}
   323  
   324  	if params.ObjectId.Value != c.ObjectID {
   325  		t.Errorf("Expected template parameter 'ObjectId' to be %s, but got %s.", params.ObjectId.Value, c.ObjectID)
   326  	}
   327  	if params.TenantId.Value != c.TenantID {
   328  		t.Errorf("Expected template parameter 'TenantId' to be %s, but got %s.", params.TenantId.Value, c.TenantID)
   329  	}
   330  	if params.KeyVaultName.Value != c.tmpKeyVaultName {
   331  		t.Errorf("Expected template parameter 'KeyVaultName' to be %s, but got %s.", params.KeyVaultName.Value, c.tmpKeyVaultName)
   332  	}
   333  	if params.KeyVaultSecretValue.Value != c.winrmCertificate {
   334  		t.Errorf("Expected template parameter 'KeyVaultSecretValue' to be %s, but got %s.", params.KeyVaultSecretValue.Value, c.winrmCertificate)
   335  	}
   336  }
   337  
   338  // Ensure the KeyVault template is correct when tags are supplied.
   339  func TestKeyVaultDeployment03(t *testing.T) {
   340  	tags := map[string]interface{}{
   341  		"azure_tags": map[string]string{
   342  			"tag01": "value01",
   343  			"tag02": "value02",
   344  			"tag03": "value03",
   345  		},
   346  	}
   347  
   348  	c, _, _ := newConfig(tags, getArmBuilderConfigurationWithWindows(), getPackerConfiguration())
   349  	deployment, err := GetKeyVaultDeployment(c)
   350  	if err != nil {
   351  		t.Fatal(err)
   352  	}
   353  
   354  	err = approvaltests.VerifyJSONStruct(t, deployment.Properties.Template)
   355  	if err != nil {
   356  		t.Fatal(err)
   357  	}
   358  }