github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/aws/resource_aws_internet_gateway_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 TestAccAWSInternetGateway_basic(t *testing.T) {
    15  	var v, v2 ec2.InternetGateway
    16  
    17  	testNotEqual := func(*terraform.State) error {
    18  		if len(v.Attachments) == 0 {
    19  			return fmt.Errorf("IG A is not attached")
    20  		}
    21  		if len(v2.Attachments) == 0 {
    22  			return fmt.Errorf("IG B is not attached")
    23  		}
    24  
    25  		id1 := v.Attachments[0].VpcId
    26  		id2 := v2.Attachments[0].VpcId
    27  		if id1 == id2 {
    28  			return fmt.Errorf("Both attachment IDs are the same")
    29  		}
    30  
    31  		return nil
    32  	}
    33  
    34  	resource.Test(t, resource.TestCase{
    35  		PreCheck:     func() { testAccPreCheck(t) },
    36  		Providers:    testAccProviders,
    37  		CheckDestroy: testAccCheckInternetGatewayDestroy,
    38  		Steps: []resource.TestStep{
    39  			resource.TestStep{
    40  				Config: testAccInternetGatewayConfig,
    41  				Check: resource.ComposeTestCheckFunc(
    42  					testAccCheckInternetGatewayExists(
    43  						"aws_internet_gateway.foo", &v),
    44  				),
    45  			},
    46  
    47  			resource.TestStep{
    48  				Config: testAccInternetGatewayConfigChangeVPC,
    49  				Check: resource.ComposeTestCheckFunc(
    50  					testAccCheckInternetGatewayExists(
    51  						"aws_internet_gateway.foo", &v2),
    52  					testNotEqual,
    53  				),
    54  			},
    55  		},
    56  	})
    57  }
    58  
    59  func TestAccAWSInternetGateway_delete(t *testing.T) {
    60  	var ig ec2.InternetGateway
    61  
    62  	testDeleted := func(r string) resource.TestCheckFunc {
    63  		return func(s *terraform.State) error {
    64  			_, ok := s.RootModule().Resources[r]
    65  			if ok {
    66  				return fmt.Errorf("Internet Gateway %q should have been deleted", r)
    67  			}
    68  			return nil
    69  		}
    70  	}
    71  
    72  	resource.Test(t, resource.TestCase{
    73  		PreCheck:     func() { testAccPreCheck(t) },
    74  		Providers:    testAccProviders,
    75  		CheckDestroy: testAccCheckInternetGatewayDestroy,
    76  		Steps: []resource.TestStep{
    77  			resource.TestStep{
    78  				Config: testAccInternetGatewayConfig,
    79  				Check: resource.ComposeTestCheckFunc(
    80  					testAccCheckInternetGatewayExists("aws_internet_gateway.foo", &ig)),
    81  			},
    82  			resource.TestStep{
    83  				Config: testAccNoInternetGatewayConfig,
    84  				Check:  resource.ComposeTestCheckFunc(testDeleted("aws_internet_gateway.foo")),
    85  			},
    86  		},
    87  	})
    88  }
    89  
    90  func TestAccAWSInternetGateway_tags(t *testing.T) {
    91  	var v ec2.InternetGateway
    92  
    93  	resource.Test(t, resource.TestCase{
    94  		PreCheck:     func() { testAccPreCheck(t) },
    95  		Providers:    testAccProviders,
    96  		CheckDestroy: testAccCheckInternetGatewayDestroy,
    97  		Steps: []resource.TestStep{
    98  			resource.TestStep{
    99  				Config: testAccCheckInternetGatewayConfigTags,
   100  				Check: resource.ComposeTestCheckFunc(
   101  					testAccCheckInternetGatewayExists("aws_internet_gateway.foo", &v),
   102  					testAccCheckTags(&v.Tags, "foo", "bar"),
   103  				),
   104  			},
   105  
   106  			resource.TestStep{
   107  				Config: testAccCheckInternetGatewayConfigTagsUpdate,
   108  				Check: resource.ComposeTestCheckFunc(
   109  					testAccCheckInternetGatewayExists("aws_internet_gateway.foo", &v),
   110  					testAccCheckTags(&v.Tags, "foo", ""),
   111  					testAccCheckTags(&v.Tags, "bar", "baz"),
   112  				),
   113  			},
   114  		},
   115  	})
   116  }
   117  
   118  func testAccCheckInternetGatewayDestroy(s *terraform.State) error {
   119  	conn := testAccProvider.Meta().(*AWSClient).ec2conn
   120  
   121  	for _, rs := range s.RootModule().Resources {
   122  		if rs.Type != "aws_internet_gateway" {
   123  			continue
   124  		}
   125  
   126  		// Try to find the resource
   127  		resp, err := conn.DescribeInternetGateways(&ec2.DescribeInternetGatewaysInput{
   128  			InternetGatewayIds: []*string{aws.String(rs.Primary.ID)},
   129  		})
   130  		if err == nil {
   131  			if len(resp.InternetGateways) > 0 {
   132  				return fmt.Errorf("still exists")
   133  			}
   134  
   135  			return nil
   136  		}
   137  
   138  		// Verify the error is what we want
   139  		ec2err, ok := err.(awserr.Error)
   140  		if !ok {
   141  			return err
   142  		}
   143  		if ec2err.Code() != "InvalidInternetGatewayID.NotFound" {
   144  			return err
   145  		}
   146  	}
   147  
   148  	return nil
   149  }
   150  
   151  func testAccCheckInternetGatewayExists(n string, ig *ec2.InternetGateway) resource.TestCheckFunc {
   152  	return func(s *terraform.State) error {
   153  		rs, ok := s.RootModule().Resources[n]
   154  		if !ok {
   155  			return fmt.Errorf("Not found: %s", n)
   156  		}
   157  
   158  		if rs.Primary.ID == "" {
   159  			return fmt.Errorf("No ID is set")
   160  		}
   161  
   162  		conn := testAccProvider.Meta().(*AWSClient).ec2conn
   163  		resp, err := conn.DescribeInternetGateways(&ec2.DescribeInternetGatewaysInput{
   164  			InternetGatewayIds: []*string{aws.String(rs.Primary.ID)},
   165  		})
   166  		if err != nil {
   167  			return err
   168  		}
   169  		if len(resp.InternetGateways) == 0 {
   170  			return fmt.Errorf("InternetGateway not found")
   171  		}
   172  
   173  		*ig = *resp.InternetGateways[0]
   174  
   175  		return nil
   176  	}
   177  }
   178  
   179  const testAccNoInternetGatewayConfig = `
   180  resource "aws_vpc" "foo" {
   181  	cidr_block = "10.1.0.0/16"
   182  }
   183  `
   184  
   185  const testAccInternetGatewayConfig = `
   186  resource "aws_vpc" "foo" {
   187  	cidr_block = "10.1.0.0/16"
   188  }
   189  
   190  resource "aws_internet_gateway" "foo" {
   191  	vpc_id = "${aws_vpc.foo.id}"
   192  }
   193  `
   194  
   195  const testAccInternetGatewayConfigChangeVPC = `
   196  resource "aws_vpc" "foo" {
   197  	cidr_block = "10.1.0.0/16"
   198  }
   199  
   200  resource "aws_vpc" "bar" {
   201  	cidr_block = "10.2.0.0/16"
   202  }
   203  
   204  resource "aws_internet_gateway" "foo" {
   205  	vpc_id = "${aws_vpc.bar.id}"
   206  }
   207  `
   208  
   209  const testAccCheckInternetGatewayConfigTags = `
   210  resource "aws_vpc" "foo" {
   211  	cidr_block = "10.1.0.0/16"
   212  }
   213  
   214  resource "aws_internet_gateway" "foo" {
   215  	vpc_id = "${aws_vpc.foo.id}"
   216  	tags {
   217  		foo = "bar"
   218  	}
   219  }
   220  `
   221  
   222  const testAccCheckInternetGatewayConfigTagsUpdate = `
   223  resource "aws_vpc" "foo" {
   224  	cidr_block = "10.1.0.0/16"
   225  }
   226  
   227  resource "aws_internet_gateway" "foo" {
   228  	vpc_id = "${aws_vpc.foo.id}"
   229  	tags {
   230  		bar = "baz"
   231  	}
   232  }
   233  `