Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions aws/resource_aws_volume_attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ func resourceAwsVolumeAttachmentCreate(d *schema.ResourceData, meta interface{})
// a spot request and whilst the request has been fulfilled the
// instance is not running yet
stateConf := &resource.StateChangeConf{
Pending: []string{"pending"},
Target: []string{"running"},
Pending: []string{"pending", "stopping"},
Target: []string{"running", "stopped"},
Refresh: InstanceStateRefreshFunc(conn, iID, "terminated"),
Timeout: 10 * time.Minute,
Delay: 10 * time.Second,
Expand Down
69 changes: 69 additions & 0 deletions aws/resource_aws_volume_attachment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"fmt"
"log"
"testing"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
Expand Down Expand Up @@ -62,6 +64,62 @@ func TestAccAWSVolumeAttachment_skipDestroy(t *testing.T) {
})
}

func TestAccAWSVolumeAttachment_attachStopped(t *testing.T) {
var i ec2.Instance
var v ec2.Volume

stopInstance := func() {
conn := testAccProvider.Meta().(*AWSClient).ec2conn

_, err := conn.StopInstances(&ec2.StopInstancesInput{
InstanceIds: []*string{aws.String(*i.InstanceId)},
})

stateConf := &resource.StateChangeConf{
Pending: []string{"pending", "running", "stopping"},
Target: []string{"stopped"},
Refresh: InstanceStateRefreshFunc(conn, *i.InstanceId, ""),
Timeout: 10 * time.Minute,
Delay: 10 * time.Second,
MinTimeout: 3 * time.Second,
}

_, err = stateConf.WaitForState()
if err != nil {
t.Fatalf("Error waiting for instance(%s) to stop: %s", *i.InstanceId, err)
}
}

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckVolumeAttachmentDestroy,
Steps: []resource.TestStep{
{
Config: testAccVolumeAttachmentConfigInstanceOnly,
Check: resource.ComposeTestCheckFunc(
testAccCheckInstanceExists(
"aws_instance.web", &i),
),
},
{
PreConfig: stopInstance,
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]
Expand Down Expand Up @@ -96,6 +154,17 @@ func testAccCheckVolumeAttachmentDestroy(s *terraform.State) error {
return nil
}

const testAccVolumeAttachmentConfigInstanceOnly = `
resource "aws_instance" "web" {
ami = "ami-21f78e11"
availability_zone = "us-west-2a"
instance_type = "t1.micro"
tags {
Name = "HelloWorld"
}
}
`

const testAccVolumeAttachmentConfig = `
resource "aws_instance" "web" {
ami = "ami-21f78e11"
Expand Down