Merge pull request #7111 from hashicorp/pr-7077-azure-regression

azure: fix snapshot regression
This commit is contained in:
Christopher Boumenot 2018-12-18 09:53:52 -08:00 committed by GitHub
commit acacf6e59c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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

@ -17,15 +17,15 @@ type StepSnapshotDataDisks struct {
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,15 +66,14 @@ 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 resourceGroupName = stateBag.Get(constants.ArmManagedImageResourceGroupName).(string)
var location = stateBag.Get(constants.ArmLocation).(string) var location = stateBag.Get(constants.ArmLocation).(string)
@ -82,7 +81,15 @@ func (s *StepSnapshotDataDisks) Run(ctx context.Context, stateBag multistep.Stat
var additionalDisks = stateBag.Get(constants.ArmAdditionalDiskVhds).([]string) var additionalDisks = stateBag.Get(constants.ArmAdditionalDiskVhds).([]string)
var dstSnapshotPrefix = stateBag.Get(constants.ArmManagedImageDataDiskSnapshotPrefix).(string) var dstSnapshotPrefix = stateBag.Get(constants.ArmManagedImageDataDiskSnapshotPrefix).(string)
if len(additionalDisks) == 1 {
s.say(fmt.Sprintf("Snapshotting data disk ..."))
} else {
s.say(fmt.Sprintf("Snapshotting data disks ..."))
}
for i, disk := range additionalDisks { for i, disk := range additionalDisks {
s.say(fmt.Sprintf(" -> Data Disk : '%s'", disk))
dstSnapshotName := dstSnapshotPrefix + strconv.Itoa(i) dstSnapshotName := dstSnapshotPrefix + strconv.Itoa(i)
err := s.create(ctx, resourceGroupName, disk, location, tags, dstSnapshotName) err := s.create(ctx, resourceGroupName, disk, location, tags, dstSnapshotName)
@ -93,7 +100,6 @@ func (s *StepSnapshotDataDisks) Run(ctx context.Context, stateBag multistep.Stat
return multistep.ActionHalt return multistep.ActionHalt
} }
} }
}
return multistep.ActionContinue return multistep.ActionContinue
} }

View File

@ -15,7 +15,7 @@ func TestStepSnapshotDataDisksShouldFailIfSnapshotFails(t *testing.T) {
}, },
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,6 +30,22 @@ 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 {
@ -37,7 +53,7 @@ func TestStepSnapshotDataDisksShouldPassIfSnapshotPasses(t *testing.T) {
}, },
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

@ -15,15 +15,15 @@ type StepSnapshotOSDisk struct {
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,15 +64,16 @@ 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)
@ -80,6 +81,7 @@ func (s *StepSnapshotOSDisk) Run(ctx context.Context, stateBag multistep.StateBa
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)
s.say(fmt.Sprintf(" -> OS Disk : '%s'", srcUriVhd))
err := s.create(ctx, resourceGroupName, srcUriVhd, location, tags, dstSnapshotName) err := s.create(ctx, resourceGroupName, srcUriVhd, location, tags, dstSnapshotName)
if err != nil { if err != nil {
@ -88,7 +90,6 @@ func (s *StepSnapshotOSDisk) Run(ctx context.Context, stateBag multistep.StateBa
return multistep.ActionHalt return multistep.ActionHalt
} }
}
return multistep.ActionContinue return multistep.ActionContinue
} }

View File

@ -15,7 +15,7 @@ func TestStepSnapshotOSDiskShouldFailIfSnapshotFails(t *testing.T) {
}, },
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,6 +30,22 @@ 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 {
@ -37,7 +53,7 @@ func TestStepSnapshotOSDiskShouldPassIfSnapshotPasses(t *testing.T) {
}, },
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()