github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/aws/resource_aws_ami_from_instance_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/aws/aws-sdk-go/aws"
    10  	"github.com/aws/aws-sdk-go/aws/awserr"
    11  	"github.com/aws/aws-sdk-go/service/ec2"
    12  	"github.com/hashicorp/terraform/helper/resource"
    13  	"github.com/hashicorp/terraform/terraform"
    14  )
    15  
    16  func TestAccAWSAMIFromInstance(t *testing.T) {
    17  	var amiId string
    18  	snapshots := []string{}
    19  
    20  	resource.Test(t, resource.TestCase{
    21  		PreCheck:  func() { testAccPreCheck(t) },
    22  		Providers: testAccProviders,
    23  		Steps: []resource.TestStep{
    24  			resource.TestStep{
    25  				Config: testAccAWSAMIFromInstanceConfig,
    26  				Check: func(state *terraform.State) error {
    27  					rs, ok := state.RootModule().Resources["aws_ami_from_instance.test"]
    28  					if !ok {
    29  						return fmt.Errorf("AMI resource not found")
    30  					}
    31  
    32  					amiId = rs.Primary.ID
    33  
    34  					if amiId == "" {
    35  						return fmt.Errorf("AMI id is not set")
    36  					}
    37  
    38  					conn := testAccProvider.Meta().(*AWSClient).ec2conn
    39  					req := &ec2.DescribeImagesInput{
    40  						ImageIds: []*string{aws.String(amiId)},
    41  					}
    42  					describe, err := conn.DescribeImages(req)
    43  					if err != nil {
    44  						return err
    45  					}
    46  
    47  					if len(describe.Images) != 1 ||
    48  						*describe.Images[0].ImageId != rs.Primary.ID {
    49  						return fmt.Errorf("AMI not found")
    50  					}
    51  
    52  					image := describe.Images[0]
    53  					if expected := "available"; *image.State != expected {
    54  						return fmt.Errorf("invalid image state; expected %v, got %v", expected, image.State)
    55  					}
    56  					if expected := "machine"; *image.ImageType != expected {
    57  						return fmt.Errorf("wrong image type; expected %v, got %v", expected, image.ImageType)
    58  					}
    59  					if expected := "terraform-acc-ami-from-instance"; *image.Name != expected {
    60  						return fmt.Errorf("wrong name; expected %v, got %v", expected, image.Name)
    61  					}
    62  
    63  					for _, bdm := range image.BlockDeviceMappings {
    64  						if bdm.Ebs != nil && bdm.Ebs.SnapshotId != nil {
    65  							snapshots = append(snapshots, *bdm.Ebs.SnapshotId)
    66  						}
    67  					}
    68  
    69  					if expected := 1; len(snapshots) != expected {
    70  						return fmt.Errorf("wrong number of snapshots; expected %v, got %v", expected, len(snapshots))
    71  					}
    72  
    73  					return nil
    74  				},
    75  			},
    76  		},
    77  		CheckDestroy: func(state *terraform.State) error {
    78  			conn := testAccProvider.Meta().(*AWSClient).ec2conn
    79  			diReq := &ec2.DescribeImagesInput{
    80  				ImageIds: []*string{aws.String(amiId)},
    81  			}
    82  			diRes, err := conn.DescribeImages(diReq)
    83  			if err != nil {
    84  				return err
    85  			}
    86  
    87  			if len(diRes.Images) > 0 {
    88  				state := diRes.Images[0].State
    89  				return fmt.Errorf("AMI %v remains in state %v", amiId, state)
    90  			}
    91  
    92  			stillExist := make([]string, 0, len(snapshots))
    93  			checkErrors := make(map[string]error)
    94  			for _, snapshotId := range snapshots {
    95  				dsReq := &ec2.DescribeSnapshotsInput{
    96  					SnapshotIds: []*string{aws.String(snapshotId)},
    97  				}
    98  				_, err := conn.DescribeSnapshots(dsReq)
    99  				if err == nil {
   100  					stillExist = append(stillExist, snapshotId)
   101  					continue
   102  				}
   103  
   104  				awsErr, ok := err.(awserr.Error)
   105  				if !ok {
   106  					checkErrors[snapshotId] = err
   107  					continue
   108  				}
   109  
   110  				if awsErr.Code() != "InvalidSnapshot.NotFound" {
   111  					checkErrors[snapshotId] = err
   112  					continue
   113  				}
   114  			}
   115  
   116  			if len(stillExist) > 0 || len(checkErrors) > 0 {
   117  				errParts := []string{
   118  					"Expected all snapshots to be gone, but:",
   119  				}
   120  				for _, snapshotId := range stillExist {
   121  					errParts = append(
   122  						errParts,
   123  						fmt.Sprintf("- %v still exists", snapshotId),
   124  					)
   125  				}
   126  				for snapshotId, err := range checkErrors {
   127  					errParts = append(
   128  						errParts,
   129  						fmt.Sprintf("- checking %v gave error: %v", snapshotId, err),
   130  					)
   131  				}
   132  				return errors.New(strings.Join(errParts, "\n"))
   133  			}
   134  
   135  			return nil
   136  		},
   137  	})
   138  }
   139  
   140  var testAccAWSAMIFromInstanceConfig = `
   141  provider "aws" {
   142  	region = "us-east-1"
   143  }
   144  
   145  resource "aws_instance" "test" {
   146      // This AMI has one block device mapping, so we expect to have
   147      // one snapshot in our created AMI.
   148      ami = "ami-408c7f28"
   149      instance_type = "t1.micro"
   150  		tags {
   151  			Name = "testAccAWSAMIFromInstanceConfig_TestAMI"
   152  		}
   153  }
   154  
   155  resource "aws_ami_from_instance" "test" {
   156      name = "terraform-acc-ami-from-instance"
   157      description = "Testing Terraform aws_ami_from_instance resource"
   158      source_instance_id = "${aws_instance.test.id}"
   159  }
   160  `