github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/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 testAccCheckScalewayVolumeAttachmentConfig = fmt.Sprintf(`
    74  resource "scaleway_server" "base" {
    75    name = "test"
    76    # ubuntu 14.04
    77    image = "%s"
    78    type = "C1"
    79    # state = "stopped"
    80  }
    81  
    82  resource "scaleway_volume" "test" {
    83    name = "test"
    84    size_in_gb = 5
    85    type = "l_ssd"
    86  }
    87  
    88  resource "scaleway_volume_attachment" "test" {
    89    server = "${scaleway_server.base.id}"
    90    volume = "${scaleway_volume.test.id}"
    91  }`, armImageIdentifier)