github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_volume_attachment_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"testing"
     7  
     8  	"github.com/aws/aws-sdk-go/service/ec2"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  func TestAccAWSVolumeAttachment_basic(t *testing.T) {
    14  	var i ec2.Instance
    15  	var v ec2.Volume
    16  
    17  	resource.Test(t, resource.TestCase{
    18  		PreCheck:     func() { testAccPreCheck(t) },
    19  		Providers:    testAccProviders,
    20  		CheckDestroy: testAccCheckVolumeAttachmentDestroy,
    21  		Steps: []resource.TestStep{
    22  			{
    23  				Config: testAccVolumeAttachmentConfig,
    24  				Check: resource.ComposeTestCheckFunc(
    25  					resource.TestCheckResourceAttr(
    26  						"aws_volume_attachment.ebs_att", "device_name", "/dev/sdh"),
    27  					testAccCheckInstanceExists(
    28  						"aws_instance.web", &i),
    29  					testAccCheckVolumeExists(
    30  						"aws_ebs_volume.example", &v),
    31  					testAccCheckVolumeAttachmentExists(
    32  						"aws_volume_attachment.ebs_att", &i, &v),
    33  				),
    34  			},
    35  		},
    36  	})
    37  }
    38  
    39  func TestAccAWSVolumeAttachment_skipDestroy(t *testing.T) {
    40  	var i ec2.Instance
    41  	var v ec2.Volume
    42  
    43  	resource.Test(t, resource.TestCase{
    44  		PreCheck:     func() { testAccPreCheck(t) },
    45  		Providers:    testAccProviders,
    46  		CheckDestroy: testAccCheckVolumeAttachmentDestroy,
    47  		Steps: []resource.TestStep{
    48  			{
    49  				Config: testAccVolumeAttachmentConfigSkipDestroy,
    50  				Check: resource.ComposeTestCheckFunc(
    51  					resource.TestCheckResourceAttr(
    52  						"aws_volume_attachment.ebs_att", "device_name", "/dev/sdh"),
    53  					testAccCheckInstanceExists(
    54  						"aws_instance.web", &i),
    55  					testAccCheckVolumeExists(
    56  						"aws_ebs_volume.example", &v),
    57  					testAccCheckVolumeAttachmentExists(
    58  						"aws_volume_attachment.ebs_att", &i, &v),
    59  				),
    60  			},
    61  		},
    62  	})
    63  }
    64  
    65  func testAccCheckVolumeAttachmentExists(n string, i *ec2.Instance, v *ec2.Volume) resource.TestCheckFunc {
    66  	return func(s *terraform.State) error {
    67  		rs, ok := s.RootModule().Resources[n]
    68  		if !ok {
    69  			return fmt.Errorf("Not found: %s", n)
    70  		}
    71  
    72  		if rs.Primary.ID == "" {
    73  			return fmt.Errorf("No ID is set")
    74  		}
    75  
    76  		for _, b := range i.BlockDeviceMappings {
    77  			if rs.Primary.Attributes["device_name"] == *b.DeviceName {
    78  				if b.Ebs.VolumeId != nil && rs.Primary.Attributes["volume_id"] == *b.Ebs.VolumeId {
    79  					// pass
    80  					return nil
    81  				}
    82  			}
    83  		}
    84  
    85  		return fmt.Errorf("Error finding instance/volume")
    86  	}
    87  }
    88  
    89  func testAccCheckVolumeAttachmentDestroy(s *terraform.State) error {
    90  	for _, rs := range s.RootModule().Resources {
    91  		log.Printf("\n\n----- This is never called")
    92  		if rs.Type != "aws_volume_attachment" {
    93  			continue
    94  		}
    95  	}
    96  	return nil
    97  }
    98  
    99  const testAccVolumeAttachmentConfig = `
   100  resource "aws_instance" "web" {
   101  	ami = "ami-21f78e11"
   102    availability_zone = "us-west-2a"
   103  	instance_type = "t1.micro"
   104  	tags {
   105  		Name = "HelloWorld"
   106  	}
   107  }
   108  
   109  resource "aws_ebs_volume" "example" {
   110    availability_zone = "us-west-2a"
   111  	size = 1
   112  }
   113  
   114  resource "aws_volume_attachment" "ebs_att" {
   115    device_name = "/dev/sdh"
   116  	volume_id = "${aws_ebs_volume.example.id}"
   117  	instance_id = "${aws_instance.web.id}"
   118  }
   119  `
   120  
   121  const testAccVolumeAttachmentConfigSkipDestroy = `
   122  resource "aws_instance" "web" {
   123  	ami = "ami-21f78e11"
   124    	availability_zone = "us-west-2a"
   125  	instance_type = "t1.micro"
   126  	tags {
   127  		Name = "HelloWorld"
   128  	}
   129  }
   130  
   131  resource "aws_ebs_volume" "example" {
   132    	availability_zone = "us-west-2a"
   133  	size = 1
   134  	tags {
   135  		Name = "TestVolume"
   136  	}
   137  }
   138  
   139  data "aws_ebs_volume" "ebs_volume" {
   140      filter {
   141  	name = "size"
   142  	values = ["${aws_ebs_volume.example.size}"]
   143      }
   144      filter {
   145  	name = "availability-zone"
   146  	values = ["${aws_ebs_volume.example.availability_zone}"]
   147      }
   148      filter {
   149  	name = "tag:Name"
   150  	values = ["TestVolume"]
   151      }
   152  }
   153  
   154  resource "aws_volume_attachment" "ebs_att" {
   155    	device_name = "/dev/sdh"
   156  	volume_id = "${data.aws_ebs_volume.ebs_volume.id}"
   157  	instance_id = "${aws_instance.web.id}"
   158  	skip_destroy = true
   159  }
   160  `