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