2016-03-04 05:14:55 -05:00
|
|
|
package arm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/builder/azure/common/constants"
|
2016-04-21 19:50:03 -04:00
|
|
|
"github.com/mitchellh/multistep"
|
2016-03-04 05:14:55 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestStepDeleteOSDiskShouldFailIfGetFails(t *testing.T) {
|
|
|
|
var testSubject = &StepDeleteOSDisk{
|
2017-11-06 00:16:58 -05:00
|
|
|
delete: func(string, string) error { return fmt.Errorf("!! Unit Test FAIL !!") },
|
|
|
|
deleteManaged: func(string, string) error { return nil },
|
|
|
|
say: func(message string) {},
|
|
|
|
error: func(e error) {},
|
2016-03-04 05:14:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
stateBag := DeleteTestStateBagStepDeleteOSDisk("http://storage.blob.core.windows.net/images/pkrvm_os.vhd")
|
|
|
|
|
|
|
|
var result = testSubject.Run(stateBag)
|
|
|
|
if result != multistep.ActionHalt {
|
|
|
|
t.Fatalf("Expected the step to return 'ActionHalt', but got '%d'.", result)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := stateBag.GetOk(constants.Error); ok == false {
|
|
|
|
t.Fatalf("Expected the step to set stateBag['%s'], but it was not.", constants.Error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStepDeleteOSDiskShouldPassIfGetPasses(t *testing.T) {
|
|
|
|
var testSubject = &StepDeleteOSDisk{
|
|
|
|
delete: func(string, string) error { return nil },
|
|
|
|
say: func(message string) {},
|
|
|
|
error: func(e error) {},
|
|
|
|
}
|
|
|
|
|
|
|
|
stateBag := DeleteTestStateBagStepDeleteOSDisk("http://storage.blob.core.windows.net/images/pkrvm_os.vhd")
|
|
|
|
|
|
|
|
var result = testSubject.Run(stateBag)
|
|
|
|
if result != multistep.ActionContinue {
|
|
|
|
t.Fatalf("Expected the step to return 'ActionContinue', but got '%d'.", result)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := stateBag.GetOk(constants.Error); ok == true {
|
|
|
|
t.Fatalf("Expected the step to not set stateBag['%s'], but it was.", constants.Error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStepDeleteOSDiskShouldTakeStepArgumentsFromStateBag(t *testing.T) {
|
|
|
|
var actualStorageContainerName string
|
|
|
|
var actualBlobName string
|
|
|
|
|
|
|
|
var testSubject = &StepDeleteOSDisk{
|
|
|
|
delete: func(storageContainerName string, blobName string) error {
|
|
|
|
actualStorageContainerName = storageContainerName
|
|
|
|
actualBlobName = blobName
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
say: func(message string) {},
|
|
|
|
error: func(e error) {},
|
|
|
|
}
|
|
|
|
|
|
|
|
stateBag := DeleteTestStateBagStepDeleteOSDisk("http://storage.blob.core.windows.net/images/pkrvm_os.vhd")
|
|
|
|
var result = testSubject.Run(stateBag)
|
|
|
|
|
|
|
|
if result != multistep.ActionContinue {
|
|
|
|
t.Fatalf("Expected the step to return 'ActionContinue', but got '%d'.", result)
|
|
|
|
}
|
|
|
|
|
|
|
|
if actualStorageContainerName != "images" {
|
|
|
|
t.Fatalf("Expected the storage container name to be 'images', but found '%s'.", actualStorageContainerName)
|
|
|
|
}
|
|
|
|
|
|
|
|
if actualBlobName != "pkrvm_os.vhd" {
|
|
|
|
t.Fatalf("Expected the blob name to be 'pkrvm_os.vhd', but found '%s'.", actualBlobName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStepDeleteOSDiskShouldHandleComplexStorageContainerNames(t *testing.T) {
|
|
|
|
var actualStorageContainerName string
|
|
|
|
var actualBlobName string
|
|
|
|
|
|
|
|
var testSubject = &StepDeleteOSDisk{
|
|
|
|
delete: func(storageContainerName string, blobName string) error {
|
|
|
|
actualStorageContainerName = storageContainerName
|
|
|
|
actualBlobName = blobName
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
say: func(message string) {},
|
|
|
|
error: func(e error) {},
|
|
|
|
}
|
|
|
|
|
|
|
|
stateBag := DeleteTestStateBagStepDeleteOSDisk("http://storage.blob.core.windows.net/abc/def/pkrvm_os.vhd")
|
|
|
|
testSubject.Run(stateBag)
|
|
|
|
|
|
|
|
if actualStorageContainerName != "abc" {
|
|
|
|
t.Fatalf("Expected the storage container name to be 'abc/def', but found '%s'.", actualStorageContainerName)
|
|
|
|
}
|
|
|
|
|
|
|
|
if actualBlobName != "def/pkrvm_os.vhd" {
|
|
|
|
t.Fatalf("Expected the blob name to be 'pkrvm_os.vhd', but found '%s'.", actualBlobName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func DeleteTestStateBagStepDeleteOSDisk(osDiskVhd string) multistep.StateBag {
|
|
|
|
stateBag := new(multistep.BasicStateBag)
|
|
|
|
stateBag.Put(constants.ArmOSDiskVhd, osDiskVhd)
|
2017-05-29 00:06:09 -04:00
|
|
|
stateBag.Put(constants.ArmIsManagedImage, false)
|
2017-11-06 00:16:58 -05:00
|
|
|
stateBag.Put(constants.ArmIsExistingResourceGroup, false)
|
|
|
|
stateBag.Put(constants.ArmResourceGroupName, "testgroup")
|
2016-03-04 05:14:55 -05:00
|
|
|
|
|
|
|
return stateBag
|
|
|
|
}
|