fix copy logic and tests

This commit is contained in:
Megan Marsh 2019-06-17 15:38:28 -07:00
parent 8cc82ca8d2
commit 305592d8ed
2 changed files with 25 additions and 15 deletions

View File

@ -33,19 +33,25 @@ func (s *StepAMIRegionCopy) Run(ctx context.Context, state multistep.StateBag) m
ami := amis[s.OriginalRegion] ami := amis[s.OriginalRegion]
// Always copy back into original region to preserve the ami name // Always copy back into original region to preserve the ami name
s.toDelete = ami if s.EncryptBootVolume != nil || s.AMISkipBuildRegion {
if !s.AMISkipBuildRegion { // if we haven't specificed encryption and we aren't skipping the save
s.Regions = append(s.Regions, s.OriginalRegion) // to the build region, we don't have anything to delete
s.toDelete = ami
} }
if s.EncryptBootVolume != nil && *s.EncryptBootVolume { if s.EncryptBootVolume != nil {
// encrypt_boot is true, so we have to copy the temporary if !s.AMISkipBuildRegion {
// AMI with required encryption setting. s.Regions = append(s.Regions, s.OriginalRegion)
// temp image was created by stepCreateAMI. }
if s.RegionKeyIds == nil { if *s.EncryptBootVolume {
s.RegionKeyIds = make(map[string]string) // encrypt_boot is true, so we have to copy the temporary
// AMI with required encryption setting.
// temp image was created by stepCreateAMI.
if s.RegionKeyIds == nil {
s.RegionKeyIds = make(map[string]string)
}
s.RegionKeyIds[s.OriginalRegion] = s.AMIKmsKeyId
} }
s.RegionKeyIds[s.OriginalRegion] = s.AMIKmsKeyId
} }
if len(s.Regions) == 0 { if len(s.Regions) == 0 {
@ -99,6 +105,10 @@ func (s *StepAMIRegionCopy) Cleanup(state multistep.StateBag) {
ec2conn := state.Get("ec2").(*ec2.EC2) ec2conn := state.Get("ec2").(*ec2.EC2)
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
if len(s.toDelete) == 0 {
return
}
// Delete the unencrypted amis and snapshots // Delete the unencrypted amis and snapshots
ui.Say("Deregistering the AMI and deleting unencrypted temporary " + ui.Say("Deregistering the AMI and deleting unencrypted temporary " +
"AMIs and snapshots") "AMIs and snapshots")

View File

@ -101,11 +101,11 @@ func TestStepAmiRegionCopy_nil_encryption(t *testing.T) {
state := tState() state := tState()
stepAMIRegionCopy.Run(context.Background(), state) stepAMIRegionCopy.Run(context.Background(), state)
if stepAMIRegionCopy.toDelete != "ami-12345" { if stepAMIRegionCopy.toDelete != "" {
t.Fatalf("Should delete original intermediary ami even if not encrypted") t.Fatalf("Shouldn't have an intermediary ami if encrypt is nil")
} }
if len(stepAMIRegionCopy.Regions) == 0 { if len(stepAMIRegionCopy.Regions) != 0 {
t.Fatalf("Should have added original ami to original region") t.Fatalf("Should not have added original ami to original region")
} }
} }
@ -179,7 +179,7 @@ func TestStepAmiRegionCopy_true_AMISkipBuildRegion(t *testing.T) {
if stepAMIRegionCopy.toDelete == "" { if stepAMIRegionCopy.toDelete == "" {
t.Fatalf("Should delete original AMI if skip_save_build_region=true") t.Fatalf("Should delete original AMI if skip_save_build_region=true")
} }
if len(stepAMIRegionCopy.Regions) == 0 { if len(stepAMIRegionCopy.Regions) != 0 {
t.Fatalf("Should not have added original ami to Regions") t.Fatalf("Should not have added original ami to Regions")
} }
} }