Skip to content

Commit

Permalink
Merge pull request #3413 from terraform-providers/t-multi-region
Browse files Browse the repository at this point in the history
tests: Simplify multi-region test logic
  • Loading branch information
radeksimko authored Feb 16, 2018
2 parents 893f8c9 + b15cd38 commit 14cb471
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 343 deletions.
80 changes: 59 additions & 21 deletions aws/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"testing"

"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
"github.com/terraform-providers/terraform-provider-template/template"
Expand All @@ -13,6 +14,7 @@ import (

var testAccProviders map[string]terraform.ResourceProvider
var testAccProvidersWithTLS map[string]terraform.ResourceProvider
var testAccProviderFactories func(providers *[]*schema.Provider) map[string]terraform.ResourceProviderFactory
var testAccProvider *schema.Provider
var testAccTemplateProvider *schema.Provider

Expand All @@ -23,6 +25,15 @@ func init() {
"aws": testAccProvider,
"template": testAccTemplateProvider,
}
testAccProviderFactories = func(providers *[]*schema.Provider) map[string]terraform.ResourceProviderFactory {
return map[string]terraform.ResourceProviderFactory{
"aws": func() (terraform.ResourceProvider, error) {
p := Provider()
*providers = append(*providers, p.(*schema.Provider))
return p, nil
},
}
}
testAccProvidersWithTLS = map[string]terraform.ResourceProvider{
"tls": tls.Provider(),
}
Expand Down Expand Up @@ -80,31 +91,58 @@ func testAccEC2ClassicPreCheck(t *testing.T) {
}
}

func testAccAwsRegionProvider(region string, providers *[]*schema.Provider) *schema.Provider {
if region == "" || providers == nil {
return nil
}
log.Printf("[DEBUG] Checking providers for AWS region: %s", region)
for _, provider := range *providers {
// Ignore if Meta is empty, this can happen for validation providers
if provider == nil || provider.Meta() == nil {
log.Printf("[DEBUG] Skipping empty provider")
continue
func testAccAwsRegionProviderFunc(region string, providers *[]*schema.Provider) func() *schema.Provider {
return func() *schema.Provider {
if region == "" {
log.Println("[DEBUG] No region given")
return nil
}
if providers == nil {
log.Println("[DEBUG] No providers given")
return nil
}

// Ignore if Meta is not AWSClient, this will happen for other providers
client, ok := provider.Meta().(*AWSClient)
if !ok {
log.Printf("[DEBUG] Skipping non-AWS provider")
continue
log.Printf("[DEBUG] Checking providers for AWS region: %s", region)
for _, provider := range *providers {
// Ignore if Meta is empty, this can happen for validation providers
if provider == nil || provider.Meta() == nil {
log.Printf("[DEBUG] Skipping empty provider")
continue
}

// Ignore if Meta is not AWSClient, this will happen for other providers
client, ok := provider.Meta().(*AWSClient)
if !ok {
log.Printf("[DEBUG] Skipping non-AWS provider")
continue
}

clientRegion := client.region
log.Printf("[DEBUG] Checking AWS provider region %q against %q", clientRegion, region)
if clientRegion == region {
log.Printf("[DEBUG] Found AWS provider with region: %s", region)
return provider
}
}

clientRegion := client.region
log.Printf("[DEBUG] Checking AWS provider region %q against %q", clientRegion, region)
if clientRegion == region {
log.Printf("[DEBUG] Found AWS provider with region: %s", region)
return provider
log.Printf("[DEBUG] No suitable provider found for %q in %d providers", region, len(*providers))
return nil
}
}

func testAccCheckWithProviders(f func(*terraform.State, *schema.Provider) error, providers *[]*schema.Provider) resource.TestCheckFunc {
return func(s *terraform.State) error {
numberOfProviders := len(*providers)
for i, provider := range *providers {
if provider.Meta() == nil {
log.Printf("[DEBUG] Skipping empty provider %d (total: %d)", i, numberOfProviders)
continue
}
log.Printf("[DEBUG] Calling check with provider %d (total: %d)", i, numberOfProviders)
if err := f(s, provider); err != nil {
return err
}
}
return nil
}
return nil
}
69 changes: 20 additions & 49 deletions aws/resource_aws_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -642,26 +642,19 @@ func TestAccAWSInstance_multipleRegions(t *testing.T) {
// record the initialized providers so that we can use them to
// check for the instances in each region
var providers []*schema.Provider
providerFactories := map[string]terraform.ResourceProviderFactory{
"aws": func() (terraform.ResourceProvider, error) {
p := Provider()
providers = append(providers, p.(*schema.Provider))
return p, nil
},
}

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: providerFactories,
CheckDestroy: testAccCheckInstanceDestroyWithProviders(&providers),
ProviderFactories: testAccProviderFactories(&providers),
CheckDestroy: testAccCheckWithProviders(testAccCheckInstanceDestroyWithProvider, &providers),
Steps: []resource.TestStep{
{
Config: testAccInstanceConfigMultipleRegions,
Check: resource.ComposeTestCheckFunc(
testAccCheckInstanceExistsWithProviders(
"aws_instance.foo", &v, &providers),
testAccCheckInstanceExistsWithProviders(
"aws_instance.bar", &v, &providers),
testAccCheckInstanceExistsWithProvider("aws_instance.foo", &v,
testAccAwsRegionProviderFunc("us-west-2", &providers)),
testAccCheckInstanceExistsWithProvider("aws_instance.bar", &v,
testAccAwsRegionProviderFunc("us-east-1", &providers)),
),
},
},
Expand Down Expand Up @@ -1318,20 +1311,6 @@ func testAccCheckInstanceDestroy(s *terraform.State) error {
return testAccCheckInstanceDestroyWithProvider(s, testAccProvider)
}

func testAccCheckInstanceDestroyWithProviders(providers *[]*schema.Provider) resource.TestCheckFunc {
return func(s *terraform.State) error {
for _, provider := range *providers {
if provider.Meta() == nil {
continue
}
if err := testAccCheckInstanceDestroyWithProvider(s, provider); err != nil {
return err
}
}
return nil
}
}

func testAccCheckInstanceDestroyWithProvider(s *terraform.State, provider *schema.Provider) error {
conn := provider.Meta().(*AWSClient).ec2conn

Expand Down Expand Up @@ -1366,11 +1345,10 @@ func testAccCheckInstanceDestroyWithProvider(s *terraform.State, provider *schem
}

func testAccCheckInstanceExists(n string, i *ec2.Instance) resource.TestCheckFunc {
providers := []*schema.Provider{testAccProvider}
return testAccCheckInstanceExistsWithProviders(n, i, &providers)
return testAccCheckInstanceExistsWithProvider(n, i, func() *schema.Provider { return testAccProvider })
}

func testAccCheckInstanceExistsWithProviders(n string, i *ec2.Instance, providers *[]*schema.Provider) resource.TestCheckFunc {
func testAccCheckInstanceExistsWithProvider(n string, i *ec2.Instance, providerF func() *schema.Provider) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
Expand All @@ -1380,27 +1358,20 @@ func testAccCheckInstanceExistsWithProviders(n string, i *ec2.Instance, provider
if rs.Primary.ID == "" {
return fmt.Errorf("No ID is set")
}
for _, provider := range *providers {
// Ignore if Meta is empty, this can happen for validation providers
if provider.Meta() == nil {
continue
}

conn := provider.Meta().(*AWSClient).ec2conn
resp, err := conn.DescribeInstances(&ec2.DescribeInstancesInput{
InstanceIds: []*string{aws.String(rs.Primary.ID)},
})
if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidInstanceID.NotFound" {
continue
}
if err != nil {
return err
}
provider := providerF()

if len(resp.Reservations) > 0 {
*i = *resp.Reservations[0].Instances[0]
return nil
}
conn := provider.Meta().(*AWSClient).ec2conn
resp, err := conn.DescribeInstances(&ec2.DescribeInstancesInput{
InstanceIds: []*string{aws.String(rs.Primary.ID)},
})
if err != nil {
return err
}

if len(resp.Reservations) > 0 {
*i = *resp.Reservations[0].Instances[0]
return nil
}

return fmt.Errorf("Instance not found")
Expand Down
93 changes: 30 additions & 63 deletions aws/resource_aws_route53_zone_association_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,17 @@ func TestAccAWSRoute53ZoneAssociation_region(t *testing.T) {
// record the initialized providers so that we can use them to
// check for the instances in each region
var providers []*schema.Provider
providerFactories := map[string]terraform.ResourceProviderFactory{
"aws": func() (terraform.ResourceProvider, error) {
p := Provider()
providers = append(providers, p.(*schema.Provider))
return p, nil
},
}

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: providerFactories,
CheckDestroy: testAccCheckRoute53ZoneAssociationDestroyWithProviders(&providers),
ProviderFactories: testAccProviderFactories(&providers),
CheckDestroy: testAccCheckWithProviders(testAccCheckRoute53ZoneAssociationDestroyWithProvider, &providers),
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccRoute53ZoneAssociationRegionConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckRoute53ZoneAssociationExistsWithProviders("aws_route53_zone_association.foobar", &zone, &providers),
testAccCheckRoute53ZoneAssociationExistsWithProvider("aws_route53_zone_association.foobar", &zone,
testAccAwsRegionProviderFunc("us-west-2", &providers)),
),
},
},
Expand All @@ -63,20 +57,6 @@ func testAccCheckRoute53ZoneAssociationDestroy(s *terraform.State) error {
return testAccCheckRoute53ZoneAssociationDestroyWithProvider(s, testAccProvider)
}

func testAccCheckRoute53ZoneAssociationDestroyWithProviders(providers *[]*schema.Provider) resource.TestCheckFunc {
return func(s *terraform.State) error {
for _, provider := range *providers {
if provider.Meta() == nil {
continue
}
if err := testAccCheckRoute53ZoneAssociationDestroyWithProvider(s, provider); err != nil {
return err
}
}
return nil
}
}

func testAccCheckRoute53ZoneAssociationDestroyWithProvider(s *terraform.State, provider *schema.Provider) error {
conn := provider.Meta().(*AWSClient).r53conn
for _, rs := range s.RootModule().Resources {
Expand All @@ -103,55 +83,42 @@ func testAccCheckRoute53ZoneAssociationDestroyWithProvider(s *terraform.State, p
}

func testAccCheckRoute53ZoneAssociationExists(n string, zone *route53.HostedZone) resource.TestCheckFunc {
return func(s *terraform.State) error {
return testAccCheckRoute53ZoneAssociationExistsWithProvider(s, n, zone, testAccProvider)
}
return testAccCheckRoute53ZoneAssociationExistsWithProvider(n, zone, func() *schema.Provider { return testAccProvider })
}

func testAccCheckRoute53ZoneAssociationExistsWithProviders(n string, zone *route53.HostedZone, providers *[]*schema.Provider) resource.TestCheckFunc {
func testAccCheckRoute53ZoneAssociationExistsWithProvider(n string, zone *route53.HostedZone, providerF func() *schema.Provider) resource.TestCheckFunc {
return func(s *terraform.State) error {
for _, provider := range *providers {
if provider.Meta() == nil {
continue
}
if err := testAccCheckRoute53ZoneAssociationExistsWithProvider(s, n, zone, provider); err != nil {
return err
}
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}
return nil
}
}

func testAccCheckRoute53ZoneAssociationExistsWithProvider(s *terraform.State, n string, zone *route53.HostedZone, provider *schema.Provider) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}

if rs.Primary.ID == "" {
return fmt.Errorf("No zone association ID is set")
}
if rs.Primary.ID == "" {
return fmt.Errorf("No zone association ID is set")
}

zone_id, vpc_id := resourceAwsRoute53ZoneAssociationParseId(rs.Primary.ID)
zone_id, vpc_id := resourceAwsRoute53ZoneAssociationParseId(rs.Primary.ID)

conn := provider.Meta().(*AWSClient).r53conn
resp, err := conn.GetHostedZone(&route53.GetHostedZoneInput{Id: aws.String(zone_id)})
if err != nil {
return fmt.Errorf("Hosted zone err: %v", err)
}
provider := providerF()
conn := provider.Meta().(*AWSClient).r53conn
resp, err := conn.GetHostedZone(&route53.GetHostedZoneInput{Id: aws.String(zone_id)})
if err != nil {
return fmt.Errorf("Hosted zone err: %v", err)
}

exists := false
for _, vpc := range resp.VPCs {
if vpc_id == *vpc.VPCId {
exists = true
exists := false
for _, vpc := range resp.VPCs {
if vpc_id == *vpc.VPCId {
exists = true
}
}
if !exists {
return fmt.Errorf("Hosted zone association not found")
}
}
if !exists {
return fmt.Errorf("Hosted zone association not found")
}

*zone = *resp.HostedZone
return nil
*zone = *resp.HostedZone
return nil
}
}

const testAccRoute53ZoneAssociationConfig = `
Expand Down
Loading

0 comments on commit 14cb471

Please sign in to comment.