github.com/hobbeswalsh/terraform@v0.3.7-0.20150619183303-ad17cf55a0fa/builtin/providers/aws/resource_aws_spot_instance_request_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/aws/aws-sdk-go/aws"
     8  	"github.com/aws/aws-sdk-go/aws/awserr"
     9  	"github.com/aws/aws-sdk-go/service/ec2"
    10  	"github.com/hashicorp/terraform/helper/resource"
    11  	"github.com/hashicorp/terraform/terraform"
    12  )
    13  
    14  func TestAccAWSSpotInstanceRequest_basic(t *testing.T) {
    15  	var sir ec2.SpotInstanceRequest
    16  
    17  	resource.Test(t, resource.TestCase{
    18  		PreCheck:     func() { testAccPreCheck(t) },
    19  		Providers:    testAccProviders,
    20  		CheckDestroy: testAccCheckAWSSpotInstanceRequestDestroy,
    21  		Steps: []resource.TestStep{
    22  			resource.TestStep{
    23  				Config: testAccAWSSpotInstanceRequestConfig,
    24  				Check: resource.ComposeTestCheckFunc(
    25  					testAccCheckAWSSpotInstanceRequestExists(
    26  						"aws_spot_instance_request.foo", &sir),
    27  					testAccCheckAWSSpotInstanceRequestAttributes(&sir),
    28  					resource.TestCheckResourceAttr(
    29  						"aws_spot_instance_request.foo", "spot_bid_status", "fulfilled"),
    30  					resource.TestCheckResourceAttr(
    31  						"aws_spot_instance_request.foo", "spot_request_state", "active"),
    32  				),
    33  			},
    34  		},
    35  	})
    36  }
    37  
    38  func testAccCheckAWSSpotInstanceRequestDestroy(s *terraform.State) error {
    39  	conn := testAccProvider.Meta().(*AWSClient).ec2conn
    40  
    41  	for _, rs := range s.RootModule().Resources {
    42  		if rs.Type != "aws_spot_instance_request" {
    43  			continue
    44  		}
    45  
    46  		req := &ec2.DescribeSpotInstanceRequestsInput{
    47  			SpotInstanceRequestIDs: []*string{aws.String(rs.Primary.ID)},
    48  		}
    49  
    50  		resp, err := conn.DescribeSpotInstanceRequests(req)
    51  		if err == nil {
    52  			if len(resp.SpotInstanceRequests) > 0 {
    53  				return fmt.Errorf("Spot instance request is still here.")
    54  			}
    55  		}
    56  
    57  		// Verify the error is what we expect
    58  		ec2err, ok := err.(awserr.Error)
    59  		if !ok {
    60  			return err
    61  		}
    62  		if ec2err.Code() != "InvalidSpotInstanceRequestID.NotFound" {
    63  			return err
    64  		}
    65  
    66  		// Now check if the associated Spot Instance was also destroyed
    67  		instId := rs.Primary.Attributes["spot_instance_id"]
    68  		instResp, instErr := conn.DescribeInstances(&ec2.DescribeInstancesInput{
    69  			InstanceIDs: []*string{aws.String(instId)},
    70  		})
    71  		if instErr == nil {
    72  			if len(instResp.Reservations) > 0 {
    73  				return fmt.Errorf("Instance still exists.")
    74  			}
    75  
    76  			return nil
    77  		}
    78  
    79  		// Verify the error is what we expect
    80  		ec2err, ok = err.(awserr.Error)
    81  		if !ok {
    82  			return err
    83  		}
    84  		if ec2err.Code() != "InvalidInstanceID.NotFound" {
    85  			return err
    86  		}
    87  	}
    88  
    89  	return nil
    90  }
    91  
    92  func testAccCheckAWSSpotInstanceRequestExists(
    93  	n string, sir *ec2.SpotInstanceRequest) resource.TestCheckFunc {
    94  	return func(s *terraform.State) error {
    95  		rs, ok := s.RootModule().Resources[n]
    96  		if !ok {
    97  			return fmt.Errorf("Not found: %s", n)
    98  		}
    99  
   100  		if rs.Primary.ID == "" {
   101  			return fmt.Errorf("No SNS subscription with that ARN exists")
   102  		}
   103  
   104  		conn := testAccProvider.Meta().(*AWSClient).ec2conn
   105  
   106  		params := &ec2.DescribeSpotInstanceRequestsInput{
   107  			SpotInstanceRequestIDs: []*string{&rs.Primary.ID},
   108  		}
   109  		resp, err := conn.DescribeSpotInstanceRequests(params)
   110  
   111  		if err != nil {
   112  			return err
   113  		}
   114  
   115  		if v := len(resp.SpotInstanceRequests); v != 1 {
   116  			return fmt.Errorf("Expected 1 request returned, got %d", v)
   117  		}
   118  
   119  		*sir = *resp.SpotInstanceRequests[0]
   120  
   121  		return nil
   122  	}
   123  }
   124  
   125  func testAccCheckAWSSpotInstanceRequestAttributes(
   126  	sir *ec2.SpotInstanceRequest) resource.TestCheckFunc {
   127  	return func(s *terraform.State) error {
   128  		if *sir.SpotPrice != "0.050000" {
   129  			return fmt.Errorf("Unexpected spot price: %s", *sir.SpotPrice)
   130  		}
   131  		if *sir.State != "active" {
   132  			return fmt.Errorf("Unexpected request state: %s", *sir.State)
   133  		}
   134  		if *sir.Status.Code != "fulfilled" {
   135  			return fmt.Errorf("Unexpected bid status: %s", *sir.State)
   136  		}
   137  		return nil
   138  	}
   139  }
   140  
   141  const testAccAWSSpotInstanceRequestConfig = `
   142  resource "aws_spot_instance_request" "foo" {
   143  	ami = "ami-4fccb37f"
   144  	instance_type = "m1.small"
   145  
   146  	// base price is $0.044 hourly, so bidding above that should theoretically
   147  	// always fulfill
   148  	spot_price = "0.05"
   149  
   150  	// we wait for fulfillment because we want to inspect the launched instance
   151  	// and verify termination behavior
   152  	wait_for_fulfillment = true
   153  
   154  	tags {
   155  		Name = "terraform-test"
   156  	}
   157  }
   158  `