github.phpd.cn/hashicorp/packer@v1.3.2/builder/azure/arm/step_capture_image_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 "github.com/hashicorp/packer/builder/azure/common/constants" 10 "github.com/hashicorp/packer/helper/multistep" 11 ) 12 13 func TestStepCaptureImageShouldFailIfCaptureFails(t *testing.T) { 14 var testSubject = &StepCaptureImage{ 15 captureVhd: func(context.Context, string, string, *compute.VirtualMachineCaptureParameters) error { 16 return fmt.Errorf("!! Unit Test FAIL !!") 17 }, 18 generalizeVM: func(string, string) error { 19 return nil 20 }, 21 get: func(client *AzureClient) *CaptureTemplate { 22 return nil 23 }, 24 say: func(message string) {}, 25 error: func(e error) {}, 26 } 27 28 stateBag := createTestStateBagStepCaptureImage() 29 30 var result = testSubject.Run(context.Background(), stateBag) 31 if result != multistep.ActionHalt { 32 t.Fatalf("Expected the step to return 'ActionHalt', but got '%d'.", result) 33 } 34 35 if _, ok := stateBag.GetOk(constants.Error); ok == false { 36 t.Fatalf("Expected the step to set stateBag['%s'], but it was not.", constants.Error) 37 } 38 } 39 40 func TestStepCaptureImageShouldPassIfCapturePasses(t *testing.T) { 41 var testSubject = &StepCaptureImage{ 42 captureVhd: func(context.Context, string, string, *compute.VirtualMachineCaptureParameters) error { return nil }, 43 generalizeVM: func(string, string) error { 44 return nil 45 }, 46 get: func(client *AzureClient) *CaptureTemplate { 47 return nil 48 }, 49 say: func(message string) {}, 50 error: func(e error) {}, 51 } 52 53 stateBag := createTestStateBagStepCaptureImage() 54 55 var result = testSubject.Run(context.Background(), stateBag) 56 if result != multistep.ActionContinue { 57 t.Fatalf("Expected the step to return 'ActionContinue', but got '%d'.", result) 58 } 59 60 if _, ok := stateBag.GetOk(constants.Error); ok == true { 61 t.Fatalf("Expected the step to not set stateBag['%s'], but it was.", constants.Error) 62 } 63 } 64 65 func TestStepCaptureImageShouldTakeStepArgumentsFromStateBag(t *testing.T) { 66 cancelCh := make(chan<- struct{}) 67 defer close(cancelCh) 68 69 var actualResourceGroupName string 70 var actualComputeName string 71 var actualVirtualMachineCaptureParameters *compute.VirtualMachineCaptureParameters 72 actualCaptureTemplate := &CaptureTemplate{ 73 Schema: "!! Unit Test !!", 74 } 75 76 var testSubject = &StepCaptureImage{ 77 captureVhd: func(_ context.Context, resourceGroupName string, computeName string, parameters *compute.VirtualMachineCaptureParameters) error { 78 actualResourceGroupName = resourceGroupName 79 actualComputeName = computeName 80 actualVirtualMachineCaptureParameters = parameters 81 82 return nil 83 }, 84 generalizeVM: func(string, string) error { 85 return nil 86 }, 87 get: func(client *AzureClient) *CaptureTemplate { 88 return actualCaptureTemplate 89 }, 90 say: func(message string) {}, 91 error: func(e error) {}, 92 } 93 94 stateBag := createTestStateBagStepCaptureImage() 95 var result = testSubject.Run(context.Background(), stateBag) 96 97 if result != multistep.ActionContinue { 98 t.Fatalf("Expected the step to return 'ActionContinue', but got '%d'.", result) 99 } 100 101 var expectedComputeName = stateBag.Get(constants.ArmComputeName).(string) 102 var expectedResourceGroupName = stateBag.Get(constants.ArmResourceGroupName).(string) 103 var expectedVirtualMachineCaptureParameters = stateBag.Get(constants.ArmVirtualMachineCaptureParameters).(*compute.VirtualMachineCaptureParameters) 104 var expectedCaptureTemplate = stateBag.Get(constants.ArmCaptureTemplate).(*CaptureTemplate) 105 106 if actualComputeName != expectedComputeName { 107 t.Fatal("Expected StepCaptureImage to source 'constants.ArmComputeName' from the state bag, but it did not.") 108 } 109 110 if actualResourceGroupName != expectedResourceGroupName { 111 t.Fatal("Expected StepCaptureImage to source 'constants.ArmResourceGroupName' from the state bag, but it did not.") 112 } 113 114 if actualVirtualMachineCaptureParameters != expectedVirtualMachineCaptureParameters { 115 t.Fatal("Expected StepCaptureImage to source 'constants.ArmVirtualMachineCaptureParameters' from the state bag, but it did not.") 116 } 117 118 if actualCaptureTemplate != expectedCaptureTemplate { 119 t.Fatal("Expected StepCaptureImage to source 'constants.ArmCaptureTemplate' from the state bag, but it did not.") 120 } 121 } 122 123 func createTestStateBagStepCaptureImage() multistep.StateBag { 124 stateBag := new(multistep.BasicStateBag) 125 126 stateBag.Put(constants.ArmLocation, "localhost") 127 stateBag.Put(constants.ArmComputeName, "Unit Test: ComputeName") 128 stateBag.Put(constants.ArmResourceGroupName, "Unit Test: ResourceGroupName") 129 stateBag.Put(constants.ArmVirtualMachineCaptureParameters, &compute.VirtualMachineCaptureParameters{}) 130 131 stateBag.Put(constants.ArmIsManagedImage, false) 132 stateBag.Put(constants.ArmManagedImageResourceGroupName, "") 133 stateBag.Put(constants.ArmManagedImageName, "") 134 stateBag.Put(constants.ArmManagedImageLocation, "") 135 stateBag.Put(constants.ArmImageParameters, &compute.Image{}) 136 137 return stateBag 138 }