github.com/darmach/terratest@v0.34.8-0.20210517103231-80931f95e3ff/modules/aws/region_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestGetRandomRegion(t *testing.T) {
    11  	t.Parallel()
    12  
    13  	randomRegion := GetRandomRegion(t, nil, nil)
    14  	assertLooksLikeRegionName(t, randomRegion)
    15  }
    16  
    17  func TestGetRandomRegionExcludesForbiddenRegions(t *testing.T) {
    18  	t.Parallel()
    19  
    20  	approvedRegions := []string{"ca-central-1", "us-east-1", "us-east-2", "us-west-1", "us-west-2", "eu-west-1", "eu-west-2", "eu-central-1", "ap-southeast-1", "ap-northeast-1", "ap-northeast-2", "ap-south-1"}
    21  	forbiddenRegions := []string{"us-west-2", "ap-northeast-2"}
    22  
    23  	for i := 0; i < 1000; i++ {
    24  		randomRegion := GetRandomRegion(t, approvedRegions, forbiddenRegions)
    25  		assert.NotContains(t, forbiddenRegions, randomRegion)
    26  	}
    27  }
    28  
    29  func TestGetAllAwsRegions(t *testing.T) {
    30  	t.Parallel()
    31  
    32  	regions := GetAllAwsRegions(t)
    33  
    34  	// The typical account had access to 15 regions as of April, 2018: https://aws.amazon.com/about-aws/global-infrastructure/
    35  	assert.True(t, len(regions) >= 15, "Number of regions: %d", len(regions))
    36  	for _, region := range regions {
    37  		assertLooksLikeRegionName(t, region)
    38  	}
    39  }
    40  
    41  func assertLooksLikeRegionName(t *testing.T, regionName string) {
    42  	assert.Regexp(t, "[a-z]{2}-[a-z]+?-[[:digit:]]+", regionName)
    43  }
    44  
    45  func TestGetAvailabilityZones(t *testing.T) {
    46  	t.Parallel()
    47  
    48  	randomRegion := GetRandomStableRegion(t, nil, nil)
    49  	azs := GetAvailabilityZones(t, randomRegion)
    50  
    51  	// Every AWS account has access to different AZs, so he best we can do is make sure we get at least one back
    52  	assert.True(t, len(azs) > 1)
    53  	for _, az := range azs {
    54  		assert.Regexp(t, fmt.Sprintf("^%s[a-z]$", randomRegion), az)
    55  	}
    56  }