github.com/mitchellh/packer@v1.3.2/builder/azure/arm/capture_template_test.go (about)

     1  package arm
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  )
     7  
     8  var captureTemplate01 = `{
     9      "operationId": "ac1c7c38-a591-41b3-89bd-ea39fceace1b",
    10      "status": "Succeeded",
    11      "startTime": "2016-04-04T21:07:25.2900874+00:00",
    12      "endTime": "2016-04-04T21:07:26.4776321+00:00",
    13      "properties": {
    14          "output": {
    15              "$schema": "http://schema.management.azure.com/schemas/2014-04-01-preview/VM_IP.json",
    16              "contentVersion": "1.0.0.0",
    17              "parameters": {
    18                  "vmName": {
    19                      "type": "string"
    20                  },
    21                  "vmSize": {
    22                      "type": "string",
    23                      "defaultValue": "Standard_A2"
    24                  },
    25                  "adminUserName": {
    26                      "type": "string"
    27                  },
    28                  "adminPassword": {
    29                      "type": "securestring"
    30                  },
    31                  "networkInterfaceId": {
    32                      "type": "string"
    33                  }
    34              },
    35              "resources": [
    36                  {
    37                      "apiVersion": "2015-06-15",
    38                      "properties": {
    39                          "hardwareProfile": {
    40                              "vmSize": "[parameters('vmSize')]"
    41                          },
    42                          "storageProfile": {
    43                              "osDisk": {
    44                                  "osType": "Linux",
    45                                  "name": "packer-osDisk.32118633-6dc9-449f-83b6-a7d2983bec14.vhd",
    46                                  "createOption": "FromImage",
    47                                  "image": {
    48                                      "uri": "http://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-osDisk.32118633-6dc9-449f-83b6-a7d2983bec14.vhd"
    49                                  },
    50                                  "vhd": {
    51                                      "uri": "http://storage.blob.core.windows.net/vmcontainerce1a1b75-f480-47cb-8e6e-55142e4a5f68/osDisk.ce1a1b75-f480-47cb-8e6e-55142e4a5f68.vhd"
    52                                  },
    53                                  "caching": "ReadWrite"
    54  							},
    55  							"dataDisks": [
    56  								{
    57  									"lun": 0,
    58  									"name": "packer-datadisk-0.32118633-6dc9-449f-83b6-a7d2983bec14.vhd",
    59  									"createOption": "Empty",
    60  									"image": {
    61  										"uri": "http://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-datadisk-0.32118633-6dc9-449f-83b6-a7d2983bec14.vhd"
    62  									},
    63  									"vhd": {
    64  										"uri": "http://storage.blob.core.windows.net/vmcontainerce1a1b75-f480-47cb-8e6e-55142e4a5f68/datadisk-0.ce1a1b75-f480-47cb-8e6e-55142e4a5f68.vhd"
    65  									},
    66  									"caching": "ReadWrite"
    67  								},
    68  								{
    69  									"lun": 1,
    70  									"name": "packer-datadisk-1.32118633-6dc9-449f-83b6-a7d2983bec14.vhd",
    71  									"createOption": "Empty",
    72  									"image": {
    73  										"uri": "http://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-datadisk-1.32118633-6dc9-449f-83b6-a7d2983bec14.vhd"
    74  									},
    75  									"vhd": {
    76  										"uri": "http://storage.blob.core.windows.net/vmcontainerce1a1b75-f480-47cb-8e6e-55142e4a5f68/datadisk-1.ce1a1b75-f480-47cb-8e6e-55142e4a5f68.vhd"
    77  									},
    78  									"caching": "ReadWrite"
    79  								}
    80  							]
    81                          },
    82                          "osProfile": {
    83                              "computerName": "[parameters('vmName')]",
    84                              "adminUsername": "[parameters('adminUsername')]",
    85                              "adminPassword": "[parameters('adminPassword')]"
    86                          },
    87                          "networkProfile": {
    88                              "networkInterfaces": [
    89                                  {
    90                                      "id": "[parameters('networkInterfaceId')]"
    91                                  }
    92                              ]
    93                          },
    94                          "diagnosticsProfile": {
    95                              "bootDiagnostics": {
    96                                  "enabled": false
    97                              }
    98                          },
    99                          "provisioningState": 0
   100                      },
   101                      "name": "[parameters('vmName')]",
   102                      "type": "Microsoft.Compute/virtualMachines",
   103                      "location": "southcentralus"
   104                  }
   105              ]
   106          }
   107      }
   108  }`
   109  
   110  var captureTemplate02 = `{
   111      "operationId": "ac1c7c38-a591-41b3-89bd-ea39fceace1b",
   112      "status": "Succeeded",
   113      "startTime": "2016-04-04T21:07:25.2900874+00:00",
   114      "endTime": "2016-04-04T21:07:26.4776321+00:00"
   115  }`
   116  
   117  func TestCaptureParseJson(t *testing.T) {
   118  	var operation CaptureOperation
   119  	err := json.Unmarshal([]byte(captureTemplate01), &operation)
   120  	if err != nil {
   121  		t.Fatalf("failed to the sample capture operation: %s", err)
   122  	}
   123  
   124  	testSubject := operation.Properties.Output
   125  	if testSubject.Schema != "http://schema.management.azure.com/schemas/2014-04-01-preview/VM_IP.json" {
   126  		t.Errorf("Schema's value was unexpected: %s", testSubject.Schema)
   127  	}
   128  	if testSubject.ContentVersion != "1.0.0.0" {
   129  		t.Errorf("ContentVersion's value was unexpected: %s", testSubject.ContentVersion)
   130  	}
   131  
   132  	// == Parameters ====================================
   133  	if len(testSubject.Parameters) != 5 {
   134  		t.Fatalf("expected parameters to have 5 keys, but got %d", len(testSubject.Parameters))
   135  	}
   136  	if _, ok := testSubject.Parameters["vmName"]; !ok {
   137  		t.Errorf("Parameters['vmName'] was an expected parameters, but it did not exist")
   138  	}
   139  	if testSubject.Parameters["vmName"].Type != "string" {
   140  		t.Errorf("Parameters['vmName'].Type == 'string', but got '%s'", testSubject.Parameters["vmName"].Type)
   141  	}
   142  	if _, ok := testSubject.Parameters["vmSize"]; !ok {
   143  		t.Errorf("Parameters['vmSize'] was an expected parameters, but it did not exist")
   144  	}
   145  	if testSubject.Parameters["vmSize"].Type != "string" {
   146  		t.Errorf("Parameters['vmSize'].Type == 'string', but got '%s'", testSubject.Parameters["vmSize"])
   147  	}
   148  	if testSubject.Parameters["vmSize"].DefaultValue != "Standard_A2" {
   149  		t.Errorf("Parameters['vmSize'].DefaultValue == 'string', but got '%s'", testSubject.Parameters["vmSize"].DefaultValue)
   150  	}
   151  
   152  	// == Resources =====================================
   153  	if len(testSubject.Resources) != 1 {
   154  		t.Fatalf("expected resources to have length 1, but got %d", len(testSubject.Resources))
   155  	}
   156  	if testSubject.Resources[0].Name != "[parameters('vmName')]" {
   157  		t.Errorf("Resources[0].Name's value was unexpected: %s", testSubject.Resources[0].Name)
   158  	}
   159  	if testSubject.Resources[0].Type != "Microsoft.Compute/virtualMachines" {
   160  		t.Errorf("Resources[0].Type's value was unexpected: %s", testSubject.Resources[0].Type)
   161  	}
   162  	if testSubject.Resources[0].Location != "southcentralus" {
   163  		t.Errorf("Resources[0].Location's value was unexpected: %s", testSubject.Resources[0].Location)
   164  	}
   165  
   166  	// == Resources/Properties =====================================
   167  	if testSubject.Resources[0].Properties.ProvisioningState != 0 {
   168  		t.Errorf("Resources[0].Properties.ProvisioningState's value was unexpected: %d", testSubject.Resources[0].Properties.ProvisioningState)
   169  	}
   170  
   171  	// == Resources/Properties/HardwareProfile ======================
   172  	hardwareProfile := testSubject.Resources[0].Properties.HardwareProfile
   173  	if hardwareProfile.VMSize != "[parameters('vmSize')]" {
   174  		t.Errorf("Resources[0].Properties.HardwareProfile.VMSize's value was unexpected: %s", hardwareProfile.VMSize)
   175  	}
   176  
   177  	// == Resources/Properties/StorageProfile/OSDisk ================
   178  	osDisk := testSubject.Resources[0].Properties.StorageProfile.OSDisk
   179  	if osDisk.OSType != "Linux" {
   180  		t.Errorf("Resources[0].Properties.StorageProfile.OSDisk.OSDisk's value was unexpected: %s", osDisk.OSType)
   181  	}
   182  	if osDisk.Name != "packer-osDisk.32118633-6dc9-449f-83b6-a7d2983bec14.vhd" {
   183  		t.Errorf("Resources[0].Properties.StorageProfile.OSDisk.Name's value was unexpected: %s", osDisk.Name)
   184  	}
   185  	if osDisk.CreateOption != "FromImage" {
   186  		t.Errorf("Resources[0].Properties.StorageProfile.OSDisk.CreateOption's value was unexpected: %s", osDisk.CreateOption)
   187  	}
   188  	if osDisk.Image.Uri != "http://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-osDisk.32118633-6dc9-449f-83b6-a7d2983bec14.vhd" {
   189  		t.Errorf("Resources[0].Properties.StorageProfile.OSDisk.Image.Uri's value was unexpected: %s", osDisk.Image.Uri)
   190  	}
   191  	if osDisk.Vhd.Uri != "http://storage.blob.core.windows.net/vmcontainerce1a1b75-f480-47cb-8e6e-55142e4a5f68/osDisk.ce1a1b75-f480-47cb-8e6e-55142e4a5f68.vhd" {
   192  		t.Errorf("Resources[0].Properties.StorageProfile.OSDisk.Vhd.Uri's value was unexpected: %s", osDisk.Vhd.Uri)
   193  	}
   194  	if osDisk.Caching != "ReadWrite" {
   195  		t.Errorf("Resources[0].Properties.StorageProfile.OSDisk.Caching's value was unexpected: %s", osDisk.Caching)
   196  	}
   197  
   198  	// == Resources/Properties/StorageProfile/DataDisks ================
   199  	dataDisks := testSubject.Resources[0].Properties.StorageProfile.DataDisks
   200  	if len(dataDisks) != 2 {
   201  		t.Errorf("Resources[0].Properties.StorageProfile.DataDisks, 2 disks expected but was: %d", len(dataDisks))
   202  	}
   203  	if dataDisks[0].Name != "packer-datadisk-0.32118633-6dc9-449f-83b6-a7d2983bec14.vhd" {
   204  		t.Errorf("Resources[0].Properties.StorageProfile.dataDisks[0].Name's value was unexpected: %s", dataDisks[0].Name)
   205  	}
   206  	if dataDisks[0].CreateOption != "Empty" {
   207  		t.Errorf("Resources[0].Properties.StorageProfile.dataDisks[0].CreateOption's value was unexpected: %s", dataDisks[0].CreateOption)
   208  	}
   209  	if dataDisks[0].Image.Uri != "http://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-datadisk-0.32118633-6dc9-449f-83b6-a7d2983bec14.vhd" {
   210  		t.Errorf("Resources[0].Properties.StorageProfile.dataDisks[0].Image.Uri's value was unexpected: %s", dataDisks[0].Image.Uri)
   211  	}
   212  	if dataDisks[0].Vhd.Uri != "http://storage.blob.core.windows.net/vmcontainerce1a1b75-f480-47cb-8e6e-55142e4a5f68/datadisk-0.ce1a1b75-f480-47cb-8e6e-55142e4a5f68.vhd" {
   213  		t.Errorf("Resources[0].Properties.StorageProfile.dataDisks[0].Vhd.Uri's value was unexpected: %s", dataDisks[0].Vhd.Uri)
   214  	}
   215  	if dataDisks[0].Caching != "ReadWrite" {
   216  		t.Errorf("Resources[0].Properties.StorageProfile.dataDisks[0].Caching's value was unexpected: %s", dataDisks[0].Caching)
   217  	}
   218  	if dataDisks[1].Name != "packer-datadisk-1.32118633-6dc9-449f-83b6-a7d2983bec14.vhd" {
   219  		t.Errorf("Resources[0].Properties.StorageProfile.dataDisks[1].Name's value was unexpected: %s", dataDisks[1].Name)
   220  	}
   221  	if dataDisks[1].CreateOption != "Empty" {
   222  		t.Errorf("Resources[0].Properties.StorageProfile.dataDisks[1].CreateOption's value was unexpected: %s", dataDisks[1].CreateOption)
   223  	}
   224  	if dataDisks[1].Image.Uri != "http://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-datadisk-1.32118633-6dc9-449f-83b6-a7d2983bec14.vhd" {
   225  		t.Errorf("Resources[0].Properties.StorageProfile.dataDisks[1].Image.Uri's value was unexpected: %s", dataDisks[1].Image.Uri)
   226  	}
   227  	if dataDisks[1].Vhd.Uri != "http://storage.blob.core.windows.net/vmcontainerce1a1b75-f480-47cb-8e6e-55142e4a5f68/datadisk-1.ce1a1b75-f480-47cb-8e6e-55142e4a5f68.vhd" {
   228  		t.Errorf("Resources[0].Properties.StorageProfile.dataDisks[1].Vhd.Uri's value was unexpected: %s", dataDisks[1].Vhd.Uri)
   229  	}
   230  	if dataDisks[1].Caching != "ReadWrite" {
   231  		t.Errorf("Resources[0].Properties.StorageProfile.dataDisks[1].Caching's value was unexpected: %s", dataDisks[1].Caching)
   232  	}
   233  
   234  	// == Resources/Properties/OSProfile ============================
   235  	osProfile := testSubject.Resources[0].Properties.OSProfile
   236  	if osProfile.AdminPassword != "[parameters('adminPassword')]" {
   237  		t.Errorf("Resources[0].Properties.OSProfile.AdminPassword's value was unexpected: %s", osProfile.AdminPassword)
   238  	}
   239  	if osProfile.AdminUsername != "[parameters('adminUsername')]" {
   240  		t.Errorf("Resources[0].Properties.OSProfile.AdminUsername's value was unexpected: %s", osProfile.AdminUsername)
   241  	}
   242  	if osProfile.ComputerName != "[parameters('vmName')]" {
   243  		t.Errorf("Resources[0].Properties.OSProfile.ComputerName's value was unexpected: %s", osProfile.ComputerName)
   244  	}
   245  
   246  	// == Resources/Properties/NetworkProfile =======================
   247  	networkProfile := testSubject.Resources[0].Properties.NetworkProfile
   248  	if len(networkProfile.NetworkInterfaces) != 1 {
   249  		t.Errorf("Count of Resources[0].Properties.NetworkProfile.NetworkInterfaces was expected to be 1, but go %d", len(networkProfile.NetworkInterfaces))
   250  	}
   251  	if networkProfile.NetworkInterfaces[0].Id != "[parameters('networkInterfaceId')]" {
   252  		t.Errorf("Resources[0].Properties.NetworkProfile.NetworkInterfaces[0].Id's value was unexpected: %s", networkProfile.NetworkInterfaces[0].Id)
   253  	}
   254  
   255  	// == Resources/Properties/DiagnosticsProfile ===================
   256  	diagnosticsProfile := testSubject.Resources[0].Properties.DiagnosticsProfile
   257  	if diagnosticsProfile.BootDiagnostics.Enabled != false {
   258  		t.Errorf("Resources[0].Properties.DiagnosticsProfile.BootDiagnostics.Enabled's value was unexpected: %t", diagnosticsProfile.BootDiagnostics.Enabled)
   259  	}
   260  }
   261  
   262  func TestCaptureEmptyOperationJson(t *testing.T) {
   263  	var operation CaptureOperation
   264  	err := json.Unmarshal([]byte(captureTemplate02), &operation)
   265  	if err != nil {
   266  		t.Fatalf("failed to the sample capture operation: %s", err)
   267  	}
   268  
   269  	if operation.Properties != nil {
   270  		t.Errorf("JSON contained no properties, but value was not nil: %+v", operation.Properties)
   271  	}
   272  }