aws: Add regression test for renaming ecs_cluster

This commit is contained in:
Radek Simko 2015-07-12 14:31:09 +01:00
parent 21e4b5e3cf
commit 9882cc59d8
2 changed files with 113 additions and 0 deletions

View File

@ -2,6 +2,7 @@ package aws
import (
"fmt"
"regexp"
"testing"
"github.com/aws/aws-sdk-go/aws"
@ -128,6 +129,39 @@ func TestAccAWSEcsServiceWithFamilyAndRevision(t *testing.T) {
})
}
// Regression for https://github.com/hashicorp/terraform/issues/2427
func TestAccAWSEcsServiceWithRenamedCluster(t *testing.T) {
originalRegexp := regexp.MustCompile(
"^arn:aws:ecs:[^:]+:[0-9]+:cluster/terraformecstest3$")
modifiedRegexp := regexp.MustCompile(
"^arn:aws:ecs:[^:]+:[0-9]+:cluster/terraformecstest3modified$")
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSEcsServiceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAWSEcsServiceWithRenamedCluster,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSEcsServiceExists("aws_ecs_service.ghost"),
resource.TestMatchResourceAttr(
"aws_ecs_service.ghost", "cluster", originalRegexp),
),
},
resource.TestStep{
Config: testAccAWSEcsServiceWithRenamedClusterModified,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSEcsServiceExists("aws_ecs_service.ghost"),
resource.TestMatchResourceAttr(
"aws_ecs_service.ghost", "cluster", modifiedRegexp),
),
},
},
})
}
func testAccCheckAWSEcsServiceDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).ecsconn
@ -274,3 +308,55 @@ resource "aws_ecs_service" "jenkins" {
desired_count = 1
}
`
var testAccAWSEcsServiceWithRenamedCluster = `
resource "aws_ecs_cluster" "default" {
name = "terraformecstest3"
}
resource "aws_ecs_task_definition" "ghost" {
family = "ghost"
container_definitions = <<DEFINITION
[
{
"cpu": 128,
"essential": true,
"image": "ghost:latest",
"memory": 128,
"name": "ghost"
}
]
DEFINITION
}
resource "aws_ecs_service" "ghost" {
name = "ghost"
cluster = "${aws_ecs_cluster.default.id}"
task_definition = "${aws_ecs_task_definition.ghost.family}:${aws_ecs_task_definition.ghost.revision}"
desired_count = 1
}
`
var testAccAWSEcsServiceWithRenamedClusterModified = `
resource "aws_ecs_cluster" "default" {
name = "terraformecstest3modified"
}
resource "aws_ecs_task_definition" "ghost" {
family = "ghost"
container_definitions = <<DEFINITION
[
{
"cpu": 128,
"essential": true,
"image": "ghost:latest",
"memory": 128,
"name": "ghost"
}
]
DEFINITION
}
resource "aws_ecs_service" "ghost" {
name = "ghost"
cluster = "${aws_ecs_cluster.default.id}"
task_definition = "${aws_ecs_task_definition.ghost.family}:${aws_ecs_task_definition.ghost.revision}"
desired_count = 1
}
`

View File

@ -7,6 +7,7 @@ import (
"log"
"os"
"path/filepath"
"regexp"
"strings"
"testing"
@ -324,6 +325,32 @@ func TestCheckResourceAttr(name, key, value string) TestCheckFunc {
}
}
func TestMatchResourceAttr(name, key string, r *regexp.Regexp) TestCheckFunc {
return func(s *terraform.State) error {
ms := s.RootModule()
rs, ok := ms.Resources[name]
if !ok {
return fmt.Errorf("Not found: %s", name)
}
is := rs.Primary
if is == nil {
return fmt.Errorf("No primary instance: %s", name)
}
if !r.MatchString(is.Attributes[key]) {
return fmt.Errorf(
"%s: Attribute '%s' didn't match %q, got %#v",
name,
key,
r.String(),
is.Attributes[key])
}
return nil
}
}
// TestCheckResourceAttrPtr is like TestCheckResourceAttr except the
// value is a pointer so that it can be updated while the test is running.
// It will only be dereferenced at the point this step is run.