terraform/builtin/providers/aws/resource_aws_opsworks_insta...

307 lines
8.3 KiB
Go
Raw Normal View History

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/opsworks"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)
func TestAccAWSOpsworksInstance_importBasic(t *testing.T) {
stackName := fmt.Sprintf("tf-%d", acctest.RandInt())
resourceName := "aws_opsworks_instance.tf-acc"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAwsOpsworksInstanceDestroy,
Steps: []resource.TestStep{
{
Config: testAccAwsOpsworksInstanceConfigCreate(stackName),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"state"}, //state is something we pass to the API and get back as status :(
},
},
})
}
func TestAccAWSOpsworksInstance(t *testing.T) {
stackName := fmt.Sprintf("tf-%d", acctest.RandInt())
var opsinst opsworks.Instance
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAwsOpsworksInstanceDestroy,
Steps: []resource.TestStep{
{
Config: testAccAwsOpsworksInstanceConfigCreate(stackName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSOpsworksInstanceExists(
"aws_opsworks_instance.tf-acc", &opsinst),
testAccCheckAWSOpsworksInstanceAttributes(&opsinst),
resource.TestCheckResourceAttr(
"aws_opsworks_instance.tf-acc", "hostname", "tf-acc1",
),
resource.TestCheckResourceAttr(
"aws_opsworks_instance.tf-acc", "instance_type", "t2.micro",
),
resource.TestCheckResourceAttr(
"aws_opsworks_instance.tf-acc", "state", "stopped",
),
resource.TestCheckResourceAttr(
"aws_opsworks_instance.tf-acc", "layer_ids.#", "1",
),
resource.TestCheckResourceAttr(
"aws_opsworks_instance.tf-acc", "install_updates_on_boot", "true",
),
resource.TestCheckResourceAttr(
"aws_opsworks_instance.tf-acc", "architecture", "x86_64",
),
resource.TestCheckResourceAttr(
"aws_opsworks_instance.tf-acc", "tenancy", "default",
),
resource.TestCheckResourceAttr(
provider/aws: Remove restriction on running aws_opsworks_* on us-east-1 (#12688) Fixes: #11824 A full set of acceptance tests for *all* OpsWorks tests are below. There is a single issue, opsworks_instance isn't ordering the layer_ids so it's non-deterministic which can come back. This introduces a false positive. The opsworks tests are running both against us-east-1 for some and us-west-2 for others. This must be marked as a backwards incompatibility in 0.9 just incase people are using the opsworks region parameter to try and override the provider region ``` % make testacc TEST=./builtin/providers/aws TESTARGS='-run=TestAccAWSOpsworksApplication' 2 ↵ ✹ ==> Checking that code complies with gofmt requirements... go generate $(go list ./... | grep -v /terraform/vendor/) 2017/03/14 17:11:36 Generated command/internal_plugin_list.go TF_ACC=1 go test ./builtin/providers/aws -v -run=TestAccAWSOpsworksApplication -timeout 120m === RUN TestAccAWSOpsworksApplication --- PASS: TestAccAWSOpsworksApplication (164.29s) PASS ok github.com/hashicorp/terraform/builtin/providers/aws 164.319s ``` ``` % make testacc TEST=./builtin/providers/aws TESTARGS='-run=TestAccAWSOpsworksCustomLayer' ✹ ==> Checking that code complies with gofmt requirements... go generate $(go list ./... | grep -v /terraform/vendor/) 2017/03/14 17:16:18 Generated command/internal_plugin_list.go TF_ACC=1 go test ./builtin/providers/aws -v -run=TestAccAWSOpsworksCustomLayer -timeout 120m === RUN TestAccAWSOpsworksCustomLayerImportBasic --- PASS: TestAccAWSOpsworksCustomLayerImportBasic (105.98s) === RUN TestAccAWSOpsworksCustomLayer --- PASS: TestAccAWSOpsworksCustomLayer (92.68s) PASS ok github.com/hashicorp/terraform/builtin/providers/aws 198.684s ``` ``` % make testacc TEST=./builtin/providers/aws TESTARGS='-run=TestAccAWSOpsworksPermission' ✹ ==> Checking that code complies with gofmt requirements... go generate $(go list ./... | grep -v /terraform/vendor/) 2017/03/14 17:23:28 Generated command/internal_plugin_list.go TF_ACC=1 go test ./builtin/providers/aws -v -run=TestAccAWSOpsworksPermission -timeout 120m === RUN TestAccAWSOpsworksPermission --- PASS: TestAccAWSOpsworksPermission (226.36s) PASS ok github.com/hashicorp/terraform/builtin/providers/aws 226.386s ``` ``` % make testacc TEST=./builtin/providers/aws TESTARGS='-run=TestAccAWSOpsworksRailsAppLayer' ✹ ==> Checking that code complies with gofmt requirements... go generate $(go list ./... | grep -v /terraform/vendor/) 2017/03/14 17:28:01 Generated command/internal_plugin_list.go TF_ACC=1 go test ./builtin/providers/aws -v -run=TestAccAWSOpsworksRailsAppLayer -timeout 120m === RUN TestAccAWSOpsworksRailsAppLayer --- PASS: TestAccAWSOpsworksRailsAppLayer (129.39s) PASS ok github.com/hashicorp/terraform/builtin/providers/aws 129.426s ``` ``` % make testacc TEST=./builtin/providers/aws TESTARGS='-run=TestAccAWSOpsworksUserProfile' ✹ ==> Checking that code complies with gofmt requirements... go generate $(go list ./... | grep -v /terraform/vendor/) 2017/03/14 17:37:46 Generated command/internal_plugin_list.go TF_ACC=1 go test ./builtin/providers/aws -v -run=TestAccAWSOpsworksUserProfile -timeout 120m === RUN TestAccAWSOpsworksUserProfile --- PASS: TestAccAWSOpsworksUserProfile (42.60s) PASS ok github.com/hashicorp/terraform/builtin/providers/aws 42.624s ``` ``` % make testacc TEST=./builtin/providers/aws TESTARGS='-run=TestAccAWSOpsworksStack' 2 ↵ ✹ ==> Checking that code complies with gofmt requirements... go generate $(go list ./... | grep -v /terraform/vendor/) 2017/03/14 18:00:50 Generated command/internal_plugin_list.go TF_ACC=1 go test ./builtin/providers/aws -v -run=TestAccAWSOpsworksStack -timeout 120m === RUN TestAccAWSOpsworksStackImportBasic --- PASS: TestAccAWSOpsworksStackImportBasic (50.28s) === RUN TestAccAWSOpsworksStackNoVpc --- PASS: TestAccAWSOpsworksStackNoVpc (25.92s) === RUN TestAccAWSOpsworksStackVpc --- PASS: TestAccAWSOpsworksStackVpc (76.80s) PASS ok github.com/hashicorp/terraform/builtin/providers/aws 153.025s ``` ``` % make testacc TEST=./builtin/providers/aws TESTARGS='-run=TestAccAWSOpsworksRdsDbInstance' ✹ ==> Checking that code complies with gofmt requirements... go generate $(go list ./... | grep -v /terraform/vendor/) 2017/03/14 17:38:36 Generated command/internal_plugin_list.go TF_ACC=1 go test ./builtin/providers/aws -v -run=TestAccAWSOpsworksRdsDbInstance -timeout 120m === RUN TestAccAWSOpsworksRdsDbInstance --- PASS: TestAccAWSOpsworksRdsDbInstance (916.15s) PASS ok github.com/hashicorp/terraform/builtin/providers/aws 916.177s ``` ``` % make testacc TEST=./builtin/providers/aws TESTARGS='-run=TestAccAWSOpsworksInstance' 2 ↵ ==> Checking that code complies with gofmt requirements... go generate $(go list ./... | grep -v /terraform/vendor/) 2017/03/14 17:26:45 Generated command/internal_plugin_list.go TF_ACC=1 go test ./builtin/providers/aws -v -run=TestAccAWSOpsworksInstance -timeout 120m === RUN TestAccAWSOpsworksInstance_importBasic --- PASS: TestAccAWSOpsworksInstance_importBasic (85.52s) === RUN TestAccAWSOpsworksInstance --- FAIL: TestAccAWSOpsworksInstance (114.09s) testing.go:268: Step 1 error: After applying this step, the plan was not empty: DIFF: UPDATE: aws_opsworks_instance.tf-acc layer_ids.0: "4c29b76f-df72-409e-a5c4-9459b7d9b4a4" => "a4a859cf-2a07-447e-a151-a97a6d827db7" layer_ids.1: "a4a859cf-2a07-447e-a151-a97a6d827db7" => "4c29b76f-df72-409e-a5c4-9459b7d9b4a4" FAIL exit status 1 FAIL github.com/hashicorp/terraform/builtin/providers/aws 199.643s make: *** [testacc] Error 1 ```
2017-03-15 15:17:53 +01:00
"aws_opsworks_instance.tf-acc", "os", "Amazon Linux 2016.09", // inherited from opsworks_stack_test
),
resource.TestCheckResourceAttr(
"aws_opsworks_instance.tf-acc", "root_device_type", "ebs", // inherited from opsworks_stack_test
),
resource.TestCheckResourceAttr(
"aws_opsworks_instance.tf-acc", "availability_zone", "us-west-2a", // inherited from opsworks_stack_test
),
),
},
{
Config: testAccAwsOpsworksInstanceConfigUpdate(stackName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSOpsworksInstanceExists(
"aws_opsworks_instance.tf-acc", &opsinst),
testAccCheckAWSOpsworksInstanceAttributes(&opsinst),
resource.TestCheckResourceAttr(
"aws_opsworks_instance.tf-acc", "hostname", "tf-acc1",
),
resource.TestCheckResourceAttr(
"aws_opsworks_instance.tf-acc", "instance_type", "t2.small",
),
resource.TestCheckResourceAttr(
"aws_opsworks_instance.tf-acc", "layer_ids.#", "2",
),
resource.TestCheckResourceAttr(
"aws_opsworks_instance.tf-acc", "os", "Amazon Linux 2015.09",
),
resource.TestCheckResourceAttr(
"aws_opsworks_instance.tf-acc", "tenancy", "default",
),
),
},
},
})
}
func testAccCheckAWSOpsworksInstanceExists(
n string, opsinst *opsworks.Instance) 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 Opsworks Instance is set")
}
conn := testAccProvider.Meta().(*AWSClient).opsworksconn
params := &opsworks.DescribeInstancesInput{
InstanceIds: []*string{&rs.Primary.ID},
}
resp, err := conn.DescribeInstances(params)
if err != nil {
return err
}
if v := len(resp.Instances); v != 1 {
return fmt.Errorf("Expected 1 request returned, got %d", v)
}
*opsinst = *resp.Instances[0]
return nil
}
}
func testAccCheckAWSOpsworksInstanceAttributes(
opsinst *opsworks.Instance) resource.TestCheckFunc {
return func(s *terraform.State) error {
// Depending on the timing, the state could be requested or stopped
if *opsinst.Status != "stopped" && *opsinst.Status != "requested" {
return fmt.Errorf("Unexpected request status: %s", *opsinst.Status)
}
if *opsinst.AvailabilityZone != "us-west-2a" {
return fmt.Errorf("Unexpected availability zone: %s", *opsinst.AvailabilityZone)
}
if *opsinst.Architecture != "x86_64" {
return fmt.Errorf("Unexpected architecture: %s", *opsinst.Architecture)
}
if *opsinst.Tenancy != "default" {
return fmt.Errorf("Unexpected tenancy: %s", *opsinst.Tenancy)
}
if *opsinst.InfrastructureClass != "ec2" {
return fmt.Errorf("Unexpected infrastructure class: %s", *opsinst.InfrastructureClass)
}
if *opsinst.RootDeviceType != "ebs" {
return fmt.Errorf("Unexpected root device type: %s", *opsinst.RootDeviceType)
}
if *opsinst.VirtualizationType != "hvm" {
return fmt.Errorf("Unexpected virtualization type: %s", *opsinst.VirtualizationType)
}
return nil
}
}
func testAccCheckAwsOpsworksInstanceDestroy(s *terraform.State) error {
opsworksconn := testAccProvider.Meta().(*AWSClient).opsworksconn
for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_opsworks_instance" {
continue
}
req := &opsworks.DescribeInstancesInput{
InstanceIds: []*string{
aws.String(rs.Primary.ID),
},
}
_, err := opsworksconn.DescribeInstances(req)
if err != nil {
if awserr, ok := err.(awserr.Error); ok {
if awserr.Code() == "ResourceNotFoundException" {
// not found, good to go
return nil
}
}
return err
}
}
return fmt.Errorf("Fall through error on OpsWorks instance test")
}
func testAccAwsOpsworksInstanceConfigCreate(name string) string {
return fmt.Sprintf(`
resource "aws_security_group" "tf-ops-acc-web" {
name = "%s-web"
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_security_group" "tf-ops-acc-php" {
name = "%s-php"
ingress {
from_port = 8080
to_port = 8080
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_opsworks_static_web_layer" "tf-acc" {
stack_id = "${aws_opsworks_stack.tf-acc.id}"
custom_security_group_ids = [
"${aws_security_group.tf-ops-acc-web.id}",
]
}
resource "aws_opsworks_php_app_layer" "tf-acc" {
stack_id = "${aws_opsworks_stack.tf-acc.id}"
custom_security_group_ids = [
"${aws_security_group.tf-ops-acc-php.id}",
]
}
resource "aws_opsworks_instance" "tf-acc" {
stack_id = "${aws_opsworks_stack.tf-acc.id}"
layer_ids = [
"${aws_opsworks_static_web_layer.tf-acc.id}",
]
instance_type = "t2.micro"
state = "stopped"
hostname = "tf-acc1"
}
%s
`, name, name, testAccAwsOpsworksStackConfigVpcCreate(name))
}
func testAccAwsOpsworksInstanceConfigUpdate(name string) string {
return fmt.Sprintf(`
resource "aws_security_group" "tf-ops-acc-web" {
name = "%s-web"
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_security_group" "tf-ops-acc-php" {
name = "%s-php"
ingress {
from_port = 8080
to_port = 8080
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_opsworks_static_web_layer" "tf-acc" {
stack_id = "${aws_opsworks_stack.tf-acc.id}"
custom_security_group_ids = [
"${aws_security_group.tf-ops-acc-web.id}",
]
}
resource "aws_opsworks_php_app_layer" "tf-acc" {
stack_id = "${aws_opsworks_stack.tf-acc.id}"
custom_security_group_ids = [
"${aws_security_group.tf-ops-acc-php.id}",
]
}
resource "aws_opsworks_instance" "tf-acc" {
stack_id = "${aws_opsworks_stack.tf-acc.id}"
layer_ids = [
"${aws_opsworks_static_web_layer.tf-acc.id}",
"${aws_opsworks_php_app_layer.tf-acc.id}",
]
instance_type = "t2.small"
state = "stopped"
hostname = "tf-acc1"
os = "Amazon Linux 2015.09"
}
%s
`, name, name, testAccAwsOpsworksStackConfigVpcCreate(name))
}