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