github.com/someshkoli/terratest@v0.41.1/modules/gcp/region_test.go (about)

     1  //go:build gcp
     2  // +build gcp
     3  
     4  // NOTE: We use build tags to differentiate GCP testing for better isolation and parallelism when executing our tests.
     5  
     6  package gcp
     7  
     8  import (
     9  	"strings"
    10  	"testing"
    11  
    12  	"github.com/stretchr/testify/assert"
    13  )
    14  
    15  func TestGetRandomRegion(t *testing.T) {
    16  	t.Parallel()
    17  
    18  	projectID := GetGoogleProjectIDFromEnvVar(t)
    19  
    20  	randomRegion := GetRandomRegion(t, projectID, nil, nil)
    21  	assertLooksLikeRegionName(t, randomRegion)
    22  }
    23  
    24  func TestGetRandomZone(t *testing.T) {
    25  	t.Parallel()
    26  
    27  	projectID := GetGoogleProjectIDFromEnvVar(t)
    28  
    29  	randomZone := GetRandomZone(t, projectID, nil, nil, nil)
    30  	assertLooksLikeZoneName(t, randomZone)
    31  }
    32  
    33  func TestGetRandomRegionExcludesForbiddenRegions(t *testing.T) {
    34  	t.Parallel()
    35  
    36  	projectID := GetGoogleProjectIDFromEnvVar(t)
    37  
    38  	approvedRegions := []string{"asia-east1", "asia-northeast1", "asia-south1", "asia-southeast1", "australia-southeast1", "europe-north1", "europe-west1", "europe-west2", "europe-west3", "northamerica-northeast1", "southamerica-east1", "us-central1", "us-east1", "us-east4", "us-west2"}
    39  	forbiddenRegions := []string{"europe-west4", "us-west1"}
    40  
    41  	for i := 0; i < 1000; i++ {
    42  		randomRegion := GetRandomRegion(t, projectID, approvedRegions, forbiddenRegions)
    43  		assert.NotContains(t, forbiddenRegions, randomRegion)
    44  	}
    45  }
    46  
    47  func TestGetRandomZoneExcludesForbiddenZones(t *testing.T) {
    48  	t.Parallel()
    49  
    50  	projectID := GetGoogleProjectIDFromEnvVar(t)
    51  
    52  	approvedZones := []string{"us-east1-b", "us-east1-c", "us-east1-d", "us-east4-a", "us-east4-b", "us-east4-c", "us-west2-a", "us-west2-b", "us-west2-c", "us-central1-f", "europe-west2-b"}
    53  	forbiddenZones := []string{"us-east1-a", "europe-west1-a", "europe-west2-a", "europe-west2-c"}
    54  
    55  	for i := 0; i < 1000; i++ {
    56  		randomZone := GetRandomZone(t, projectID, approvedZones, forbiddenZones, nil)
    57  		assert.NotContains(t, forbiddenZones, randomZone)
    58  	}
    59  }
    60  
    61  func TestGetRandomZoneExcludesForbiddenRegions(t *testing.T) {
    62  	t.Parallel()
    63  
    64  	projectID := GetGoogleProjectIDFromEnvVar(t)
    65  
    66  	approvedZones := []string{"us-east1-b", "us-east1-c", "us-east1-d", "us-east4-a", "us-east4-b", "us-east4-c", "us-west2-a", "us-west2-b", "us-west2-c", "us-central1-f", "europe-west2-b"}
    67  	forbiddenRegions := []string{"europe-west2"}
    68  
    69  	for i := 0; i < 1000; i++ {
    70  		randomZone := GetRandomZone(t, projectID, approvedZones, nil, forbiddenRegions)
    71  
    72  		for _, forbiddenRegion := range forbiddenRegions {
    73  			assert.True(t, !isInRegion(randomZone, forbiddenRegion), "Expected that selected zone %s would not be in region %s, but it is.", randomZone, forbiddenRegion)
    74  		}
    75  	}
    76  }
    77  
    78  func TestGetAllGcpRegions(t *testing.T) {
    79  	t.Parallel()
    80  
    81  	projectID := GetGoogleProjectIDFromEnvVar(t)
    82  
    83  	regions := GetAllGcpRegions(t, projectID)
    84  
    85  	// The typical account had access to 17 regions as of August, 2018: https://cloud.google.com/compute/docs/regions-zones/
    86  	assert.True(t, len(regions) >= 17, "Number of regions: %d", len(regions))
    87  	for _, region := range regions {
    88  		assertLooksLikeRegionName(t, region)
    89  	}
    90  }
    91  
    92  func TestGetAllGcpZones(t *testing.T) {
    93  	t.Parallel()
    94  
    95  	projectID := GetGoogleProjectIDFromEnvVar(t)
    96  
    97  	zones := GetAllGcpZones(t, projectID)
    98  
    99  	// The typical account had access to 52 zones as of August, 2018: https://cloud.google.com/compute/docs/regions-zones/
   100  	assert.True(t, len(zones) >= 52, "Number of zones: %d", len(zones))
   101  	for _, zone := range zones {
   102  		assertLooksLikeZoneName(t, zone)
   103  	}
   104  }
   105  
   106  func TestGetRandomZoneForRegion(t *testing.T) {
   107  	t.Parallel()
   108  
   109  	projectID := GetGoogleProjectIDFromEnvVar(t)
   110  
   111  	regions := []string{
   112  		"us-west1",
   113  		"us-west2",
   114  		"us-central1",
   115  	}
   116  
   117  	for _, region := range regions {
   118  		zone, err := GetRandomZoneForRegionE(t, projectID, region)
   119  		if err != nil {
   120  			t.Fatal(err)
   121  		}
   122  
   123  		assert.True(t, strings.Contains(zone, region), "Expected zone %s to be in region %s", zone, region)
   124  	}
   125  }
   126  
   127  func TestGetInRegion(t *testing.T) {
   128  	t.Parallel()
   129  
   130  	testData := []struct {
   131  		zone     string
   132  		region   string
   133  		expected bool
   134  	}{
   135  		{"us-west2a", "us-west2", true},
   136  		{"us-west2b", "us-west2", true},
   137  		{"us-west2a", "us-east1", false},
   138  	}
   139  
   140  	for _, td := range testData {
   141  		actual := isInRegion(td.zone, td.region)
   142  		assert.Equal(t, td.expected, actual, "Expected %t for isInRegion(%s, %s) but got %t", td.expected, td.zone, td.region, actual)
   143  	}
   144  }
   145  
   146  func TestGetInRegions(t *testing.T) {
   147  	t.Parallel()
   148  
   149  	testData := []struct {
   150  		zone     string
   151  		regions  []string
   152  		expected bool
   153  	}{
   154  		{"us-west2a", []string{"us-west2", "us-east1"}, true},
   155  		{"us-west2b", []string{"us-west2", "us-east1"}, true},
   156  		{"us-west2a", []string{"us-west2", "us-east1"}, true},
   157  		{"us-west2a", []string{"us-east1", "europe-west1"}, false},
   158  	}
   159  
   160  	for _, td := range testData {
   161  		actual := isInRegions(td.zone, td.regions)
   162  		assert.Equal(t, td.expected, actual, "Expected %t for isInRegions(%s, %v) but got %t", td.expected, td.zone, td.regions, actual)
   163  	}
   164  }
   165  
   166  func assertLooksLikeRegionName(t *testing.T, regionName string) {
   167  	assert.Regexp(t, "[a-z]+-[a-z]+[[:digit:]]+", regionName)
   168  }
   169  
   170  func assertLooksLikeZoneName(t *testing.T, zoneName string) {
   171  	assert.Regexp(t, "[a-z]+-[a-z]+[[:digit:]]+-[a-z]{1}", zoneName)
   172  }