mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-20 11:48:24 -06:00
This is an iteration on the great work done by @dalehamel in PRs #2095 and #2109. The core team went back and forth on how to best model Spot Instance Requests, requesting and then rejecting a separate-resource implementation in #2109. After more internal discussion, we landed once again on a separate resource to model Spot Instance Requests. Out of respect for @dalehamel's already-significant donated time, with this I'm attempting to pick up the work to take this across the finish line. Important architectural decisions represented here: * Spot Instance Requests are always of type "persistent", to properly match Terraform's declarative model. * The spot_instance_request resource exports several attributes that are expected to be constantly changing as the spot market changes: spot_bid_status, spot_request_state, and instance_id. Creating additional resource dependencies based on these attributes is not recommended, as Terraform diffs will be continually generated to keep up with the live changes. * When a Spot Instance Request is deleted/canceled, an attempt is made to terminate the last-known attached spot instance. Race conditions dictate that this attempt cannot guarantee that the associated spot instance is terminated immediately. Implementation notes: * This version of aws_spot_instance_request borrows a lot of common code from aws_instance. * In order to facilitate borrowing, we introduce `awsInstanceOpts`, an internal representation of instance details that's meant to be shared between resources. The goal here would be to refactor ASG Launch Configurations to use the same struct. * The new aws_spot_instance_request acc. test is passing. * All aws_instance acc. tests remain passing.
159 lines
4.0 KiB
Go
159 lines
4.0 KiB
Go
package aws
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
"github.com/hashicorp/terraform/terraform"
|
|
)
|
|
|
|
func TestAccAWSSpotInstanceRequest_basic(t *testing.T) {
|
|
var sir ec2.SpotInstanceRequest
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
Providers: testAccProviders,
|
|
CheckDestroy: testAccCheckAWSSpotInstanceRequestDestroy,
|
|
Steps: []resource.TestStep{
|
|
resource.TestStep{
|
|
Config: testAccAWSSpotInstanceRequestConfig,
|
|
Check: resource.ComposeTestCheckFunc(
|
|
testAccCheckAWSSpotInstanceRequestExists(
|
|
"aws_spot_instance_request.foo", &sir),
|
|
testAccCheckAWSSpotInstanceRequestAttributes(&sir),
|
|
resource.TestCheckResourceAttr(
|
|
"aws_spot_instance_request.foo", "spot_bid_status", "fulfilled"),
|
|
resource.TestCheckResourceAttr(
|
|
"aws_spot_instance_request.foo", "spot_request_state", "active"),
|
|
),
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
func testAccCheckAWSSpotInstanceRequestDestroy(s *terraform.State) error {
|
|
conn := testAccProvider.Meta().(*AWSClient).ec2conn
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
if rs.Type != "aws_spot_instance_request" {
|
|
continue
|
|
}
|
|
|
|
req := &ec2.DescribeSpotInstanceRequestsInput{
|
|
SpotInstanceRequestIDs: []*string{aws.String(rs.Primary.ID)},
|
|
}
|
|
|
|
resp, err := conn.DescribeSpotInstanceRequests(req)
|
|
if err == nil {
|
|
if len(resp.SpotInstanceRequests) > 0 {
|
|
return fmt.Errorf("Spot instance request is still here.")
|
|
}
|
|
}
|
|
|
|
// Verify the error is what we expect
|
|
ec2err, ok := err.(awserr.Error)
|
|
if !ok {
|
|
return err
|
|
}
|
|
if ec2err.Code() != "InvalidSpotInstanceRequestID.NotFound" {
|
|
return err
|
|
}
|
|
|
|
// Now check if the associated Spot Instance was also destroyed
|
|
instId := rs.Primary.Attributes["spot_instance_id"]
|
|
instResp, instErr := conn.DescribeInstances(&ec2.DescribeInstancesInput{
|
|
InstanceIDs: []*string{aws.String(instId)},
|
|
})
|
|
if instErr == nil {
|
|
if len(instResp.Reservations) > 0 {
|
|
return fmt.Errorf("Instance still exists.")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// Verify the error is what we expect
|
|
ec2err, ok = err.(awserr.Error)
|
|
if !ok {
|
|
return err
|
|
}
|
|
if ec2err.Code() != "InvalidInstanceID.NotFound" {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func testAccCheckAWSSpotInstanceRequestExists(
|
|
n string, sir *ec2.SpotInstanceRequest) 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 SNS subscription with that ARN exists")
|
|
}
|
|
|
|
conn := testAccProvider.Meta().(*AWSClient).ec2conn
|
|
|
|
params := &ec2.DescribeSpotInstanceRequestsInput{
|
|
SpotInstanceRequestIDs: []*string{&rs.Primary.ID},
|
|
}
|
|
resp, err := conn.DescribeSpotInstanceRequests(params)
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if v := len(resp.SpotInstanceRequests); v != 1 {
|
|
return fmt.Errorf("Expected 1 request returned, got %d", v)
|
|
}
|
|
|
|
*sir = *resp.SpotInstanceRequests[0]
|
|
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func testAccCheckAWSSpotInstanceRequestAttributes(
|
|
sir *ec2.SpotInstanceRequest) resource.TestCheckFunc {
|
|
return func(s *terraform.State) error {
|
|
if *sir.SpotPrice != "0.050000" {
|
|
return fmt.Errorf("Unexpected spot price: %s", *sir.SpotPrice)
|
|
}
|
|
if *sir.State != "active" {
|
|
return fmt.Errorf("Unexpected request state: %s", *sir.State)
|
|
}
|
|
if *sir.Status.Code != "fulfilled" {
|
|
return fmt.Errorf("Unexpected bid status: %s", *sir.State)
|
|
}
|
|
return nil
|
|
}
|
|
}
|
|
|
|
const testAccAWSSpotInstanceRequestConfig = `
|
|
resource "aws_spot_instance_request" "foo" {
|
|
ami = "ami-4fccb37f"
|
|
instance_type = "m1.small"
|
|
|
|
// base price is $0.044 hourly, so bidding above that should theoretically
|
|
// always fulfill
|
|
spot_price = "0.05"
|
|
|
|
// we wait for fulfillment because we want to inspect the launched instance
|
|
// and verify termination behavior
|
|
wait_for_fulfillment = true
|
|
|
|
tags {
|
|
Name = "terraform-test"
|
|
}
|
|
}
|
|
`
|