github.com/mitchellh/packer@v1.3.2/builder/azure/common/template/template_parameters_test.go (about)

     1  package template
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"strings"
     7  	"testing"
     8  )
     9  
    10  func TestTemplateParametersShouldHaveExpectedKeys(t *testing.T) {
    11  	params := TemplateParameters{
    12  		AdminUsername:              &TemplateParameter{Value: "sentinel"},
    13  		AdminPassword:              &TemplateParameter{Value: "sentinel"},
    14  		DnsNameForPublicIP:         &TemplateParameter{Value: "sentinel"},
    15  		OSDiskName:                 &TemplateParameter{Value: "sentinel"},
    16  		StorageAccountBlobEndpoint: &TemplateParameter{Value: "sentinel"},
    17  		VMName:                     &TemplateParameter{Value: "sentinel"},
    18  		VMSize:                     &TemplateParameter{Value: "sentinel"},
    19  	}
    20  
    21  	bs, err := json.Marshal(params)
    22  	if err != nil {
    23  		t.Fail()
    24  	}
    25  
    26  	var doc map[string]*json.RawMessage
    27  	err = json.Unmarshal(bs, &doc)
    28  
    29  	if err != nil {
    30  		t.Fail()
    31  	}
    32  
    33  	expectedKeys := []string{
    34  		"adminUsername",
    35  		"adminPassword",
    36  		"dnsNameForPublicIP",
    37  		"osDiskName",
    38  		"storageAccountBlobEndpoint",
    39  		"vmSize",
    40  		"vmName",
    41  	}
    42  
    43  	for _, expectedKey := range expectedKeys {
    44  		_, containsKey := doc[expectedKey]
    45  		if containsKey == false {
    46  			t.Fatalf("Expected template parameters to contain the key value '%s', but it did not!", expectedKey)
    47  		}
    48  	}
    49  }
    50  
    51  func TestParameterValuesShouldBeSet(t *testing.T) {
    52  	params := TemplateParameters{
    53  		AdminUsername:              &TemplateParameter{Value: "adminusername00"},
    54  		AdminPassword:              &TemplateParameter{Value: "adminpassword00"},
    55  		DnsNameForPublicIP:         &TemplateParameter{Value: "dnsnameforpublicip00"},
    56  		OSDiskName:                 &TemplateParameter{Value: "osdiskname00"},
    57  		StorageAccountBlobEndpoint: &TemplateParameter{Value: "storageaccountblobendpoint00"},
    58  		VMName:                     &TemplateParameter{Value: "vmname00"},
    59  		VMSize:                     &TemplateParameter{Value: "vmsize00"},
    60  	}
    61  
    62  	bs, err := json.Marshal(params)
    63  	if err != nil {
    64  		t.Fail()
    65  	}
    66  
    67  	var doc map[string]map[string]interface{}
    68  	err = json.Unmarshal(bs, &doc)
    69  
    70  	if err != nil {
    71  		t.Fail()
    72  	}
    73  
    74  	for k, v := range doc {
    75  		var expectedValue = fmt.Sprintf("%s00", strings.ToLower(k))
    76  		var actualValue, exists = v["value"]
    77  		if exists != true {
    78  			t.Errorf("Expected to find a 'value' key under '%s', but it was missing!", k)
    79  		}
    80  
    81  		if expectedValue != actualValue {
    82  			t.Errorf("Expected '%s', but actual was '%s'!", expectedValue, actualValue)
    83  		}
    84  	}
    85  }
    86  
    87  func TestEmptyValuesShouldBeOmitted(t *testing.T) {
    88  	params := TemplateParameters{
    89  		AdminUsername: &TemplateParameter{Value: "adminusername00"},
    90  	}
    91  
    92  	bs, err := json.Marshal(params)
    93  	if err != nil {
    94  		t.Fail()
    95  	}
    96  
    97  	var doc map[string]map[string]interface{}
    98  	err = json.Unmarshal(bs, &doc)
    99  
   100  	if err != nil {
   101  		t.Fail()
   102  	}
   103  
   104  	if len(doc) != 1 {
   105  		t.Errorf("Failed to omit empty template parameters from the JSON document!")
   106  		t.Errorf("doc=%+v", doc)
   107  		t.Fail()
   108  	}
   109  }