github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/scaleway/resource_scaleway_volume_test.go (about) 1 package scaleway 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/hashicorp/terraform/helper/resource" 8 "github.com/hashicorp/terraform/terraform" 9 ) 10 11 func TestAccScalewayVolume_Basic(t *testing.T) { 12 resource.Test(t, resource.TestCase{ 13 PreCheck: func() { testAccPreCheck(t) }, 14 Providers: testAccProviders, 15 CheckDestroy: testAccCheckScalewayVolumeDestroy, 16 Steps: []resource.TestStep{ 17 resource.TestStep{ 18 Config: testAccCheckScalewayVolumeConfig, 19 Check: resource.ComposeTestCheckFunc( 20 testAccCheckScalewayVolumeExists("scaleway_volume.test"), 21 testAccCheckScalewayVolumeAttributes("scaleway_volume.test"), 22 ), 23 }, 24 }, 25 }) 26 } 27 28 func testAccCheckScalewayVolumeDestroy(s *terraform.State) error { 29 client := testAccProvider.Meta().(*Client).scaleway 30 31 for _, rs := range s.RootModule().Resources { 32 if rs.Type != "scaleway" { 33 continue 34 } 35 36 _, err := client.GetVolume(rs.Primary.ID) 37 38 if err == nil { 39 return fmt.Errorf("Volume still exists") 40 } 41 } 42 43 return nil 44 } 45 46 func testAccCheckScalewayVolumeAttributes(n string) resource.TestCheckFunc { 47 return func(s *terraform.State) error { 48 rs, ok := s.RootModule().Resources[n] 49 if !ok { 50 return fmt.Errorf("Unknown resource: %s", n) 51 } 52 53 client := testAccProvider.Meta().(*Client).scaleway 54 volume, err := client.GetVolume(rs.Primary.ID) 55 56 if err != nil { 57 return err 58 } 59 60 if volume.Name != "test" { 61 return fmt.Errorf("volume has wrong name: %q", volume.Name) 62 } 63 if volume.Size != 2e+09 { 64 return fmt.Errorf("volume has wrong size: %d", volume.Size) 65 } 66 if volume.VolumeType != "l_ssd" { 67 return fmt.Errorf("volume has volume type: %q", volume.VolumeType) 68 } 69 70 return nil 71 } 72 } 73 74 func testAccCheckScalewayVolumeExists(n string) resource.TestCheckFunc { 75 return func(s *terraform.State) error { 76 rs, ok := s.RootModule().Resources[n] 77 78 if !ok { 79 return fmt.Errorf("Not found: %s", n) 80 } 81 82 if rs.Primary.ID == "" { 83 return fmt.Errorf("No Volume ID is set") 84 } 85 86 client := testAccProvider.Meta().(*Client).scaleway 87 volume, err := client.GetVolume(rs.Primary.ID) 88 89 if err != nil { 90 return err 91 } 92 93 if volume.Identifier != rs.Primary.ID { 94 return fmt.Errorf("Record not found") 95 } 96 97 return nil 98 } 99 } 100 101 var testAccCheckScalewayVolumeConfig = ` 102 resource "scaleway_volume" "test" { 103 name = "test" 104 size_in_gb = 2 105 type = "l_ssd" 106 } 107 `