github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/triton/resource_key_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 TestAccTritonKey_basic(t *testing.T) {
    15  	keyName := fmt.Sprintf("acctest-%d", acctest.RandInt())
    16  	config := fmt.Sprintf(testAccTritonKey_basic, keyName, testAccTritonKey_basicMaterial)
    17  
    18  	resource.Test(t, resource.TestCase{
    19  		PreCheck:     func() { testAccPreCheck(t) },
    20  		Providers:    testAccProviders,
    21  		CheckDestroy: testCheckTritonKeyDestroy,
    22  		Steps: []resource.TestStep{
    23  			resource.TestStep{
    24  				Config: config,
    25  				Check: resource.ComposeTestCheckFunc(
    26  					testCheckTritonKeyExists("triton_key.test"),
    27  					func(*terraform.State) error {
    28  						time.Sleep(10 * time.Second)
    29  						return nil
    30  					},
    31  				),
    32  			},
    33  		},
    34  	})
    35  }
    36  
    37  func testCheckTritonKeyExists(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.GetKey(rs.Primary.ID)
    47  		if err != nil {
    48  			return fmt.Errorf("Bad: Check Key Exists: %s", err)
    49  		}
    50  
    51  		if rule == nil {
    52  			return fmt.Errorf("Bad: Key %q does not exist", rs.Primary.ID)
    53  		}
    54  
    55  		return nil
    56  	}
    57  }
    58  
    59  func testCheckTritonKeyDestroy(s *terraform.State) error {
    60  	conn := testAccProvider.Meta().(*cloudapi.Client)
    61  
    62  	return resource.Retry(1*time.Minute, func() *resource.RetryError {
    63  		for _, rs := range s.RootModule().Resources {
    64  			if rs.Type != "triton_key" {
    65  				continue
    66  			}
    67  
    68  			resp, err := conn.GetKey(rs.Primary.ID)
    69  			if err != nil {
    70  				return nil
    71  			}
    72  
    73  			if resp != nil {
    74  				return resource.RetryableError(fmt.Errorf("Bad: Key %q still exists", rs.Primary.ID))
    75  			}
    76  		}
    77  
    78  		return nil
    79  	})
    80  }
    81  
    82  var testAccTritonKey_basic = `
    83  resource "triton_key" "test" {
    84      name = "%s"
    85      key = "%s"
    86  }
    87  `
    88  
    89  const testAccTritonKey_basicMaterial = `ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDL18KJIe8N7FxcgOMtabo10qZEDyYUSlOpsh/EYrugQCQHMKuNytog1lhFNZNk4LGNAz5L8/btG9+/axY/PfundbjR3SXt0hupAGQIVHuygWTr7foj5iGhckrEM+r3eMCXqoCnIFLhDZLDcq/zN2MxNbqDKcWSYmc8ul9dZWuiQpKOL+0nNXjhYA8Ewu+07kVAtsZD0WfvnAUjxmYb3rB15eBWk7gLxHrOPfZpeDSvOOX2bmzikpLn+L5NKrJsLrzO6hU/rpxD4OTHLULcsnIts3lYH8hShU8uY5ry94PBzdix++se3pUGvNSe967fKlHw3Ymh9nE/LJDQnzTNyFMj James@jn-mpb13`