github.com/jrperritt/terraform@v0.1.1-0.20170525065507-96f391dafc38/builtin/providers/triton/resource_key_test.go (about)

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