github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/aws/resource_aws_network_interface_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/aws/aws-sdk-go/aws" 8 "github.com/aws/aws-sdk-go/aws/awserr" 9 "github.com/aws/aws-sdk-go/service/ec2" 10 "github.com/hashicorp/terraform/helper/resource" 11 "github.com/hashicorp/terraform/terraform" 12 ) 13 14 func TestAccAWSENI_basic(t *testing.T) { 15 var conf ec2.NetworkInterface 16 17 resource.Test(t, resource.TestCase{ 18 PreCheck: func() { testAccPreCheck(t) }, 19 Providers: testAccProviders, 20 CheckDestroy: testAccCheckAWSENIDestroy, 21 Steps: []resource.TestStep{ 22 resource.TestStep{ 23 Config: testAccAWSENIConfig, 24 Check: resource.ComposeTestCheckFunc( 25 testAccCheckAWSENIExists("aws_network_interface.bar", &conf), 26 testAccCheckAWSENIAttributes(&conf), 27 resource.TestCheckResourceAttr( 28 "aws_network_interface.bar", "private_ips.#", "1"), 29 resource.TestCheckResourceAttr( 30 "aws_network_interface.bar", "tags.Name", "bar_interface"), 31 ), 32 }, 33 }, 34 }) 35 } 36 37 func TestAccAWSENI_attached(t *testing.T) { 38 var conf ec2.NetworkInterface 39 40 resource.Test(t, resource.TestCase{ 41 PreCheck: func() { testAccPreCheck(t) }, 42 Providers: testAccProviders, 43 CheckDestroy: testAccCheckAWSENIDestroy, 44 Steps: []resource.TestStep{ 45 resource.TestStep{ 46 Config: testAccAWSENIConfigWithAttachment, 47 Check: resource.ComposeTestCheckFunc( 48 testAccCheckAWSENIExists("aws_network_interface.bar", &conf), 49 testAccCheckAWSENIAttributesWithAttachment(&conf), 50 resource.TestCheckResourceAttr( 51 "aws_network_interface.bar", "private_ips.#", "1"), 52 resource.TestCheckResourceAttr( 53 "aws_network_interface.bar", "tags.Name", "bar_interface"), 54 ), 55 }, 56 }, 57 }) 58 } 59 60 func TestAccAWSENI_ignoreExternalAttachment(t *testing.T) { 61 var conf ec2.NetworkInterface 62 63 resource.Test(t, resource.TestCase{ 64 PreCheck: func() { testAccPreCheck(t) }, 65 Providers: testAccProviders, 66 CheckDestroy: testAccCheckAWSENIDestroy, 67 Steps: []resource.TestStep{ 68 resource.TestStep{ 69 Config: testAccAWSENIConfigExternalAttachment, 70 Check: resource.ComposeTestCheckFunc( 71 testAccCheckAWSENIExists("aws_network_interface.bar", &conf), 72 testAccCheckAWSENIAttributes(&conf), 73 testAccCheckAWSENIMakeExternalAttachment("aws_instance.foo", &conf), 74 ), 75 }, 76 }, 77 }) 78 } 79 80 func TestAccAWSENI_sourceDestCheck(t *testing.T) { 81 var conf ec2.NetworkInterface 82 83 resource.Test(t, resource.TestCase{ 84 PreCheck: func() { testAccPreCheck(t) }, 85 Providers: testAccProviders, 86 CheckDestroy: testAccCheckAWSENIDestroy, 87 Steps: []resource.TestStep{ 88 resource.TestStep{ 89 Config: testAccAWSENIConfigWithSourceDestCheck, 90 Check: resource.ComposeTestCheckFunc( 91 testAccCheckAWSENIExists("aws_network_interface.bar", &conf), 92 resource.TestCheckResourceAttr( 93 "aws_network_interface.bar", "source_dest_check", "false"), 94 ), 95 }, 96 }, 97 }) 98 } 99 100 func TestAccAWSENI_computedIPs(t *testing.T) { 101 var conf ec2.NetworkInterface 102 103 resource.Test(t, resource.TestCase{ 104 PreCheck: func() { testAccPreCheck(t) }, 105 Providers: testAccProviders, 106 CheckDestroy: testAccCheckAWSENIDestroy, 107 Steps: []resource.TestStep{ 108 resource.TestStep{ 109 Config: testAccAWSENIConfigWithNoPrivateIPs, 110 Check: resource.ComposeTestCheckFunc( 111 testAccCheckAWSENIExists("aws_network_interface.bar", &conf), 112 resource.TestCheckResourceAttr( 113 "aws_network_interface.bar", "private_ips.#", "1"), 114 ), 115 }, 116 }, 117 }) 118 } 119 120 func testAccCheckAWSENIExists(n string, res *ec2.NetworkInterface) resource.TestCheckFunc { 121 return func(s *terraform.State) error { 122 rs, ok := s.RootModule().Resources[n] 123 if !ok { 124 return fmt.Errorf("Not found: %s", n) 125 } 126 127 if rs.Primary.ID == "" { 128 return fmt.Errorf("No ENI ID is set") 129 } 130 131 conn := testAccProvider.Meta().(*AWSClient).ec2conn 132 describe_network_interfaces_request := &ec2.DescribeNetworkInterfacesInput{ 133 NetworkInterfaceIds: []*string{aws.String(rs.Primary.ID)}, 134 } 135 describeResp, err := conn.DescribeNetworkInterfaces(describe_network_interfaces_request) 136 137 if err != nil { 138 return err 139 } 140 141 if len(describeResp.NetworkInterfaces) != 1 || 142 *describeResp.NetworkInterfaces[0].NetworkInterfaceId != rs.Primary.ID { 143 return fmt.Errorf("ENI not found") 144 } 145 146 *res = *describeResp.NetworkInterfaces[0] 147 148 return nil 149 } 150 } 151 152 func testAccCheckAWSENIAttributes(conf *ec2.NetworkInterface) resource.TestCheckFunc { 153 return func(s *terraform.State) error { 154 155 if conf.Attachment != nil { 156 return fmt.Errorf("expected attachment to be nil") 157 } 158 159 if *conf.AvailabilityZone != "us-west-2a" { 160 return fmt.Errorf("expected availability_zone to be us-west-2a, but was %s", *conf.AvailabilityZone) 161 } 162 163 if len(conf.Groups) != 1 && *conf.Groups[0].GroupName != "foo" { 164 return fmt.Errorf("expected security group to be foo, but was %#v", conf.Groups) 165 } 166 167 if *conf.PrivateIpAddress != "172.16.10.100" { 168 return fmt.Errorf("expected private ip to be 172.16.10.100, but was %s", *conf.PrivateIpAddress) 169 } 170 171 if *conf.SourceDestCheck != true { 172 return fmt.Errorf("expected source_dest_check to be true, but was %t", *conf.SourceDestCheck) 173 } 174 175 if len(conf.TagSet) == 0 { 176 return fmt.Errorf("expected tags") 177 } 178 179 return nil 180 } 181 } 182 183 func testAccCheckAWSENIAttributesWithAttachment(conf *ec2.NetworkInterface) resource.TestCheckFunc { 184 return func(s *terraform.State) error { 185 186 if conf.Attachment == nil { 187 return fmt.Errorf("expected attachment to be set, but was nil") 188 } 189 190 if *conf.Attachment.DeviceIndex != 1 { 191 return fmt.Errorf("expected attachment device index to be 1, but was %d", *conf.Attachment.DeviceIndex) 192 } 193 194 if *conf.AvailabilityZone != "us-west-2a" { 195 return fmt.Errorf("expected availability_zone to be us-west-2a, but was %s", *conf.AvailabilityZone) 196 } 197 198 if len(conf.Groups) != 1 && *conf.Groups[0].GroupName != "foo" { 199 return fmt.Errorf("expected security group to be foo, but was %#v", conf.Groups) 200 } 201 202 if *conf.PrivateIpAddress != "172.16.10.100" { 203 return fmt.Errorf("expected private ip to be 172.16.10.100, but was %s", *conf.PrivateIpAddress) 204 } 205 206 return nil 207 } 208 } 209 210 func testAccCheckAWSENIDestroy(s *terraform.State) error { 211 for _, rs := range s.RootModule().Resources { 212 if rs.Type != "aws_network_interface" { 213 continue 214 } 215 216 conn := testAccProvider.Meta().(*AWSClient).ec2conn 217 describe_network_interfaces_request := &ec2.DescribeNetworkInterfacesInput{ 218 NetworkInterfaceIds: []*string{aws.String(rs.Primary.ID)}, 219 } 220 _, err := conn.DescribeNetworkInterfaces(describe_network_interfaces_request) 221 222 if err != nil { 223 if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidNetworkInterfaceID.NotFound" { 224 return nil 225 } 226 227 return err 228 } 229 } 230 231 return nil 232 } 233 234 func testAccCheckAWSENIMakeExternalAttachment(n string, conf *ec2.NetworkInterface) resource.TestCheckFunc { 235 return func(s *terraform.State) error { 236 rs, ok := s.RootModule().Resources[n] 237 if !ok || rs.Primary.ID == "" { 238 return fmt.Errorf("Not found: %s", n) 239 } 240 attach_request := &ec2.AttachNetworkInterfaceInput{ 241 DeviceIndex: aws.Int64(2), 242 InstanceId: aws.String(rs.Primary.ID), 243 NetworkInterfaceId: conf.NetworkInterfaceId, 244 } 245 conn := testAccProvider.Meta().(*AWSClient).ec2conn 246 _, attach_err := conn.AttachNetworkInterface(attach_request) 247 if attach_err != nil { 248 return fmt.Errorf("Error attaching ENI: %s", attach_err) 249 } 250 return nil 251 } 252 } 253 254 const testAccAWSENIConfig = ` 255 resource "aws_vpc" "foo" { 256 cidr_block = "172.16.0.0/16" 257 } 258 259 resource "aws_subnet" "foo" { 260 vpc_id = "${aws_vpc.foo.id}" 261 cidr_block = "172.16.10.0/24" 262 availability_zone = "us-west-2a" 263 } 264 265 resource "aws_security_group" "foo" { 266 vpc_id = "${aws_vpc.foo.id}" 267 description = "foo" 268 name = "foo" 269 270 egress { 271 from_port = 0 272 to_port = 0 273 protocol = "tcp" 274 cidr_blocks = ["10.0.0.0/16"] 275 } 276 } 277 278 resource "aws_network_interface" "bar" { 279 subnet_id = "${aws_subnet.foo.id}" 280 private_ips = ["172.16.10.100"] 281 security_groups = ["${aws_security_group.foo.id}"] 282 tags { 283 Name = "bar_interface" 284 } 285 } 286 ` 287 288 const testAccAWSENIConfigWithSourceDestCheck = ` 289 resource "aws_vpc" "foo" { 290 cidr_block = "172.16.0.0/16" 291 } 292 293 resource "aws_subnet" "foo" { 294 vpc_id = "${aws_vpc.foo.id}" 295 cidr_block = "172.16.10.0/24" 296 availability_zone = "us-west-2a" 297 } 298 299 resource "aws_network_interface" "bar" { 300 subnet_id = "${aws_subnet.foo.id}" 301 source_dest_check = false 302 private_ips = ["172.16.10.100"] 303 } 304 ` 305 306 const testAccAWSENIConfigWithNoPrivateIPs = ` 307 resource "aws_vpc" "foo" { 308 cidr_block = "172.16.0.0/16" 309 } 310 311 resource "aws_subnet" "foo" { 312 vpc_id = "${aws_vpc.foo.id}" 313 cidr_block = "172.16.10.0/24" 314 availability_zone = "us-west-2a" 315 } 316 317 resource "aws_network_interface" "bar" { 318 subnet_id = "${aws_subnet.foo.id}" 319 source_dest_check = false 320 } 321 ` 322 323 const testAccAWSENIConfigWithAttachment = ` 324 resource "aws_vpc" "foo" { 325 cidr_block = "172.16.0.0/16" 326 tags { 327 Name = "tf-eni-test" 328 } 329 } 330 331 resource "aws_subnet" "foo" { 332 vpc_id = "${aws_vpc.foo.id}" 333 cidr_block = "172.16.10.0/24" 334 availability_zone = "us-west-2a" 335 tags { 336 Name = "tf-eni-test" 337 } 338 } 339 340 resource "aws_subnet" "bar" { 341 vpc_id = "${aws_vpc.foo.id}" 342 cidr_block = "172.16.11.0/24" 343 availability_zone = "us-west-2a" 344 tags { 345 Name = "tf-eni-test" 346 } 347 } 348 349 resource "aws_security_group" "foo" { 350 vpc_id = "${aws_vpc.foo.id}" 351 description = "foo" 352 name = "foo" 353 } 354 355 resource "aws_instance" "foo" { 356 ami = "ami-c5eabbf5" 357 instance_type = "t2.micro" 358 subnet_id = "${aws_subnet.bar.id}" 359 associate_public_ip_address = false 360 private_ip = "172.16.11.50" 361 tags { 362 Name = "tf-eni-test" 363 } 364 } 365 366 resource "aws_network_interface" "bar" { 367 subnet_id = "${aws_subnet.foo.id}" 368 private_ips = ["172.16.10.100"] 369 security_groups = ["${aws_security_group.foo.id}"] 370 attachment { 371 instance = "${aws_instance.foo.id}" 372 device_index = 1 373 } 374 tags { 375 Name = "bar_interface" 376 } 377 } 378 ` 379 380 const testAccAWSENIConfigExternalAttachment = ` 381 resource "aws_vpc" "foo" { 382 cidr_block = "172.16.0.0/16" 383 tags { 384 Name = "tf-eni-test" 385 } 386 } 387 388 resource "aws_subnet" "foo" { 389 vpc_id = "${aws_vpc.foo.id}" 390 cidr_block = "172.16.10.0/24" 391 availability_zone = "us-west-2a" 392 tags { 393 Name = "tf-eni-test" 394 } 395 } 396 397 resource "aws_subnet" "bar" { 398 vpc_id = "${aws_vpc.foo.id}" 399 cidr_block = "172.16.11.0/24" 400 availability_zone = "us-west-2a" 401 tags { 402 Name = "tf-eni-test" 403 } 404 } 405 406 resource "aws_security_group" "foo" { 407 vpc_id = "${aws_vpc.foo.id}" 408 description = "foo" 409 name = "foo" 410 } 411 412 resource "aws_instance" "foo" { 413 ami = "ami-c5eabbf5" 414 instance_type = "t2.micro" 415 subnet_id = "${aws_subnet.bar.id}" 416 associate_public_ip_address = false 417 private_ip = "172.16.11.50" 418 tags { 419 Name = "tf-eni-test" 420 } 421 } 422 423 resource "aws_network_interface" "bar" { 424 subnet_id = "${aws_subnet.foo.id}" 425 private_ips = ["172.16.10.100"] 426 security_groups = ["${aws_security_group.foo.id}"] 427 tags { 428 Name = "bar_interface" 429 } 430 } 431 `