2018-11-06 22:23:17 -05:00
|
|
|
package arm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"github.com/hashicorp/packer/builder/azure/common/constants"
|
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestStepSnapshotOSDiskShouldFailIfSnapshotFails(t *testing.T) {
|
|
|
|
var testSubject = &StepSnapshotOSDisk{
|
|
|
|
create: func(context.Context, string, string, string, map[string]*string, string) error {
|
|
|
|
return fmt.Errorf("!! Unit Test FAIL !!")
|
|
|
|
},
|
2018-12-13 16:54:19 -05:00
|
|
|
say: func(message string) {},
|
|
|
|
error: func(e error) {},
|
|
|
|
enable: func() bool { return true },
|
2018-11-06 22:23:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
stateBag := createTestStateBagStepSnapshotOSDisk()
|
|
|
|
|
|
|
|
var result = testSubject.Run(context.Background(), 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-13 16:54:19 -05:00
|
|
|
func TestStepSnapshotOSDiskShouldNotExecute(t *testing.T) {
|
|
|
|
var testSubject = &StepSnapshotOSDisk{
|
|
|
|
create: func(context.Context, string, string, string, map[string]*string, string) error {
|
|
|
|
return fmt.Errorf("!! Unit Test FAIL !!")
|
|
|
|
},
|
|
|
|
say: func(message string) {},
|
|
|
|
error: func(e error) {},
|
|
|
|
enable: func() bool { return false },
|
|
|
|
}
|
|
|
|
|
|
|
|
var result = testSubject.Run(context.Background(), nil)
|
|
|
|
if result != multistep.ActionContinue {
|
|
|
|
t.Fatalf("Expected the step to return 'ActionContinue', but got '%d'.", result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-06 22:23:17 -05:00
|
|
|
func TestStepSnapshotOSDiskShouldPassIfSnapshotPasses(t *testing.T) {
|
|
|
|
var testSubject = &StepSnapshotOSDisk{
|
|
|
|
create: func(context.Context, string, string, string, map[string]*string, string) error {
|
|
|
|
return nil
|
|
|
|
},
|
2018-12-13 16:54:19 -05:00
|
|
|
say: func(message string) {},
|
|
|
|
error: func(e error) {},
|
|
|
|
enable: func() bool { return true },
|
2018-11-06 22:23:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
stateBag := createTestStateBagStepSnapshotOSDisk()
|
|
|
|
|
|
|
|
var result = testSubject.Run(context.Background(), 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 createTestStateBagStepSnapshotOSDisk() multistep.StateBag {
|
|
|
|
stateBag := new(multistep.BasicStateBag)
|
|
|
|
|
2018-11-09 19:05:25 -05:00
|
|
|
stateBag.Put(constants.ArmManagedImageResourceGroupName, "Unit Test: ResourceGroupName")
|
2018-11-06 22:23:17 -05:00
|
|
|
stateBag.Put(constants.ArmLocation, "Unit Test: Location")
|
|
|
|
|
|
|
|
value := "Unit Test: Tags"
|
|
|
|
tags := map[string]*string{
|
|
|
|
"tag01": &value,
|
|
|
|
}
|
|
|
|
|
|
|
|
stateBag.Put(constants.ArmTags, tags)
|
|
|
|
|
|
|
|
stateBag.Put(constants.ArmOSDiskVhd, "subscriptions/123-456-789/resourceGroups/existingresourcegroup/providers/Microsoft.Compute/disks/osdisk")
|
|
|
|
stateBag.Put(constants.ArmManagedImageOSDiskSnapshotName, "Unit Test: ManagedImageOSDiskSnapshotName")
|
|
|
|
|
|
|
|
return stateBag
|
2018-11-07 13:08:15 -05:00
|
|
|
}
|