github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/alicloud/resource_alicloud_nat_gateway_test.go (about) 1 package alicloud 2 3 import ( 4 "fmt" 5 "github.com/denverdino/aliyungo/common" 6 "github.com/hashicorp/terraform/helper/resource" 7 "github.com/hashicorp/terraform/terraform" 8 "testing" 9 ) 10 11 func TestAccAlicloudNatGateway_basic(t *testing.T) { 12 var nat NatGatewaySetType 13 14 testCheck := func(*terraform.State) error { 15 if nat.BusinessStatus != "Normal" { 16 return fmt.Errorf("abnormal instance status") 17 } 18 19 if len(nat.BandwidthPackageIds.BandwidthPackageId) == 0 { 20 return fmt.Errorf("no bandwidth package: %#v", nat.BandwidthPackageIds.BandwidthPackageId) 21 } 22 23 return nil 24 } 25 26 resource.Test(t, resource.TestCase{ 27 PreCheck: func() { 28 testAccPreCheck(t) 29 }, 30 31 // module name 32 IDRefreshName: "alicloud_nat_gateway.foo", 33 Providers: testAccProviders, 34 CheckDestroy: testAccCheckNatGatewayDestroy, 35 Steps: []resource.TestStep{ 36 resource.TestStep{ 37 Config: testAccNatGatewayConfig, 38 Check: resource.ComposeTestCheckFunc( 39 testAccCheckNatGatewayExists( 40 "alicloud_nat_gateway.foo", &nat), 41 testCheck, 42 resource.TestCheckResourceAttr( 43 "alicloud_nat_gateway.foo", 44 "spec", 45 "Small"), 46 resource.TestCheckResourceAttr( 47 "alicloud_nat_gateway.foo", 48 "name", 49 "test_foo"), 50 ), 51 }, 52 }, 53 }) 54 55 } 56 57 func TestAccAlicloudNatGateway_spec(t *testing.T) { 58 var nat NatGatewaySetType 59 60 resource.Test(t, resource.TestCase{ 61 PreCheck: func() { 62 testAccPreCheck(t) 63 }, 64 65 // module name 66 IDRefreshName: "alicloud_nat_gateway.foo", 67 Providers: testAccProviders, 68 CheckDestroy: testAccCheckNatGatewayDestroy, 69 Steps: []resource.TestStep{ 70 resource.TestStep{ 71 Config: testAccNatGatewayConfigSpec, 72 Check: resource.ComposeTestCheckFunc( 73 testAccCheckNatGatewayExists( 74 "alicloud_nat_gateway.foo", &nat), 75 resource.TestCheckResourceAttr( 76 "alicloud_nat_gateway.foo", 77 "spec", 78 "Middle"), 79 ), 80 }, 81 82 resource.TestStep{ 83 Config: testAccNatGatewayConfigSpecUpgrade, 84 Check: resource.ComposeTestCheckFunc( 85 testAccCheckNatGatewayExists( 86 "alicloud_nat_gateway.foo", &nat), 87 resource.TestCheckResourceAttr( 88 "alicloud_nat_gateway.foo", 89 "spec", 90 "Large"), 91 ), 92 }, 93 }, 94 }) 95 96 } 97 98 func testAccCheckNatGatewayExists(n string, nat *NatGatewaySetType) resource.TestCheckFunc { 99 return func(s *terraform.State) error { 100 rs, ok := s.RootModule().Resources[n] 101 if !ok { 102 return fmt.Errorf("Not found: %s", n) 103 } 104 105 if rs.Primary.ID == "" { 106 return fmt.Errorf("No Gateway ID is set") 107 } 108 109 client := testAccProvider.Meta().(*AliyunClient) 110 instance, err := client.DescribeNatGateway(rs.Primary.ID) 111 112 if err != nil { 113 return err 114 } 115 if instance == nil { 116 return fmt.Errorf("Nat gateway not found") 117 } 118 119 *nat = *instance 120 return nil 121 } 122 } 123 124 func testAccCheckNatGatewayDestroy(s *terraform.State) error { 125 client := testAccProvider.Meta().(*AliyunClient) 126 127 for _, rs := range s.RootModule().Resources { 128 if rs.Type != "alicloud_nat_gateway" { 129 continue 130 } 131 132 // Try to find the Nat gateway 133 instance, err := client.DescribeNatGateway(rs.Primary.ID) 134 135 if instance != nil { 136 return fmt.Errorf("Nat gateway still exist") 137 } 138 139 if err != nil { 140 // Verify the error is what we want 141 e, _ := err.(*common.Error) 142 143 if !notFoundError(e) { 144 return err 145 } 146 } 147 148 } 149 150 return nil 151 } 152 153 const testAccNatGatewayConfig = ` 154 resource "alicloud_vpc" "foo" { 155 name = "tf_test_foo" 156 cidr_block = "172.16.0.0/12" 157 } 158 159 resource "alicloud_vswitch" "foo" { 160 vpc_id = "${alicloud_vpc.foo.id}" 161 cidr_block = "172.16.0.0/21" 162 availability_zone = "cn-beijing-b" 163 } 164 165 resource "alicloud_nat_gateway" "foo" { 166 vpc_id = "${alicloud_vpc.foo.id}" 167 spec = "Small" 168 name = "test_foo" 169 bandwidth_packages = [{ 170 ip_count = 1 171 bandwidth = 5 172 zone = "cn-beijing-b" 173 }, { 174 ip_count = 2 175 bandwidth = 10 176 zone = "cn-beijing-b" 177 }] 178 depends_on = [ 179 "alicloud_vswitch.foo"] 180 } 181 ` 182 183 const testAccNatGatewayConfigSpec = ` 184 resource "alicloud_vpc" "foo" { 185 name = "tf_test_foo" 186 cidr_block = "172.16.0.0/12" 187 } 188 189 resource "alicloud_vswitch" "foo" { 190 vpc_id = "${alicloud_vpc.foo.id}" 191 cidr_block = "172.16.0.0/21" 192 availability_zone = "cn-beijing-b" 193 } 194 195 resource "alicloud_nat_gateway" "foo" { 196 vpc_id = "${alicloud_vpc.foo.id}" 197 spec = "Middle" 198 name = "test_foo" 199 bandwidth_packages = [{ 200 ip_count = 1 201 bandwidth = 5 202 zone = "cn-beijing-b" 203 }, { 204 ip_count = 2 205 bandwidth = 10 206 zone = "cn-beijing-b" 207 }] 208 depends_on = [ 209 "alicloud_vswitch.foo"] 210 } 211 ` 212 213 const testAccNatGatewayConfigSpecUpgrade = ` 214 resource "alicloud_vpc" "foo" { 215 name = "tf_test_foo" 216 cidr_block = "172.16.0.0/12" 217 } 218 219 resource "alicloud_vswitch" "foo" { 220 vpc_id = "${alicloud_vpc.foo.id}" 221 cidr_block = "172.16.0.0/21" 222 availability_zone = "cn-beijing-b" 223 } 224 225 resource "alicloud_nat_gateway" "foo" { 226 vpc_id = "${alicloud_vpc.foo.id}" 227 spec = "Large" 228 name = "test_foo" 229 bandwidth_packages = [{ 230 ip_count = 1 231 bandwidth = 5 232 zone = "cn-beijing-b" 233 }, { 234 ip_count = 2 235 bandwidth = 10 236 zone = "cn-beijing-b" 237 }] 238 depends_on = [ 239 "alicloud_vswitch.foo"] 240 } 241 `