Skip to content

Commit

Permalink
ebs: cleanup AMI for deregistration test
Browse files Browse the repository at this point in the history
The AMI we create for the deregistration test would linger after the
test was executed, which is less than idea, so this commit ensures we do
remove the AMI after the test executes.
  • Loading branch information
lbajolet-hashicorp committed Nov 19, 2024
1 parent 77a234c commit 19dad02
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions builder/ebs/builder_acc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1561,6 +1561,18 @@ func TestAccBuilder_DeregistrationProtection(t *testing.T) {
logfile)
}

defer func() {
err := removeDeregistrationProtection(ami)
if err != nil {
t.Logf("failed to remove deregistration protection for ami %q, will need to be manually cleaned-up", ami.Name)
return
}
err = ami.CleanUpAmi()
if err != nil {
t.Logf("failed to remove ami %q: %s, will need to be manually cleaned-up", ami.Name, err)
}
}()

logs, err := os.ReadFile(logfile)
if err != nil {
return fmt.Errorf("couldn't read logs from logfile %s: %s", logfile, err)
Expand All @@ -1576,6 +1588,27 @@ func TestAccBuilder_DeregistrationProtection(t *testing.T) {
acctest.TestPlugin(t, testcase)
}

func removeDeregistrationProtection(ami amazon_acc.AMIHelper) error {
images, err := ami.GetAmi()
if err != nil || len(images) != 1 {
return fmt.Errorf("Failed to find ami %s at region %s", ami.Name, ami.Region)
}

ec2Conn, err := testEC2Conn(ami.Region)
if err != nil {
return fmt.Errorf("Failed to connect to AWS on region %q: %s", ami.Region, err)
}

_, err = ec2Conn.DisableImageDeregistrationProtection(&ec2.DisableImageDeregistrationProtectionInput{
ImageId: images[0].ImageId,
})
if err != nil {
return err
}

return nil
}

func checkDeregistrationProtectionEnabled(ami amazon_acc.AMIHelper) error {
images, err := ami.GetAmi()
if err != nil || len(images) == 0 {
Expand Down

0 comments on commit 19dad02

Please sign in to comment.