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