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