github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/aws/resource_aws_vpc_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 TestAccAWSVpc_basic(t *testing.T) {
    15  	var vpc ec2.Vpc
    16  
    17  	resource.Test(t, resource.TestCase{
    18  		PreCheck:     func() { testAccPreCheck(t) },
    19  		Providers:    testAccProviders,
    20  		CheckDestroy: testAccCheckVpcDestroy,
    21  		Steps: []resource.TestStep{
    22  			{
    23  				Config: testAccVpcConfig,
    24  				Check: resource.ComposeTestCheckFunc(
    25  					testAccCheckVpcExists("aws_vpc.foo", &vpc),
    26  					testAccCheckVpcCidr(&vpc, "10.1.0.0/16"),
    27  					resource.TestCheckResourceAttr(
    28  						"aws_vpc.foo", "cidr_block", "10.1.0.0/16"),
    29  					resource.TestCheckResourceAttrSet(
    30  						"aws_vpc.foo", "default_route_table_id"),
    31  					resource.TestCheckResourceAttr(
    32  						"aws_vpc.foo", "enable_dns_support", "true"),
    33  				),
    34  			},
    35  		},
    36  	})
    37  }
    38  
    39  func TestAccAWSVpc_enableIpv6(t *testing.T) {
    40  	var vpc ec2.Vpc
    41  
    42  	resource.Test(t, resource.TestCase{
    43  		PreCheck:     func() { testAccPreCheck(t) },
    44  		Providers:    testAccProviders,
    45  		CheckDestroy: testAccCheckVpcDestroy,
    46  		Steps: []resource.TestStep{
    47  			{
    48  				Config: testAccVpcConfigIpv6Enabled,
    49  				Check: resource.ComposeTestCheckFunc(
    50  					testAccCheckVpcExists("aws_vpc.foo", &vpc),
    51  					testAccCheckVpcCidr(&vpc, "10.1.0.0/16"),
    52  					resource.TestCheckResourceAttr(
    53  						"aws_vpc.foo", "cidr_block", "10.1.0.0/16"),
    54  					resource.TestCheckResourceAttrSet(
    55  						"aws_vpc.foo", "ipv6_association_id"),
    56  					resource.TestCheckResourceAttrSet(
    57  						"aws_vpc.foo", "ipv6_cidr_block"),
    58  				),
    59  			},
    60  		},
    61  	})
    62  }
    63  
    64  func TestAccAWSVpc_dedicatedTenancy(t *testing.T) {
    65  	var vpc ec2.Vpc
    66  
    67  	resource.Test(t, resource.TestCase{
    68  		PreCheck:     func() { testAccPreCheck(t) },
    69  		Providers:    testAccProviders,
    70  		CheckDestroy: testAccCheckVpcDestroy,
    71  		Steps: []resource.TestStep{
    72  			{
    73  				Config: testAccVpcDedicatedConfig,
    74  				Check: resource.ComposeTestCheckFunc(
    75  					testAccCheckVpcExists("aws_vpc.bar", &vpc),
    76  					resource.TestCheckResourceAttr(
    77  						"aws_vpc.bar", "instance_tenancy", "dedicated"),
    78  				),
    79  			},
    80  		},
    81  	})
    82  }
    83  
    84  func TestAccAWSVpc_tags(t *testing.T) {
    85  	var vpc ec2.Vpc
    86  
    87  	resource.Test(t, resource.TestCase{
    88  		PreCheck:     func() { testAccPreCheck(t) },
    89  		Providers:    testAccProviders,
    90  		CheckDestroy: testAccCheckVpcDestroy,
    91  		Steps: []resource.TestStep{
    92  			{
    93  				Config: testAccVpcConfigTags,
    94  				Check: resource.ComposeTestCheckFunc(
    95  					testAccCheckVpcExists("aws_vpc.foo", &vpc),
    96  					testAccCheckVpcCidr(&vpc, "10.1.0.0/16"),
    97  					resource.TestCheckResourceAttr(
    98  						"aws_vpc.foo", "cidr_block", "10.1.0.0/16"),
    99  					testAccCheckTags(&vpc.Tags, "foo", "bar"),
   100  				),
   101  			},
   102  
   103  			{
   104  				Config: testAccVpcConfigTagsUpdate,
   105  				Check: resource.ComposeTestCheckFunc(
   106  					testAccCheckVpcExists("aws_vpc.foo", &vpc),
   107  					testAccCheckTags(&vpc.Tags, "foo", ""),
   108  					testAccCheckTags(&vpc.Tags, "bar", "baz"),
   109  				),
   110  			},
   111  		},
   112  	})
   113  }
   114  
   115  func TestAccAWSVpc_update(t *testing.T) {
   116  	var vpc ec2.Vpc
   117  
   118  	resource.Test(t, resource.TestCase{
   119  		PreCheck:     func() { testAccPreCheck(t) },
   120  		Providers:    testAccProviders,
   121  		CheckDestroy: testAccCheckVpcDestroy,
   122  		Steps: []resource.TestStep{
   123  			{
   124  				Config: testAccVpcConfig,
   125  				Check: resource.ComposeTestCheckFunc(
   126  					testAccCheckVpcExists("aws_vpc.foo", &vpc),
   127  					testAccCheckVpcCidr(&vpc, "10.1.0.0/16"),
   128  					resource.TestCheckResourceAttr(
   129  						"aws_vpc.foo", "cidr_block", "10.1.0.0/16"),
   130  				),
   131  			},
   132  			{
   133  				Config: testAccVpcConfigUpdate,
   134  				Check: resource.ComposeTestCheckFunc(
   135  					testAccCheckVpcExists("aws_vpc.foo", &vpc),
   136  					resource.TestCheckResourceAttr(
   137  						"aws_vpc.foo", "enable_dns_hostnames", "true"),
   138  				),
   139  			},
   140  		},
   141  	})
   142  }
   143  
   144  func testAccCheckVpcDestroy(s *terraform.State) error {
   145  	conn := testAccProvider.Meta().(*AWSClient).ec2conn
   146  
   147  	for _, rs := range s.RootModule().Resources {
   148  		if rs.Type != "aws_vpc" {
   149  			continue
   150  		}
   151  
   152  		// Try to find the VPC
   153  		DescribeVpcOpts := &ec2.DescribeVpcsInput{
   154  			VpcIds: []*string{aws.String(rs.Primary.ID)},
   155  		}
   156  		resp, err := conn.DescribeVpcs(DescribeVpcOpts)
   157  		if err == nil {
   158  			if len(resp.Vpcs) > 0 {
   159  				return fmt.Errorf("VPCs still exist.")
   160  			}
   161  
   162  			return nil
   163  		}
   164  
   165  		// Verify the error is what we want
   166  		ec2err, ok := err.(awserr.Error)
   167  		if !ok {
   168  			return err
   169  		}
   170  		if ec2err.Code() != "InvalidVpcID.NotFound" {
   171  			return err
   172  		}
   173  	}
   174  
   175  	return nil
   176  }
   177  
   178  func testAccCheckVpcCidr(vpc *ec2.Vpc, expected string) resource.TestCheckFunc {
   179  	return func(s *terraform.State) error {
   180  		CIDRBlock := vpc.CidrBlock
   181  		if *CIDRBlock != expected {
   182  			return fmt.Errorf("Bad cidr: %s", *vpc.CidrBlock)
   183  		}
   184  
   185  		return nil
   186  	}
   187  }
   188  
   189  func testAccCheckVpcExists(n string, vpc *ec2.Vpc) resource.TestCheckFunc {
   190  	return func(s *terraform.State) error {
   191  		rs, ok := s.RootModule().Resources[n]
   192  		if !ok {
   193  			return fmt.Errorf("Not found: %s", n)
   194  		}
   195  
   196  		if rs.Primary.ID == "" {
   197  			return fmt.Errorf("No VPC ID is set")
   198  		}
   199  
   200  		conn := testAccProvider.Meta().(*AWSClient).ec2conn
   201  		DescribeVpcOpts := &ec2.DescribeVpcsInput{
   202  			VpcIds: []*string{aws.String(rs.Primary.ID)},
   203  		}
   204  		resp, err := conn.DescribeVpcs(DescribeVpcOpts)
   205  		if err != nil {
   206  			return err
   207  		}
   208  		if len(resp.Vpcs) == 0 {
   209  			return fmt.Errorf("VPC not found")
   210  		}
   211  
   212  		*vpc = *resp.Vpcs[0]
   213  
   214  		return nil
   215  	}
   216  }
   217  
   218  // https://github.com/hashicorp/terraform/issues/1301
   219  func TestAccAWSVpc_bothDnsOptionsSet(t *testing.T) {
   220  	resource.Test(t, resource.TestCase{
   221  		PreCheck:     func() { testAccPreCheck(t) },
   222  		Providers:    testAccProviders,
   223  		CheckDestroy: testAccCheckVpcDestroy,
   224  		Steps: []resource.TestStep{
   225  			{
   226  				Config: testAccVpcConfig_BothDnsOptions,
   227  				Check: resource.ComposeTestCheckFunc(
   228  					resource.TestCheckResourceAttr(
   229  						"aws_vpc.bar", "enable_dns_hostnames", "true"),
   230  					resource.TestCheckResourceAttr(
   231  						"aws_vpc.bar", "enable_dns_support", "true"),
   232  				),
   233  			},
   234  		},
   235  	})
   236  }
   237  
   238  // https://github.com/hashicorp/terraform/issues/10168
   239  func TestAccAWSVpc_DisabledDnsSupport(t *testing.T) {
   240  	resource.Test(t, resource.TestCase{
   241  		PreCheck:     func() { testAccPreCheck(t) },
   242  		Providers:    testAccProviders,
   243  		CheckDestroy: testAccCheckVpcDestroy,
   244  		Steps: []resource.TestStep{
   245  			{
   246  				Config: testAccVpcConfig_DisabledDnsSupport,
   247  				Check: resource.ComposeTestCheckFunc(
   248  					resource.TestCheckResourceAttr(
   249  						"aws_vpc.bar", "enable_dns_support", "false"),
   250  				),
   251  			},
   252  		},
   253  	})
   254  }
   255  
   256  func TestAccAWSVpc_classiclinkOptionSet(t *testing.T) {
   257  	resource.Test(t, resource.TestCase{
   258  		PreCheck:     func() { testAccPreCheck(t) },
   259  		Providers:    testAccProviders,
   260  		CheckDestroy: testAccCheckVpcDestroy,
   261  		Steps: []resource.TestStep{
   262  			{
   263  				Config: testAccVpcConfig_ClassiclinkOption,
   264  				Check: resource.ComposeTestCheckFunc(
   265  					resource.TestCheckResourceAttr(
   266  						"aws_vpc.bar", "enable_classiclink", "true"),
   267  				),
   268  			},
   269  		},
   270  	})
   271  }
   272  
   273  const testAccVpcConfig = `
   274  resource "aws_vpc" "foo" {
   275  	cidr_block = "10.1.0.0/16"
   276  }
   277  `
   278  
   279  const testAccVpcConfigIpv6Enabled = `
   280  resource "aws_vpc" "foo" {
   281  	cidr_block = "10.1.0.0/16"
   282  	assign_generated_ipv6_cidr_block = true
   283  }
   284  `
   285  
   286  const testAccVpcConfigUpdate = `
   287  resource "aws_vpc" "foo" {
   288  	cidr_block = "10.1.0.0/16"
   289  	enable_dns_hostnames = true
   290  }
   291  `
   292  
   293  const testAccVpcConfigTags = `
   294  resource "aws_vpc" "foo" {
   295  	cidr_block = "10.1.0.0/16"
   296  
   297  	tags {
   298  		foo = "bar"
   299  	}
   300  }
   301  `
   302  
   303  const testAccVpcConfigTagsUpdate = `
   304  resource "aws_vpc" "foo" {
   305  	cidr_block = "10.1.0.0/16"
   306  
   307  	tags {
   308  		bar = "baz"
   309  	}
   310  }
   311  `
   312  const testAccVpcDedicatedConfig = `
   313  resource "aws_vpc" "bar" {
   314  	instance_tenancy = "dedicated"
   315  
   316  	cidr_block = "10.2.0.0/16"
   317  }
   318  `
   319  
   320  const testAccVpcConfig_BothDnsOptions = `
   321  provider "aws" {
   322  	region = "eu-central-1"
   323  }
   324  
   325  resource "aws_vpc" "bar" {
   326  	cidr_block = "10.2.0.0/16"
   327  
   328  	enable_dns_hostnames = true
   329  	enable_dns_support = true
   330  }
   331  `
   332  
   333  const testAccVpcConfig_DisabledDnsSupport = `
   334  provider "aws" {
   335  	region = "us-west-2"
   336  }
   337  
   338  resource "aws_vpc" "bar" {
   339  	cidr_block = "10.2.0.0/16"
   340  
   341  	enable_dns_support = false
   342  }
   343  `
   344  
   345  const testAccVpcConfig_ClassiclinkOption = `
   346  resource "aws_vpc" "bar" {
   347  	cidr_block = "172.2.0.0/16"
   348  
   349  	enable_classiclink = true
   350  }
   351  `