github.com/yoctocloud/packer@v0.6.2-0.20160520224004-e11a0a18423f/builder/azure/arm/step_deploy_template_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/mitchellh/multistep"
    11  	"github.com/mitchellh/packer/builder/azure/common/constants"
    12  )
    13  
    14  func TestStepDeployTemplateShouldFailIfDeployFails(t *testing.T) {
    15  	var testSubject = &StepDeployTemplate{
    16  		template: Linux,
    17  		deploy: func(string, string, *TemplateParameters, <-chan struct{}) error {
    18  			return fmt.Errorf("!! Unit Test FAIL !!")
    19  		},
    20  		say:   func(message string) {},
    21  		error: func(e error) {},
    22  	}
    23  
    24  	stateBag := createTestStateBagStepDeployTemplate()
    25  
    26  	var result = testSubject.Run(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 TestStepDeployTemplateShouldPassIfDeployPasses(t *testing.T) {
    37  	var testSubject = &StepDeployTemplate{
    38  		template: Linux,
    39  		deploy:   func(string, string, *TemplateParameters, <-chan struct{}) error { return nil },
    40  		say:      func(message string) {},
    41  		error:    func(e error) {},
    42  	}
    43  
    44  	stateBag := createTestStateBagStepDeployTemplate()
    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 TestStepDeployTemplateShouldTakeStepArgumentsFromStateBag(t *testing.T) {
    57  	var actualResourceGroupName string
    58  	var actualDeploymentName string
    59  	var actualTemplateParameters *TemplateParameters
    60  
    61  	var testSubject = &StepDeployTemplate{
    62  		template: Linux,
    63  		deploy: func(resourceGroupName string, deploymentName string, templateParameter *TemplateParameters, cancelCh <-chan struct{}) error {
    64  			actualResourceGroupName = resourceGroupName
    65  			actualDeploymentName = deploymentName
    66  			actualTemplateParameters = templateParameter
    67  
    68  			return nil
    69  		},
    70  		say:   func(message string) {},
    71  		error: func(e error) {},
    72  	}
    73  
    74  	stateBag := createTestStateBagStepValidateTemplate()
    75  	var result = testSubject.Run(stateBag)
    76  
    77  	if result != multistep.ActionContinue {
    78  		t.Fatalf("Expected the step to return 'ActionContinue', but got '%d'.", result)
    79  	}
    80  
    81  	var expectedDeploymentName = stateBag.Get(constants.ArmDeploymentName).(string)
    82  	var expectedResourceGroupName = stateBag.Get(constants.ArmResourceGroupName).(string)
    83  	var expectedTemplateParameters = stateBag.Get(constants.ArmTemplateParameters).(*TemplateParameters)
    84  
    85  	if actualDeploymentName != expectedDeploymentName {
    86  		t.Fatalf("Expected StepValidateTemplate to source 'constants.ArmDeploymentName' from the state bag, but it did not.")
    87  	}
    88  
    89  	if actualResourceGroupName != expectedResourceGroupName {
    90  		t.Fatalf("Expected the step to source 'constants.ArmResourceGroupName' from the state bag, but it did not.")
    91  	}
    92  
    93  	if actualTemplateParameters != expectedTemplateParameters {
    94  		t.Fatalf("Expected the step to source 'constants.ArmTemplateParameters' from the state bag, but it did not.")
    95  	}
    96  }
    97  
    98  func createTestStateBagStepDeployTemplate() multistep.StateBag {
    99  	stateBag := new(multistep.BasicStateBag)
   100  
   101  	stateBag.Put(constants.ArmDeploymentName, "Unit Test: DeploymentName")
   102  	stateBag.Put(constants.ArmResourceGroupName, "Unit Test: ResourceGroupName")
   103  	stateBag.Put(constants.ArmTemplateParameters, &TemplateParameters{})
   104  
   105  	return stateBag
   106  }