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

     1  package arm
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"testing"
     7  
     8  	"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2018-04-01/compute"
     9  
    10  	"github.com/hashicorp/packer/builder/azure/common/constants"
    11  
    12  	"github.com/hashicorp/packer/helper/multistep"
    13  )
    14  
    15  func TestStepGetAdditionalDiskShouldFailIfGetFails(t *testing.T) {
    16  	var testSubject = &StepGetDataDisk{
    17  		query: func(context.Context, string, string) (compute.VirtualMachine, error) {
    18  			return createVirtualMachineWithDataDisksFromUri("test.vhd"), fmt.Errorf("!! Unit Test FAIL !!")
    19  		},
    20  		say:   func(message string) {},
    21  		error: func(e error) {},
    22  	}
    23  
    24  	stateBag := createTestStateBagStepGetAdditionalDisks()
    25  
    26  	var result = testSubject.Run(context.Background(), stateBag)
    27  	if result != multistep.ActionHalt {
    28  		t.Fatalf("Expected the step to return 'ActionHalt', but got '%d'.", result)
    29  	}
    30  
    31  	if _, ok := stateBag.GetOk(constants.Error); ok == false {
    32  		t.Fatalf("Expected the step to set stateBag['%s'], but it was not.", constants.Error)
    33  	}
    34  }
    35  
    36  func TestStepGetAdditionalDiskShouldPassIfGetPasses(t *testing.T) {
    37  	var testSubject = &StepGetDataDisk{
    38  		query: func(context.Context, string, string) (compute.VirtualMachine, error) {
    39  			return createVirtualMachineWithDataDisksFromUri("test.vhd"), nil
    40  		},
    41  		say:   func(message string) {},
    42  		error: func(e error) {},
    43  	}
    44  
    45  	stateBag := createTestStateBagStepGetAdditionalDisks()
    46  
    47  	var result = testSubject.Run(context.Background(), stateBag)
    48  	if result != multistep.ActionContinue {
    49  		t.Fatalf("Expected the step to return 'ActionContinue', but got '%d'.", result)
    50  	}
    51  
    52  	if _, ok := stateBag.GetOk(constants.Error); ok == true {
    53  		t.Fatalf("Expected the step to not set stateBag['%s'], but it was.", constants.Error)
    54  	}
    55  }
    56  
    57  func TestStepGetAdditionalDiskShouldTakeValidateArgumentsFromStateBag(t *testing.T) {
    58  	var actualResourceGroupName string
    59  	var actualComputeName string
    60  
    61  	var testSubject = &StepGetDataDisk{
    62  		query: func(ctx context.Context, resourceGroupName string, computeName string) (compute.VirtualMachine, error) {
    63  			actualResourceGroupName = resourceGroupName
    64  			actualComputeName = computeName
    65  
    66  			return createVirtualMachineWithDataDisksFromUri("test.vhd"), nil
    67  		},
    68  		say:   func(message string) {},
    69  		error: func(e error) {},
    70  	}
    71  
    72  	stateBag := createTestStateBagStepGetAdditionalDisks()
    73  	var result = testSubject.Run(context.Background(), stateBag)
    74  
    75  	if result != multistep.ActionContinue {
    76  		t.Fatalf("Expected the step to return 'ActionContinue', but got '%d'.", result)
    77  	}
    78  
    79  	var expectedComputeName = stateBag.Get(constants.ArmComputeName).(string)
    80  	var expectedResourceGroupName = stateBag.Get(constants.ArmResourceGroupName).(string)
    81  
    82  	if actualComputeName != expectedComputeName {
    83  		t.Fatal("Expected the step to source 'constants.ArmResourceGroupName' from the state bag, but it did not.")
    84  	}
    85  
    86  	if actualResourceGroupName != expectedResourceGroupName {
    87  		t.Fatal("Expected the step to source 'constants.ArmResourceGroupName' from the state bag, but it did not.")
    88  	}
    89  
    90  	expectedAdditionalDiskVhds, ok := stateBag.GetOk(constants.ArmAdditionalDiskVhds)
    91  	if !ok {
    92  		t.Fatalf("Expected the state bag to have a value for '%s', but it did not.", constants.ArmAdditionalDiskVhds)
    93  	}
    94  
    95  	expectedAdditionalDiskVhd := expectedAdditionalDiskVhds.([]string)
    96  	if expectedAdditionalDiskVhd[0] != "test.vhd" {
    97  		t.Fatalf("Expected the value of stateBag[%s] to be 'test.vhd', but got '%s'.", constants.ArmAdditionalDiskVhds, expectedAdditionalDiskVhd[0])
    98  	}
    99  }
   100  
   101  func createTestStateBagStepGetAdditionalDisks() multistep.StateBag {
   102  	stateBag := new(multistep.BasicStateBag)
   103  
   104  	stateBag.Put(constants.ArmComputeName, "Unit Test: ComputeName")
   105  	stateBag.Put(constants.ArmResourceGroupName, "Unit Test: ResourceGroupName")
   106  
   107  	return stateBag
   108  }
   109  
   110  func createVirtualMachineWithDataDisksFromUri(vhdUri string) compute.VirtualMachine {
   111  	vm := compute.VirtualMachine{
   112  		VirtualMachineProperties: &compute.VirtualMachineProperties{
   113  			StorageProfile: &compute.StorageProfile{
   114  				OsDisk: &compute.OSDisk{
   115  					Vhd: &compute.VirtualHardDisk{
   116  						URI: &vhdUri,
   117  					},
   118  				},
   119  				DataDisks: &[]compute.DataDisk{
   120  					{
   121  						Vhd: &compute.VirtualHardDisk{
   122  							URI: &vhdUri,
   123  						},
   124  					},
   125  				},
   126  			},
   127  		},
   128  	}
   129  
   130  	return vm
   131  }