github.com/jefferai/terraform@v0.3.7-0.20150310153852-f7512ca29fcf/builtin/providers/digitalocean/resource_digitalocean_ssh_key_test.go (about) 1 package digitalocean 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/hashicorp/terraform/helper/resource" 8 "github.com/hashicorp/terraform/terraform" 9 "github.com/pearkes/digitalocean" 10 ) 11 12 func TestAccDigitalOceanSSHKey_Basic(t *testing.T) { 13 var key digitalocean.SSHKey 14 15 resource.Test(t, resource.TestCase{ 16 PreCheck: func() { testAccPreCheck(t) }, 17 Providers: testAccProviders, 18 CheckDestroy: testAccCheckDigitalOceanSSHKeyDestroy, 19 Steps: []resource.TestStep{ 20 resource.TestStep{ 21 Config: testAccCheckDigitalOceanSSHKeyConfig_basic, 22 Check: resource.ComposeTestCheckFunc( 23 testAccCheckDigitalOceanSSHKeyExists("digitalocean_ssh_key.foobar", &key), 24 testAccCheckDigitalOceanSSHKeyAttributes(&key), 25 resource.TestCheckResourceAttr( 26 "digitalocean_ssh_key.foobar", "name", "foobar"), 27 resource.TestCheckResourceAttr( 28 "digitalocean_ssh_key.foobar", "public_key", "abcdef"), 29 ), 30 }, 31 }, 32 }) 33 } 34 35 func testAccCheckDigitalOceanSSHKeyDestroy(s *terraform.State) error { 36 client := testAccProvider.Meta().(*digitalocean.Client) 37 38 for _, rs := range s.RootModule().Resources { 39 if rs.Type != "digitalocean_ssh_key" { 40 continue 41 } 42 43 // Try to find the key 44 _, err := client.RetrieveSSHKey(rs.Primary.ID) 45 46 if err == nil { 47 fmt.Errorf("SSH key still exists") 48 } 49 } 50 51 return nil 52 } 53 54 func testAccCheckDigitalOceanSSHKeyAttributes(key *digitalocean.SSHKey) resource.TestCheckFunc { 55 return func(s *terraform.State) error { 56 57 if key.Name != "foobar" { 58 return fmt.Errorf("Bad name: %s", key.Name) 59 } 60 61 return nil 62 } 63 } 64 65 func testAccCheckDigitalOceanSSHKeyExists(n string, key *digitalocean.SSHKey) resource.TestCheckFunc { 66 return func(s *terraform.State) error { 67 rs, ok := s.RootModule().Resources[n] 68 69 if !ok { 70 return fmt.Errorf("Not found: %s", n) 71 } 72 73 if rs.Primary.ID == "" { 74 return fmt.Errorf("No Record ID is set") 75 } 76 77 client := testAccProvider.Meta().(*digitalocean.Client) 78 79 foundKey, err := client.RetrieveSSHKey(rs.Primary.ID) 80 81 if err != nil { 82 return err 83 } 84 85 if foundKey.Name != rs.Primary.ID { 86 return fmt.Errorf("Record not found") 87 } 88 89 *key = foundKey 90 91 return nil 92 } 93 } 94 95 const testAccCheckDigitalOceanSSHKeyConfig_basic = ` 96 resource "digitalocean_ssh_key" "foobar" { 97 name = "foobar" 98 public_key = "abcdef" 99 }`