github.com/andresvia/terraform@v0.6.15-0.20160412045437-d51c75946785/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 testCheckTritonMachineExists(name string) resource.TestCheckFunc {
    38  	return func(s *terraform.State) error {
    39  		// Ensure we have enough information in state to look up in API
    40  		rs, ok := s.RootModule().Resources[name]
    41  		if !ok {
    42  			return fmt.Errorf("Not found: %s", name)
    43  		}
    44  		conn := testAccProvider.Meta().(*cloudapi.Client)
    45  
    46  		rule, err := conn.GetMachine(rs.Primary.ID)
    47  		if err != nil {
    48  			return fmt.Errorf("Bad: Check Machine Exists: %s", err)
    49  		}
    50  
    51  		if rule == nil {
    52  			return fmt.Errorf("Bad: Machine %q does not exist", rs.Primary.ID)
    53  		}
    54  
    55  		return nil
    56  	}
    57  }
    58  
    59  func testCheckTritonMachineDestroy(s *terraform.State) error {
    60  	conn := testAccProvider.Meta().(*cloudapi.Client)
    61  
    62  	for _, rs := range s.RootModule().Resources {
    63  		if rs.Type != "triton_machine" {
    64  			continue
    65  		}
    66  
    67  		resp, err := conn.GetMachine(rs.Primary.ID)
    68  		if err != nil {
    69  			return nil
    70  		}
    71  
    72  		if resp != nil {
    73  			return fmt.Errorf("Bad: Machine %q still exists", rs.Primary.ID)
    74  		}
    75  	}
    76  
    77  	return nil
    78  }
    79  
    80  var testAccTritonMachine_basic = `
    81  resource "triton_machine" "test" {
    82    name = "%s"
    83    package = "g3-standard-0.25-smartos"
    84    image = "842e6fa6-6e9b-11e5-8402-1b490459e334"
    85  
    86    tags = {
    87  	test = "hello!"
    88    }
    89  }
    90  `