github.com/pmcatominey/terraform@v0.7.0-rc2.0.20160708105029-1401a52a5cc5/builtin/providers/rundeck/resource_private_key_test.go (about) 1 package rundeck 2 3 import ( 4 "fmt" 5 "strings" 6 "testing" 7 8 "github.com/apparentlymart/go-rundeck-api/rundeck" 9 10 "github.com/hashicorp/terraform/helper/resource" 11 "github.com/hashicorp/terraform/terraform" 12 ) 13 14 func TestAccPrivateKey_basic(t *testing.T) { 15 var key rundeck.KeyMeta 16 17 resource.Test(t, resource.TestCase{ 18 PreCheck: func() { testAccPreCheck(t) }, 19 Providers: testAccProviders, 20 CheckDestroy: testAccPrivateKeyCheckDestroy(&key), 21 Steps: []resource.TestStep{ 22 resource.TestStep{ 23 Config: testAccPrivateKeyConfig_basic, 24 Check: resource.ComposeTestCheckFunc( 25 testAccPrivateKeyCheckExists("rundeck_private_key.test", &key), 26 func(s *terraform.State) error { 27 if expected := "keys/terraform_acceptance_tests/private_key"; key.Path != expected { 28 return fmt.Errorf("wrong path; expected %v, got %v", expected, key.Path) 29 } 30 if !strings.HasSuffix(key.URL, "/storage/keys/terraform_acceptance_tests/private_key") { 31 return fmt.Errorf("wrong URL; expected to end with the key path") 32 } 33 if expected := "file"; key.ResourceType != expected { 34 return fmt.Errorf("wrong resource type; expected %v, got %v", expected, key.ResourceType) 35 } 36 if expected := "private"; key.KeyType != expected { 37 return fmt.Errorf("wrong key type; expected %v, got %v", expected, key.KeyType) 38 } 39 // Rundeck won't let us re-retrieve a private key payload, so we can't test 40 // that the key material was submitted and stored correctly. 41 return nil 42 }, 43 ), 44 }, 45 }, 46 }) 47 } 48 49 func testAccPrivateKeyCheckDestroy(key *rundeck.KeyMeta) resource.TestCheckFunc { 50 return func(s *terraform.State) error { 51 client := testAccProvider.Meta().(*rundeck.Client) 52 _, err := client.GetKeyMeta(key.Path) 53 if err == nil { 54 return fmt.Errorf("key still exists") 55 } 56 if _, ok := err.(*rundeck.NotFoundError); !ok { 57 return fmt.Errorf("got something other than NotFoundError (%v) when getting key", err) 58 } 59 60 return nil 61 } 62 } 63 64 func testAccPrivateKeyCheckExists(rn string, key *rundeck.KeyMeta) resource.TestCheckFunc { 65 return func(s *terraform.State) error { 66 rs, ok := s.RootModule().Resources[rn] 67 if !ok { 68 return fmt.Errorf("resource not found: %s", rn) 69 } 70 71 if rs.Primary.ID == "" { 72 return fmt.Errorf("key id not set") 73 } 74 75 client := testAccProvider.Meta().(*rundeck.Client) 76 gotKey, err := client.GetKeyMeta(rs.Primary.ID) 77 if err != nil { 78 return fmt.Errorf("error getting key metadata: %s", err) 79 } 80 81 *key = *gotKey 82 83 return nil 84 } 85 } 86 87 const testAccPrivateKeyConfig_basic = ` 88 resource "rundeck_private_key" "test" { 89 path = "terraform_acceptance_tests/private_key" 90 key_material = "this is not a real private key" 91 } 92 `