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