github.com/kikitux/packer@v0.10.1-0.20160322154024-6237df566f9f/builder/azure/arm/step_validate_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 TestStepValidateTemplateShouldFailIfValidateFails(t *testing.T) {
    15  
    16  	var testSubject = &StepValidateTemplate{
    17  		validate: func(string, string, *TemplateParameters) error { return fmt.Errorf("!! Unit Test FAIL !!") },
    18  		say:      func(message string) {},
    19  		error:    func(e error) {},
    20  	}
    21  
    22  	stateBag := createTestStateBagStepValidateTemplate()
    23  
    24  	var result = testSubject.Run(stateBag)
    25  	if result != multistep.ActionHalt {
    26  		t.Fatalf("Expected the step to return 'ActionHalt', but got '%d'.", result)
    27  	}
    28  
    29  	if _, ok := stateBag.GetOk(constants.Error); ok == false {
    30  		t.Fatalf("Expected the step to set stateBag['%s'], but it was not.", constants.Error)
    31  	}
    32  }
    33  
    34  func TestStepValidateTemplateShouldPassIfValidatePasses(t *testing.T) {
    35  	var testSubject = &StepValidateTemplate{
    36  		validate: func(string, string, *TemplateParameters) error { return nil },
    37  		say:      func(message string) {},
    38  		error:    func(e error) {},
    39  	}
    40  
    41  	stateBag := createTestStateBagStepValidateTemplate()
    42  
    43  	var result = testSubject.Run(stateBag)
    44  	if result != multistep.ActionContinue {
    45  		t.Fatalf("Expected the step to return 'ActionContinue', but got '%d'.", result)
    46  	}
    47  
    48  	if _, ok := stateBag.GetOk(constants.Error); ok == true {
    49  		t.Fatalf("Expected the step to not set stateBag['%s'], but it was.", constants.Error)
    50  	}
    51  }
    52  
    53  func TestStepValidateTemplateShouldTakeStepArgumentsFromStateBag(t *testing.T) {
    54  	var actualResourceGroupName string
    55  	var actualDeploymentName string
    56  	var actualTemplateParameters *TemplateParameters
    57  
    58  	var testSubject = &StepValidateTemplate{
    59  		validate: func(resourceGroupName string, deploymentName string, templateParameter *TemplateParameters) error {
    60  			actualResourceGroupName = resourceGroupName
    61  			actualDeploymentName = deploymentName
    62  			actualTemplateParameters = templateParameter
    63  
    64  			return nil
    65  		},
    66  		say:   func(message string) {},
    67  		error: func(e error) {},
    68  	}
    69  
    70  	stateBag := createTestStateBagStepValidateTemplate()
    71  	var result = testSubject.Run(stateBag)
    72  
    73  	if result != multistep.ActionContinue {
    74  		t.Fatalf("Expected the step to return 'ActionContinue', but got '%d'.", result)
    75  	}
    76  
    77  	var expectedDeploymentName = stateBag.Get(constants.ArmDeploymentName).(string)
    78  	var expectedResourceGroupName = stateBag.Get(constants.ArmResourceGroupName).(string)
    79  	var expectedTemplateParameters = stateBag.Get(constants.ArmTemplateParameters).(*TemplateParameters)
    80  
    81  	if actualDeploymentName != expectedDeploymentName {
    82  		t.Fatalf("Expected the step to source 'constants.ArmDeploymentName' from the state bag, but it did not.")
    83  	}
    84  
    85  	if actualResourceGroupName != expectedResourceGroupName {
    86  		t.Fatalf("Expected the step to source 'constants.ArmResourceGroupName' from the state bag, but it did not.")
    87  	}
    88  
    89  	if actualTemplateParameters != expectedTemplateParameters {
    90  		t.Fatalf("Expected the step to source 'constants.ArmTemplateParameters' from the state bag, but it did not.")
    91  	}
    92  }
    93  
    94  func createTestStateBagStepValidateTemplate() multistep.StateBag {
    95  	stateBag := new(multistep.BasicStateBag)
    96  
    97  	stateBag.Put(constants.ArmDeploymentName, "Unit Test: DeploymentName")
    98  	stateBag.Put(constants.ArmResourceGroupName, "Unit Test: ResourceGroupName")
    99  	stateBag.Put(constants.ArmTemplateParameters, &TemplateParameters{})
   100  
   101  	return stateBag
   102  }