github.phpd.cn/hashicorp/packer@v1.3.2/builder/azure/arm/step_get_os_disk_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 TestStepGetOSDiskShouldFailIfGetFails(t *testing.T) {
    16  	var testSubject = &StepGetOSDisk{
    17  		query: func(context.Context, string, string) (compute.VirtualMachine, error) {
    18  			return createVirtualMachineFromUri("test.vhd"), fmt.Errorf("!! Unit Test FAIL !!")
    19  		},
    20  		say:   func(message string) {},
    21  		error: func(e error) {},
    22  	}
    23  
    24  	stateBag := createTestStateBagStepGetOSDisk()
    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 TestStepGetOSDiskShouldPassIfGetPasses(t *testing.T) {
    37  	var testSubject = &StepGetOSDisk{
    38  		query: func(context.Context, string, string) (compute.VirtualMachine, error) {
    39  			return createVirtualMachineFromUri("test.vhd"), nil
    40  		},
    41  		say:   func(message string) {},
    42  		error: func(e error) {},
    43  	}
    44  
    45  	stateBag := createTestStateBagStepGetOSDisk()
    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 TestStepGetOSDiskShouldTakeValidateArgumentsFromStateBag(t *testing.T) {
    58  	var actualResourceGroupName string
    59  	var actualComputeName string
    60  
    61  	var testSubject = &StepGetOSDisk{
    62  		query: func(ctx context.Context, resourceGroupName string, computeName string) (compute.VirtualMachine, error) {
    63  			actualResourceGroupName = resourceGroupName
    64  			actualComputeName = computeName
    65  
    66  			return createVirtualMachineFromUri("test.vhd"), nil
    67  		},
    68  		say:   func(message string) {},
    69  		error: func(e error) {},
    70  	}
    71  
    72  	stateBag := createTestStateBagStepGetOSDisk()
    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  	expectedOSDiskVhd, ok := stateBag.GetOk(constants.ArmOSDiskVhd)
    91  	if !ok {
    92  		t.Fatalf("Expected the state bag to have a value for '%s', but it did not.", constants.ArmOSDiskVhd)
    93  	}
    94  
    95  	if expectedOSDiskVhd != "test.vhd" {
    96  		t.Fatalf("Expected the value of stateBag[%s] to be 'test.vhd', but got '%s'.", constants.ArmOSDiskVhd, expectedOSDiskVhd)
    97  	}
    98  }
    99  
   100  func createTestStateBagStepGetOSDisk() multistep.StateBag {
   101  	stateBag := new(multistep.BasicStateBag)
   102  
   103  	stateBag.Put(constants.ArmComputeName, "Unit Test: ComputeName")
   104  	stateBag.Put(constants.ArmResourceGroupName, "Unit Test: ResourceGroupName")
   105  
   106  	return stateBag
   107  }
   108  
   109  func createVirtualMachineFromUri(vhdUri string) compute.VirtualMachine {
   110  	vm := compute.VirtualMachine{
   111  		VirtualMachineProperties: &compute.VirtualMachineProperties{
   112  			StorageProfile: &compute.StorageProfile{
   113  				OsDisk: &compute.OSDisk{
   114  					Vhd: &compute.VirtualHardDisk{
   115  						URI: &vhdUri,
   116  					},
   117  				},
   118  			},
   119  		},
   120  	}
   121  
   122  	return vm
   123  }