github.com/ottenhoff/terraform@v0.7.0-rc1.0.20160607213102-ac2d195cc560/builtin/providers/triton/resource_machine_test.go (about) 1 package triton 2 3 import ( 4 "fmt" 5 "testing" 6 "time" 7 8 "github.com/hashicorp/terraform/helper/acctest" 9 "github.com/hashicorp/terraform/helper/resource" 10 "github.com/hashicorp/terraform/terraform" 11 "github.com/joyent/gosdc/cloudapi" 12 ) 13 14 func TestAccTritonMachine_basic(t *testing.T) { 15 machineName := fmt.Sprintf("acctest-%d", acctest.RandInt()) 16 config := fmt.Sprintf(testAccTritonMachine_basic, machineName) 17 18 resource.Test(t, resource.TestCase{ 19 PreCheck: func() { testAccPreCheck(t) }, 20 Providers: testAccProviders, 21 CheckDestroy: testCheckTritonMachineDestroy, 22 Steps: []resource.TestStep{ 23 resource.TestStep{ 24 Config: config, 25 Check: resource.ComposeTestCheckFunc( 26 testCheckTritonMachineExists("triton_machine.test"), 27 func(*terraform.State) error { 28 time.Sleep(10 * time.Second) 29 return nil 30 }, 31 ), 32 }, 33 }, 34 }) 35 } 36 37 func TestAccTritonMachine_nic(t *testing.T) { 38 machineName := fmt.Sprintf("acctest-%d", acctest.RandInt()) 39 config := fmt.Sprintf(testAccTritonMachine_withnic, machineName, machineName) 40 41 resource.Test(t, resource.TestCase{ 42 PreCheck: func() { testAccPreCheck(t) }, 43 Providers: testAccProviders, 44 CheckDestroy: testCheckTritonMachineDestroy, 45 Steps: []resource.TestStep{ 46 resource.TestStep{ 47 Config: config, 48 Check: resource.ComposeTestCheckFunc( 49 testCheckTritonMachineExists("triton_machine.test"), 50 func(*terraform.State) error { 51 time.Sleep(10 * time.Second) 52 return nil 53 }, 54 testCheckTritonMachineHasFabric("triton_machine.test", "triton_fabric.test"), 55 ), 56 }, 57 }, 58 }) 59 } 60 61 func TestAccTritonMachine_addnic(t *testing.T) { 62 machineName := fmt.Sprintf("acctest-%d", acctest.RandInt()) 63 without := fmt.Sprintf(testAccTritonMachine_withoutnic, machineName, machineName) 64 with := fmt.Sprintf(testAccTritonMachine_withnic, machineName, machineName) 65 66 resource.Test(t, resource.TestCase{ 67 PreCheck: func() { testAccPreCheck(t) }, 68 Providers: testAccProviders, 69 CheckDestroy: testCheckTritonMachineDestroy, 70 Steps: []resource.TestStep{ 71 resource.TestStep{ 72 Config: without, 73 Check: resource.ComposeTestCheckFunc( 74 testCheckTritonMachineExists("triton_machine.test"), 75 func(*terraform.State) error { 76 time.Sleep(10 * time.Second) 77 return nil 78 }, 79 testCheckTritonMachineHasNoFabric("triton_machine.test", "triton_fabric.test"), 80 ), 81 }, 82 resource.TestStep{ 83 Config: with, 84 Check: resource.ComposeTestCheckFunc( 85 testCheckTritonMachineExists("triton_machine.test"), 86 testCheckTritonMachineHasFabric("triton_machine.test", "triton_fabric.test"), 87 ), 88 }, 89 }, 90 }) 91 } 92 93 func testCheckTritonMachineExists(name string) resource.TestCheckFunc { 94 return func(s *terraform.State) error { 95 // Ensure we have enough information in state to look up in API 96 rs, ok := s.RootModule().Resources[name] 97 if !ok { 98 return fmt.Errorf("Not found: %s", name) 99 } 100 conn := testAccProvider.Meta().(*cloudapi.Client) 101 102 rule, err := conn.GetMachine(rs.Primary.ID) 103 if err != nil { 104 return fmt.Errorf("Bad: Check Machine Exists: %s", err) 105 } 106 107 if rule == nil { 108 return fmt.Errorf("Bad: Machine %q does not exist", rs.Primary.ID) 109 } 110 111 return nil 112 } 113 } 114 115 func testCheckTritonMachineHasFabric(name, fabricName string) resource.TestCheckFunc { 116 return func(s *terraform.State) error { 117 // Ensure we have enough information in state to look up in API 118 machine, ok := s.RootModule().Resources[name] 119 if !ok { 120 return fmt.Errorf("Not found: %s", name) 121 } 122 123 network, ok := s.RootModule().Resources[fabricName] 124 if !ok { 125 return fmt.Errorf("Not found: %s", fabricName) 126 } 127 conn := testAccProvider.Meta().(*cloudapi.Client) 128 129 nics, err := conn.ListNICs(machine.Primary.ID) 130 if err != nil { 131 return fmt.Errorf("Bad: Check NICs Exist: %s", err) 132 } 133 134 for _, nic := range nics { 135 if nic.Network == network.Primary.ID { 136 return nil 137 } 138 } 139 140 return fmt.Errorf("Bad: Machine %q does not have Fabric %q", machine.Primary.ID, network.Primary.ID) 141 } 142 } 143 144 func testCheckTritonMachineHasNoFabric(name, fabricName string) resource.TestCheckFunc { 145 return func(s *terraform.State) error { 146 // Ensure we have enough information in state to look up in API 147 machine, ok := s.RootModule().Resources[name] 148 if !ok { 149 return fmt.Errorf("Not found: %s", name) 150 } 151 152 network, ok := s.RootModule().Resources[fabricName] 153 if !ok { 154 return fmt.Errorf("Not found: %s", fabricName) 155 } 156 conn := testAccProvider.Meta().(*cloudapi.Client) 157 158 nics, err := conn.ListNICs(machine.Primary.ID) 159 if err != nil { 160 return fmt.Errorf("Bad: Check NICs Exist: %s", err) 161 } 162 163 for _, nic := range nics { 164 if nic.Network == network.Primary.ID { 165 return fmt.Errorf("Bad: Machine %q has Fabric %q", machine.Primary.ID, network.Primary.ID) 166 } 167 } 168 169 return nil 170 } 171 } 172 173 func testCheckTritonMachineDestroy(s *terraform.State) error { 174 conn := testAccProvider.Meta().(*cloudapi.Client) 175 176 for _, rs := range s.RootModule().Resources { 177 if rs.Type != "triton_machine" { 178 continue 179 } 180 181 resp, err := conn.GetMachine(rs.Primary.ID) 182 if err != nil { 183 return nil 184 } 185 186 if resp != nil { 187 return fmt.Errorf("Bad: Machine %q still exists", rs.Primary.ID) 188 } 189 } 190 191 return nil 192 } 193 194 func TestAccTritonMachine_firewall(t *testing.T) { 195 machineName := fmt.Sprintf("acctest-%d", acctest.RandInt()) 196 disabled_config := fmt.Sprintf(testAccTritonMachine_firewall_0, machineName) 197 enabled_config := fmt.Sprintf(testAccTritonMachine_firewall_1, machineName) 198 199 resource.Test(t, resource.TestCase{ 200 PreCheck: func() { testAccPreCheck(t) }, 201 Providers: testAccProviders, 202 CheckDestroy: testCheckTritonMachineDestroy, 203 Steps: []resource.TestStep{ 204 resource.TestStep{ 205 Config: enabled_config, 206 Check: resource.ComposeTestCheckFunc( 207 testCheckTritonMachineExists("triton_machine.test"), 208 resource.TestCheckResourceAttr( 209 "triton_machine.test", "firewall_enabled", "true"), 210 ), 211 }, 212 resource.TestStep{ 213 Config: disabled_config, 214 Check: resource.ComposeTestCheckFunc( 215 testCheckTritonMachineExists("triton_machine.test"), 216 resource.TestCheckResourceAttr( 217 "triton_machine.test", "firewall_enabled", "false"), 218 ), 219 }, 220 resource.TestStep{ 221 Config: enabled_config, 222 Check: resource.ComposeTestCheckFunc( 223 testCheckTritonMachineExists("triton_machine.test"), 224 resource.TestCheckResourceAttr( 225 "triton_machine.test", "firewall_enabled", "true"), 226 ), 227 }, 228 }, 229 }) 230 } 231 232 func TestAccTritonMachine_metadata(t *testing.T) { 233 machineName := fmt.Sprintf("acctest-%d", acctest.RandInt()) 234 basic := fmt.Sprintf(testAccTritonMachine_metadata_1, machineName) 235 add_metadata := fmt.Sprintf(testAccTritonMachine_metadata_1, machineName) 236 237 resource.Test(t, resource.TestCase{ 238 PreCheck: func() { testAccPreCheck(t) }, 239 Providers: testAccProviders, 240 CheckDestroy: testCheckTritonMachineDestroy, 241 Steps: []resource.TestStep{ 242 resource.TestStep{ 243 Config: basic, 244 Check: resource.ComposeTestCheckFunc( 245 testCheckTritonMachineExists("triton_machine.test"), 246 ), 247 }, 248 resource.TestStep{ 249 Config: add_metadata, 250 Check: resource.ComposeTestCheckFunc( 251 testCheckTritonMachineExists("triton_machine.test"), 252 resource.TestCheckResourceAttr( 253 "triton_machine.test", "user_data", "hello"), 254 ), 255 }, 256 }, 257 }) 258 } 259 260 var testAccTritonMachine_basic = ` 261 provider "triton" { 262 url = "https://us-west-1.api.joyentcloud.com" 263 } 264 265 resource "triton_machine" "test" { 266 name = "%s" 267 package = "g3-standard-0.25-smartos" 268 image = "c20b4b7c-e1a6-11e5-9a4d-ef590901732e" 269 270 tags = { 271 test = "hello!" 272 } 273 } 274 ` 275 276 var testAccTritonMachine_firewall_0 = ` 277 provider "triton" { 278 url = "https://us-west-1.api.joyentcloud.com" 279 } 280 281 resource "triton_machine" "test" { 282 name = "%s" 283 package = "g3-standard-0.25-smartos" 284 image = "c20b4b7c-e1a6-11e5-9a4d-ef590901732e" 285 286 firewall_enabled = 0 287 } 288 ` 289 var testAccTritonMachine_firewall_1 = ` 290 provider "triton" { 291 url = "https://us-west-1.api.joyentcloud.com" 292 } 293 294 resource "triton_machine" "test" { 295 name = "%s" 296 package = "g3-standard-0.25-smartos" 297 image = "c20b4b7c-e1a6-11e5-9a4d-ef590901732e" 298 299 firewall_enabled = 1 300 } 301 ` 302 303 var testAccTritonMachine_metadata_1 = ` 304 provider "triton" { 305 url = "https://us-west-1.api.joyentcloud.com" 306 } 307 308 resource "triton_machine" "test" { 309 name = "%s" 310 package = "g3-standard-0.25-smartos" 311 image = "c20b4b7c-e1a6-11e5-9a4d-ef590901732e" 312 313 user_data = "hello" 314 315 tags = { 316 test = "hello!" 317 } 318 } 319 ` 320 321 var testAccTritonMachine_withnic = ` 322 resource "triton_fabric" "test" { 323 name = "%s-network" 324 description = "test network" 325 vlan_id = 2 # every DC seems to have a vlan 2 available 326 327 subnet = "10.0.0.0/22" 328 gateway = "10.0.0.1" 329 provision_start_ip = "10.0.0.5" 330 provision_end_ip = "10.0.3.250" 331 332 resolvers = ["8.8.8.8", "8.8.4.4"] 333 } 334 335 resource "triton_machine" "test" { 336 name = "%s" 337 package = "g3-standard-0.25-smartos" 338 image = "842e6fa6-6e9b-11e5-8402-1b490459e334" 339 340 tags = { 341 test = "hello!" 342 } 343 344 nic { network = "${triton_fabric.test.id}" } 345 } 346 ` 347 348 var testAccTritonMachine_withoutnic = ` 349 resource "triton_fabric" "test" { 350 name = "%s-network" 351 description = "test network" 352 vlan_id = 2 # every DC seems to have a vlan 2 available 353 354 subnet = "10.0.0.0/22" 355 gateway = "10.0.0.1" 356 provision_start_ip = "10.0.0.5" 357 provision_end_ip = "10.0.3.250" 358 359 resolvers = ["8.8.8.8", "8.8.4.4"] 360 } 361 362 resource "triton_machine" "test" { 363 name = "%s" 364 package = "g3-standard-0.25-smartos" 365 image = "842e6fa6-6e9b-11e5-8402-1b490459e334" 366 367 tags = { 368 test = "hello!" 369 } 370 } 371 `