Skip to content

Commit c36b56c

Browse files
authored
Merge pull request #10199 from terraform-providers/rfd-at002-eip
Import test refactor for EIP resources
2 parents b59d481 + 6006087 commit c36b56c

File tree

3 files changed

+265
-152
lines changed

3 files changed

+265
-152
lines changed

aws/provider_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,16 @@ func testAccEC2ClassicPreCheck(t *testing.T) {
216216
}
217217
}
218218

219+
func testAccEC2VPCOnlyPreCheck(t *testing.T) {
220+
client := testAccProvider.Meta().(*AWSClient)
221+
platforms := client.supportedplatforms
222+
region := client.region
223+
if hasEc2Classic(platforms) {
224+
t.Skipf("This test can only in regions without EC2 Classic, platforms available in %s: %q",
225+
region, platforms)
226+
}
227+
}
228+
219229
func testAccHasServicePreCheck(service string, t *testing.T) {
220230
if partition, ok := endpoints.PartitionForRegion(endpoints.DefaultPartitions(), testAccGetRegion()); ok {
221231
if _, ok := partition.Services()[service]; !ok {

aws/resource_aws_eip_association_test.go

Lines changed: 107 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ import (
1212
"github.com/hashicorp/terraform-plugin-sdk/terraform"
1313
)
1414

15-
func TestAccAWSEIPAssociation_importInstance(t *testing.T) {
15+
func TestAccAWSEIPAssociation_instance(t *testing.T) {
1616
resourceName := "aws_eip_association.test"
17+
var a ec2.Address
1718

1819
resource.ParallelTest(t, resource.TestCase{
1920
PreCheck: func() { testAccPreCheck(t) },
@@ -22,6 +23,14 @@ func TestAccAWSEIPAssociation_importInstance(t *testing.T) {
2223
Steps: []resource.TestStep{
2324
{
2425
Config: testAccAWSEIPAssociationConfig_instance,
26+
Check: resource.ComposeTestCheckFunc(
27+
testAccCheckAWSEIPExists(
28+
"aws_eip.test", &a),
29+
testAccCheckAWSEIPAssociationExists(
30+
resourceName, &a),
31+
testAccCheckAWSEIPAssociationExists(
32+
resourceName, &a),
33+
),
2534
},
2635
{
2736
ResourceName: resourceName,
@@ -32,8 +41,9 @@ func TestAccAWSEIPAssociation_importInstance(t *testing.T) {
3241
})
3342
}
3443

35-
func TestAccAWSEIPAssociation_importNetworkInterface(t *testing.T) {
44+
func TestAccAWSEIPAssociation_networkInterface(t *testing.T) {
3645
resourceName := "aws_eip_association.test"
46+
var a ec2.Address
3747

3848
resource.ParallelTest(t, resource.TestCase{
3949
PreCheck: func() { testAccPreCheck(t) },
@@ -42,6 +52,12 @@ func TestAccAWSEIPAssociation_importNetworkInterface(t *testing.T) {
4252
Steps: []resource.TestStep{
4353
{
4454
Config: testAccAWSEIPAssociationConfig_networkInterface,
55+
Check: resource.ComposeTestCheckFunc(
56+
testAccCheckAWSEIPExists(
57+
"aws_eip.test", &a),
58+
testAccCheckAWSEIPAssociationExists(
59+
resourceName, &a),
60+
),
4561
},
4662
{
4763
ResourceName: resourceName,
@@ -54,35 +70,42 @@ func TestAccAWSEIPAssociation_importNetworkInterface(t *testing.T) {
5470

5571
func TestAccAWSEIPAssociation_basic(t *testing.T) {
5672
var a ec2.Address
73+
resourceName := "aws_eip_association.by_allocation_id"
5774

5875
resource.ParallelTest(t, resource.TestCase{
59-
PreCheck: func() { testAccPreCheck(t) },
76+
PreCheck: func() { testAccPreCheck(t); testAccEC2VPCOnlyPreCheck(t) },
6077
Providers: testAccProviders,
6178
CheckDestroy: testAccCheckAWSEIPAssociationDestroy,
6279
Steps: []resource.TestStep{
6380
{
6481
Config: testAccAWSEIPAssociationConfig,
6582
Check: resource.ComposeTestCheckFunc(
6683
testAccCheckAWSEIPExists(
67-
"aws_eip.bar.0", &a),
84+
"aws_eip.test.0", &a),
6885
testAccCheckAWSEIPAssociationExists(
6986
"aws_eip_association.by_allocation_id", &a),
7087
testAccCheckAWSEIPExists(
71-
"aws_eip.bar.1", &a),
88+
"aws_eip.test.1", &a),
7289
testAccCheckAWSEIPAssociationExists(
7390
"aws_eip_association.by_public_ip", &a),
7491
testAccCheckAWSEIPExists(
75-
"aws_eip.bar.2", &a),
92+
"aws_eip.test.2", &a),
7693
testAccCheckAWSEIPAssociationExists(
7794
"aws_eip_association.to_eni", &a),
7895
),
7996
},
97+
{
98+
ResourceName: resourceName,
99+
ImportState: true,
100+
ImportStateVerify: true,
101+
},
80102
},
81103
})
82104
}
83105

84106
func TestAccAWSEIPAssociation_ec2Classic(t *testing.T) {
85107
var a ec2.Address
108+
resourceName := "aws_eip_association.test"
86109

87110
oldvar := os.Getenv("AWS_DEFAULT_REGION")
88111
os.Setenv("AWS_DEFAULT_REGION", "us-east-1")
@@ -97,19 +120,25 @@ func TestAccAWSEIPAssociation_ec2Classic(t *testing.T) {
97120
Config: testAccAWSEIPAssociationConfig_ec2Classic,
98121
Check: resource.ComposeTestCheckFunc(
99122
testAccCheckAWSEIPExists("aws_eip.test", &a),
100-
testAccCheckAWSEIPAssociationExists("aws_eip_association.test", &a),
101-
resource.TestCheckResourceAttrSet("aws_eip_association.test", "public_ip"),
102-
resource.TestCheckResourceAttr("aws_eip_association.test", "allocation_id", ""),
103-
testAccCheckAWSEIPAssociationHasIpBasedId("aws_eip_association.test", &a),
123+
testAccCheckAWSEIPAssociationExists(resourceName, &a),
124+
resource.TestCheckResourceAttrSet(resourceName, "public_ip"),
125+
resource.TestCheckResourceAttr(resourceName, "allocation_id", ""),
126+
testAccCheckAWSEIPAssociationHasIpBasedId(resourceName, &a),
104127
),
105128
},
129+
{
130+
ResourceName: resourceName,
131+
ImportState: true,
132+
ImportStateVerify: true,
133+
},
106134
},
107135
})
108136
}
109137

110138
func TestAccAWSEIPAssociation_spotInstance(t *testing.T) {
111139
var a ec2.Address
112140
rInt := acctest.RandInt()
141+
resourceName := "aws_eip_association.test"
113142

114143
resource.ParallelTest(t, resource.TestCase{
115144
PreCheck: func() { testAccPreCheck(t) },
@@ -120,11 +149,16 @@ func TestAccAWSEIPAssociation_spotInstance(t *testing.T) {
120149
Config: testAccAWSEIPAssociationConfig_spotInstance(rInt),
121150
Check: resource.ComposeTestCheckFunc(
122151
testAccCheckAWSEIPExists("aws_eip.test", &a),
123-
testAccCheckAWSEIPAssociationExists("aws_eip_association.test", &a),
124-
resource.TestCheckResourceAttrSet("aws_eip_association.test", "allocation_id"),
125-
resource.TestCheckResourceAttrSet("aws_eip_association.test", "instance_id"),
152+
testAccCheckAWSEIPAssociationExists(resourceName, &a),
153+
resource.TestCheckResourceAttrSet(resourceName, "allocation_id"),
154+
resource.TestCheckResourceAttrSet(resourceName, "instance_id"),
126155
),
127156
},
157+
{
158+
ResourceName: resourceName,
159+
ImportState: true,
160+
ImportStateVerify: true,
161+
},
128162
},
129163
})
130164
}
@@ -248,61 +282,90 @@ func testAccCheckAWSEIPAssociationDestroy(s *terraform.State) error {
248282
}
249283

250284
const testAccAWSEIPAssociationConfig = `
251-
resource "aws_vpc" "main" {
285+
data "aws_availability_zones" "available" {
286+
state = "available"
287+
}
288+
289+
resource "aws_vpc" "test" {
252290
cidr_block = "192.168.0.0/24"
253291
tags = {
254292
Name = "terraform-testacc-eip-association"
255293
}
256294
}
257-
resource "aws_subnet" "sub" {
258-
vpc_id = "${aws_vpc.main.id}"
295+
296+
data "aws_ami" "amzn-ami-minimal-pv" {
297+
most_recent = true
298+
owners = ["amazon"]
299+
300+
filter {
301+
name = "name"
302+
values = ["amzn-ami-minimal-pv-*"]
303+
}
304+
}
305+
306+
resource "aws_subnet" "test" {
307+
vpc_id = "${aws_vpc.test.id}"
259308
cidr_block = "192.168.0.0/25"
260-
availability_zone = "us-west-2a"
309+
availability_zone = "${data.aws_availability_zones.available.names[0]}"
261310
tags = {
262311
Name = "tf-acc-eip-association"
263312
}
264313
}
265-
resource "aws_internet_gateway" "igw" {
266-
vpc_id = "${aws_vpc.main.id}"
314+
resource "aws_internet_gateway" "test" {
315+
vpc_id = "${aws_vpc.test.id}"
267316
}
268-
resource "aws_instance" "foo" {
317+
resource "aws_instance" "test" {
269318
count = 2
270-
ami = "ami-21f78e11"
271-
availability_zone = "us-west-2a"
272-
instance_type = "t1.micro"
273-
subnet_id = "${aws_subnet.sub.id}"
319+
ami = "${data.aws_ami.amzn-ami-minimal-pv.id}"
320+
availability_zone = "${data.aws_availability_zones.available.names[0]}"
321+
instance_type = "m1.small"
322+
subnet_id = "${aws_subnet.test.id}"
274323
private_ip = "192.168.0.${count.index+10}"
275324
}
276-
resource "aws_eip" "bar" {
325+
resource "aws_eip" "test" {
277326
count = 3
278327
vpc = true
279328
}
280329
resource "aws_eip_association" "by_allocation_id" {
281-
allocation_id = "${aws_eip.bar.0.id}"
282-
instance_id = "${aws_instance.foo.0.id}"
283-
depends_on = ["aws_instance.foo"]
330+
allocation_id = "${aws_eip.test.0.id}"
331+
instance_id = "${aws_instance.test.0.id}"
332+
depends_on = ["aws_instance.test"]
284333
}
285334
resource "aws_eip_association" "by_public_ip" {
286-
public_ip = "${aws_eip.bar.1.public_ip}"
287-
instance_id = "${aws_instance.foo.1.id}"
288-
depends_on = ["aws_instance.foo"]
335+
public_ip = "${aws_eip.test.1.public_ip}"
336+
instance_id = "${aws_instance.test.1.id}"
337+
depends_on = ["aws_instance.test"]
289338
}
290339
resource "aws_eip_association" "to_eni" {
291-
allocation_id = "${aws_eip.bar.2.id}"
292-
network_interface_id = "${aws_network_interface.baz.id}"
340+
allocation_id = "${aws_eip.test.2.id}"
341+
network_interface_id = "${aws_network_interface.test.id}"
293342
}
294-
resource "aws_network_interface" "baz" {
295-
subnet_id = "${aws_subnet.sub.id}"
343+
resource "aws_network_interface" "test" {
344+
subnet_id = "${aws_subnet.test.id}"
296345
private_ips = ["192.168.0.50"]
297-
depends_on = ["aws_instance.foo"]
346+
depends_on = ["aws_instance.test"]
298347
attachment {
299-
instance = "${aws_instance.foo.0.id}"
348+
instance = "${aws_instance.test.0.id}"
300349
device_index = 1
301350
}
302351
}
303352
`
304353

305354
const testAccAWSEIPAssociationConfigDisappears = `
355+
data "aws_availability_zones" "available" {
356+
state = "available"
357+
}
358+
359+
data "aws_ami" "amzn-ami-minimal-pv" {
360+
most_recent = true
361+
owners = ["amazon"]
362+
363+
filter {
364+
name = "name"
365+
values = ["amzn-ami-minimal-pv-*"]
366+
}
367+
}
368+
306369
resource "aws_vpc" "main" {
307370
cidr_block = "192.168.0.0/24"
308371
tags = {
@@ -312,7 +375,7 @@ resource "aws_vpc" "main" {
312375
resource "aws_subnet" "sub" {
313376
vpc_id = "${aws_vpc.main.id}"
314377
cidr_block = "192.168.0.0/25"
315-
availability_zone = "us-west-2a"
378+
availability_zone = "${data.aws_availability_zones.available.names[0]}"
316379
tags = {
317380
Name = "tf-acc-eip-association-disappears"
318381
}
@@ -321,9 +384,9 @@ resource "aws_internet_gateway" "igw" {
321384
vpc_id = "${aws_vpc.main.id}"
322385
}
323386
resource "aws_instance" "foo" {
324-
ami = "ami-21f78e11"
325-
availability_zone = "us-west-2a"
326-
instance_type = "t1.micro"
387+
ami = "${data.aws_ami.amzn-ami-minimal-pv.id}"
388+
availability_zone = "${data.aws_availability_zones.available.names[0]}"
389+
instance_type = "m1.small"
327390
subnet_id = "${aws_subnet.sub.id}"
328391
}
329392
resource "aws_eip" "bar" {
@@ -341,7 +404,9 @@ provider "aws" {
341404
342405
resource "aws_eip" "test" {}
343406
344-
data "aws_availability_zones" "available" {}
407+
data "aws_availability_zones" "available" {
408+
state = "available"
409+
}
345410
346411
data "aws_ami" "ubuntu" {
347412
most_recent = true

0 commit comments

Comments
 (0)