github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/digitalocean/resource_digitalocean_volume_test.go (about)

     1  package digitalocean
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/digitalocean/godo"
     8  	"github.com/hashicorp/terraform/helper/acctest"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  func TestAccDigitalOceanVolume_Basic(t *testing.T) {
    14  	name := fmt.Sprintf("volume-%s", acctest.RandString(10))
    15  
    16  	volume := godo.Volume{
    17  		Name: name,
    18  	}
    19  
    20  	resource.Test(t, resource.TestCase{
    21  		PreCheck:     func() { testAccPreCheck(t) },
    22  		Providers:    testAccProviders,
    23  		CheckDestroy: testAccCheckDigitalOceanVolumeDestroy,
    24  		Steps: []resource.TestStep{
    25  			{
    26  				Config: fmt.Sprintf(testAccCheckDigitalOceanVolumeConfig_basic, name),
    27  				Check: resource.ComposeTestCheckFunc(
    28  					testAccCheckDigitalOceanVolumeExists("digitalocean_volume.foobar", &volume),
    29  					resource.TestCheckResourceAttr(
    30  						"digitalocean_volume.foobar", "name", name),
    31  					resource.TestCheckResourceAttr(
    32  						"digitalocean_volume.foobar", "size", "100"),
    33  					resource.TestCheckResourceAttr(
    34  						"digitalocean_volume.foobar", "region", "nyc1"),
    35  					resource.TestCheckResourceAttr(
    36  						"digitalocean_volume.foobar", "description", "peace makes plenty"),
    37  				),
    38  			},
    39  		},
    40  	})
    41  }
    42  
    43  const testAccCheckDigitalOceanVolumeConfig_basic = `
    44  resource "digitalocean_volume" "foobar" {
    45  	region      = "nyc1"
    46  	name        = "%s"
    47  	size        = 100
    48  	description = "peace makes plenty"
    49  }`
    50  
    51  func testAccCheckDigitalOceanVolumeExists(rn string, volume *godo.Volume) resource.TestCheckFunc {
    52  	return func(s *terraform.State) error {
    53  		rs, ok := s.RootModule().Resources[rn]
    54  		if !ok {
    55  			return fmt.Errorf("not found: %s", rn)
    56  		}
    57  		if rs.Primary.ID == "" {
    58  			return fmt.Errorf("no volume ID is set")
    59  		}
    60  
    61  		client := testAccProvider.Meta().(*godo.Client)
    62  
    63  		got, _, err := client.Storage.GetVolume(rs.Primary.ID)
    64  		if err != nil {
    65  			return err
    66  		}
    67  		if got.Name != volume.Name {
    68  			return fmt.Errorf("wrong volume found, want %q got %q", volume.Name, got.Name)
    69  		}
    70  		// get the computed volume details
    71  		*volume = *got
    72  		return nil
    73  	}
    74  }
    75  
    76  func testAccCheckDigitalOceanVolumeDestroy(s *terraform.State) error {
    77  	client := testAccProvider.Meta().(*godo.Client)
    78  
    79  	for _, rs := range s.RootModule().Resources {
    80  		if rs.Type != "digitalocean_volume" {
    81  			continue
    82  		}
    83  
    84  		// Try to find the volume
    85  		_, _, err := client.Storage.GetVolume(rs.Primary.ID)
    86  
    87  		if err == nil {
    88  			return fmt.Errorf("Volume still exists")
    89  		}
    90  	}
    91  
    92  	return nil
    93  }
    94  
    95  func TestAccDigitalOceanVolume_Droplet(t *testing.T) {
    96  	var (
    97  		volume  = godo.Volume{Name: fmt.Sprintf("volume-%s", acctest.RandString(10))}
    98  		droplet godo.Droplet
    99  	)
   100  	rInt := acctest.RandInt()
   101  
   102  	resource.Test(t, resource.TestCase{
   103  		PreCheck:     func() { testAccPreCheck(t) },
   104  		Providers:    testAccProviders,
   105  		CheckDestroy: testAccCheckDigitalOceanVolumeDestroy,
   106  		Steps: []resource.TestStep{
   107  			{
   108  				Config: testAccCheckDigitalOceanVolumeConfig_droplet(rInt, volume.Name),
   109  				Check: resource.ComposeTestCheckFunc(
   110  					testAccCheckDigitalOceanVolumeExists("digitalocean_volume.foobar", &volume),
   111  					testAccCheckDigitalOceanDropletExists("digitalocean_droplet.foobar", &droplet),
   112  					// the droplet should see an attached volume
   113  					resource.TestCheckResourceAttr(
   114  						"digitalocean_droplet.foobar", "volume_ids.#", "1"),
   115  				),
   116  			},
   117  		},
   118  	})
   119  }
   120  
   121  func testAccCheckDigitalOceanVolumeConfig_droplet(rInt int, vName string) string {
   122  	return fmt.Sprintf(`
   123  resource "digitalocean_volume" "foobar" {
   124  	region      = "nyc1"
   125  	name        = "%s"
   126  	size        = 100
   127  	description = "peace makes plenty"
   128  }
   129  
   130  resource "digitalocean_droplet" "foobar" {
   131    name               = "baz-%d"
   132    size               = "1gb"
   133    image              = "centos-7-x64"
   134    region             = "nyc1"
   135    ipv6               = true
   136    private_networking = true
   137    volume_ids         = ["${digitalocean_volume.foobar.id}"]
   138  }`, vName, rInt)
   139  }