github.com/rahart/packer@v0.12.2-0.20161229105310-282bb6ad370f/builder/azure/arm/step_delete_resource_group_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 TestStepDeleteResourceGroupShouldFailIfDeleteFails(t *testing.T) {
    15  	var testSubject = &StepDeleteResourceGroup{
    16  		delete: func(string, <-chan struct{}) error { return fmt.Errorf("!! Unit Test FAIL !!") },
    17  		say:    func(message string) {},
    18  		error:  func(e error) {},
    19  	}
    20  
    21  	stateBag := DeleteTestStateBagStepDeleteResourceGroup()
    22  
    23  	var result = testSubject.Run(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 TestStepDeleteResourceGroupShouldPassIfDeletePasses(t *testing.T) {
    34  	var testSubject = &StepDeleteResourceGroup{
    35  		delete: func(string, <-chan struct{}) error { return nil },
    36  		say:    func(message string) {},
    37  		error:  func(e error) {},
    38  	}
    39  
    40  	stateBag := DeleteTestStateBagStepDeleteResourceGroup()
    41  
    42  	var result = testSubject.Run(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 TestStepDeleteResourceGroupShouldDeleteStateBagArmResourceGroupCreated(t *testing.T) {
    53  	var testSubject = &StepDeleteResourceGroup{
    54  		delete: func(resourceGroupName string, cancelCh <-chan struct{}) error {
    55  			return nil
    56  		},
    57  		say:   func(message string) {},
    58  		error: func(e error) {},
    59  	}
    60  
    61  	stateBag := DeleteTestStateBagStepDeleteResourceGroup()
    62  	testSubject.Run(stateBag)
    63  
    64  	value, ok := stateBag.GetOk(constants.ArmIsResourceGroupCreated)
    65  	if !ok {
    66  		t.Fatal("Expected the resource bag value arm.IsResourceGroupCreated to exist")
    67  	}
    68  
    69  	if value.(bool) {
    70  		t.Fatalf("Expected arm.IsResourceGroupCreated to be false, but got %q", value)
    71  	}
    72  }
    73  
    74  func DeleteTestStateBagStepDeleteResourceGroup() multistep.StateBag {
    75  	stateBag := new(multistep.BasicStateBag)
    76  	stateBag.Put(constants.ArmResourceGroupName, "Unit Test: ResourceGroupName")
    77  	stateBag.Put(constants.ArmIsResourceGroupCreated, "Unit Test: IsResourceGroupCreated")
    78  
    79  	return stateBag
    80  }