github.com/kikitux/packer@v0.10.1-0.20160322154024-6237df566f9f/builder/azure/arm/step_capture_image_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 "fmt" 8 "testing" 9 10 "github.com/Azure/azure-sdk-for-go/arm/compute" 11 "github.com/mitchellh/multistep" 12 "github.com/mitchellh/packer/builder/azure/common/constants" 13 ) 14 15 func TestStepCaptureImageShouldFailIfCaptureFails(t *testing.T) { 16 17 var testSubject = &StepCaptureImage{ 18 capture: func(string, string, *compute.VirtualMachineCaptureParameters) error { 19 return fmt.Errorf("!! Unit Test FAIL !!") 20 }, 21 say: func(message string) {}, 22 error: func(e error) {}, 23 } 24 25 stateBag := createTestStateBagStepCaptureImage() 26 27 var result = testSubject.Run(stateBag) 28 if result != multistep.ActionHalt { 29 t.Fatalf("Expected the step to return 'ActionHalt', but got '%d'.", result) 30 } 31 32 if _, ok := stateBag.GetOk(constants.Error); ok == false { 33 t.Fatalf("Expected the step to set stateBag['%s'], but it was not.", constants.Error) 34 } 35 } 36 37 func TestStepCaptureImageShouldPassIfCapturePasses(t *testing.T) { 38 var testSubject = &StepCaptureImage{ 39 capture: func(string, string, *compute.VirtualMachineCaptureParameters) error { return nil }, 40 say: func(message string) {}, 41 error: func(e error) {}, 42 } 43 44 stateBag := createTestStateBagStepCaptureImage() 45 46 var result = testSubject.Run(stateBag) 47 if result != multistep.ActionContinue { 48 t.Fatalf("Expected the step to return 'ActionContinue', but got '%d'.", result) 49 } 50 51 if _, ok := stateBag.GetOk(constants.Error); ok == true { 52 t.Fatalf("Expected the step to not set stateBag['%s'], but it was.", constants.Error) 53 } 54 } 55 56 func TestStepCaptureImageShouldTakeStepArgumentsFromStateBag(t *testing.T) { 57 var actualResourceGroupName string 58 var actualComputeName string 59 var actualVirtualMachineCaptureParameters *compute.VirtualMachineCaptureParameters 60 61 var testSubject = &StepCaptureImage{ 62 capture: func(resourceGroupName string, computeName string, parameters *compute.VirtualMachineCaptureParameters) error { 63 actualResourceGroupName = resourceGroupName 64 actualComputeName = computeName 65 actualVirtualMachineCaptureParameters = parameters 66 67 return nil 68 }, 69 say: func(message string) {}, 70 error: func(e error) {}, 71 } 72 73 stateBag := createTestStateBagStepCaptureImage() 74 var result = testSubject.Run(stateBag) 75 76 if result != multistep.ActionContinue { 77 t.Fatalf("Expected the step to return 'ActionContinue', but got '%d'.", result) 78 } 79 80 var expectedComputeName = stateBag.Get(constants.ArmComputeName).(string) 81 var expectedResourceGroupName = stateBag.Get(constants.ArmResourceGroupName).(string) 82 var expectedVirtualMachineCaptureParameters = stateBag.Get(constants.ArmVirtualMachineCaptureParameters).(*compute.VirtualMachineCaptureParameters) 83 84 if actualComputeName != expectedComputeName { 85 t.Fatalf("Expected StepCaptureImage to source 'constants.ArmComputeName' from the state bag, but it did not.") 86 } 87 88 if actualResourceGroupName != expectedResourceGroupName { 89 t.Fatalf("Expected StepCaptureImage to source 'constants.ArmResourceGroupName' from the state bag, but it did not.") 90 } 91 92 if actualVirtualMachineCaptureParameters != expectedVirtualMachineCaptureParameters { 93 t.Fatalf("Expected StepCaptureImage to source 'constants.ArmVirtualMachineCaptureParameters' from the state bag, but it did not.") 94 } 95 } 96 97 func createTestStateBagStepCaptureImage() multistep.StateBag { 98 stateBag := new(multistep.BasicStateBag) 99 100 stateBag.Put(constants.ArmComputeName, "Unit Test: ComputeName") 101 stateBag.Put(constants.ArmResourceGroupName, "Unit Test: ResourceGroupName") 102 stateBag.Put(constants.ArmVirtualMachineCaptureParameters, &compute.VirtualMachineCaptureParameters{}) 103 104 return stateBag 105 }