github.com/richardbowden/terraform@v0.6.12-0.20160901200758-30ea22c25211/builtin/providers/scaleway/resource_scaleway_volume_attachment_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 TestAccScalewayVolumeAttachment_Basic(t *testing.T) { 12 resource.Test(t, resource.TestCase{ 13 PreCheck: func() { testAccPreCheck(t) }, 14 Providers: testAccProviders, 15 CheckDestroy: testAccCheckScalewayVolumeAttachmentDestroy, 16 Steps: []resource.TestStep{ 17 resource.TestStep{ 18 Config: testAccCheckScalewayVolumeAttachmentConfig, 19 Check: resource.ComposeTestCheckFunc( 20 testAccCheckScalewayVolumeAttachmentExists("scaleway_volume_attachment.test"), 21 ), 22 }, 23 }, 24 }) 25 } 26 27 func testAccCheckScalewayVolumeAttachmentDestroy(s *terraform.State) error { 28 client := testAccProvider.Meta().(*Client).scaleway 29 30 for _, rs := range s.RootModule().Resources { 31 if rs.Type != "scaleway" { 32 continue 33 } 34 35 s, err := client.GetServer(rs.Primary.Attributes["server"]) 36 if err != nil { 37 fmt.Printf("Failed getting server: %q", err) 38 return err 39 } 40 41 for _, volume := range s.Volumes { 42 if volume.Identifier == rs.Primary.Attributes["volume"] { 43 return fmt.Errorf("Attachment still exists") 44 } 45 } 46 } 47 48 return nil 49 } 50 51 func testAccCheckScalewayVolumeAttachmentExists(n string) resource.TestCheckFunc { 52 return func(s *terraform.State) error { 53 client := testAccProvider.Meta().(*Client).scaleway 54 55 rs, _ := s.RootModule().Resources[n] 56 57 server, err := client.GetServer(rs.Primary.Attributes["server"]) 58 if err != nil { 59 fmt.Printf("Failed getting server: %q", err) 60 return err 61 } 62 63 for _, volume := range server.Volumes { 64 if volume.Identifier == rs.Primary.Attributes["volume"] { 65 return nil 66 } 67 } 68 69 return fmt.Errorf("Attachment does not exist") 70 } 71 } 72 73 var x86_64ImageIdentifier = "aecaed73-51a5-4439-a127-6d8229847145" 74 75 var testAccCheckScalewayVolumeAttachmentConfig = fmt.Sprintf(` 76 resource "scaleway_server" "base" { 77 name = "test" 78 # ubuntu 14.04 79 image = "%s" 80 type = "C2S" 81 # state = "stopped" 82 } 83 84 resource "scaleway_volume" "test" { 85 name = "test" 86 size_in_gb = 20 87 type = "l_ssd" 88 } 89 90 resource "scaleway_volume_attachment" "test" { 91 server = "${scaleway_server.base.id}" 92 volume = "${scaleway_volume.test.id}" 93 }`, x86_64ImageIdentifier)