github.com/hobbeswalsh/terraform@v0.3.7-0.20150619183303-ad17cf55a0fa/builtin/providers/digitalocean/resource_digitalocean_droplet_test.go (about) 1 package digitalocean 2 3 import ( 4 "fmt" 5 "strings" 6 "testing" 7 8 "github.com/hashicorp/terraform/helper/resource" 9 "github.com/hashicorp/terraform/terraform" 10 "github.com/pearkes/digitalocean" 11 ) 12 13 func TestAccDigitalOceanDroplet_Basic(t *testing.T) { 14 var droplet digitalocean.Droplet 15 16 resource.Test(t, resource.TestCase{ 17 PreCheck: func() { testAccPreCheck(t) }, 18 Providers: testAccProviders, 19 CheckDestroy: testAccCheckDigitalOceanDropletDestroy, 20 Steps: []resource.TestStep{ 21 resource.TestStep{ 22 Config: testAccCheckDigitalOceanDropletConfig_basic, 23 Check: resource.ComposeTestCheckFunc( 24 testAccCheckDigitalOceanDropletExists("digitalocean_droplet.foobar", &droplet), 25 testAccCheckDigitalOceanDropletAttributes(&droplet), 26 resource.TestCheckResourceAttr( 27 "digitalocean_droplet.foobar", "name", "foo"), 28 resource.TestCheckResourceAttr( 29 "digitalocean_droplet.foobar", "size", "512mb"), 30 resource.TestCheckResourceAttr( 31 "digitalocean_droplet.foobar", "image", "centos-5-8-x32"), 32 resource.TestCheckResourceAttr( 33 "digitalocean_droplet.foobar", "region", "nyc3"), 34 resource.TestCheckResourceAttr( 35 "digitalocean_droplet.foobar", "user_data", "foobar"), 36 ), 37 }, 38 }, 39 }) 40 } 41 42 func TestAccDigitalOceanDroplet_Update(t *testing.T) { 43 var droplet digitalocean.Droplet 44 45 resource.Test(t, resource.TestCase{ 46 PreCheck: func() { testAccPreCheck(t) }, 47 Providers: testAccProviders, 48 CheckDestroy: testAccCheckDigitalOceanDropletDestroy, 49 Steps: []resource.TestStep{ 50 resource.TestStep{ 51 Config: testAccCheckDigitalOceanDropletConfig_basic, 52 Check: resource.ComposeTestCheckFunc( 53 testAccCheckDigitalOceanDropletExists("digitalocean_droplet.foobar", &droplet), 54 testAccCheckDigitalOceanDropletAttributes(&droplet), 55 ), 56 }, 57 58 resource.TestStep{ 59 Config: testAccCheckDigitalOceanDropletConfig_RenameAndResize, 60 Check: resource.ComposeTestCheckFunc( 61 testAccCheckDigitalOceanDropletExists("digitalocean_droplet.foobar", &droplet), 62 testAccCheckDigitalOceanDropletRenamedAndResized(&droplet), 63 resource.TestCheckResourceAttr( 64 "digitalocean_droplet.foobar", "name", "baz"), 65 resource.TestCheckResourceAttr( 66 "digitalocean_droplet.foobar", "size", "1gb"), 67 ), 68 }, 69 }, 70 }) 71 } 72 73 func TestAccDigitalOceanDroplet_PrivateNetworkingIpv6(t *testing.T) { 74 var droplet digitalocean.Droplet 75 76 resource.Test(t, resource.TestCase{ 77 PreCheck: func() { testAccPreCheck(t) }, 78 Providers: testAccProviders, 79 CheckDestroy: testAccCheckDigitalOceanDropletDestroy, 80 Steps: []resource.TestStep{ 81 resource.TestStep{ 82 Config: testAccCheckDigitalOceanDropletConfig_PrivateNetworkingIpv6, 83 Check: resource.ComposeTestCheckFunc( 84 testAccCheckDigitalOceanDropletExists("digitalocean_droplet.foobar", &droplet), 85 testAccCheckDigitalOceanDropletAttributes_PrivateNetworkingIpv6(&droplet), 86 resource.TestCheckResourceAttr( 87 "digitalocean_droplet.foobar", "private_networking", "true"), 88 resource.TestCheckResourceAttr( 89 "digitalocean_droplet.foobar", "ipv6", "true"), 90 ), 91 }, 92 }, 93 }) 94 } 95 96 func testAccCheckDigitalOceanDropletDestroy(s *terraform.State) error { 97 client := testAccProvider.Meta().(*digitalocean.Client) 98 99 for _, rs := range s.RootModule().Resources { 100 if rs.Type != "digitalocean_droplet" { 101 continue 102 } 103 104 // Try to find the Droplet 105 _, err := client.RetrieveDroplet(rs.Primary.ID) 106 107 // Wait 108 109 if err != nil && !strings.Contains(err.Error(), "404") { 110 return fmt.Errorf( 111 "Error waiting for droplet (%s) to be destroyed: %s", 112 rs.Primary.ID, err) 113 } 114 } 115 116 return nil 117 } 118 119 func testAccCheckDigitalOceanDropletAttributes(droplet *digitalocean.Droplet) resource.TestCheckFunc { 120 return func(s *terraform.State) error { 121 122 if droplet.ImageSlug() != "centos-5-8-x32" { 123 return fmt.Errorf("Bad image_slug: %s", droplet.ImageSlug()) 124 } 125 126 if droplet.SizeSlug != "512mb" { 127 return fmt.Errorf("Bad size_slug: %s", droplet.SizeSlug) 128 } 129 130 if droplet.RegionSlug() != "nyc3" { 131 return fmt.Errorf("Bad region_slug: %s", droplet.RegionSlug()) 132 } 133 134 if droplet.Name != "foo" { 135 return fmt.Errorf("Bad name: %s", droplet.Name) 136 } 137 return nil 138 } 139 } 140 141 func testAccCheckDigitalOceanDropletRenamedAndResized(droplet *digitalocean.Droplet) resource.TestCheckFunc { 142 return func(s *terraform.State) error { 143 144 if droplet.SizeSlug != "1gb" { 145 return fmt.Errorf("Bad size_slug: %s", droplet.SizeSlug) 146 } 147 148 if droplet.Name != "baz" { 149 return fmt.Errorf("Bad name: %s", droplet.Name) 150 } 151 152 return nil 153 } 154 } 155 156 func testAccCheckDigitalOceanDropletAttributes_PrivateNetworkingIpv6(droplet *digitalocean.Droplet) resource.TestCheckFunc { 157 return func(s *terraform.State) error { 158 159 if droplet.ImageSlug() != "centos-5-8-x32" { 160 return fmt.Errorf("Bad image_slug: %s", droplet.ImageSlug()) 161 } 162 163 if droplet.SizeSlug != "1gb" { 164 return fmt.Errorf("Bad size_slug: %s", droplet.SizeSlug) 165 } 166 167 if droplet.RegionSlug() != "sgp1" { 168 return fmt.Errorf("Bad region_slug: %s", droplet.RegionSlug()) 169 } 170 171 if droplet.Name != "baz" { 172 return fmt.Errorf("Bad name: %s", droplet.Name) 173 } 174 175 if droplet.IPV4Address("private") == "" { 176 return fmt.Errorf("No ipv4 private: %s", droplet.IPV4Address("private")) 177 } 178 179 // if droplet.IPV6Address("private") == "" { 180 // return fmt.Errorf("No ipv6 private: %s", droplet.IPV6Address("private")) 181 // } 182 183 if droplet.NetworkingType() != "private" { 184 return fmt.Errorf("Bad networking type: %s", droplet.NetworkingType()) 185 } 186 187 if droplet.IPV4Address("public") == "" { 188 return fmt.Errorf("No ipv4 public: %s", droplet.IPV4Address("public")) 189 } 190 191 if droplet.IPV6Address("public") == "" { 192 return fmt.Errorf("No ipv6 public: %s", droplet.IPV6Address("public")) 193 } 194 195 return nil 196 } 197 } 198 199 func testAccCheckDigitalOceanDropletExists(n string, droplet *digitalocean.Droplet) resource.TestCheckFunc { 200 return func(s *terraform.State) error { 201 rs, ok := s.RootModule().Resources[n] 202 if !ok { 203 return fmt.Errorf("Not found: %s", n) 204 } 205 206 if rs.Primary.ID == "" { 207 return fmt.Errorf("No Droplet ID is set") 208 } 209 210 client := testAccProvider.Meta().(*digitalocean.Client) 211 212 retrieveDroplet, err := client.RetrieveDroplet(rs.Primary.ID) 213 214 if err != nil { 215 return err 216 } 217 218 if retrieveDroplet.StringId() != rs.Primary.ID { 219 return fmt.Errorf("Droplet not found") 220 } 221 222 *droplet = retrieveDroplet 223 224 return nil 225 } 226 } 227 228 // Not sure if this check should remain here as the underlaying 229 // function is changed and is tested indirectly by almost all 230 // other test already 231 // 232 //func Test_new_droplet_state_refresh_func(t *testing.T) { 233 // droplet := digitalocean.Droplet{ 234 // Name: "foobar", 235 // } 236 // resourceMap, _ := resource_digitalocean_droplet_update_state( 237 // &terraform.InstanceState{Attributes: map[string]string{}}, &droplet) 238 // 239 // // See if we can access our attribute 240 // if _, ok := resourceMap.Attributes["name"]; !ok { 241 // t.Fatalf("bad name: %s", resourceMap.Attributes) 242 // } 243 // 244 //} 245 246 const testAccCheckDigitalOceanDropletConfig_basic = ` 247 resource "digitalocean_droplet" "foobar" { 248 name = "foo" 249 size = "512mb" 250 image = "centos-5-8-x32" 251 region = "nyc3" 252 user_data = "foobar" 253 } 254 ` 255 256 const testAccCheckDigitalOceanDropletConfig_RenameAndResize = ` 257 resource "digitalocean_droplet" "foobar" { 258 name = "baz" 259 size = "1gb" 260 image = "centos-5-8-x32" 261 region = "nyc3" 262 } 263 ` 264 265 // IPV6 only in singapore 266 const testAccCheckDigitalOceanDropletConfig_PrivateNetworkingIpv6 = ` 267 resource "digitalocean_droplet" "foobar" { 268 name = "baz" 269 size = "1gb" 270 image = "centos-5-8-x32" 271 region = "sgp1" 272 ipv6 = true 273 private_networking = true 274 } 275 `