Skip to content

Commit

Permalink
resource/aws_launch_configuration: Allow missing EC2 Image during roo…
Browse files Browse the repository at this point in the history
…t block device lookup

Reference: #10182

Previous output from new acceptance testing:

```
--- FAIL: TestAccAWSLaunchConfiguration_RootBlockDevice_AmiDisappears (329.50s)
    testing.go:640: Step 0 error: errors during follow-up refresh:

        Error: No images found for AMI ami-03f8153b368160e27
```

Output from acceptance testing:

```
--- PASS: TestAccAWSAMI_basic (60.58s)
--- PASS: TestAccAWSAMI_disappears (55.68s)
--- PASS: TestAccAWSAMI_snapshotSize (61.57s)
--- PASS: TestAccAWSAMI_tags (73.23s)

--- PASS: TestAccAWSInstance_addSecondaryInterface (119.45s)
--- PASS: TestAccAWSInstance_addSecurityGroupNetworkInterface (120.56s)
--- PASS: TestAccAWSInstance_associatePublic_defaultPrivate (75.15s)
--- PASS: TestAccAWSInstance_associatePublic_defaultPublic (74.26s)
--- PASS: TestAccAWSInstance_associatePublic_explicitPrivate (74.07s)
--- PASS: TestAccAWSInstance_associatePublic_explicitPublic (94.78s)
--- PASS: TestAccAWSInstance_associatePublic_overridePrivate (103.83s)
--- PASS: TestAccAWSInstance_associatePublic_overridePublic (74.45s)
--- PASS: TestAccAWSInstance_associatePublicIPAndPrivateIP (77.27s)
--- PASS: TestAccAWSInstance_basic (187.91s)
--- PASS: TestAccAWSInstance_blockDevices (79.58s)
--- PASS: TestAccAWSInstance_changeInstanceType (346.21s)
--- PASS: TestAccAWSInstance_CreditSpecification_Empty_NonBurstable (309.18s)
--- PASS: TestAccAWSInstance_creditSpecification_isNotAppliedToNonBurstable (95.71s)
--- PASS: TestAccAWSInstance_creditSpecification_standardCpuCredits (94.60s)
--- PASS: TestAccAWSInstance_creditSpecification_standardCpuCredits_t2Tot3Taint (416.77s)
--- PASS: TestAccAWSInstance_creditSpecification_unknownCpuCredits_t2 (84.38s)
--- PASS: TestAccAWSInstance_creditSpecification_unknownCpuCredits_t3 (277.51s)
--- PASS: TestAccAWSInstance_creditSpecification_unlimitedCpuCredits (94.46s)
--- PASS: TestAccAWSInstance_creditSpecification_unlimitedCpuCredits_t2Tot3Taint (205.09s)
--- PASS: TestAccAWSInstance_creditSpecification_unspecifiedDefaultsToStandard (75.03s)
--- PASS: TestAccAWSInstance_CreditSpecification_UnspecifiedToEmpty_NonBurstable (66.25s)
--- PASS: TestAccAWSInstance_creditSpecification_updateCpuCredits (129.64s)
--- PASS: TestAccAWSInstance_creditSpecificationT3_standardCpuCredits (96.86s)
--- PASS: TestAccAWSInstance_creditSpecificationT3_unlimitedCpuCredits (93.24s)
--- PASS: TestAccAWSInstance_creditSpecificationT3_unspecifiedDefaultsToUnlimited (302.23s)
--- PASS: TestAccAWSInstance_creditSpecificationT3_updateCpuCredits (105.73s)
--- PASS: TestAccAWSInstance_disableApiTermination (134.68s)
--- PASS: TestAccAWSInstance_disappears (99.99s)
--- PASS: TestAccAWSInstance_EbsBlockDevice_KmsKeyArn (86.99s)
--- PASS: TestAccAWSInstance_forceNewAndTagsDrift (249.42s)
--- PASS: TestAccAWSInstance_getPasswordData_falseToTrue (153.39s)
--- PASS: TestAccAWSInstance_getPasswordData_trueToFalse (144.29s)
--- PASS: TestAccAWSInstance_GP2IopsDevice (180.27s)
--- PASS: TestAccAWSInstance_GP2WithIopsValue (63.42s)
--- PASS: TestAccAWSInstance_hibernation (184.12s)
--- PASS: TestAccAWSInstance_inDefaultVpcBySgId (82.35s)
--- PASS: TestAccAWSInstance_inDefaultVpcBySgName (72.00s)
--- PASS: TestAccAWSInstance_inEc2Classic (87.12s)
--- PASS: TestAccAWSInstance_instanceProfileChange (165.02s)
--- PASS: TestAccAWSInstance_ipv6_supportAddressCount (73.56s)
--- PASS: TestAccAWSInstance_ipv6_supportAddressCountWithIpv4 (83.80s)
--- PASS: TestAccAWSInstance_ipv6AddressCountAndSingleAddressCausesError (9.88s)
--- PASS: TestAccAWSInstance_keyPairCheck (184.21s)
--- PASS: TestAccAWSInstance_multipleRegions (249.46s)
--- PASS: TestAccAWSInstance_NetworkInstanceRemovingAllSecurityGroups (80.15s)
--- PASS: TestAccAWSInstance_NetworkInstanceSecurityGroups (76.11s)
--- PASS: TestAccAWSInstance_NetworkInstanceVPCSecurityGroupIDs (75.10s)
--- PASS: TestAccAWSInstance_noAMIEphemeralDevices (180.15s)
--- PASS: TestAccAWSInstance_placementGroup (65.13s)
--- PASS: TestAccAWSInstance_primaryNetworkInterface (75.72s)
--- PASS: TestAccAWSInstance_primaryNetworkInterfaceSourceDestCheck (86.88s)
--- PASS: TestAccAWSInstance_privateIP (74.08s)
--- PASS: TestAccAWSInstance_RootBlockDevice_KmsKeyArn (316.78s)
--- PASS: TestAccAWSInstance_rootBlockDeviceMismatch (186.67s)
--- PASS: TestAccAWSInstance_rootInstanceStore (88.21s)
--- PASS: TestAccAWSInstance_sourceDestCheck (111.85s)
--- PASS: TestAccAWSInstance_tags (237.27s)
--- PASS: TestAccAWSInstance_UserData_EmptyStringToUnspecified (92.43s)
--- PASS: TestAccAWSInstance_UserData_UnspecifiedToEmptyString (108.86s)
--- PASS: TestAccAWSInstance_userDataBase64 (221.53s)
--- PASS: TestAccAWSInstance_volumeTags (100.55s)
--- PASS: TestAccAWSInstance_volumeTagsComputed (104.70s)
--- PASS: TestAccAWSInstance_vpc (73.95s)
--- PASS: TestAccAWSInstance_withIamInstanceProfile (112.28s)

--- PASS: TestAccAWSInstanceDataSource_AzUserData (221.28s)
--- PASS: TestAccAWSInstanceDataSource_basic (301.34s)
--- PASS: TestAccAWSInstanceDataSource_blockDevices (90.86s)
--- PASS: TestAccAWSInstanceDataSource_creditSpecification (101.85s)
--- PASS: TestAccAWSInstanceDataSource_EbsBlockDevice_KmsKeyId (205.68s)
--- PASS: TestAccAWSInstanceDataSource_getPasswordData_falseToTrue (176.04s)
--- PASS: TestAccAWSInstanceDataSource_getPasswordData_trueToFalse (172.89s)
--- PASS: TestAccAWSInstanceDataSource_GetUserData (123.47s)
--- PASS: TestAccAWSInstanceDataSource_GetUserData_NoUserData (115.57s)
--- PASS: TestAccAWSInstanceDataSource_gp2IopsDevice (95.30s)
--- PASS: TestAccAWSInstanceDataSource_keyPair (189.29s)
--- PASS: TestAccAWSInstanceDataSource_PlacementGroup (186.27s)
--- PASS: TestAccAWSInstanceDataSource_privateIP (108.66s)
--- PASS: TestAccAWSInstanceDataSource_RootBlockDevice_KmsKeyId (110.15s)
--- PASS: TestAccAWSInstanceDataSource_rootInstanceStore (89.90s)
--- PASS: TestAccAWSInstanceDataSource_SecurityGroups (147.45s)
--- PASS: TestAccAWSInstanceDataSource_tags (135.92s)
--- PASS: TestAccAWSInstanceDataSource_VPC (83.45s)
--- PASS: TestAccAWSInstanceDataSource_VPCSecurityGroups (191.31s)

--- PASS: TestAccAWSInstancesDataSource_basic (207.14s)
--- PASS: TestAccAWSInstancesDataSource_instance_state_names (191.23s)
--- PASS: TestAccAWSInstancesDataSource_tags (201.39s)

--- PASS: TestAccAWSLaunchConfiguration_basic (17.97s)
--- PASS: TestAccAWSLaunchConfiguration_ebs_noDevice (10.28s)
--- PASS: TestAccAWSLaunchConfiguration_encryptedRootBlockDevice (13.95s)
--- PASS: TestAccAWSLaunchConfiguration_RootBlockDevice_AmiDisappears (334.21s)
--- PASS: TestAccAWSLaunchConfiguration_RootBlockDevice_VolumeSize (25.12s)
--- PASS: TestAccAWSLaunchConfiguration_updateEbsBlockDevices (16.63s)
--- PASS: TestAccAWSLaunchConfiguration_userData (16.13s)
--- PASS: TestAccAWSLaunchConfiguration_withBlockDevices (11.22s)
--- PASS: TestAccAWSLaunchConfiguration_withEncryption (13.61s)
--- PASS: TestAccAWSLaunchConfiguration_withIAMProfile (29.79s)
--- PASS: TestAccAWSLaunchConfiguration_withInstanceStoreAMI (13.55s)
--- PASS: TestAccAWSLaunchConfiguration_withSpotPrice (10.84s)
--- PASS: TestAccAWSLaunchConfiguration_withVpcClassicLink (19.56s)

--- PASS: TestAccAWSLaunchConfigurationDataSource_basic (8.90s)
--- PASS: TestAccAWSLaunchConfigurationDataSource_securityGroups (11.48s)
```
  • Loading branch information
bflad committed Feb 12, 2020
1 parent d6263e8 commit af7ee2b
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 32 deletions.
36 changes: 36 additions & 0 deletions aws/resource_aws_ami_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,28 @@ func TestAccAWSAMI_basic(t *testing.T) {
})
}

func TestAccAWSAMI_disappears(t *testing.T) {
var ami ec2.Image
resourceName := "aws_ami.test"
rName := acctest.RandomWithPrefix("tf-acc-test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAmiDestroy,
Steps: []resource.TestStep{
{
Config: testAccAmiConfig_basic(rName, 8),
Check: resource.ComposeTestCheckFunc(
testAccCheckAmiExists(resourceName, &ami),
testAccCheckAmiDisappears(&ami),
),
ExpectNonEmptyPlan: true,
},
},
})
}

func TestAccAWSAMI_tags(t *testing.T) {
var ami ec2.Image
resourceName := "aws_ami.test"
Expand Down Expand Up @@ -164,6 +186,20 @@ func testAccCheckAmiDestroy(s *terraform.State) error {
return nil
}

func testAccCheckAmiDisappears(image *ec2.Image) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).ec2conn

input := &ec2.DeregisterImageInput{
ImageId: image.ImageId,
}

_, err := conn.DeregisterImage(input)

return err
}
}

func testAccCheckAmiExists(n string, ami *ec2.Image) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down
2 changes: 1 addition & 1 deletion aws/resource_aws_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -1436,7 +1436,7 @@ func fetchRootDeviceName(ami string, conn *ec2.EC2) (*string, error) {

// For a bad image, we just return nil so we don't block a refresh
if len(res.Images) == 0 {
return nil, fmt.Errorf("No images found for AMI %s", ami)
return nil, nil
}

image := res.Images[0]
Expand Down
114 changes: 83 additions & 31 deletions aws/resource_aws_launch_configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,18 +153,49 @@ func TestAccAWSLaunchConfiguration_withInstanceStoreAMI(t *testing.T) {
})
}

func TestAccAWSLaunchConfiguration_updateRootBlockDevice(t *testing.T) {
func TestAccAWSLaunchConfiguration_RootBlockDevice_AmiDisappears(t *testing.T) {
var ami ec2.Image
var conf autoscaling.LaunchConfiguration
amiCopyResourceName := "aws_ami_copy.test"
resourceName := "aws_launch_configuration.test"
rInt := acctest.RandInt()
rName := acctest.RandomWithPrefix("tf-acc-test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSLaunchConfigurationDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSLaunchConfigurationConfigWithRootBlockDevice(rInt),
Config: testAccAWSLaunchConfigurationConfigWithRootBlockDeviceCopiedAmi(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSLaunchConfigurationExists(resourceName, &conf),
testAccCheckAmiExists(amiCopyResourceName, &ami),
testAccCheckAmiDisappears(&ami),
),
ExpectNonEmptyPlan: true,
},
{
Config: testAccAWSLaunchConfigurationConfigWithRootBlockDeviceVolumeSize(rName, 10),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSLaunchConfigurationExists(resourceName, &conf),
),
},
},
})
}

func TestAccAWSLaunchConfiguration_RootBlockDevice_VolumeSize(t *testing.T) {
var conf autoscaling.LaunchConfiguration
resourceName := "aws_launch_configuration.test"
rName := acctest.RandomWithPrefix("tf-acc-test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSLaunchConfigurationDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSLaunchConfigurationConfigWithRootBlockDeviceVolumeSize(rName, 11),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSLaunchConfigurationExists(resourceName, &conf),
resource.TestCheckResourceAttr(resourceName, "root_block_device.0.volume_size", "11"),
Expand All @@ -177,7 +208,7 @@ func TestAccAWSLaunchConfiguration_updateRootBlockDevice(t *testing.T) {
ImportStateVerifyIgnore: []string{"associate_public_ip_address", "name_prefix"},
},
{
Config: testAccAWSLaunchConfigurationConfigWithRootBlockDeviceUpdated(rInt),
Config: testAccAWSLaunchConfigurationConfigWithRootBlockDeviceVolumeSize(rName, 20),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSLaunchConfigurationExists(resourceName, &conf),
resource.TestCheckResourceAttr(resourceName, "root_block_device.0.volume_size", "20"),
Expand Down Expand Up @@ -575,6 +606,25 @@ data "aws_ami" "ubuntu" {
`)
}

func testAccAWSLaunchConfigurationConfig_HvmEbsAmi() string {
return fmt.Sprintf(`
data "aws_ami" "amzn-ami-minimal-hvm-ebs" {
most_recent = true
owners = ["amazon"]
filter {
name = "name"
values = ["amzn-ami-minimal-hvm-*"]
}
filter {
name = "root-device-type"
values = ["ebs"]
}
}
`)
}

func testAccAWSLaunchConfigurationConfig_instanceStoreAMI() string {
return fmt.Sprintf(`
data "aws_ami" "amzn-ami-minimal-pv" {
Expand Down Expand Up @@ -606,21 +656,40 @@ resource "aws_launch_configuration" "test" {
`, rName)
}

func testAccAWSLaunchConfigurationConfigWithRootBlockDevice(rInt int) string {
return testAccAWSLaunchConfigurationConfig_ami() + fmt.Sprintf(`
func testAccAWSLaunchConfigurationConfigWithRootBlockDeviceCopiedAmi(rName string) string {
return testAccAWSLaunchConfigurationConfig_HvmEbsAmi() + fmt.Sprintf(`
data "aws_region" "current" {}
resource "aws_ami_copy" "test" {
name = %[1]q
source_ami_id = data.aws_ami.amzn-ami-minimal-hvm-ebs.id
source_ami_region = data.aws_region.current.name
}
resource "aws_launch_configuration" "test" {
name_prefix = "tf-acc-test-%d"
image_id = "${data.aws_ami.ubuntu.id}"
instance_type = "m1.small"
user_data = "testtest-user-data"
associate_public_ip_address = true
name = %[1]q
image_id = aws_ami_copy.test.id
instance_type = "t3.micro"
root_block_device {
volume_type = "gp2"
volume_size = 11
volume_size = 10
}
}
`, rInt)
`, rName)
}

func testAccAWSLaunchConfigurationConfigWithRootBlockDeviceVolumeSize(rName string, volumeSize int) string {
return testAccAWSLaunchConfigurationConfig_HvmEbsAmi() + fmt.Sprintf(`
resource "aws_launch_configuration" "test" {
name = %[1]q
image_id = data.aws_ami.amzn-ami-minimal-hvm-ebs.id
instance_type = "t3.micro"
root_block_device {
volume_size = %[2]d
}
}
`, rName, volumeSize)
}

func testAccAWSLaunchConfigurationConfigWithEncryptedRootBlockDevice(rInt int) string {
Expand Down Expand Up @@ -659,23 +728,6 @@ resource "aws_launch_configuration" "test" {
`, rInt, rInt, rInt)
}

func testAccAWSLaunchConfigurationConfigWithRootBlockDeviceUpdated(rInt int) string {
return testAccAWSLaunchConfigurationConfig_ami() + fmt.Sprintf(`
resource "aws_launch_configuration" "test" {
name_prefix = "tf-acc-test-%d"
image_id = "${data.aws_ami.ubuntu.id}"
instance_type = "m1.small"
user_data = "testtest-user-data"
associate_public_ip_address = true
root_block_device {
volume_type = "gp2"
volume_size = 20
}
}
`, rInt)
}

func testAccAWSLaunchConfigurationConfig() string {
return testAccAWSLaunchConfigurationConfig_ami() + fmt.Sprintf(`
resource "aws_launch_configuration" "test" {
Expand Down

0 comments on commit af7ee2b

Please sign in to comment.