github.com/mitchellh/packer@v1.3.2/builder/azure/arm/step_deploy_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 TestStepDeployTemplateShouldFailIfDeployFails(t *testing.T) {
    13  	var testSubject = &StepDeployTemplate{
    14  		deploy: func(context.Context, string, string) error {
    15  			return fmt.Errorf("!! Unit Test FAIL !!")
    16  		},
    17  		say:   func(message string) {},
    18  		error: func(e error) {},
    19  	}
    20  
    21  	stateBag := createTestStateBagStepDeployTemplate()
    22  
    23  	var result = testSubject.Run(context.Background(), stateBag)
    24  	if result != multistep.ActionHalt {
    25  		t.Fatalf("Expected the step to return 'ActionHalt', but got '%d'.", result)
    26  	}
    27  
    28  	if _, ok := stateBag.GetOk(constants.Error); ok == false {
    29  		t.Fatalf("Expected the step to set stateBag['%s'], but it was not.", constants.Error)
    30  	}
    31  }
    32  
    33  func TestStepDeployTemplateShouldPassIfDeployPasses(t *testing.T) {
    34  	var testSubject = &StepDeployTemplate{
    35  		deploy: func(context.Context, string, string) error { return nil },
    36  		say:    func(message string) {},
    37  		error:  func(e error) {},
    38  	}
    39  
    40  	stateBag := createTestStateBagStepDeployTemplate()
    41  
    42  	var result = testSubject.Run(context.Background(), stateBag)
    43  	if result != multistep.ActionContinue {
    44  		t.Fatalf("Expected the step to return 'ActionContinue', but got '%d'.", result)
    45  	}
    46  
    47  	if _, ok := stateBag.GetOk(constants.Error); ok == true {
    48  		t.Fatalf("Expected the step to not set stateBag['%s'], but it was.", constants.Error)
    49  	}
    50  }
    51  
    52  func TestStepDeployTemplateShouldTakeStepArgumentsFromStateBag(t *testing.T) {
    53  	var actualResourceGroupName string
    54  	var actualDeploymentName string
    55  
    56  	var testSubject = &StepDeployTemplate{
    57  		deploy: func(_ context.Context, resourceGroupName string, deploymentName string) error {
    58  			actualResourceGroupName = resourceGroupName
    59  			actualDeploymentName = deploymentName
    60  
    61  			return nil
    62  		},
    63  		say:   func(message string) {},
    64  		error: func(e error) {},
    65  		name:  "--deployment-name--",
    66  	}
    67  
    68  	stateBag := createTestStateBagStepValidateTemplate()
    69  	var result = testSubject.Run(context.Background(), stateBag)
    70  
    71  	if result != multistep.ActionContinue {
    72  		t.Fatalf("Expected the step to return 'ActionContinue', but got '%d'.", result)
    73  	}
    74  
    75  	var expectedResourceGroupName = stateBag.Get(constants.ArmResourceGroupName).(string)
    76  
    77  	if actualDeploymentName != "--deployment-name--" {
    78  		t.Fatal("Expected StepValidateTemplate to source 'constants.ArmDeploymentName' from the state bag, but it did not.")
    79  	}
    80  
    81  	if actualResourceGroupName != expectedResourceGroupName {
    82  		t.Fatal("Expected the step to source 'constants.ArmResourceGroupName' from the state bag, but it did not.")
    83  	}
    84  }
    85  
    86  func TestStepDeployTemplateDeleteImageShouldFailWhenImageUrlCannotBeParsed(t *testing.T) {
    87  	var testSubject = &StepDeployTemplate{
    88  		say:   func(message string) {},
    89  		error: func(e error) {},
    90  		name:  "--deployment-name--",
    91  	}
    92  	// Invalid URL per https://golang.org/src/net/url/url_test.go
    93  	err := testSubject.deleteImage(context.TODO(), "image", "http://[fe80::1%en0]/", "Unit Test: ResourceGroupName")
    94  	if err == nil {
    95  		t.Fatal("Expected a failure because of the failed image name")
    96  	}
    97  }
    98  
    99  func TestStepDeployTemplateDeleteImageShouldFailWithInvalidImage(t *testing.T) {
   100  	var testSubject = &StepDeployTemplate{
   101  		say:   func(message string) {},
   102  		error: func(e error) {},
   103  		name:  "--deployment-name--",
   104  	}
   105  	err := testSubject.deleteImage(context.TODO(), "image", "storage.blob.core.windows.net/abc", "Unit Test: ResourceGroupName")
   106  	if err == nil {
   107  		t.Fatal("Expected a failure because of the failed image name")
   108  	}
   109  }
   110  
   111  func createTestStateBagStepDeployTemplate() multistep.StateBag {
   112  	stateBag := new(multistep.BasicStateBag)
   113  
   114  	stateBag.Put(constants.ArmDeploymentName, "Unit Test: DeploymentName")
   115  	stateBag.Put(constants.ArmResourceGroupName, "Unit Test: ResourceGroupName")
   116  
   117  	return stateBag
   118  }