github.com/kikitux/packer@v0.10.1-0.20160322154024-6237df566f9f/builder/azure/arm/config_test.go (about)

     1  // Copyright (c) Microsoft Corporation. All rights reserved.
     2  // Licensed under the MIT License. See the LICENSE file in builder/azure for license information.
     3  
     4  package arm
     5  
     6  import (
     7  	"bytes"
     8  	"encoding/json"
     9  	"fmt"
    10  	"testing"
    11  	"time"
    12  )
    13  
    14  // List of configuration parameters that are required by the ARM builder.
    15  var requiredConfigValues = []string{
    16  	"capture_name_prefix",
    17  	"capture_container_name",
    18  	"client_id",
    19  	"client_secret",
    20  	"image_offer",
    21  	"image_publisher",
    22  	"image_sku",
    23  	"location",
    24  	"storage_account",
    25  	"subscription_id",
    26  	"tenant_id",
    27  }
    28  
    29  func TestConfigShouldProvideReasonableDefaultValues(t *testing.T) {
    30  	c, _, err := newConfig(getArmBuilderConfiguration(), getPackerConfiguration())
    31  
    32  	if err != nil {
    33  		t.Errorf("Expected configuration creation to succeed, but it failed!\n")
    34  		t.Fatalf(" errors: %s\n", err)
    35  	}
    36  
    37  	if c.UserName == "" {
    38  		t.Errorf("Expected 'UserName' to be populated, but it was empty!")
    39  	}
    40  
    41  	if c.VMSize == "" {
    42  		t.Errorf("Expected 'VMSize' to be populated, but it was empty!")
    43  	}
    44  }
    45  
    46  func TestConfigShouldBeAbleToOverrideDefaultedValues(t *testing.T) {
    47  	builderValues := make(map[string]string)
    48  
    49  	// Populate the dictionary with all of the required values.
    50  	for _, v := range requiredConfigValues {
    51  		builderValues[v] = "--some-value--"
    52  	}
    53  
    54  	builderValues["ssh_password"] = "override_password"
    55  	builderValues["ssh_username"] = "override_username"
    56  	builderValues["vm_size"] = "override_vm_size"
    57  
    58  	c, _, _ := newConfig(getArmBuilderConfigurationFromMap(builderValues), getPackerConfiguration())
    59  
    60  	if c.Password != "override_password" {
    61  		t.Errorf("Expected 'Password' to be set to 'override_password', but found '%s'!", c.Password)
    62  	}
    63  
    64  	if c.Comm.SSHPassword != "override_password" {
    65  		t.Errorf("Expected 'c.Comm.SSHPassword' to be set to 'override_password', but found '%s'!", c.Comm.SSHPassword)
    66  	}
    67  
    68  	if c.UserName != "override_username" {
    69  		t.Errorf("Expected 'UserName' to be set to 'override_username', but found '%s'!", c.UserName)
    70  	}
    71  
    72  	if c.Comm.SSHUsername != "override_username" {
    73  		t.Errorf("Expected 'c.Comm.SSHUsername' to be set to 'override_username', but found '%s'!", c.Comm.SSHUsername)
    74  	}
    75  
    76  	if c.VMSize != "override_vm_size" {
    77  		t.Errorf("Expected 'vm_size' to be set to 'override_username', but found '%s'!", c.VMSize)
    78  	}
    79  }
    80  
    81  func TestConfigShouldDefaultVMSizeToStandardA1(t *testing.T) {
    82  	c, _, _ := newConfig(getArmBuilderConfiguration(), getPackerConfiguration())
    83  
    84  	if c.VMSize != "Standard_A1" {
    85  		t.Errorf("Expected 'VMSize' to default to 'Standard_A1', but got '%s'.", c.VMSize)
    86  	}
    87  }
    88  
    89  func TestUserShouldProvideRequiredValues(t *testing.T) {
    90  	builderValues := make(map[string]string)
    91  
    92  	// Populate the dictionary with all of the required values.
    93  	for _, v := range requiredConfigValues {
    94  		builderValues[v] = "--some-value--"
    95  	}
    96  
    97  	// Ensure we can successfully create a config.
    98  	_, _, err := newConfig(getArmBuilderConfigurationFromMap(builderValues), getPackerConfiguration())
    99  	if err != nil {
   100  		t.Errorf("Expected configuration creation to succeed, but it failed!\n")
   101  		t.Fatalf(" -> %+v\n", builderValues)
   102  	}
   103  
   104  	// Take away a required element, and ensure construction fails.
   105  	for _, v := range requiredConfigValues {
   106  		delete(builderValues, v)
   107  
   108  		_, _, err := newConfig(getArmBuilderConfigurationFromMap(builderValues), getPackerConfiguration())
   109  		if err == nil {
   110  			t.Errorf("Expected configuration creation to fail, but it succeeded!\n")
   111  			t.Fatalf(" -> %+v\n", builderValues)
   112  		}
   113  
   114  		builderValues[v] = "--some-value--"
   115  	}
   116  }
   117  
   118  func TestSystemShouldDefineRuntimeValues(t *testing.T) {
   119  	c, _, _ := newConfig(getArmBuilderConfiguration(), getPackerConfiguration())
   120  
   121  	if c.Password == "" {
   122  		t.Errorf("Expected Password to not be empty, but it was '%s'!", c.Password)
   123  	}
   124  
   125  	if c.tmpComputeName == "" {
   126  		t.Errorf("Expected tmpComputeName to not be empty, but it was '%s'!", c.tmpComputeName)
   127  	}
   128  
   129  	if c.tmpDeploymentName == "" {
   130  		t.Errorf("Expected tmpDeploymentName to not be empty, but it was '%s'!", c.tmpDeploymentName)
   131  	}
   132  
   133  	if c.tmpResourceGroupName == "" {
   134  		t.Errorf("Expected tmpResourceGroupName to not be empty, but it was '%s'!", c.tmpResourceGroupName)
   135  	}
   136  
   137  	if c.tmpOSDiskName == "" {
   138  		t.Errorf("Expected tmpOSDiskName to not be empty, but it was '%s'!", c.tmpOSDiskName)
   139  	}
   140  }
   141  
   142  func TestConfigShouldTransformToTemplateParameters(t *testing.T) {
   143  	c, _, _ := newConfig(getArmBuilderConfiguration(), getPackerConfiguration())
   144  	templateParameters := c.toTemplateParameters()
   145  
   146  	if templateParameters.AdminUsername.Value != c.UserName {
   147  		t.Errorf("Expected AdminUsername to be equal to config's AdminUsername, but they were '%s' and '%s' respectively.", templateParameters.AdminUsername.Value, c.UserName)
   148  	}
   149  
   150  	if templateParameters.DnsNameForPublicIP.Value != c.tmpComputeName {
   151  		t.Errorf("Expected DnsNameForPublicIP to be equal to config's DnsNameForPublicIP, but they were '%s' and '%s' respectively.", templateParameters.DnsNameForPublicIP.Value, c.tmpComputeName)
   152  	}
   153  
   154  	if templateParameters.ImageOffer.Value != c.ImageOffer {
   155  		t.Errorf("Expected ImageOffer to be equal to config's ImageOffer, but they were '%s' and '%s' respectively.", templateParameters.ImageOffer.Value, c.ImageOffer)
   156  	}
   157  
   158  	if templateParameters.ImagePublisher.Value != c.ImagePublisher {
   159  		t.Errorf("Expected ImagePublisher to be equal to config's ImagePublisher, but they were '%s' and '%s' respectively.", templateParameters.ImagePublisher.Value, c.ImagePublisher)
   160  	}
   161  
   162  	if templateParameters.ImageSku.Value != c.ImageSku {
   163  		t.Errorf("Expected ImageSku to be equal to config's ImageSku, but they were '%s' and '%s' respectively.", templateParameters.ImageSku.Value, c.ImageSku)
   164  	}
   165  
   166  	if templateParameters.OSDiskName.Value != c.tmpOSDiskName {
   167  		t.Errorf("Expected OSDiskName to be equal to config's OSDiskName, but they were '%s' and '%s' respectively.", templateParameters.OSDiskName.Value, c.tmpOSDiskName)
   168  	}
   169  
   170  	if templateParameters.StorageAccountName.Value != c.StorageAccount {
   171  		t.Errorf("Expected StorageAccountName to be equal to config's StorageAccountName, but they were '%s' and '%s' respectively.", templateParameters.StorageAccountName.Value, c.StorageAccount)
   172  	}
   173  
   174  	if templateParameters.VMName.Value != c.tmpComputeName {
   175  		t.Errorf("Expected VMName to be equal to config's VMName, but they were '%s' and '%s' respectively.", templateParameters.VMName.Value, c.tmpComputeName)
   176  	}
   177  
   178  	if templateParameters.VMSize.Value != c.VMSize {
   179  		t.Errorf("Expected VMSize to be equal to config's VMSize, but they were '%s' and '%s' respectively.", templateParameters.VMSize.Value, c.VMSize)
   180  	}
   181  }
   182  
   183  func TestConfigShouldTransformToVirtualMachineCaptureParameters(t *testing.T) {
   184  	c, _, _ := newConfig(getArmBuilderConfiguration(), getPackerConfiguration())
   185  	parameters := c.toVirtualMachineCaptureParameters()
   186  
   187  	if *parameters.DestinationContainerName != c.CaptureContainerName {
   188  		t.Errorf("Expected DestinationContainerName to be equal to config's CaptureContainerName, but they were '%s' and '%s' respectively.", *parameters.DestinationContainerName, c.CaptureContainerName)
   189  	}
   190  
   191  	if *parameters.VhdPrefix != c.CaptureNamePrefix {
   192  		t.Errorf("Expected DestinationContainerName to be equal to config's CaptureContainerName, but they were '%s' and '%s' respectively.", *parameters.VhdPrefix, c.CaptureNamePrefix)
   193  	}
   194  
   195  	if *parameters.OverwriteVhds != false {
   196  		t.Error("Expected OverwriteVhds to be false, but it was not.")
   197  	}
   198  }
   199  
   200  func TestConfigShouldSupportPackersConfigElements(t *testing.T) {
   201  	c, _, err := newConfig(
   202  		getArmBuilderConfiguration(),
   203  		getPackerConfiguration(),
   204  		getPackerCommunicatorConfiguration())
   205  
   206  	if err != nil {
   207  		t.Fatal(err)
   208  	}
   209  
   210  	if c.Comm.SSHTimeout != 1*time.Hour {
   211  		t.Errorf("Expected Comm.SSHTimeout to be a duration of an hour, but got '%s' instead.", c.Comm.SSHTimeout)
   212  	}
   213  
   214  	if c.Comm.WinRMTimeout != 2*time.Hour {
   215  		t.Errorf("Expected Comm.WinRMTimeout to be a durationof two hours, but got '%s' instead.", c.Comm.WinRMTimeout)
   216  	}
   217  }
   218  
   219  func getArmBuilderConfiguration() interface{} {
   220  	m := make(map[string]string)
   221  	for _, v := range requiredConfigValues {
   222  		m[v] = fmt.Sprintf("%s00", v)
   223  	}
   224  
   225  	return getArmBuilderConfigurationFromMap(m)
   226  }
   227  
   228  func getArmBuilderConfigurationFromMap(kvp map[string]string) interface{} {
   229  	bs := bytes.NewBufferString("{")
   230  
   231  	for k, v := range kvp {
   232  		bs.WriteString(fmt.Sprintf("\"%s\": \"%s\",\n", k, v))
   233  	}
   234  
   235  	// remove the trailing ",\n" because JSON
   236  	bs.Truncate(bs.Len() - 2)
   237  	bs.WriteString("}")
   238  
   239  	var config interface{}
   240  	json.Unmarshal([]byte(bs.String()), &config)
   241  
   242  	return config
   243  }
   244  
   245  func getPackerConfiguration() interface{} {
   246  	var doc = `{
   247  		"packer_user_variables": {
   248  			"sa": "my_storage_account"
   249  		},
   250  		"packer_build_name": "azure-arm-vm",
   251  		"packer_builder_type": "azure-arm-vm",
   252  		"packer_debug": "false",
   253  		"packer_force": "false",
   254  		"packer_template_path": "/home/jenkins/azure-arm-vm/template.json"
   255  	}`
   256  
   257  	var config interface{}
   258  	json.Unmarshal([]byte(doc), &config)
   259  
   260  	return config
   261  }
   262  
   263  func getPackerCommunicatorConfiguration() interface{} {
   264  	var doc = `{
   265  		"ssh_timeout": "1h",
   266  		"winrm_timeout": "2h"
   267  	}`
   268  
   269  	var config interface{}
   270  	json.Unmarshal([]byte(doc), &config)
   271  
   272  	return config
   273  }