From fa44e455fa7860f54e7f3394440f0767033d5959 Mon Sep 17 00:00:00 2001 From: Clint Shryock Date: Fri, 22 May 2015 17:02:05 -0500 Subject: [PATCH] provider/aws: Add accdeptance test for volume attachment --- .../aws/resource_aws_ebs_volume_test.go | 36 +++++++ .../resource_aws_volume_attachment_test.go | 93 +++++++++++++++++++ 2 files changed, 129 insertions(+) create mode 100644 builtin/providers/aws/resource_aws_volume_attachment_test.go diff --git a/builtin/providers/aws/resource_aws_ebs_volume_test.go b/builtin/providers/aws/resource_aws_ebs_volume_test.go index 8a555183ff..d5bc212031 100644 --- a/builtin/providers/aws/resource_aws_ebs_volume_test.go +++ b/builtin/providers/aws/resource_aws_ebs_volume_test.go @@ -1,23 +1,59 @@ package aws import ( + "fmt" "testing" + "github.com/awslabs/aws-sdk-go/aws" + "github.com/awslabs/aws-sdk-go/service/ec2" "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" ) func TestAccAWSEBSVolume(t *testing.T) { + var v ec2.Volume resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, Steps: []resource.TestStep{ resource.TestStep{ Config: testAccAwsEbsVolumeConfig, + Check: resource.ComposeTestCheckFunc( + testAccCheckVolumeExists("aws_ebs_volume.test", &v), + ), }, }, }) } +func testAccCheckVolumeExists(n string, v *ec2.Volume) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[n] + if !ok { + return fmt.Errorf("Not found: %s", n) + } + + if rs.Primary.ID == "" { + return fmt.Errorf("No ID is set") + } + + conn := testAccProvider.Meta().(*AWSClient).ec2conn + + request := &ec2.DescribeVolumesInput{ + VolumeIDs: []*string{aws.String(rs.Primary.ID)}, + } + + response, err := conn.DescribeVolumes(request) + if err == nil { + if response.Volumes != nil && len(response.Volumes) > 0 { + *v = *response.Volumes[0] + return nil + } + } + return fmt.Errorf("Error finding EC2 volume %s", rs.Primary.ID) + } +} + const testAccAwsEbsVolumeConfig = ` resource "aws_ebs_volume" "test" { availability_zone = "us-west-2a" diff --git a/builtin/providers/aws/resource_aws_volume_attachment_test.go b/builtin/providers/aws/resource_aws_volume_attachment_test.go new file mode 100644 index 0000000000..dcbcea1f25 --- /dev/null +++ b/builtin/providers/aws/resource_aws_volume_attachment_test.go @@ -0,0 +1,93 @@ +package aws + +import ( + "fmt" + "log" + "testing" + + "github.com/awslabs/aws-sdk-go/service/ec2" + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" +) + +func TestAccAWSVolumeAttachment_basic(t *testing.T) { + var i ec2.Instance + var v ec2.Volume + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckVolumeAttachmentDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccVolumeAttachmentConfig, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr( + "aws_volume_attachment.ebs_att", "device_name", "/dev/sdh"), + testAccCheckInstanceExists( + "aws_instance.web", &i), + testAccCheckVolumeExists( + "aws_ebs_volume.example", &v), + testAccCheckVolumeAttachmentExists( + "aws_volume_attachment.ebs_att", &i, &v), + ), + }, + }, + }) +} + +func testAccCheckVolumeAttachmentExists(n string, i *ec2.Instance, v *ec2.Volume) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[n] + if !ok { + return fmt.Errorf("Not found: %s", n) + } + + if rs.Primary.ID == "" { + return fmt.Errorf("No ID is set") + } + + for _, b := range i.BlockDeviceMappings { + if rs.Primary.Attributes["device_name"] == *b.DeviceName { + if b.EBS.VolumeID != nil && rs.Primary.Attributes["volume_id"] == *b.EBS.VolumeID { + // pass + return nil + } + } + } + + return fmt.Errorf("Error finding instance/volume") + } +} + +func testAccCheckVolumeAttachmentDestroy(s *terraform.State) error { + for _, rs := range s.RootModule().Resources { + log.Printf("\n\n----- This is never called") + if rs.Type != "aws_volume_attachment" { + continue + } + } + return nil +} + +const testAccVolumeAttachmentConfig = ` +resource "aws_instance" "web" { + ami = "ami-21f78e11" + availability_zone = "us-west-2a" + instance_type = "t1.micro" + tags { + Name = "HelloWorld" + } +} + +resource "aws_ebs_volume" "example" { + availability_zone = "us-west-2a" + size = 5 +} + +resource "aws_volume_attachment" "ebs_att" { + device_name = "/dev/sdh" + volume_id = "${aws_ebs_volume.example.id}" + instance_id = "${aws_instance.web.id}" +} +`