github.phpd.cn/hashicorp/packer@v1.3.2/builder/azure/arm/step_validate_template_test.go (about)

     1  package arm
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/packer/builder/azure/common/constants"
     9  	"github.com/hashicorp/packer/helper/multistep"
    10  )
    11  
    12  func TestStepValidateTemplateShouldFailIfValidateFails(t *testing.T) {
    13  	var testSubject = &StepValidateTemplate{
    14  		validate: func(context.Context, string, string) error { return fmt.Errorf("!! Unit Test FAIL !!") },
    15  		say:      func(message string) {},
    16  		error:    func(e error) {},
    17  	}
    18  
    19  	stateBag := createTestStateBagStepValidateTemplate()
    20  
    21  	var result = testSubject.Run(context.Background(), stateBag)
    22  	if result != multistep.ActionHalt {
    23  		t.Fatalf("Expected the step to return 'ActionHalt', but got '%d'.", result)
    24  	}
    25  
    26  	if _, ok := stateBag.GetOk(constants.Error); ok == false {
    27  		t.Fatalf("Expected the step to set stateBag['%s'], but it was not.", constants.Error)
    28  	}
    29  }
    30  
    31  func TestStepValidateTemplateShouldPassIfValidatePasses(t *testing.T) {
    32  	var testSubject = &StepValidateTemplate{
    33  		validate: func(context.Context, string, string) error { return nil },
    34  		say:      func(message string) {},
    35  		error:    func(e error) {},
    36  	}
    37  
    38  	stateBag := createTestStateBagStepValidateTemplate()
    39  
    40  	var result = testSubject.Run(context.Background(), stateBag)
    41  	if result != multistep.ActionContinue {
    42  		t.Fatalf("Expected the step to return 'ActionContinue', but got '%d'.", result)
    43  	}
    44  
    45  	if _, ok := stateBag.GetOk(constants.Error); ok == true {
    46  		t.Fatalf("Expected the step to not set stateBag['%s'], but it was.", constants.Error)
    47  	}
    48  }
    49  
    50  func TestStepValidateTemplateShouldTakeStepArgumentsFromStateBag(t *testing.T) {
    51  	var actualResourceGroupName string
    52  	var actualDeploymentName string
    53  
    54  	var testSubject = &StepValidateTemplate{
    55  		validate: func(ctx context.Context, resourceGroupName string, deploymentName string) error {
    56  			actualResourceGroupName = resourceGroupName
    57  			actualDeploymentName = deploymentName
    58  
    59  			return nil
    60  		},
    61  		say:   func(message string) {},
    62  		error: func(e error) {},
    63  	}
    64  
    65  	stateBag := createTestStateBagStepValidateTemplate()
    66  	var result = testSubject.Run(context.Background(), stateBag)
    67  
    68  	if result != multistep.ActionContinue {
    69  		t.Fatalf("Expected the step to return 'ActionContinue', but got '%d'.", result)
    70  	}
    71  
    72  	var expectedDeploymentName = stateBag.Get(constants.ArmDeploymentName).(string)
    73  	var expectedResourceGroupName = stateBag.Get(constants.ArmResourceGroupName).(string)
    74  
    75  	if actualDeploymentName != expectedDeploymentName {
    76  		t.Fatal("Expected the step to source 'constants.ArmDeploymentName' from the state bag, but it did not.")
    77  	}
    78  
    79  	if actualResourceGroupName != expectedResourceGroupName {
    80  		t.Fatal("Expected the step to source 'constants.ArmResourceGroupName' from the state bag, but it did not.")
    81  	}
    82  }
    83  
    84  func createTestStateBagStepValidateTemplate() multistep.StateBag {
    85  	stateBag := new(multistep.BasicStateBag)
    86  
    87  	stateBag.Put(constants.ArmDeploymentName, "Unit Test: DeploymentName")
    88  	stateBag.Put(constants.ArmResourceGroupName, "Unit Test: ResourceGroupName")
    89  
    90  	return stateBag
    91  }