Skip to content

Commit

Permalink
Test : Add basic test for instance with spot mode
Browse files Browse the repository at this point in the history
  • Loading branch information
sousmangoosta committed Jun 1, 2023
1 parent 8e296b9 commit 1f5c27f
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions internal/service/ec2/ec2_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5551,6 +5551,46 @@ func defaultSubnetCount(ctx context.Context, t *testing.T) int {
return len(subnets)
}

func TestAccEC2Instance_basicWithSpot(t *testing.T) {
ctx := acctest.Context(t)
var v ec2.Instance
resourceName := "aws_instance.test"
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)

resource.ParallelTest(t, resource.TestCase{
// No subnet_id specified requires default VPC with default subnets.
PreCheck: func() { acctest.PreCheck(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, ec2.EndpointsID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckInstanceDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccInstanceConfig_basicWithSpot(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckInstanceExists(ctx, resourceName, &v),
resource.TestCheckResourceAttrSet(resourceName, "spot_instance_request_id"),
resource.TestCheckResourceAttr(resourceName, "instance_lifecycle", "spot"),
resource.TestCheckResourceAttr(resourceName, "instance_market_options.#", "1"),
resource.TestCheckResourceAttr(resourceName, "instance_market_options.0.%", "2"),
resource.TestCheckResourceAttr(resourceName, "instance_market_options.0.market_type", "spot"),
resource.TestCheckResourceAttr(resourceName, "instance_market_options.0.spot_options.#", "1"),
resource.TestCheckResourceAttr(resourceName, "instance_market_options.0.spot_options.0.%", "4"),
resource.TestCheckResourceAttr(resourceName, "instance_market_options.0.spot_options.0.instance_interruption_behavior", "terminate"),
resource.TestCheckResourceAttrSet(resourceName, "instance_market_options.0.spot_options.0.max_price"),
resource.TestCheckResourceAttr(resourceName, "instance_market_options.0.spot_options.0.spot_instance_type", "one-time"),
resource.TestCheckResourceAttr(resourceName, "instance_market_options.0.spot_options.0.valid_until", ""),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"user_data_replace_on_change"},
},
},
})
}

// testAccLatestAmazonLinuxPVEBSAMIConfig returns the configuration for a data source that
// describes the latest Amazon Linux AMI using PV virtualization and an EBS root device.
// The data source is named 'amzn-ami-minimal-pv-ebs'.
Expand Down Expand Up @@ -8849,3 +8889,19 @@ resource "aws_instance" "test" {
}
`, rName))
}

func testAccInstanceConfig_basicWithSpot(rName string) string {
return acctest.ConfigCompose(
acctest.ConfigLatestAmazonLinux2HVMEBSARM64AMI(),
acctest.AvailableEC2InstanceTypeForRegion("t4g.nano"),
fmt.Sprintf(`
resource "aws_instance" "test" {
ami = data.aws_ami.amzn2-ami-minimal-hvm-ebs-arm64.id
instance_market_options {}
instance_type = data.aws_ec2_instance_type_offering.available.instance_type
tags = {
Name = %[1]q
}
}
`, rName))
}

0 comments on commit 1f5c27f

Please sign in to comment.