github.com/mmcquillan/packer@v1.1.1-0.20171009221028-c85cf0483a5d/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                          },
    56                          "osProfile": {
    57                              "computerName": "[parameters('vmName')]",
    58                              "adminUsername": "[parameters('adminUsername')]",
    59                              "adminPassword": "[parameters('adminPassword')]"
    60                          },
    61                          "networkProfile": {
    62                              "networkInterfaces": [
    63                                  {
    64                                      "id": "[parameters('networkInterfaceId')]"
    65                                  }
    66                              ]
    67                          },
    68                          "diagnosticsProfile": {
    69                              "bootDiagnostics": {
    70                                  "enabled": false
    71                              }
    72                          },
    73                          "provisioningState": 0
    74                      },
    75                      "name": "[parameters('vmName')]",
    76                      "type": "Microsoft.Compute/virtualMachines",
    77                      "location": "southcentralus"
    78                  }
    79              ]
    80          }
    81      }
    82  }`
    83  
    84  var captureTemplate02 = `{
    85      "operationId": "ac1c7c38-a591-41b3-89bd-ea39fceace1b",
    86      "status": "Succeeded",
    87      "startTime": "2016-04-04T21:07:25.2900874+00:00",
    88      "endTime": "2016-04-04T21:07:26.4776321+00:00"
    89  }`
    90  
    91  func TestCaptureParseJson(t *testing.T) {
    92  	var operation CaptureOperation
    93  	err := json.Unmarshal([]byte(captureTemplate01), &operation)
    94  	if err != nil {
    95  		t.Fatalf("failed to the sample capture operation: %s", err)
    96  	}
    97  
    98  	testSubject := operation.Properties.Output
    99  	if testSubject.Schema != "http://schema.management.azure.com/schemas/2014-04-01-preview/VM_IP.json" {
   100  		t.Errorf("Schema's value was unexpected: %s", testSubject.Schema)
   101  	}
   102  	if testSubject.ContentVersion != "1.0.0.0" {
   103  		t.Errorf("ContentVersion's value was unexpected: %s", testSubject.ContentVersion)
   104  	}
   105  
   106  	// == Parameters ====================================
   107  	if len(testSubject.Parameters) != 5 {
   108  		t.Fatalf("expected parameters to have 5 keys, but got %d", len(testSubject.Parameters))
   109  	}
   110  	if _, ok := testSubject.Parameters["vmName"]; !ok {
   111  		t.Errorf("Parameters['vmName'] was an expected parameters, but it did not exist")
   112  	}
   113  	if testSubject.Parameters["vmName"].Type != "string" {
   114  		t.Errorf("Parameters['vmName'].Type == 'string', but got '%s'", testSubject.Parameters["vmName"].Type)
   115  	}
   116  	if _, ok := testSubject.Parameters["vmSize"]; !ok {
   117  		t.Errorf("Parameters['vmSize'] was an expected parameters, but it did not exist")
   118  	}
   119  	if testSubject.Parameters["vmSize"].Type != "string" {
   120  		t.Errorf("Parameters['vmSize'].Type == 'string', but got '%s'", testSubject.Parameters["vmSize"])
   121  	}
   122  	if testSubject.Parameters["vmSize"].DefaultValue != "Standard_A2" {
   123  		t.Errorf("Parameters['vmSize'].DefaultValue == 'string', but got '%s'", testSubject.Parameters["vmSize"].DefaultValue)
   124  	}
   125  
   126  	// == Resources =====================================
   127  	if len(testSubject.Resources) != 1 {
   128  		t.Fatalf("expected resources to have length 1, but got %d", len(testSubject.Resources))
   129  	}
   130  	if testSubject.Resources[0].Name != "[parameters('vmName')]" {
   131  		t.Errorf("Resources[0].Name's value was unexpected: %s", testSubject.Resources[0].Name)
   132  	}
   133  	if testSubject.Resources[0].Type != "Microsoft.Compute/virtualMachines" {
   134  		t.Errorf("Resources[0].Type's value was unexpected: %s", testSubject.Resources[0].Type)
   135  	}
   136  	if testSubject.Resources[0].Location != "southcentralus" {
   137  		t.Errorf("Resources[0].Location's value was unexpected: %s", testSubject.Resources[0].Location)
   138  	}
   139  
   140  	// == Resources/Properties =====================================
   141  	if testSubject.Resources[0].Properties.ProvisioningState != 0 {
   142  		t.Errorf("Resources[0].Properties.ProvisioningState's value was unexpected: %d", testSubject.Resources[0].Properties.ProvisioningState)
   143  	}
   144  
   145  	// == Resources/Properties/HardwareProfile ======================
   146  	hardwareProfile := testSubject.Resources[0].Properties.HardwareProfile
   147  	if hardwareProfile.VMSize != "[parameters('vmSize')]" {
   148  		t.Errorf("Resources[0].Properties.HardwareProfile.VMSize's value was unexpected: %s", hardwareProfile.VMSize)
   149  	}
   150  
   151  	// == Resources/Properties/StorageProfile/OSDisk ================
   152  	osDisk := testSubject.Resources[0].Properties.StorageProfile.OSDisk
   153  	if osDisk.OSType != "Linux" {
   154  		t.Errorf("Resources[0].Properties.StorageProfile.OSDisk.OSDisk's value was unexpected: %s", osDisk.OSType)
   155  	}
   156  	if osDisk.Name != "packer-osDisk.32118633-6dc9-449f-83b6-a7d2983bec14.vhd" {
   157  		t.Errorf("Resources[0].Properties.StorageProfile.OSDisk.Name's value was unexpected: %s", osDisk.Name)
   158  	}
   159  	if osDisk.CreateOption != "FromImage" {
   160  		t.Errorf("Resources[0].Properties.StorageProfile.OSDisk.CreateOption's value was unexpected: %s", osDisk.CreateOption)
   161  	}
   162  	if osDisk.Image.Uri != "http://storage.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-osDisk.32118633-6dc9-449f-83b6-a7d2983bec14.vhd" {
   163  		t.Errorf("Resources[0].Properties.StorageProfile.OSDisk.Image.Uri's value was unexpected: %s", osDisk.Image.Uri)
   164  	}
   165  	if osDisk.Vhd.Uri != "http://storage.blob.core.windows.net/vmcontainerce1a1b75-f480-47cb-8e6e-55142e4a5f68/osDisk.ce1a1b75-f480-47cb-8e6e-55142e4a5f68.vhd" {
   166  		t.Errorf("Resources[0].Properties.StorageProfile.OSDisk.Vhd.Uri's value was unexpected: %s", osDisk.Vhd.Uri)
   167  	}
   168  	if osDisk.Caching != "ReadWrite" {
   169  		t.Errorf("Resources[0].Properties.StorageProfile.OSDisk.Caching's value was unexpected: %s", osDisk.Caching)
   170  	}
   171  
   172  	// == Resources/Properties/OSProfile ============================
   173  	osProfile := testSubject.Resources[0].Properties.OSProfile
   174  	if osProfile.AdminPassword != "[parameters('adminPassword')]" {
   175  		t.Errorf("Resources[0].Properties.OSProfile.AdminPassword's value was unexpected: %s", osProfile.AdminPassword)
   176  	}
   177  	if osProfile.AdminUsername != "[parameters('adminUsername')]" {
   178  		t.Errorf("Resources[0].Properties.OSProfile.AdminUsername's value was unexpected: %s", osProfile.AdminUsername)
   179  	}
   180  	if osProfile.ComputerName != "[parameters('vmName')]" {
   181  		t.Errorf("Resources[0].Properties.OSProfile.ComputerName's value was unexpected: %s", osProfile.ComputerName)
   182  	}
   183  
   184  	// == Resources/Properties/NetworkProfile =======================
   185  	networkProfile := testSubject.Resources[0].Properties.NetworkProfile
   186  	if len(networkProfile.NetworkInterfaces) != 1 {
   187  		t.Errorf("Count of Resources[0].Properties.NetworkProfile.NetworkInterfaces was expected to be 1, but go %d", len(networkProfile.NetworkInterfaces))
   188  	}
   189  	if networkProfile.NetworkInterfaces[0].Id != "[parameters('networkInterfaceId')]" {
   190  		t.Errorf("Resources[0].Properties.NetworkProfile.NetworkInterfaces[0].Id's value was unexpected: %s", networkProfile.NetworkInterfaces[0].Id)
   191  	}
   192  
   193  	// == Resources/Properties/DiagnosticsProfile ===================
   194  	diagnosticsProfile := testSubject.Resources[0].Properties.DiagnosticsProfile
   195  	if diagnosticsProfile.BootDiagnostics.Enabled != false {
   196  		t.Errorf("Resources[0].Properties.DiagnosticsProfile.BootDiagnostics.Enabled's value was unexpected: %t", diagnosticsProfile.BootDiagnostics.Enabled)
   197  	}
   198  }
   199  
   200  func TestCaptureEmptyOperationJson(t *testing.T) {
   201  	var operation CaptureOperation
   202  	err := json.Unmarshal([]byte(captureTemplate02), &operation)
   203  	if err != nil {
   204  		t.Fatalf("failed to the sample capture operation: %s", err)
   205  	}
   206  
   207  	if operation.Properties != nil {
   208  		t.Errorf("JSON contained no properties, but value was not nil: %+v", operation.Properties)
   209  	}
   210  }