github.com/chalford/terraform@v0.3.7-0.20150113080010-a78c69a8c81f/builtin/providers/aws/resource_aws_internet_gateway_test.go (about)

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