github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/packet/resource_packet_volume_test.go (about)

     1  package packet
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/acctest"
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  	"github.com/packethost/packngo"
    11  )
    12  
    13  func TestAccPacketVolume_Basic(t *testing.T) {
    14  	var volume packngo.Volume
    15  
    16  	rs := acctest.RandString(10)
    17  
    18  	resource.Test(t, resource.TestCase{
    19  		PreCheck:     func() { testAccPreCheck(t) },
    20  		Providers:    testAccProviders,
    21  		CheckDestroy: testAccCheckPacketVolumeDestroy,
    22  		Steps: []resource.TestStep{
    23  			resource.TestStep{
    24  				Config: fmt.Sprintf(testAccCheckPacketVolumeConfig_basic, rs),
    25  				Check: resource.ComposeTestCheckFunc(
    26  					testAccCheckPacketVolumeExists("packet_volume.foobar", &volume),
    27  					resource.TestCheckResourceAttr(
    28  						"packet_volume.foobar", "plan", "storage_1"),
    29  					resource.TestCheckResourceAttr(
    30  						"packet_volume.foobar", "billing_cycle", "hourly"),
    31  					resource.TestCheckResourceAttr(
    32  						"packet_volume.foobar", "size", "100"),
    33  				),
    34  			},
    35  		},
    36  	})
    37  }
    38  
    39  func testAccCheckPacketVolumeDestroy(s *terraform.State) error {
    40  	client := testAccProvider.Meta().(*packngo.Client)
    41  
    42  	for _, rs := range s.RootModule().Resources {
    43  		if rs.Type != "packet_volume" {
    44  			continue
    45  		}
    46  		if _, _, err := client.Volumes.Get(rs.Primary.ID); err == nil {
    47  			return fmt.Errorf("Volume still exists")
    48  		}
    49  	}
    50  
    51  	return nil
    52  }
    53  
    54  func testAccCheckPacketVolumeExists(n string, volume *packngo.Volume) resource.TestCheckFunc {
    55  	return func(s *terraform.State) error {
    56  		rs, ok := s.RootModule().Resources[n]
    57  		if !ok {
    58  			return fmt.Errorf("Not found: %s", n)
    59  		}
    60  		if rs.Primary.ID == "" {
    61  			return fmt.Errorf("No Record ID is set")
    62  		}
    63  
    64  		client := testAccProvider.Meta().(*packngo.Client)
    65  
    66  		foundVolume, _, err := client.Volumes.Get(rs.Primary.ID)
    67  		if err != nil {
    68  			return err
    69  		}
    70  		if foundVolume.ID != rs.Primary.ID {
    71  			return fmt.Errorf("Record not found: %v - %v", rs.Primary.ID, foundVolume)
    72  		}
    73  
    74  		*volume = *foundVolume
    75  
    76  		return nil
    77  	}
    78  }
    79  
    80  const testAccCheckPacketVolumeConfig_basic = `
    81  resource "packet_project" "foobar" {
    82      name = "%s"
    83  }
    84  
    85  resource "packet_volume" "foobar" {
    86      plan = "storage_1"
    87      billing_cycle = "hourly"
    88      size = 100
    89      project_id = "${packet_project.foobar.id}"
    90      facility = "ewr1"
    91      snapshot_policies = { snapshot_frequency = "1day", snapshot_count = 7 }
    92  }`