github.com/jsoriano/terraform@v0.6.7-0.20151026070445-8b70867fdd95/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_PrivateNetworkingIpv6(t *testing.T) { 75 var droplet 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_PrivateNetworkingIpv6, 84 Check: resource.ComposeTestCheckFunc( 85 testAccCheckDigitalOceanDropletExists("digitalocean_droplet.foobar", &droplet), 86 testAccCheckDigitalOceanDropletAttributes_PrivateNetworkingIpv6(&droplet), 87 resource.TestCheckResourceAttr( 88 "digitalocean_droplet.foobar", "private_networking", "true"), 89 resource.TestCheckResourceAttr( 90 "digitalocean_droplet.foobar", "ipv6", "true"), 91 ), 92 }, 93 }, 94 }) 95 } 96 97 func testAccCheckDigitalOceanDropletDestroy(s *terraform.State) error { 98 client := testAccProvider.Meta().(*godo.Client) 99 100 for _, rs := range s.RootModule().Resources { 101 if rs.Type != "digitalocean_droplet" { 102 continue 103 } 104 105 id, err := strconv.Atoi(rs.Primary.ID) 106 if err != nil { 107 return err 108 } 109 110 // Try to find the Droplet 111 _, _, err = client.Droplets.Get(id) 112 113 // Wait 114 115 if err != nil && !strings.Contains(err.Error(), "404") { 116 return fmt.Errorf( 117 "Error waiting for droplet (%s) to be destroyed: %s", 118 rs.Primary.ID, err) 119 } 120 } 121 122 return nil 123 } 124 125 func testAccCheckDigitalOceanDropletAttributes(droplet *godo.Droplet) resource.TestCheckFunc { 126 return func(s *terraform.State) error { 127 128 if droplet.Image.Slug != "centos-5-8-x32" { 129 return fmt.Errorf("Bad image_slug: %s", droplet.Image.Slug) 130 } 131 132 if droplet.Size.Slug != "512mb" { 133 return fmt.Errorf("Bad size_slug: %s", droplet.Size.Slug) 134 } 135 136 if droplet.Region.Slug != "nyc3" { 137 return fmt.Errorf("Bad region_slug: %s", droplet.Region.Slug) 138 } 139 140 if droplet.Name != "foo" { 141 return fmt.Errorf("Bad name: %s", droplet.Name) 142 } 143 return nil 144 } 145 } 146 147 func testAccCheckDigitalOceanDropletRenamedAndResized(droplet *godo.Droplet) resource.TestCheckFunc { 148 return func(s *terraform.State) error { 149 150 if droplet.Size.Slug != "1gb" { 151 return fmt.Errorf("Bad size_slug: %s", droplet.SizeSlug) 152 } 153 154 if droplet.Name != "baz" { 155 return fmt.Errorf("Bad name: %s", droplet.Name) 156 } 157 158 return nil 159 } 160 } 161 162 func testAccCheckDigitalOceanDropletAttributes_PrivateNetworkingIpv6(droplet *godo.Droplet) resource.TestCheckFunc { 163 return func(s *terraform.State) error { 164 165 if droplet.Image.Slug != "centos-5-8-x32" { 166 return fmt.Errorf("Bad image_slug: %s", droplet.Image.Slug) 167 } 168 169 if droplet.Size.Slug != "1gb" { 170 return fmt.Errorf("Bad size_slug: %s", droplet.Size.Slug) 171 } 172 173 if droplet.Region.Slug != "sgp1" { 174 return fmt.Errorf("Bad region_slug: %s", droplet.Region.Slug) 175 } 176 177 if droplet.Name != "baz" { 178 return fmt.Errorf("Bad name: %s", droplet.Name) 179 } 180 181 if findIPv4AddrByType(droplet, "private") == "" { 182 return fmt.Errorf("No ipv4 private: %s", findIPv4AddrByType(droplet, "private")) 183 } 184 185 // if droplet.IPV6Address("private") == "" { 186 // return fmt.Errorf("No ipv6 private: %s", droplet.IPV6Address("private")) 187 // } 188 189 if findIPv4AddrByType(droplet, "public") == "" { 190 return fmt.Errorf("No ipv4 public: %s", findIPv4AddrByType(droplet, "public")) 191 } 192 193 if findIPv6AddrByType(droplet, "public") == "" { 194 return fmt.Errorf("No ipv6 public: %s", findIPv6AddrByType(droplet, "public")) 195 } 196 197 return nil 198 } 199 } 200 201 func testAccCheckDigitalOceanDropletExists(n string, droplet *godo.Droplet) resource.TestCheckFunc { 202 return func(s *terraform.State) error { 203 rs, ok := s.RootModule().Resources[n] 204 if !ok { 205 return fmt.Errorf("Not found: %s", n) 206 } 207 208 if rs.Primary.ID == "" { 209 return fmt.Errorf("No Droplet ID is set") 210 } 211 212 client := testAccProvider.Meta().(*godo.Client) 213 214 id, err := strconv.Atoi(rs.Primary.ID) 215 if err != nil { 216 return err 217 } 218 219 // Try to find the Droplet 220 retrieveDroplet, _, err := client.Droplets.Get(id) 221 222 if err != nil { 223 return err 224 } 225 226 if strconv.Itoa(retrieveDroplet.ID) != rs.Primary.ID { 227 return fmt.Errorf("Droplet not found") 228 } 229 230 *droplet = *retrieveDroplet 231 232 return nil 233 } 234 } 235 236 // Not sure if this check should remain here as the underlaying 237 // function is changed and is tested indirectly by almost all 238 // other test already 239 // 240 //func Test_new_droplet_state_refresh_func(t *testing.T) { 241 // droplet := godo.Droplet{ 242 // Name: "foobar", 243 // } 244 // resourceMap, _ := resource_digitalocean_droplet_update_state( 245 // &terraform.InstanceState{Attributes: map[string]string{}}, &droplet) 246 // 247 // // See if we can access our attribute 248 // if _, ok := resourceMap.Attributes["name"]; !ok { 249 // t.Fatalf("bad name: %s", resourceMap.Attributes) 250 // } 251 // 252 //} 253 254 const testAccCheckDigitalOceanDropletConfig_basic = ` 255 resource "digitalocean_droplet" "foobar" { 256 name = "foo" 257 size = "512mb" 258 image = "centos-5-8-x32" 259 region = "nyc3" 260 user_data = "foobar" 261 } 262 ` 263 264 const testAccCheckDigitalOceanDropletConfig_RenameAndResize = ` 265 resource "digitalocean_droplet" "foobar" { 266 name = "baz" 267 size = "1gb" 268 image = "centos-5-8-x32" 269 region = "nyc3" 270 } 271 ` 272 273 // IPV6 only in singapore 274 const testAccCheckDigitalOceanDropletConfig_PrivateNetworkingIpv6 = ` 275 resource "digitalocean_droplet" "foobar" { 276 name = "baz" 277 size = "1gb" 278 image = "centos-5-8-x32" 279 region = "sgp1" 280 ipv6 = true 281 private_networking = true 282 } 283 `