opentofu/builtin/providers/spotinst/resource_spotinst_aws_group_test.go
Liran Polak f37800ae62 New Provider: Spotinst (#5001)
* providers/spotinst: Add support for Spotinst resources

* providers/spotinst: Fix merge conflict - layouts/docs.erb

* docs/providers/spotinst: Fix the resource description field

* providers/spotinst: Fix the acceptance tests

* providers/spotinst: Mark the device_index as a required field

* providers/spotinst: Change the associate_public_ip_address field to TypeBool

* docs/providers/spotinst: Update the description of the adjustment field

* providers/spotinst: Rename IamRole to IamInstanceProfile to make it more compatible with the AWS provider

* docs/providers/spotinst: Rename iam_role to iam_instance_profile

* providers/spotinst: Deprecate the iam_role attribute

* providers/spotinst: Fix a misspelled var (IamRole)

* providers/spotinst: Fix possible null pointer exception related to "iam_instance_profile"

* docs/providers/spotinst: Add "load_balancer_names" missing description

* providers/spotinst: New resource "spotinst_subscription" added

* providers/spotinst: Eliminate a possible null pointer exception in "spotinst_aws_group"

* providers/spotinst: Eliminate a possible null pointer exception in "spotinst_subscription"

* providers/spotinst: Mark spotinst_subscription as deleted in destroy

* providers/spotinst: Add support for custom event format in spotinst_subscription

* providers/spotinst: Disable the destroy step of spotinst_subscription

* providers/spotinst: Add support for update subscriptions

* providers/spotinst: Merge fixed conflict - layouts/docs.erb

* providers/spotinst: Vendor dependencies

* providers/spotinst: Return a detailed error message

* provider/spotinst: Update the plugin list

* providers/spotinst: Vendor dependencies using govendor

* providers/spotinst: New resource "spotinst_healthcheck" added

* providers/spotinst: Update the Spotinst SDK

* providers/spotinst: Comment out unnecessary log.Printf

* providers/spotinst: Fix the acceptance tests

* providers/spotinst: Gofmt fixes

* providers/spotinst: Use multiple functions to expand each block

* providers/spotinst: Allow ondemand_count to be zero

* providers/spotinst: Change security_group_ids from TypeSet to TypeList

* providers/spotinst: Remove unnecessary `ForceNew` fields

* providers/spotinst: Update the Spotinst SDK

* providers/spotinst: Add support for capacity unit

* providers/spotinst: Add support for EBS volume pool

* providers/spotinst: Delete health check

* providers/spotinst: Allow to set multiple availability zones

* providers/spotinst: Gofmt

* providers/spotinst: Omit empty strings from the load_balancer_names field

* providers/spotinst: Update the Spotinst SDK to v1.1.9

* providers/spotinst: Add support for new strategy parameters

* providers/spotinst: Update the Spotinst SDK to v1.2.0

* providers/spotinst: Add support for Kubernetes integration

* providers/spotinst: Fix merge conflict - vendor/vendor.json

* providers/spotinst: Update the Spotinst SDK to v1.2.1

* providers/spotinst: Add support for Application Load Balancers

* providers/spotinst: Do not allow to set ondemand_count to 0

* providers/spotinst: Update the Spotinst SDK to v1.2.2

* providers/spotinst: Add support for scaling policy operators

* providers/spotinst: Add dimensions to spotinst_aws_group tests

* providers/spotinst: Allow both ARN and name for IAM instance profiles

* providers/spotinst: Allow ondemand_count=0

* providers/spotinst: Split out the set funcs into flatten style funcs

* providers/spotinst: Update the Spotinst SDK to v1.2.3

* providers/spotinst: Add support for EBS optimized flag

* providers/spotinst: Update the Spotinst SDK to v2.0.0

* providers/spotinst: Use stringutil.Stringify for debugging

* providers/spotinst: Update the Spotinst SDK to v2.0.1

* providers/spotinst: Key pair is now optional

* providers/spotinst: Make sure we do not nullify signals on strategy update

* providers/spotinst: Hash both Strategy and EBS Block Device

* providers/spotinst: Hash AWS load balancer

* providers/spotinst: Update the Spotinst SDK to v2.0.2

* providers/spotinst: Verify namespace exists before appending policy

* providers/spotinst: Image ID will be in a separate block from now on, so as to allow ignoring changes only on the image ID. This change is backwards compatible.

* providers/spotinst: user data decoded when returned from spotinst api, so that TF compares the two states properly, and does not update without cause.
2017-02-22 22:57:16 +02:00

216 lines
5.4 KiB
Go
Executable File

package spotinst
import (
"fmt"
"testing"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"github.com/spotinst/spotinst-sdk-go/spotinst"
)
func TestAccSpotinstGroup_Basic(t *testing.T) {
var group spotinst.AwsGroup
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckSpotinstGroupDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckSpotinstGroupConfigBasic,
Check: resource.ComposeTestCheckFunc(
testAccCheckSpotinstGroupExists("spotinst_aws_group.foo", &group),
testAccCheckSpotinstGroupAttributes(&group),
resource.TestCheckResourceAttr("spotinst_aws_group.foo", "name", "terraform"),
resource.TestCheckResourceAttr("spotinst_aws_group.foo", "description", "terraform"),
),
},
},
})
}
func TestAccSpotinstGroup_Updated(t *testing.T) {
var group spotinst.AwsGroup
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckSpotinstGroupDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckSpotinstGroupConfigBasic,
Check: resource.ComposeTestCheckFunc(
testAccCheckSpotinstGroupExists("spotinst_aws_group.foo", &group),
testAccCheckSpotinstGroupAttributes(&group),
resource.TestCheckResourceAttr("spotinst_aws_group.foo", "name", "terraform"),
resource.TestCheckResourceAttr("spotinst_aws_group.foo", "description", "terraform"),
),
},
{
Config: testAccCheckSpotinstGroupConfigNewValue,
Check: resource.ComposeTestCheckFunc(
testAccCheckSpotinstGroupExists("spotinst_aws_group.foo", &group),
testAccCheckSpotinstGroupAttributesUpdated(&group),
resource.TestCheckResourceAttr("spotinst_aws_group.foo", "name", "terraform_updated"),
resource.TestCheckResourceAttr("spotinst_aws_group.foo", "description", "terraform_updated"),
),
},
},
})
}
func testAccCheckSpotinstGroupDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*spotinst.Client)
for _, rs := range s.RootModule().Resources {
if rs.Type != "spotinst_aws_group" {
continue
}
input := &spotinst.ReadAwsGroupInput{ID: spotinst.String(rs.Primary.ID)}
resp, err := client.AwsGroupService.Read(input)
if err == nil && resp != nil && resp.Group != nil {
return fmt.Errorf("Group still exists")
}
}
return nil
}
func testAccCheckSpotinstGroupAttributes(group *spotinst.AwsGroup) resource.TestCheckFunc {
return func(s *terraform.State) error {
if spotinst.StringValue(group.Name) != "terraform" {
return fmt.Errorf("Bad content: %v", group.Name)
}
return nil
}
}
func testAccCheckSpotinstGroupAttributesUpdated(group *spotinst.AwsGroup) resource.TestCheckFunc {
return func(s *terraform.State) error {
if spotinst.StringValue(group.Name) != "terraform_updated" {
return fmt.Errorf("Bad content: %v", group.Name)
}
return nil
}
}
func testAccCheckSpotinstGroupExists(n string, group *spotinst.AwsGroup) 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 resource ID is set")
}
client := testAccProvider.Meta().(*spotinst.Client)
input := &spotinst.ReadAwsGroupInput{ID: spotinst.String(rs.Primary.ID)}
resp, err := client.AwsGroupService.Read(input)
if err != nil {
return err
}
if spotinst.StringValue(resp.Group.Name) != rs.Primary.Attributes["name"] {
return fmt.Errorf("Group not found: %+v,\n %+v\n", resp.Group, rs.Primary.Attributes)
}
*group = *resp.Group
return nil
}
}
const testAccCheckSpotinstGroupConfigBasic = `
resource "spotinst_aws_group" "foo" {
name = "terraform"
description = "terraform"
product = "Linux/UNIX"
capacity {
target = 0
minimum = 0
maximum = 5
}
strategy {
risk = 100
}
instance_types {
ondemand = "c3.large"
spot = ["c3.large", "m4.xlarge"]
}
availability_zone {
name = "us-west-2b"
}
launch_specification {
monitoring = false
image_id = "ami-f0091d91"
key_pair = "east"
security_group_ids = ["default"]
}
scaling_up_policy {
policy_name = "Scaling Policy 1"
metric_name = "CPUUtilization"
statistic = "average"
unit = "percent"
threshold = 80
adjustment = 1
namespace = "AWS/EC2"
operator = "gte"
period = 300
evaluation_periods = 2
cooldown = 300
dimensions {
env = "prod"
}
}
}`
const testAccCheckSpotinstGroupConfigNewValue = `
resource "spotinst_aws_group" "foo" {
name = "terraform_updated"
description = "terraform_updated"
product = "Linux/UNIX"
capacity {
target = 0
minimum = 0
maximum = 5
}
strategy {
risk = 100
}
instance_types {
ondemand = "c3.large"
spot = ["c3.large", "m4.xlarge"]
}
availability_zone {
name = "us-west-2b"
}
launch_specification {
monitoring = false
image_id = "ami-f0091d91"
key_pair = "east"
security_group_ids = ["default"]
}
scaling_up_policy {
policy_name = "Scaling Policy 2"
metric_name = "CPUUtilization"
statistic = "average"
unit = "percent"
threshold = 80
adjustment = 1
namespace = "AWS/EC2"
operator = "gte"
period = 300
evaluation_periods = 2
cooldown = 300
dimensions {
env = "dev"
}
}
}`