Skip to content

Commit 5c9cae5

Browse files
committed
test: destroy snapshot on teardown
1 parent fb87041 commit 5c9cae5

1 file changed

Lines changed: 36 additions & 15 deletions

File tree

builder/ebs/acceptance/aws.go

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package amazon_acc
55

66
import (
77
"context"
8+
"errors"
89
"fmt"
910
"time"
1011

@@ -52,25 +53,45 @@ func (a *AMIHelper) CleanUpAmi() error {
5253
if err != nil {
5354
return fmt.Errorf("AWSAMICleanUp: Unable to find Image %s: %s", a.Name, err.Error())
5455
}
56+
if resp == nil {
57+
return errors.New("AWSAMICleanUp: Response from describe images should not be nil")
58+
}
59+
if len(resp.Images) == 0 {
60+
return errors.New("AWSAMICleanUp: No image was found by describes images")
61+
}
5562

56-
if resp != nil && len(resp.Images) > 0 {
57-
ctx = context.TODO()
58-
err = retry.Config{
59-
Tries: 11,
60-
ShouldRetry: func(err error) bool {
61-
return true // TODO make retry more specific to eventual consitencey
62-
},
63-
RetryDelay: (&retry.Backoff{InitialBackoff: 200 * time.Millisecond, MaxBackoff: 30 * time.Second, Multiplier: 2}).Linear,
64-
}.Run(ctx, func(ctx context.Context) error {
65-
_, err = regionconn.DeregisterImage(&ec2.DeregisterImageInput{
66-
ImageId: resp.Images[0].ImageId,
67-
})
68-
return err
63+
image := resp.Images[0]
64+
ctx = context.TODO()
65+
err = retry.Config{
66+
Tries: 11,
67+
ShouldRetry: func(err error) bool {
68+
return true // TODO make retry more specific to eventual consitencey
69+
},
70+
RetryDelay: (&retry.Backoff{InitialBackoff: 200 * time.Millisecond, MaxBackoff: 30 * time.Second, Multiplier: 2}).Linear,
71+
}.Run(ctx, func(ctx context.Context) error {
72+
_, err = regionconn.DeregisterImage(&ec2.DeregisterImageInput{
73+
ImageId: image.ImageId,
6974
})
70-
7175
if err != nil {
72-
return fmt.Errorf("AWSAMICleanUp: Unable to Deregister Image %s", err.Error())
76+
return err
77+
}
78+
if len(image.BlockDeviceMappings) == 0 {
79+
return fmt.Errorf("AWSAMICleanUp: Image should contain at least 1 BlockDeviceMapping, got %d", len(image.BlockDeviceMappings))
7380
}
81+
for _, bdm := range image.BlockDeviceMappings {
82+
if bdm.Ebs != nil && bdm.Ebs.SnapshotId != nil {
83+
_, err = regionconn.DeleteSnapshot(&ec2.DeleteSnapshotInput{
84+
SnapshotId: bdm.Ebs.SnapshotId,
85+
})
86+
return err
87+
88+
}
89+
}
90+
return nil
91+
})
92+
93+
if err != nil {
94+
return fmt.Errorf("AWSAMICleanUp: Unable to Deregister Image %s", err.Error())
7495
}
7596

7697
return nil

0 commit comments

Comments
 (0)