terraform/builtin/providers/triton/resource_machine_test.go

148 lines
3.6 KiB
Go
Raw Normal View History

package triton
import (
"fmt"
"testing"
"time"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"github.com/joyent/gosdc/cloudapi"
)
func TestAccTritonMachine_basic(t *testing.T) {
machineName := fmt.Sprintf("acctest-%d", acctest.RandInt())
config := fmt.Sprintf(testAccTritonMachine_basic, machineName)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckTritonMachineDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: config,
Check: resource.ComposeTestCheckFunc(
testCheckTritonMachineExists("triton_machine.test"),
func(*terraform.State) error {
time.Sleep(10 * time.Second)
return nil
},
),
},
},
})
}
func testCheckTritonMachineExists(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
// Ensure we have enough information in state to look up in API
rs, ok := s.RootModule().Resources[name]
if !ok {
return fmt.Errorf("Not found: %s", name)
}
conn := testAccProvider.Meta().(*cloudapi.Client)
rule, err := conn.GetMachine(rs.Primary.ID)
if err != nil {
return fmt.Errorf("Bad: Check Machine Exists: %s", err)
}
if rule == nil {
return fmt.Errorf("Bad: Machine %q does not exist", rs.Primary.ID)
}
return nil
}
}
func testCheckTritonMachineDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*cloudapi.Client)
for _, rs := range s.RootModule().Resources {
if rs.Type != "triton_machine" {
continue
}
resp, err := conn.GetMachine(rs.Primary.ID)
if err != nil {
return nil
}
if resp != nil {
return fmt.Errorf("Bad: Machine %q still exists", rs.Primary.ID)
}
}
return nil
}
func TestAccTritonMachine_firewall(t *testing.T) {
machineName := fmt.Sprintf("acctest-%d", acctest.RandInt())
disabled_config := fmt.Sprintf(testAccTritonMachine_firewall_0, machineName)
enabled_config := fmt.Sprintf(testAccTritonMachine_firewall_1, machineName)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckTritonMachineDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: enabled_config,
Check: resource.ComposeTestCheckFunc(
testCheckTritonMachineExists("triton_machine.test"),
resource.TestCheckResourceAttr(
"triton_machine.test", "firewall_enabled", "true"),
),
},
resource.TestStep{
Config: disabled_config,
Check: resource.ComposeTestCheckFunc(
testCheckTritonMachineExists("triton_machine.test"),
resource.TestCheckResourceAttr(
"triton_machine.test", "firewall_enabled", "false"),
),
},
resource.TestStep{
Config: enabled_config,
Check: resource.ComposeTestCheckFunc(
testCheckTritonMachineExists("triton_machine.test"),
resource.TestCheckResourceAttr(
"triton_machine.test", "firewall_enabled", "true"),
),
},
},
})
}
var testAccTritonMachine_basic = `
resource "triton_machine" "test" {
name = "%s"
package = "t4-standard-128M"
image = "eb9fc1ea-e19a-11e5-bb27-8b954d8c125c"
tags = {
test = "hello!"
}
}
`
var testAccTritonMachine_firewall_0 = `
resource "triton_machine" "test" {
name = "%s"
package = "t4-standard-128M"
image = "eb9fc1ea-e19a-11e5-bb27-8b954d8c125c"
firewall_enabled = 0
}
`
var testAccTritonMachine_firewall_1 = `
resource "triton_machine" "test" {
name = "%s"
package = "t4-standard-128M"
image = "eb9fc1ea-e19a-11e5-bb27-8b954d8c125c"
firewall_enabled = 1
}
`