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