github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/alicloud/resource_alicloud_vpc_test.go (about)

     1  package alicloud
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/denverdino/aliyungo/common"
     8  	"github.com/denverdino/aliyungo/ecs"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  func TestAccAlicloudVpc_basic(t *testing.T) {
    14  	var vpc ecs.VpcSetType
    15  
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck: func() {
    18  			testAccPreCheck(t)
    19  		},
    20  
    21  		// module name
    22  		IDRefreshName: "alicloud_vpc.foo",
    23  		Providers:     testAccProviders,
    24  		CheckDestroy:  testAccCheckVpcDestroy,
    25  		Steps: []resource.TestStep{
    26  			resource.TestStep{
    27  				Config: testAccVpcConfig,
    28  				Check: resource.ComposeTestCheckFunc(
    29  					testAccCheckVpcExists("alicloud_vpc.foo", &vpc),
    30  					resource.TestCheckResourceAttr(
    31  						"alicloud_vpc.foo", "cidr_block", "172.16.0.0/12"),
    32  					resource.TestCheckResourceAttrSet(
    33  						"alicloud_vpc.foo", "router_id"),
    34  					resource.TestCheckResourceAttrSet(
    35  						"alicloud_vpc.foo", "router_table_id"),
    36  				),
    37  			},
    38  		},
    39  	})
    40  
    41  }
    42  
    43  func TestAccAlicloudVpc_update(t *testing.T) {
    44  	var vpc ecs.VpcSetType
    45  
    46  	resource.Test(t, resource.TestCase{
    47  		PreCheck: func() {
    48  			testAccPreCheck(t)
    49  		},
    50  		Providers:    testAccProviders,
    51  		CheckDestroy: testAccCheckVpcDestroy,
    52  		Steps: []resource.TestStep{
    53  			resource.TestStep{
    54  				Config: testAccVpcConfig,
    55  				Check: resource.ComposeTestCheckFunc(
    56  					testAccCheckVpcExists("alicloud_vpc.foo", &vpc),
    57  					resource.TestCheckResourceAttr(
    58  						"alicloud_vpc.foo", "cidr_block", "172.16.0.0/12"),
    59  				),
    60  			},
    61  			resource.TestStep{
    62  				Config: testAccVpcConfigUpdate,
    63  				Check: resource.ComposeTestCheckFunc(
    64  					testAccCheckVpcExists("alicloud_vpc.foo", &vpc),
    65  					resource.TestCheckResourceAttr(
    66  						"alicloud_vpc.foo", "name", "tf_test_bar"),
    67  				),
    68  			},
    69  		},
    70  	})
    71  }
    72  
    73  func testAccCheckVpcExists(n string, vpc *ecs.VpcSetType) resource.TestCheckFunc {
    74  	return func(s *terraform.State) error {
    75  		rs, ok := s.RootModule().Resources[n]
    76  		if !ok {
    77  			return fmt.Errorf("Not found: %s", n)
    78  		}
    79  
    80  		if rs.Primary.ID == "" {
    81  			return fmt.Errorf("No VPC ID is set")
    82  		}
    83  
    84  		client := testAccProvider.Meta().(*AliyunClient)
    85  		instance, err := client.DescribeVpc(rs.Primary.ID)
    86  
    87  		if err != nil {
    88  			return err
    89  		}
    90  		if instance == nil {
    91  			return fmt.Errorf("VPC not found")
    92  		}
    93  
    94  		*vpc = *instance
    95  		return nil
    96  	}
    97  }
    98  
    99  func testAccCheckVpcDestroy(s *terraform.State) error {
   100  	client := testAccProvider.Meta().(*AliyunClient)
   101  
   102  	for _, rs := range s.RootModule().Resources {
   103  		if rs.Type != "alicloud_vpc" {
   104  			continue
   105  		}
   106  
   107  		// Try to find the VPC
   108  		instance, err := client.DescribeVpc(rs.Primary.ID)
   109  
   110  		if instance != nil {
   111  			return fmt.Errorf("VPCs still exist")
   112  		}
   113  
   114  		if err != nil {
   115  			// Verify the error is what we want
   116  			e, _ := err.(*common.Error)
   117  
   118  			if e.ErrorResponse.Code != "InvalidVpcID.NotFound" {
   119  				return err
   120  			}
   121  		}
   122  
   123  	}
   124  
   125  	return nil
   126  }
   127  
   128  const testAccVpcConfig = `
   129  resource "alicloud_vpc" "foo" {
   130          name = "tf_test_foo"
   131          cidr_block = "172.16.0.0/12"
   132  }
   133  `
   134  
   135  const testAccVpcConfigUpdate = `
   136  resource "alicloud_vpc" "foo" {
   137  	cidr_block = "172.16.0.0/12"
   138  	name = "tf_test_bar"
   139  }
   140  `