azure: fix snapshot regression

This commit is contained in:
Christopher Boumenot 2018-12-13 13:54:19 -08:00
parent 47257c0d07
commit f9230aeb95
7 changed files with 109 additions and 72 deletions

View File

@ -183,8 +183,8 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
NewStepGetOSDisk(azureClient, ui), NewStepGetOSDisk(azureClient, ui),
NewStepGetAdditionalDisks(azureClient, ui), NewStepGetAdditionalDisks(azureClient, ui),
NewStepPowerOffCompute(azureClient, ui), NewStepPowerOffCompute(azureClient, ui),
NewStepSnapshotOSDisk(azureClient, ui, b.config.isManagedImage()), NewStepSnapshotOSDisk(azureClient, ui, b.config),
NewStepSnapshotDataDisks(azureClient, ui, b.config.isManagedImage()), NewStepSnapshotDataDisks(azureClient, ui, b.config),
NewStepCaptureImage(azureClient, ui), NewStepCaptureImage(azureClient, ui),
NewStepDeleteResourceGroup(azureClient, ui), NewStepDeleteResourceGroup(azureClient, ui),
NewStepDeleteOSDisk(azureClient, ui), NewStepDeleteOSDisk(azureClient, ui),
@ -220,8 +220,8 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
&packerCommon.StepProvision{}, &packerCommon.StepProvision{},
NewStepGetOSDisk(azureClient, ui), NewStepGetOSDisk(azureClient, ui),
NewStepGetAdditionalDisks(azureClient, ui), NewStepGetAdditionalDisks(azureClient, ui),
NewStepSnapshotOSDisk(azureClient, ui, b.config.isManagedImage()), NewStepSnapshotOSDisk(azureClient, ui, b.config),
NewStepSnapshotDataDisks(azureClient, ui, b.config.isManagedImage()), NewStepSnapshotDataDisks(azureClient, ui, b.config),
NewStepPowerOffCompute(azureClient, ui), NewStepPowerOffCompute(azureClient, ui),
NewStepCaptureImage(azureClient, ui), NewStepCaptureImage(azureClient, ui),
NewStepDeleteResourceGroup(azureClient, ui), NewStepDeleteResourceGroup(azureClient, ui),

View File

@ -56,8 +56,8 @@ var (
reCaptureNamePrefix = regexp.MustCompile("^[A-Za-z0-9][A-Za-z0-9_\\-\\.]{0,23}$") reCaptureNamePrefix = regexp.MustCompile("^[A-Za-z0-9][A-Za-z0-9_\\-\\.]{0,23}$")
reManagedDiskName = regexp.MustCompile(validManagedDiskName) reManagedDiskName = regexp.MustCompile(validManagedDiskName)
reResourceGroupName = regexp.MustCompile(validResourceGroupNameRe) reResourceGroupName = regexp.MustCompile(validResourceGroupNameRe)
reSnapshotName = regexp.MustCompile("^[A-Za-z0-9_]{10,79}$") reSnapshotName = regexp.MustCompile("^[A-Za-z0-9_]{1,79}$")
reSnapshotPrefix = regexp.MustCompile("^[A-Za-z0-9_]{10,59}$") reSnapshotPrefix = regexp.MustCompile("^[A-Za-z0-9_]{1,59}$")
) )
type PlanInformation struct { type PlanInformation struct {

View File

@ -669,7 +669,6 @@ func TestConfigShouldRejectMalformedManagedImageOSDiskSnapshotName(t *testing.T)
} }
malformedManagedImageOSDiskSnapshotName := []string{ malformedManagedImageOSDiskSnapshotName := []string{
"min_ten",
"-leading-hyphen", "-leading-hyphen",
"trailing-hyphen-", "trailing-hyphen-",
"trailing-period.", "trailing-period.",
@ -720,7 +719,6 @@ func TestConfigShouldRejectMalformedManagedImageDataDiskSnapshotPrefix(t *testin
} }
malformedManagedImageDataDiskSnapshotPrefix := []string{ malformedManagedImageDataDiskSnapshotPrefix := []string{
"more_ten",
"-leading-hyphen", "-leading-hyphen",
"trailing-hyphen-", "trailing-hyphen-",
"trailing-period.", "trailing-period.",

View File

@ -13,19 +13,19 @@ import (
) )
type StepSnapshotDataDisks struct { type StepSnapshotDataDisks struct {
client *AzureClient client *AzureClient
create func(ctx context.Context, resourceGroupName string, srcUriVhd string, location string, tags map[string]*string, dstSnapshotName string) error create func(ctx context.Context, resourceGroupName string, srcUriVhd string, location string, tags map[string]*string, dstSnapshotName string) error
say func(message string) say func(message string)
error func(e error) error func(e error)
isManagedImage bool enable func() bool
} }
func NewStepSnapshotDataDisks(client *AzureClient, ui packer.Ui, isManagedImage bool) *StepSnapshotDataDisks { func NewStepSnapshotDataDisks(client *AzureClient, ui packer.Ui, config *Config) *StepSnapshotDataDisks {
var step = &StepSnapshotDataDisks{ var step = &StepSnapshotDataDisks{
client: client, client: client,
say: func(message string) { ui.Say(message) }, say: func(message string) { ui.Say(message) },
error: func(e error) { ui.Error(e.Error()) }, error: func(e error) { ui.Error(e.Error()) },
isManagedImage: isManagedImage, enable: func() bool { return config.isManagedImage() && config.ManagedImageDataDiskSnapshotPrefix != "" },
} }
step.create = step.createDataDiskSnapshot step.create = step.createDataDiskSnapshot
@ -66,32 +66,38 @@ func (s *StepSnapshotDataDisks) createDataDiskSnapshot(ctx context.Context, reso
return err return err
} }
s.say(fmt.Sprintf(" -> Managed Image Data Disk Snapshot : '%s'", *(createdSnapshot.ID))) s.say(fmt.Sprintf(" -> Snapshot ID : '%s'", *(createdSnapshot.ID)))
return nil return nil
} }
func (s *StepSnapshotDataDisks) Run(ctx context.Context, stateBag multistep.StateBag) multistep.StepAction { func (s *StepSnapshotDataDisks) Run(ctx context.Context, stateBag multistep.StateBag) multistep.StepAction {
if s.isManagedImage { if !s.enable() {
return multistep.ActionContinue
}
s.say("Taking snapshot of data disk ...") var resourceGroupName = stateBag.Get(constants.ArmManagedImageResourceGroupName).(string)
var location = stateBag.Get(constants.ArmLocation).(string)
var tags = stateBag.Get(constants.ArmTags).(map[string]*string)
var additionalDisks = stateBag.Get(constants.ArmAdditionalDiskVhds).([]string)
var dstSnapshotPrefix = stateBag.Get(constants.ArmManagedImageDataDiskSnapshotPrefix).(string)
var resourceGroupName = stateBag.Get(constants.ArmManagedImageResourceGroupName).(string) if len(additionalDisks) == 1 {
var location = stateBag.Get(constants.ArmLocation).(string) s.say(fmt.Sprintf("Snapshotting data disk ..."))
var tags = stateBag.Get(constants.ArmTags).(map[string]*string) } else {
var additionalDisks = stateBag.Get(constants.ArmAdditionalDiskVhds).([]string) s.say(fmt.Sprintf("Snapshotting data disks ..."))
var dstSnapshotPrefix = stateBag.Get(constants.ArmManagedImageDataDiskSnapshotPrefix).(string) }
for i, disk := range additionalDisks { for i, disk := range additionalDisks {
dstSnapshotName := dstSnapshotPrefix + strconv.Itoa(i) s.say(fmt.Sprintf(" -> Data Disk : '%s'", disk))
err := s.create(ctx, resourceGroupName, disk, location, tags, dstSnapshotName)
if err != nil { dstSnapshotName := dstSnapshotPrefix + strconv.Itoa(i)
stateBag.Put(constants.Error, err) err := s.create(ctx, resourceGroupName, disk, location, tags, dstSnapshotName)
s.error(err)
return multistep.ActionHalt if err != nil {
} stateBag.Put(constants.Error, err)
s.error(err)
return multistep.ActionHalt
} }
} }

View File

@ -13,9 +13,9 @@ func TestStepSnapshotDataDisksShouldFailIfSnapshotFails(t *testing.T) {
create: func(context.Context, string, string, string, map[string]*string, string) error { create: func(context.Context, string, string, string, map[string]*string, string) error {
return fmt.Errorf("!! Unit Test FAIL !!") return fmt.Errorf("!! Unit Test FAIL !!")
}, },
say: func(message string) {}, say: func(message string) {},
error: func(e error) {}, error: func(e error) {},
isManagedImage: true, enable: func() bool { return true },
} }
stateBag := createTestStateBagStepSnapshotDataDisks() stateBag := createTestStateBagStepSnapshotDataDisks()
@ -30,14 +30,30 @@ func TestStepSnapshotDataDisksShouldFailIfSnapshotFails(t *testing.T) {
} }
} }
func TestStepSnapshotDataDisksShouldNotExecute(t *testing.T) {
var testSubject = &StepSnapshotDataDisks{
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)
}
}
func TestStepSnapshotDataDisksShouldPassIfSnapshotPasses(t *testing.T) { func TestStepSnapshotDataDisksShouldPassIfSnapshotPasses(t *testing.T) {
var testSubject = &StepSnapshotDataDisks{ var testSubject = &StepSnapshotDataDisks{
create: func(context.Context, string, string, string, map[string]*string, string) error { create: func(context.Context, string, string, string, map[string]*string, string) error {
return nil return nil
}, },
say: func(message string) {}, say: func(message string) {},
error: func(e error) {}, error: func(e error) {},
isManagedImage: true, enable: func() bool { return true },
} }
stateBag := createTestStateBagStepSnapshotDataDisks() stateBag := createTestStateBagStepSnapshotDataDisks()

View File

@ -11,19 +11,19 @@ import (
) )
type StepSnapshotOSDisk struct { type StepSnapshotOSDisk struct {
client *AzureClient client *AzureClient
create func(ctx context.Context, resourceGroupName string, srcUriVhd string, location string, tags map[string]*string, dstSnapshotName string) error create func(ctx context.Context, resourceGroupName string, srcUriVhd string, location string, tags map[string]*string, dstSnapshotName string) error
say func(message string) say func(message string)
error func(e error) error func(e error)
isManagedImage bool enable func() bool
} }
func NewStepSnapshotOSDisk(client *AzureClient, ui packer.Ui, isManagedImage bool) *StepSnapshotOSDisk { func NewStepSnapshotOSDisk(client *AzureClient, ui packer.Ui, config *Config) *StepSnapshotOSDisk {
var step = &StepSnapshotOSDisk{ var step = &StepSnapshotOSDisk{
client: client, client: client,
say: func(message string) { ui.Say(message) }, say: func(message string) { ui.Say(message) },
error: func(e error) { ui.Error(e.Error()) }, error: func(e error) { ui.Error(e.Error()) },
isManagedImage: isManagedImage, enable: func() bool { return config.isManagedImage() && config.ManagedImageOSDiskSnapshotName != "" },
} }
step.create = step.createSnapshot step.create = step.createSnapshot
@ -64,30 +64,31 @@ func (s *StepSnapshotOSDisk) createSnapshot(ctx context.Context, resourceGroupNa
return err return err
} }
s.say(fmt.Sprintf(" -> Managed Image OS Disk Snapshot : '%s'", *(createdSnapshot.ID))) s.say(fmt.Sprintf(" -> Snapshot ID : '%s'", *(createdSnapshot.ID)))
return nil return nil
} }
func (s *StepSnapshotOSDisk) Run(ctx context.Context, stateBag multistep.StateBag) multistep.StepAction { func (s *StepSnapshotOSDisk) Run(ctx context.Context, stateBag multistep.StateBag) multistep.StepAction {
if s.isManagedImage { if !s.enable() {
return multistep.ActionContinue
}
s.say("Taking snapshot of OS disk ...") s.say("Snapshotting OS disk ...")
var resourceGroupName = stateBag.Get(constants.ArmManagedImageResourceGroupName).(string) var resourceGroupName = stateBag.Get(constants.ArmManagedImageResourceGroupName).(string)
var location = stateBag.Get(constants.ArmLocation).(string) var location = stateBag.Get(constants.ArmLocation).(string)
var tags = stateBag.Get(constants.ArmTags).(map[string]*string) var tags = stateBag.Get(constants.ArmTags).(map[string]*string)
var srcUriVhd = stateBag.Get(constants.ArmOSDiskVhd).(string) var srcUriVhd = stateBag.Get(constants.ArmOSDiskVhd).(string)
var dstSnapshotName = stateBag.Get(constants.ArmManagedImageOSDiskSnapshotName).(string) var dstSnapshotName = stateBag.Get(constants.ArmManagedImageOSDiskSnapshotName).(string)
err := s.create(ctx, resourceGroupName, srcUriVhd, location, tags, dstSnapshotName) s.say(fmt.Sprintf(" -> OS Disk : '%s'", srcUriVhd))
err := s.create(ctx, resourceGroupName, srcUriVhd, location, tags, dstSnapshotName)
if err != nil { if err != nil {
stateBag.Put(constants.Error, err) stateBag.Put(constants.Error, err)
s.error(err) s.error(err)
return multistep.ActionHalt return multistep.ActionHalt
}
} }
return multistep.ActionContinue return multistep.ActionContinue

View File

@ -13,9 +13,9 @@ func TestStepSnapshotOSDiskShouldFailIfSnapshotFails(t *testing.T) {
create: func(context.Context, string, string, string, map[string]*string, string) error { create: func(context.Context, string, string, string, map[string]*string, string) error {
return fmt.Errorf("!! Unit Test FAIL !!") return fmt.Errorf("!! Unit Test FAIL !!")
}, },
say: func(message string) {}, say: func(message string) {},
error: func(e error) {}, error: func(e error) {},
isManagedImage: true, enable: func() bool { return true },
} }
stateBag := createTestStateBagStepSnapshotOSDisk() stateBag := createTestStateBagStepSnapshotOSDisk()
@ -30,14 +30,30 @@ func TestStepSnapshotOSDiskShouldFailIfSnapshotFails(t *testing.T) {
} }
} }
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)
}
}
func TestStepSnapshotOSDiskShouldPassIfSnapshotPasses(t *testing.T) { func TestStepSnapshotOSDiskShouldPassIfSnapshotPasses(t *testing.T) {
var testSubject = &StepSnapshotOSDisk{ var testSubject = &StepSnapshotOSDisk{
create: func(context.Context, string, string, string, map[string]*string, string) error { create: func(context.Context, string, string, string, map[string]*string, string) error {
return nil return nil
}, },
say: func(message string) {}, say: func(message string) {},
error: func(e error) {}, error: func(e error) {},
isManagedImage: true, enable: func() bool { return true },
} }
stateBag := createTestStateBagStepSnapshotOSDisk() stateBag := createTestStateBagStepSnapshotOSDisk()