2016-03-04 05:14:55 -05:00
|
|
|
// Copyright (c) Microsoft Corporation. All rights reserved.
|
2016-05-06 23:32:18 -04:00
|
|
|
// Licensed under the MIT License. See the LICENSE file in builder/azure for license information.
|
2016-03-04 05:14:55 -05:00
|
|
|
|
2016-05-21 02:01:16 -04:00
|
|
|
package template
|
2016-03-04 05:14:55 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestTemplateParametersShouldHaveExpectedKeys(t *testing.T) {
|
|
|
|
params := TemplateParameters{
|
2016-04-21 19:50:03 -04:00
|
|
|
AdminUsername: &TemplateParameter{"sentinel"},
|
|
|
|
AdminPassword: &TemplateParameter{"sentinel"},
|
|
|
|
DnsNameForPublicIP: &TemplateParameter{"sentinel"},
|
|
|
|
OSDiskName: &TemplateParameter{"sentinel"},
|
|
|
|
StorageAccountBlobEndpoint: &TemplateParameter{"sentinel"},
|
|
|
|
VMName: &TemplateParameter{"sentinel"},
|
|
|
|
VMSize: &TemplateParameter{"sentinel"},
|
2016-03-04 05:14:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
bs, err := json.Marshal(params)
|
|
|
|
if err != nil {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
|
|
|
|
var doc map[string]*json.RawMessage
|
|
|
|
err = json.Unmarshal(bs, &doc)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedKeys := []string{
|
|
|
|
"adminUsername",
|
|
|
|
"adminPassword",
|
|
|
|
"dnsNameForPublicIP",
|
|
|
|
"osDiskName",
|
2016-04-21 19:50:03 -04:00
|
|
|
"storageAccountBlobEndpoint",
|
2016-03-04 05:14:55 -05:00
|
|
|
"vmSize",
|
|
|
|
"vmName",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, expectedKey := range expectedKeys {
|
|
|
|
_, containsKey := doc[expectedKey]
|
|
|
|
if containsKey == false {
|
|
|
|
t.Fatalf("Expected template parameters to contain the key value '%s', but it did not!", expectedKey)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParameterValuesShouldBeSet(t *testing.T) {
|
|
|
|
params := TemplateParameters{
|
2016-04-21 19:50:03 -04:00
|
|
|
AdminUsername: &TemplateParameter{"adminusername00"},
|
|
|
|
AdminPassword: &TemplateParameter{"adminpassword00"},
|
|
|
|
DnsNameForPublicIP: &TemplateParameter{"dnsnameforpublicip00"},
|
|
|
|
OSDiskName: &TemplateParameter{"osdiskname00"},
|
|
|
|
StorageAccountBlobEndpoint: &TemplateParameter{"storageaccountblobendpoint00"},
|
|
|
|
VMName: &TemplateParameter{"vmname00"},
|
|
|
|
VMSize: &TemplateParameter{"vmsize00"},
|
2016-03-04 05:14:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
bs, err := json.Marshal(params)
|
|
|
|
if err != nil {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
|
|
|
|
var doc map[string]map[string]interface{}
|
|
|
|
err = json.Unmarshal(bs, &doc)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range doc {
|
|
|
|
var expectedValue = fmt.Sprintf("%s00", strings.ToLower(k))
|
|
|
|
var actualValue, exists = v["value"]
|
|
|
|
if exists != true {
|
|
|
|
t.Errorf("Expected to find a 'value' key under '%s', but it was missing!", k)
|
|
|
|
}
|
|
|
|
|
|
|
|
if expectedValue != actualValue {
|
|
|
|
t.Errorf("Expected '%s', but actual was '%s'!", expectedValue, actualValue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEmptyValuesShouldBeOmitted(t *testing.T) {
|
|
|
|
params := TemplateParameters{
|
|
|
|
AdminUsername: &TemplateParameter{"adminusername00"},
|
|
|
|
}
|
|
|
|
|
|
|
|
bs, err := json.Marshal(params)
|
|
|
|
if err != nil {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
|
|
|
|
var doc map[string]map[string]interface{}
|
|
|
|
err = json.Unmarshal(bs, &doc)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(doc) != 1 {
|
|
|
|
t.Errorf("Failed to omit empty template parameters from the JSON document!")
|
|
|
|
t.Errorf("doc=%+v", doc)
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|