summaryrefslogtreecommitdiff
path: root/vendor/github.com/mitchellh/packer/builder/azure/arm/capture_template_test.go
blob: 8c7a714055c41da1c5f1f700c7470acbf889f5f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package arm

import (
	"encoding/json"
	"testing"
)

var captureTemplate01 = `{
    "operationId": "ac1c7c38-a591-41b3-89bd-ea39fceace1b",
    "status": "Succeeded",
    "startTime": "2016-04-04T21:07:25.2900874+00:00",
    "endTime": "2016-04-04T21:07:26.4776321+00:00",
    "properties": {
        "output": {
            "$schema": "http://schema.management.azure.com/schemas/2014-04-01-preview/VM_IP.json",
            "contentVersion": "1.0.0.0",
            "parameters": {
                "vmName": {
                    "type": "string"
                },
                "vmSize": {
                    "type": "string",
                    "defaultValue": "Standard_A2"
                },
                "adminUserName": {
                    "type": "string"
                },
                "adminPassword": {
                    "type": "securestring"
                },
                "networkInterfaceId": {
                    "type": "string"
                }
            },
            "resources": [
                {
                    "apiVersion": "2015-06-15",
                    "properties": {
                        "hardwareProfile": {
                            "vmSize": "[parameters('vmSize')]"
                        },
                        "storageProfile": {
                            "osDisk": {
                                "osType": "Linux",
                                "name": "packer-osDisk.32118633-6dc9-449f-83b6-a7d2983bec14.vhd",
                                "createOption": "FromImage",
                                "image": {
                                    "uri": "http://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-osDisk.32118633-6dc9-449f-83b6-a7d2983bec14.vhd"
                                },
                                "vhd": {
                                    "uri": "http://storage.blob.core.windows.net/vmcontainerce1a1b75-f480-47cb-8e6e-55142e4a5f68/osDisk.ce1a1b75-f480-47cb-8e6e-55142e4a5f68.vhd"
                                },
                                "caching": "ReadWrite"
                            }
                        },
                        "osProfile": {
                            "computerName": "[parameters('vmName')]",
                            "adminUsername": "[parameters('adminUsername')]",
                            "adminPassword": "[parameters('adminPassword')]"
                        },
                        "networkProfile": {
                            "networkInterfaces": [
                                {
                                    "id": "[parameters('networkInterfaceId')]"
                                }
                            ]
                        },
                        "diagnosticsProfile": {
                            "bootDiagnostics": {
                                "enabled": false
                            }
                        },
                        "provisioningState": 0
                    },
                    "name": "[parameters('vmName')]",
                    "type": "Microsoft.Compute/virtualMachines",
                    "location": "southcentralus"
                }
            ]
        }
    }
}`

var captureTemplate02 = `{
    "operationId": "ac1c7c38-a591-41b3-89bd-ea39fceace1b",
    "status": "Succeeded",
    "startTime": "2016-04-04T21:07:25.2900874+00:00",
    "endTime": "2016-04-04T21:07:26.4776321+00:00"
}`

func TestCaptureParseJson(t *testing.T) {
	var operation CaptureOperation
	err := json.Unmarshal([]byte(captureTemplate01), &operation)
	if err != nil {
		t.Fatalf("failed to the sample capture operation: %s", err)
	}

	testSubject := operation.Properties.Output
	if testSubject.Schema != "http://schema.management.azure.com/schemas/2014-04-01-preview/VM_IP.json" {
		t.Errorf("Schema's value was unexpected: %s", testSubject.Schema)
	}
	if testSubject.ContentVersion != "1.0.0.0" {
		t.Errorf("ContentVersion's value was unexpected: %s", testSubject.ContentVersion)
	}

	// == Parameters ====================================
	if len(testSubject.Parameters) != 5 {
		t.Fatalf("expected parameters to have 5 keys, but got %d", len(testSubject.Parameters))
	}
	if _, ok := testSubject.Parameters["vmName"]; !ok {
		t.Errorf("Parameters['vmName'] was an expected parameters, but it did not exist")
	}
	if testSubject.Parameters["vmName"].Type != "string" {
		t.Errorf("Parameters['vmName'].Type == 'string', but got '%s'", testSubject.Parameters["vmName"].Type)
	}
	if _, ok := testSubject.Parameters["vmSize"]; !ok {
		t.Errorf("Parameters['vmSize'] was an expected parameters, but it did not exist")
	}
	if testSubject.Parameters["vmSize"].Type != "string" {
		t.Errorf("Parameters['vmSize'].Type == 'string', but got '%s'", testSubject.Parameters["vmSize"])
	}
	if testSubject.Parameters["vmSize"].DefaultValue != "Standard_A2" {
		t.Errorf("Parameters['vmSize'].DefaultValue == 'string', but got '%s'", testSubject.Parameters["vmSize"].DefaultValue)
	}

	// == Resources =====================================
	if len(testSubject.Resources) != 1 {
		t.Fatalf("expected resources to have length 1, but got %d", len(testSubject.Resources))
	}
	if testSubject.Resources[0].Name != "[parameters('vmName')]" {
		t.Errorf("Resources[0].Name's value was unexpected: %s", testSubject.Resources[0].Name)
	}
	if testSubject.Resources[0].Type != "Microsoft.Compute/virtualMachines" {
		t.Errorf("Resources[0].Type's value was unexpected: %s", testSubject.Resources[0].Type)
	}
	if testSubject.Resources[0].Location != "southcentralus" {
		t.Errorf("Resources[0].Location's value was unexpected: %s", testSubject.Resources[0].Location)
	}

	// == Resources/Properties =====================================
	if testSubject.Resources[0].Properties.ProvisioningState != 0 {
		t.Errorf("Resources[0].Properties.ProvisioningState's value was unexpected: %d", testSubject.Resources[0].Properties.ProvisioningState)
	}

	// == Resources/Properties/HardwareProfile ======================
	hardwareProfile := testSubject.Resources[0].Properties.HardwareProfile
	if hardwareProfile.VMSize != "[parameters('vmSize')]" {
		t.Errorf("Resources[0].Properties.HardwareProfile.VMSize's value was unexpected: %s", hardwareProfile.VMSize)
	}

	// == Resources/Properties/StorageProfile/OSDisk ================
	osDisk := testSubject.Resources[0].Properties.StorageProfile.OSDisk
	if osDisk.OSType != "Linux" {
		t.Errorf("Resources[0].Properties.StorageProfile.OSDisk.OSDisk's value was unexpected: %s", osDisk.OSType)
	}
	if osDisk.Name != "packer-osDisk.32118633-6dc9-449f-83b6-a7d2983bec14.vhd" {
		t.Errorf("Resources[0].Properties.StorageProfile.OSDisk.Name's value was unexpected: %s", osDisk.Name)
	}
	if osDisk.CreateOption != "FromImage" {
		t.Errorf("Resources[0].Properties.StorageProfile.OSDisk.CreateOption's value was unexpected: %s", osDisk.CreateOption)
	}
	if osDisk.Image.Uri != "http://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-osDisk.32118633-6dc9-449f-83b6-a7d2983bec14.vhd" {
		t.Errorf("Resources[0].Properties.StorageProfile.OSDisk.Image.Uri's value was unexpected: %s", osDisk.Image.Uri)
	}
	if osDisk.Vhd.Uri != "http://storage.blob.core.windows.net/vmcontainerce1a1b75-f480-47cb-8e6e-55142e4a5f68/osDisk.ce1a1b75-f480-47cb-8e6e-55142e4a5f68.vhd" {
		t.Errorf("Resources[0].Properties.StorageProfile.OSDisk.Vhd.Uri's value was unexpected: %s", osDisk.Vhd.Uri)
	}
	if osDisk.Caching != "ReadWrite" {
		t.Errorf("Resources[0].Properties.StorageProfile.OSDisk.Caching's value was unexpected: %s", osDisk.Caching)
	}

	// == Resources/Properties/OSProfile ============================
	osProfile := testSubject.Resources[0].Properties.OSProfile
	if osProfile.AdminPassword != "[parameters('adminPassword')]" {
		t.Errorf("Resources[0].Properties.OSProfile.AdminPassword's value was unexpected: %s", osProfile.AdminPassword)
	}
	if osProfile.AdminUsername != "[parameters('adminUsername')]" {
		t.Errorf("Resources[0].Properties.OSProfile.AdminUsername's value was unexpected: %s", osProfile.AdminUsername)
	}
	if osProfile.ComputerName != "[parameters('vmName')]" {
		t.Errorf("Resources[0].Properties.OSProfile.ComputerName's value was unexpected: %s", osProfile.ComputerName)
	}

	// == Resources/Properties/NetworkProfile =======================
	networkProfile := testSubject.Resources[0].Properties.NetworkProfile
	if len(networkProfile.NetworkInterfaces) != 1 {
		t.Errorf("Count of Resources[0].Properties.NetworkProfile.NetworkInterfaces was expected to be 1, but go %d", len(networkProfile.NetworkInterfaces))
	}
	if networkProfile.NetworkInterfaces[0].Id != "[parameters('networkInterfaceId')]" {
		t.Errorf("Resources[0].Properties.NetworkProfile.NetworkInterfaces[0].Id's value was unexpected: %s", networkProfile.NetworkInterfaces[0].Id)
	}

	// == Resources/Properties/DiagnosticsProfile ===================
	diagnosticsProfile := testSubject.Resources[0].Properties.DiagnosticsProfile
	if diagnosticsProfile.BootDiagnostics.Enabled != false {
		t.Errorf("Resources[0].Properties.DiagnosticsProfile.BootDiagnostics.Enabled's value was unexpected: %t", diagnosticsProfile.BootDiagnostics.Enabled)
	}
}

func TestCaptureEmptyOperationJson(t *testing.T) {
	var operation CaptureOperation
	err := json.Unmarshal([]byte(captureTemplate02), &operation)
	if err != nil {
		t.Fatalf("failed to the sample capture operation: %s", err)
	}

	if operation.Properties != nil {
		t.Errorf("JSON contained no properties, but value was not nil: %+v", operation.Properties)
	}
}