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