github.com/mitchellh/packer@v1.3.2/builder/azure/arm/step_delete_os_disk_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 TestStepDeleteOSDiskShouldFailIfGetFails(t *testing.T) {
    14  	var testSubject = &StepDeleteOSDisk{
    15  		delete:        func(string, string) error { return fmt.Errorf("!! Unit Test FAIL !!") },
    16  		deleteManaged: func(context.Context, string, string) error { return nil },
    17  		say:           func(message string) {},
    18  		error:         func(e error) {},
    19  	}
    20  
    21  	stateBag := DeleteTestStateBagStepDeleteOSDisk("http://storage.blob.core.windows.net/images/pkrvm_os.vhd")
    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 TestStepDeleteOSDiskShouldPassIfGetPasses(t *testing.T) {
    34  	var testSubject = &StepDeleteOSDisk{
    35  		delete: func(string, string) error { return nil },
    36  		say:    func(message string) {},
    37  		error:  func(e error) {},
    38  	}
    39  
    40  	stateBag := DeleteTestStateBagStepDeleteOSDisk("http://storage.blob.core.windows.net/images/pkrvm_os.vhd")
    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 TestStepDeleteOSDiskShouldTakeStepArgumentsFromStateBag(t *testing.T) {
    53  	var actualStorageContainerName string
    54  	var actualBlobName string
    55  
    56  	var testSubject = &StepDeleteOSDisk{
    57  		delete: func(storageContainerName string, blobName string) error {
    58  			actualStorageContainerName = storageContainerName
    59  			actualBlobName = blobName
    60  			return nil
    61  		},
    62  		say:   func(message string) {},
    63  		error: func(e error) {},
    64  	}
    65  
    66  	stateBag := DeleteTestStateBagStepDeleteOSDisk("http://storage.blob.core.windows.net/images/pkrvm_os.vhd")
    67  	var result = testSubject.Run(context.Background(), stateBag)
    68  
    69  	if result != multistep.ActionContinue {
    70  		t.Fatalf("Expected the step to return 'ActionContinue', but got '%d'.", result)
    71  	}
    72  
    73  	if actualStorageContainerName != "images" {
    74  		t.Fatalf("Expected the storage container name to be 'images', but found '%s'.", actualStorageContainerName)
    75  	}
    76  
    77  	if actualBlobName != "pkrvm_os.vhd" {
    78  		t.Fatalf("Expected the blob name to be 'pkrvm_os.vhd', but found '%s'.", actualBlobName)
    79  	}
    80  }
    81  
    82  func TestStepDeleteOSDiskShouldHandleComplexStorageContainerNames(t *testing.T) {
    83  	var actualStorageContainerName string
    84  	var actualBlobName string
    85  
    86  	var testSubject = &StepDeleteOSDisk{
    87  		delete: func(storageContainerName string, blobName string) error {
    88  			actualStorageContainerName = storageContainerName
    89  			actualBlobName = blobName
    90  			return nil
    91  		},
    92  		say:   func(message string) {},
    93  		error: func(e error) {},
    94  	}
    95  
    96  	stateBag := DeleteTestStateBagStepDeleteOSDisk("http://storage.blob.core.windows.net/abc/def/pkrvm_os.vhd")
    97  	testSubject.Run(context.Background(), stateBag)
    98  
    99  	if actualStorageContainerName != "abc" {
   100  		t.Fatalf("Expected the storage container name to be 'abc/def', but found '%s'.", actualStorageContainerName)
   101  	}
   102  
   103  	if actualBlobName != "def/pkrvm_os.vhd" {
   104  		t.Fatalf("Expected the blob name to be 'pkrvm_os.vhd', but found '%s'.", actualBlobName)
   105  	}
   106  }
   107  
   108  func TestStepDeleteOSDiskShouldFailIfVHDNameCannotBeURLParsed(t *testing.T) {
   109  	var testSubject = &StepDeleteOSDisk{
   110  		delete:        func(string, string) error { return nil },
   111  		say:           func(message string) {},
   112  		error:         func(e error) {},
   113  		deleteManaged: func(context.Context, string, string) error { return nil },
   114  	}
   115  
   116  	// Invalid URL per https://golang.org/src/net/url/url_test.go
   117  	stateBag := DeleteTestStateBagStepDeleteOSDisk("http://[fe80::1%en0]/")
   118  
   119  	var result = testSubject.Run(context.Background(), stateBag)
   120  	if result != multistep.ActionHalt {
   121  		t.Fatalf("Expected the step to return 'ActionHalt', but got '%v'.", result)
   122  	}
   123  
   124  	if _, ok := stateBag.GetOk(constants.Error); ok == false {
   125  		t.Fatalf("Expected the step to not stateBag['%s'], but it was.", constants.Error)
   126  	}
   127  }
   128  func TestStepDeleteOSDiskShouldFailIfVHDNameIsTooShort(t *testing.T) {
   129  	var testSubject = &StepDeleteOSDisk{
   130  		delete:        func(string, string) error { return nil },
   131  		say:           func(message string) {},
   132  		error:         func(e error) {},
   133  		deleteManaged: func(context.Context, string, string) error { return nil },
   134  	}
   135  
   136  	stateBag := DeleteTestStateBagStepDeleteOSDisk("storage.blob.core.windows.net/abc")
   137  
   138  	var result = testSubject.Run(context.Background(), stateBag)
   139  	if result != multistep.ActionHalt {
   140  		t.Fatalf("Expected the step to return 'ActionHalt', but got '%d'.", result)
   141  	}
   142  
   143  	if _, ok := stateBag.GetOk(constants.Error); ok == false {
   144  		t.Fatalf("Expected the step to not stateBag['%s'], but it was.", constants.Error)
   145  	}
   146  }
   147  
   148  func TestStepDeleteOSDiskShouldPassIfManagedDiskInTempResourceGroup(t *testing.T) {
   149  	var testSubject = &StepDeleteOSDisk{
   150  		delete: func(string, string) error { return nil },
   151  		say:    func(message string) {},
   152  		error:  func(e error) {},
   153  	}
   154  
   155  	stateBag := new(multistep.BasicStateBag)
   156  	stateBag.Put(constants.ArmOSDiskVhd, "subscriptions/123-456-789/resourceGroups/existingresourcegroup/providers/Microsoft.Compute/disks/osdisk")
   157  	stateBag.Put(constants.ArmIsManagedImage, true)
   158  	stateBag.Put(constants.ArmIsExistingResourceGroup, false)
   159  	stateBag.Put(constants.ArmResourceGroupName, "testgroup")
   160  
   161  	var result = testSubject.Run(context.Background(), stateBag)
   162  	if result != multistep.ActionContinue {
   163  		t.Fatalf("Expected the step to return 'ActionContinue', but got '%d'.", result)
   164  	}
   165  
   166  	if _, ok := stateBag.GetOk(constants.Error); ok == true {
   167  		t.Fatalf("Expected the step to not set stateBag['%s'], but it was.", constants.Error)
   168  	}
   169  }
   170  
   171  func TestStepDeleteOSDiskShouldFailIfManagedDiskInExistingResourceGroupFailsToDelete(t *testing.T) {
   172  	var testSubject = &StepDeleteOSDisk{
   173  		delete:        func(string, string) error { return nil },
   174  		say:           func(message string) {},
   175  		error:         func(e error) {},
   176  		deleteManaged: func(context.Context, string, string) error { return errors.New("UNIT TEST FAIL!") },
   177  	}
   178  
   179  	stateBag := new(multistep.BasicStateBag)
   180  	stateBag.Put(constants.ArmOSDiskVhd, "subscriptions/123-456-789/resourceGroups/existingresourcegroup/providers/Microsoft.Compute/disks/osdisk")
   181  	stateBag.Put(constants.ArmIsManagedImage, true)
   182  	stateBag.Put(constants.ArmIsExistingResourceGroup, true)
   183  	stateBag.Put(constants.ArmResourceGroupName, "testgroup")
   184  
   185  	var result = testSubject.Run(context.Background(), stateBag)
   186  	if result != multistep.ActionHalt {
   187  		t.Fatalf("Expected the step to return 'ActionHalt', but got '%d'.", result)
   188  	}
   189  
   190  	if _, ok := stateBag.GetOk(constants.Error); ok == false {
   191  		t.Fatalf("Expected the step to not stateBag['%s'], but it was.", constants.Error)
   192  	}
   193  }
   194  
   195  func TestStepDeleteOSDiskShouldFailIfManagedDiskInExistingResourceGroupIsDeleted(t *testing.T) {
   196  	var testSubject = &StepDeleteOSDisk{
   197  		delete:        func(string, string) error { return nil },
   198  		say:           func(message string) {},
   199  		error:         func(e error) {},
   200  		deleteManaged: func(context.Context, string, string) error { return nil },
   201  	}
   202  
   203  	stateBag := new(multistep.BasicStateBag)
   204  	stateBag.Put(constants.ArmOSDiskVhd, "subscriptions/123-456-789/resourceGroups/existingresourcegroup/providers/Microsoft.Compute/disks/osdisk")
   205  	stateBag.Put(constants.ArmIsManagedImage, true)
   206  	stateBag.Put(constants.ArmIsExistingResourceGroup, true)
   207  	stateBag.Put(constants.ArmResourceGroupName, "testgroup")
   208  
   209  	var result = testSubject.Run(context.Background(), stateBag)
   210  	if result != multistep.ActionContinue {
   211  		t.Fatalf("Expected the step to return 'ActionContinue', but got '%d'.", result)
   212  	}
   213  
   214  	if _, ok := stateBag.GetOk(constants.Error); ok == true {
   215  		t.Fatalf("Expected the step to not set stateBag['%s'], but it was.", constants.Error)
   216  	}
   217  }
   218  
   219  func DeleteTestStateBagStepDeleteOSDisk(osDiskVhd string) multistep.StateBag {
   220  	stateBag := new(multistep.BasicStateBag)
   221  	stateBag.Put(constants.ArmOSDiskVhd, osDiskVhd)
   222  	stateBag.Put(constants.ArmIsManagedImage, false)
   223  	stateBag.Put(constants.ArmIsExistingResourceGroup, false)
   224  	stateBag.Put(constants.ArmResourceGroupName, "testgroup")
   225  
   226  	return stateBag
   227  }