github.com/mmcquillan/packer@v1.1.1-0.20171009221028-c85cf0483a5d/builder/azure/arm/step_delete_os_disk_test.go (about)

     1  package arm
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/packer/builder/azure/common/constants"
     8  	"github.com/mitchellh/multistep"
     9  )
    10  
    11  func TestStepDeleteOSDiskShouldFailIfGetFails(t *testing.T) {
    12  	var testSubject = &StepDeleteOSDisk{
    13  		delete: func(string, string) error { return fmt.Errorf("!! Unit Test FAIL !!") },
    14  		say:    func(message string) {},
    15  		error:  func(e error) {},
    16  	}
    17  
    18  	stateBag := DeleteTestStateBagStepDeleteOSDisk("http://storage.blob.core.windows.net/images/pkrvm_os.vhd")
    19  
    20  	var result = testSubject.Run(stateBag)
    21  	if result != multistep.ActionHalt {
    22  		t.Fatalf("Expected the step to return 'ActionHalt', but got '%d'.", result)
    23  	}
    24  
    25  	if _, ok := stateBag.GetOk(constants.Error); ok == false {
    26  		t.Fatalf("Expected the step to set stateBag['%s'], but it was not.", constants.Error)
    27  	}
    28  }
    29  
    30  func TestStepDeleteOSDiskShouldPassIfGetPasses(t *testing.T) {
    31  	var testSubject = &StepDeleteOSDisk{
    32  		delete: func(string, string) error { return nil },
    33  		say:    func(message string) {},
    34  		error:  func(e error) {},
    35  	}
    36  
    37  	stateBag := DeleteTestStateBagStepDeleteOSDisk("http://storage.blob.core.windows.net/images/pkrvm_os.vhd")
    38  
    39  	var result = testSubject.Run(stateBag)
    40  	if result != multistep.ActionContinue {
    41  		t.Fatalf("Expected the step to return 'ActionContinue', but got '%d'.", result)
    42  	}
    43  
    44  	if _, ok := stateBag.GetOk(constants.Error); ok == true {
    45  		t.Fatalf("Expected the step to not set stateBag['%s'], but it was.", constants.Error)
    46  	}
    47  }
    48  
    49  func TestStepDeleteOSDiskShouldTakeStepArgumentsFromStateBag(t *testing.T) {
    50  	var actualStorageContainerName string
    51  	var actualBlobName string
    52  
    53  	var testSubject = &StepDeleteOSDisk{
    54  		delete: func(storageContainerName string, blobName string) error {
    55  			actualStorageContainerName = storageContainerName
    56  			actualBlobName = blobName
    57  			return nil
    58  		},
    59  		say:   func(message string) {},
    60  		error: func(e error) {},
    61  	}
    62  
    63  	stateBag := DeleteTestStateBagStepDeleteOSDisk("http://storage.blob.core.windows.net/images/pkrvm_os.vhd")
    64  	var result = testSubject.Run(stateBag)
    65  
    66  	if result != multistep.ActionContinue {
    67  		t.Fatalf("Expected the step to return 'ActionContinue', but got '%d'.", result)
    68  	}
    69  
    70  	if actualStorageContainerName != "images" {
    71  		t.Fatalf("Expected the storage container name to be 'images', but found '%s'.", actualStorageContainerName)
    72  	}
    73  
    74  	if actualBlobName != "pkrvm_os.vhd" {
    75  		t.Fatalf("Expected the blob name to be 'pkrvm_os.vhd', but found '%s'.", actualBlobName)
    76  	}
    77  }
    78  
    79  func TestStepDeleteOSDiskShouldHandleComplexStorageContainerNames(t *testing.T) {
    80  	var actualStorageContainerName string
    81  	var actualBlobName string
    82  
    83  	var testSubject = &StepDeleteOSDisk{
    84  		delete: func(storageContainerName string, blobName string) error {
    85  			actualStorageContainerName = storageContainerName
    86  			actualBlobName = blobName
    87  			return nil
    88  		},
    89  		say:   func(message string) {},
    90  		error: func(e error) {},
    91  	}
    92  
    93  	stateBag := DeleteTestStateBagStepDeleteOSDisk("http://storage.blob.core.windows.net/abc/def/pkrvm_os.vhd")
    94  	testSubject.Run(stateBag)
    95  
    96  	if actualStorageContainerName != "abc" {
    97  		t.Fatalf("Expected the storage container name to be 'abc/def', but found '%s'.", actualStorageContainerName)
    98  	}
    99  
   100  	if actualBlobName != "def/pkrvm_os.vhd" {
   101  		t.Fatalf("Expected the blob name to be 'pkrvm_os.vhd', but found '%s'.", actualBlobName)
   102  	}
   103  }
   104  
   105  func DeleteTestStateBagStepDeleteOSDisk(osDiskVhd string) multistep.StateBag {
   106  	stateBag := new(multistep.BasicStateBag)
   107  	stateBag.Put(constants.ArmOSDiskVhd, osDiskVhd)
   108  	stateBag.Put(constants.ArmIsManagedImage, false)
   109  
   110  	return stateBag
   111  }