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

     1  package arm
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"fmt"
     7  	"testing"
     8  
     9  	"github.com/hashicorp/packer/builder/azure/common/constants"
    10  	"github.com/hashicorp/packer/helper/multistep"
    11  )
    12  
    13  func TestStepCreateResourceGroupShouldFailIfBothGroupNames(t *testing.T) {
    14  	stateBag := new(multistep.BasicStateBag)
    15  
    16  	stateBag.Put(constants.ArmDoubleResourceGroupNameSet, true)
    17  
    18  	value := "Unit Test: Tags"
    19  	tags := map[string]*string{
    20  		"tag01": &value,
    21  	}
    22  
    23  	stateBag.Put(constants.ArmTags, tags)
    24  	var testSubject = &StepCreateResourceGroup{
    25  		create: func(context.Context, string, string, map[string]*string) error { return nil },
    26  		say:    func(message string) {},
    27  		error:  func(e error) {},
    28  		exists: func(context.Context, string) (bool, error) { return false, nil },
    29  	}
    30  	var result = testSubject.Run(context.Background(), stateBag)
    31  	if result != multistep.ActionHalt {
    32  		t.Fatalf("Expected the step to return 'ActionHalt', but got '%d'.", result)
    33  	}
    34  
    35  	if _, ok := stateBag.GetOk(constants.Error); ok == false {
    36  		t.Fatalf("Expected the step to set stateBag['%s'], but it was not.", constants.Error)
    37  	}
    38  }
    39  
    40  func TestStepCreateResourceGroupShouldFailIfCreateFails(t *testing.T) {
    41  	var testSubject = &StepCreateResourceGroup{
    42  		create: func(context.Context, string, string, map[string]*string) error {
    43  			return fmt.Errorf("!! Unit Test FAIL !!")
    44  		},
    45  		say:    func(message string) {},
    46  		error:  func(e error) {},
    47  		exists: func(context.Context, string) (bool, error) { return false, nil },
    48  	}
    49  
    50  	stateBag := createTestStateBagStepCreateResourceGroup()
    51  
    52  	var result = testSubject.Run(context.Background(), stateBag)
    53  	if result != multistep.ActionHalt {
    54  		t.Fatalf("Expected the step to return 'ActionHalt', but got '%d'.", result)
    55  	}
    56  
    57  	if _, ok := stateBag.GetOk(constants.Error); ok == false {
    58  		t.Fatalf("Expected the step to set stateBag['%s'], but it was not.", constants.Error)
    59  	}
    60  }
    61  
    62  func TestStepCreateResourceGroupShouldFailIfExistsFails(t *testing.T) {
    63  	var testSubject = &StepCreateResourceGroup{
    64  		create: func(context.Context, string, string, map[string]*string) error { return nil },
    65  		say:    func(message string) {},
    66  		error:  func(e error) {},
    67  		exists: func(context.Context, string) (bool, error) { return false, errors.New("FAIL") },
    68  	}
    69  
    70  	stateBag := createTestStateBagStepCreateResourceGroup()
    71  
    72  	var result = testSubject.Run(context.Background(), stateBag)
    73  	if result != multistep.ActionHalt {
    74  		t.Fatalf("Expected the step to return 'ActionHalt', but got '%d'.", result)
    75  	}
    76  
    77  	if _, ok := stateBag.GetOk(constants.Error); ok == false {
    78  		t.Fatalf("Expected the step to set stateBag['%s'], but it was not.", constants.Error)
    79  	}
    80  }
    81  
    82  func TestStepCreateResourceGroupShouldPassIfCreatePasses(t *testing.T) {
    83  	var testSubject = &StepCreateResourceGroup{
    84  		create: func(context.Context, string, string, map[string]*string) error { return nil },
    85  		say:    func(message string) {},
    86  		error:  func(e error) {},
    87  		exists: func(context.Context, string) (bool, error) { return false, nil },
    88  	}
    89  
    90  	stateBag := createTestStateBagStepCreateResourceGroup()
    91  
    92  	var result = testSubject.Run(context.Background(), stateBag)
    93  	if result != multistep.ActionContinue {
    94  		t.Fatalf("Expected the step to return 'ActionContinue', but got '%d'.", result)
    95  	}
    96  
    97  	if _, ok := stateBag.GetOk(constants.Error); ok == true {
    98  		t.Fatalf("Expected the step to not set stateBag['%s'], but it was.", constants.Error)
    99  	}
   100  }
   101  
   102  func TestStepCreateResourceGroupShouldTakeStepArgumentsFromStateBag(t *testing.T) {
   103  	var actualResourceGroupName string
   104  	var actualLocation string
   105  	var actualTags map[string]*string
   106  
   107  	var testSubject = &StepCreateResourceGroup{
   108  		create: func(_ context.Context, resourceGroupName string, location string, tags map[string]*string) error {
   109  			actualResourceGroupName = resourceGroupName
   110  			actualLocation = location
   111  			actualTags = tags
   112  			return nil
   113  		},
   114  		say:    func(message string) {},
   115  		error:  func(e error) {},
   116  		exists: func(context.Context, string) (bool, error) { return false, nil },
   117  	}
   118  
   119  	stateBag := createTestStateBagStepCreateResourceGroup()
   120  	var result = testSubject.Run(context.Background(), stateBag)
   121  
   122  	if result != multistep.ActionContinue {
   123  		t.Fatalf("Expected the step to return 'ActionContinue', but got '%d'.", result)
   124  	}
   125  
   126  	var expectedResourceGroupName = stateBag.Get(constants.ArmResourceGroupName).(string)
   127  	var expectedLocation = stateBag.Get(constants.ArmLocation).(string)
   128  	var expectedTags = stateBag.Get(constants.ArmTags).(map[string]*string)
   129  
   130  	if actualResourceGroupName != expectedResourceGroupName {
   131  		t.Fatal("Expected the step to source 'constants.ArmResourceGroupName' from the state bag, but it did not.")
   132  	}
   133  
   134  	if actualLocation != expectedLocation {
   135  		t.Fatal("Expected the step to source 'constants.ArmResourceGroupName' from the state bag, but it did not.")
   136  	}
   137  
   138  	if len(expectedTags) != len(actualTags) && expectedTags["tag01"] != actualTags["tag01"] {
   139  		t.Fatal("Expected the step to source 'constants.ArmTags' from the state bag, but it did not.")
   140  	}
   141  
   142  	_, ok := stateBag.GetOk(constants.ArmIsResourceGroupCreated)
   143  	if !ok {
   144  		t.Fatal("Expected the step to add item to stateBag['constants.ArmIsResourceGroupCreated'], but it did not.")
   145  	}
   146  }
   147  
   148  func TestStepCreateResourceGroupMarkShouldFailIfTryingExistingButDoesntExist(t *testing.T) {
   149  	var testSubject = &StepCreateResourceGroup{
   150  		create: func(context.Context, string, string, map[string]*string) error {
   151  			return fmt.Errorf("!! Unit Test FAIL !!")
   152  		},
   153  		say:    func(message string) {},
   154  		error:  func(e error) {},
   155  		exists: func(context.Context, string) (bool, error) { return false, nil },
   156  	}
   157  
   158  	stateBag := createTestExistingStateBagStepCreateResourceGroup()
   159  
   160  	var result = testSubject.Run(context.Background(), stateBag)
   161  	if result != multistep.ActionHalt {
   162  		t.Fatalf("Expected the step to return 'ActionHalt', but got '%d'.", result)
   163  	}
   164  
   165  	if _, ok := stateBag.GetOk(constants.Error); ok == false {
   166  		t.Fatalf("Expected the step to set stateBag['%s'], but it was not.", constants.Error)
   167  	}
   168  }
   169  
   170  func TestStepCreateResourceGroupMarkShouldFailIfTryingTempButExist(t *testing.T) {
   171  	var testSubject = &StepCreateResourceGroup{
   172  		create: func(context.Context, string, string, map[string]*string) error {
   173  			return fmt.Errorf("!! Unit Test FAIL !!")
   174  		},
   175  		say:    func(message string) {},
   176  		error:  func(e error) {},
   177  		exists: func(context.Context, string) (bool, error) { return true, nil },
   178  	}
   179  
   180  	stateBag := createTestStateBagStepCreateResourceGroup()
   181  
   182  	var result = testSubject.Run(context.Background(), stateBag)
   183  	if result != multistep.ActionHalt {
   184  		t.Fatalf("Expected the step to return 'ActionHalt', but got '%d'.", result)
   185  	}
   186  
   187  	if _, ok := stateBag.GetOk(constants.Error); ok == false {
   188  		t.Fatalf("Expected the step to set stateBag['%s'], but it was not.", constants.Error)
   189  	}
   190  }
   191  
   192  func createTestStateBagStepCreateResourceGroup() multistep.StateBag {
   193  	stateBag := new(multistep.BasicStateBag)
   194  
   195  	stateBag.Put(constants.ArmLocation, "Unit Test: Location")
   196  	stateBag.Put(constants.ArmResourceGroupName, "Unit Test: ResourceGroupName")
   197  	stateBag.Put(constants.ArmIsExistingResourceGroup, false)
   198  
   199  	value := "Unit Test: Tags"
   200  	tags := map[string]*string{
   201  		"tag01": &value,
   202  	}
   203  
   204  	stateBag.Put(constants.ArmTags, tags)
   205  	return stateBag
   206  }
   207  
   208  func createTestExistingStateBagStepCreateResourceGroup() multistep.StateBag {
   209  	stateBag := new(multistep.BasicStateBag)
   210  
   211  	stateBag.Put(constants.ArmLocation, "Unit Test: Location")
   212  	stateBag.Put(constants.ArmResourceGroupName, "Unit Test: ResourceGroupName")
   213  	stateBag.Put(constants.ArmIsExistingResourceGroup, true)
   214  
   215  	value := "Unit Test: Tags"
   216  	tags := map[string]*string{
   217  		"tag01": &value,
   218  	}
   219  
   220  	stateBag.Put(constants.ArmTags, tags)
   221  	return stateBag
   222  }