github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/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 resource.TestStep{ 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 testAccCheckVolumeAttachmentExists(n string, i *ec2.Instance, v *ec2.Volume) resource.TestCheckFunc { 40 return func(s *terraform.State) error { 41 rs, ok := s.RootModule().Resources[n] 42 if !ok { 43 return fmt.Errorf("Not found: %s", n) 44 } 45 46 if rs.Primary.ID == "" { 47 return fmt.Errorf("No ID is set") 48 } 49 50 for _, b := range i.BlockDeviceMappings { 51 if rs.Primary.Attributes["device_name"] == *b.DeviceName { 52 if b.Ebs.VolumeId != nil && rs.Primary.Attributes["volume_id"] == *b.Ebs.VolumeId { 53 // pass 54 return nil 55 } 56 } 57 } 58 59 return fmt.Errorf("Error finding instance/volume") 60 } 61 } 62 63 func testAccCheckVolumeAttachmentDestroy(s *terraform.State) error { 64 for _, rs := range s.RootModule().Resources { 65 log.Printf("\n\n----- This is never called") 66 if rs.Type != "aws_volume_attachment" { 67 continue 68 } 69 } 70 return nil 71 } 72 73 const testAccVolumeAttachmentConfig = ` 74 resource "aws_instance" "web" { 75 ami = "ami-21f78e11" 76 availability_zone = "us-west-2a" 77 instance_type = "t1.micro" 78 tags { 79 Name = "HelloWorld" 80 } 81 } 82 83 resource "aws_ebs_volume" "example" { 84 availability_zone = "us-west-2a" 85 size = 1 86 } 87 88 resource "aws_volume_attachment" "ebs_att" { 89 device_name = "/dev/sdh" 90 volume_id = "${aws_ebs_volume.example.id}" 91 instance_id = "${aws_instance.web.id}" 92 } 93 `