-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
provider/aws: Add acceptance tests for aws_cloudformation_stack
- Loading branch information
1 parent
933400c
commit 1bf9603
Showing
1 changed file
with
186 additions
and
0 deletions.
There are no files selected for viewing
186 changes: 186 additions & 0 deletions
186
builtin/providers/aws/resource_aws_cloudformation_stack_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/cloudformation" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
func TestAccAWSCloudFormation_basic(t *testing.T) { | ||
var stack cloudformation.Stack | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckAWSCloudFormationDestroy, | ||
Steps: []resource.TestStep{ | ||
resource.TestStep{ | ||
Config: testAccAWSCloudFormationConfig, | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckCloudFormationStackExists("aws_cloudformation_stack.network", &stack), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccAWSCloudFormation_templateURL(t *testing.T) { | ||
var stack cloudformation.Stack | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckAWSCloudFormationDestroy, | ||
Steps: []resource.TestStep{ | ||
resource.TestStep{ | ||
Config: testAccAWSCloudFormationConfig_coreosK8S, | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckCloudFormationStackExists("aws_cloudformation_stack.asg-demo", &stack), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccAWSCloudFormation_allAttributes(t *testing.T) { | ||
var stack cloudformation.Stack | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckAWSCloudFormationDestroy, | ||
Steps: []resource.TestStep{ | ||
resource.TestStep{ | ||
Config: testAccAWSCloudFormationConfig_allAttributes, | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckCloudFormationStackExists("aws_cloudformation_stack.full", &stack), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckCloudFormationStackExists(n string, stack *cloudformation.Stack) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
rs, ok := s.RootModule().Resources[n] | ||
if !ok { | ||
rs = rs | ||
return fmt.Errorf("Not found: %s", n) | ||
} | ||
|
||
conn := testAccProvider.Meta().(*AWSClient).cfconn | ||
params := &cloudformation.DescribeStacksInput{ | ||
StackName: aws.String(rs.Primary.ID), | ||
} | ||
resp, err := conn.DescribeStacks(params) | ||
if err != nil { | ||
return err | ||
} | ||
if len(resp.Stacks) == 0 { | ||
return fmt.Errorf("CloudFormation stack not found") | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func testAccCheckAWSCloudFormationDestroy(s *terraform.State) error { | ||
conn := testAccProvider.Meta().(*AWSClient).cfconn | ||
|
||
for _, rs := range s.RootModule().Resources { | ||
if rs.Type != "aws_cloudformation_stack" { | ||
continue | ||
} | ||
|
||
params := cloudformation.DescribeStacksInput{ | ||
StackName: aws.String(rs.Primary.ID), | ||
} | ||
|
||
resp, err := conn.DescribeStacks(¶ms) | ||
|
||
if err == nil { | ||
if len(resp.Stacks) != 0 && | ||
*resp.Stacks[0].StackId == rs.Primary.ID { | ||
return fmt.Errorf("CloudFormation stack still exists: %q", rs.Primary.ID) | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
var testAccAWSCloudFormationConfig = ` | ||
resource "aws_cloudformation_stack" "network" { | ||
name = "tf-networking-stack" | ||
template_body = <<STACK | ||
{ | ||
"Resources" : { | ||
"MyVPC": { | ||
"Type" : "AWS::EC2::VPC", | ||
"Properties" : { | ||
"CidrBlock" : "10.0.0.0/16", | ||
"Tags" : [ | ||
{"Key": "Name", "Value": "Primary_CF_VPC"} | ||
] | ||
} | ||
} | ||
} | ||
} | ||
STACK | ||
}` | ||
|
||
var testAccAWSCloudFormationConfig_coreosK8S = ` | ||
resource "aws_key_pair" "deployer" { | ||
key_name = "deployer-key" | ||
public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 [email protected]" | ||
} | ||
resource "aws_cloudformation_stack" "asg-demo" { | ||
name = "tf-asg-demo-stack" | ||
template_url = "https://s3-us-west-2.amazonaws.com/cloudformation-templates-us-west-2/AutoScalingMultiAZWithNotifications.template" | ||
parameters { | ||
InstanceType = "t2.micro" | ||
OperatorEMail = "[email protected]" | ||
KeyName = "${aws_key_pair.deployer.key_name}" | ||
} | ||
} | ||
` | ||
|
||
var testAccAWSCloudFormationConfig_allAttributes = ` | ||
resource "aws_cloudformation_stack" "full" { | ||
name = "tf-full-stack" | ||
template_body = <<STACK | ||
{ | ||
"Resources" : { | ||
"MyVPC": { | ||
"Type" : "AWS::EC2::VPC", | ||
"Properties" : { | ||
"CidrBlock" : "10.0.0.0/16", | ||
"Tags" : [ | ||
{"Key": "Name", "Value": "Primary_CF_VPC"} | ||
] | ||
} | ||
} | ||
} | ||
} | ||
STACK | ||
capabilities = ["CAPABILITY_IAM"] | ||
notification_arns = ["${aws_sns_topic.cf-updates.arn}"] | ||
on_failure = "DELETE" | ||
timeout_in_minutes = 1 | ||
tags { | ||
Name = "My Network" | ||
User = "Terraform" | ||
} | ||
} | ||
resource "aws_sns_topic" "cf-updates" { | ||
name = "tf-cf-notifications" | ||
} | ||
` |