github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/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/triton-go"
    12  )
    13  
    14  func TestAccTritonKey_basic(t *testing.T) {
    15  	keyName := fmt.Sprintf("acctest-%d", acctest.RandInt())
    16  	publicKeyMaterial, _, err := acctest.RandSSHKeyPair("TestAccTritonKey_basic@terraform")
    17  	if err != nil {
    18  		t.Fatalf("Cannot generate test SSH key pair: %s", err)
    19  	}
    20  	config := testAccTritonKey_basic(keyName, publicKeyMaterial)
    21  
    22  	resource.Test(t, resource.TestCase{
    23  		PreCheck:     func() { testAccPreCheck(t) },
    24  		Providers:    testAccProviders,
    25  		CheckDestroy: testCheckTritonKeyDestroy,
    26  		Steps: []resource.TestStep{
    27  			{
    28  				Config: config,
    29  				Check: resource.ComposeTestCheckFunc(
    30  					testCheckTritonKeyExists("triton_key.test"),
    31  					resource.TestCheckResourceAttr("triton_key.test", "name", keyName),
    32  					resource.TestCheckResourceAttr("triton_key.test", "key", publicKeyMaterial),
    33  					func(*terraform.State) error {
    34  						time.Sleep(10 * time.Second)
    35  						return nil
    36  					},
    37  				),
    38  			},
    39  		},
    40  	})
    41  }
    42  
    43  func TestAccTritonKey_noKeyName(t *testing.T) {
    44  	keyComment := fmt.Sprintf("acctest_%d@terraform", acctest.RandInt())
    45  	keyMaterial, _, err := acctest.RandSSHKeyPair(keyComment)
    46  	if err != nil {
    47  		t.Fatalf("Cannot generate test SSH key pair: %s", err)
    48  	}
    49  	config := testAccTritonKey_noKeyName(keyMaterial)
    50  
    51  	resource.Test(t, resource.TestCase{
    52  		PreCheck:     func() { testAccPreCheck(t) },
    53  		Providers:    testAccProviders,
    54  		CheckDestroy: testCheckTritonKeyDestroy,
    55  		Steps: []resource.TestStep{
    56  			{
    57  				Config: config,
    58  				Check: resource.ComposeTestCheckFunc(
    59  					testCheckTritonKeyExists("triton_key.test"),
    60  					resource.TestCheckResourceAttr("triton_key.test", "name", keyComment),
    61  					resource.TestCheckResourceAttr("triton_key.test", "key", keyMaterial),
    62  					func(*terraform.State) error {
    63  						time.Sleep(10 * time.Second)
    64  						return nil
    65  					},
    66  				),
    67  			},
    68  		},
    69  	})
    70  }
    71  
    72  func testCheckTritonKeyExists(name string) resource.TestCheckFunc {
    73  	return func(s *terraform.State) error {
    74  		// Ensure we have enough information in state to look up in API
    75  		rs, ok := s.RootModule().Resources[name]
    76  		if !ok {
    77  			return fmt.Errorf("Not found: %s", name)
    78  		}
    79  		conn := testAccProvider.Meta().(*triton.Client)
    80  
    81  		key, err := conn.Keys().GetKey(&triton.GetKeyInput{
    82  			KeyName: rs.Primary.ID,
    83  		})
    84  		if err != nil {
    85  			return fmt.Errorf("Bad: Check Key Exists: %s", err)
    86  		}
    87  
    88  		if key == nil {
    89  			return fmt.Errorf("Bad: Key %q does not exist", rs.Primary.ID)
    90  		}
    91  
    92  		return nil
    93  	}
    94  }
    95  
    96  func testCheckTritonKeyDestroy(s *terraform.State) error {
    97  	conn := testAccProvider.Meta().(*triton.Client)
    98  
    99  	return resource.Retry(1*time.Minute, func() *resource.RetryError {
   100  		for _, rs := range s.RootModule().Resources {
   101  			if rs.Type != "triton_key" {
   102  				continue
   103  			}
   104  
   105  			key, err := conn.Keys().GetKey(&triton.GetKeyInput{
   106  				KeyName: rs.Primary.ID,
   107  			})
   108  			if err != nil {
   109  				return nil
   110  			}
   111  
   112  			if key != nil {
   113  				return resource.RetryableError(fmt.Errorf("Bad: Key %q still exists", rs.Primary.ID))
   114  			}
   115  		}
   116  
   117  		return nil
   118  	})
   119  }
   120  
   121  var testAccTritonKey_basic = func(keyName string, keyMaterial string) string {
   122  	return fmt.Sprintf(`resource "triton_key" "test" {
   123  	    name = "%s"
   124  	    key = "%s"
   125  	}
   126  	`, keyName, keyMaterial)
   127  }
   128  
   129  var testAccTritonKey_noKeyName = func(keyMaterial string) string {
   130  	return fmt.Sprintf(`resource "triton_key" "test" {
   131  	    key = "%s"
   132  	}
   133  	`, keyMaterial)
   134  }