|
1 | 1 | package product_test |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "os" |
4 | 5 | "testing" |
5 | 6 |
|
| 7 | + "github.com/aws/aws-sdk-go/aws/endpoints" |
6 | 8 | "github.com/posit-dev/team-operator/api/product" |
7 | 9 | "github.com/stretchr/testify/assert" |
8 | 10 | "github.com/stretchr/testify/require" |
@@ -80,3 +82,54 @@ func TestLabelMerge(t *testing.T) { |
80 | 82 | expected = map[string]string{"vorpal": "sword"} |
81 | 83 | assert.Equal(t, expected, result) |
82 | 84 | } |
| 85 | + |
| 86 | +func TestGetAWSRegion(t *testing.T) { |
| 87 | + tests := []struct { |
| 88 | + name string |
| 89 | + awsRegion string |
| 90 | + awsDefault string |
| 91 | + want string |
| 92 | + }{ |
| 93 | + {"AWS_REGION set", "us-west-2", "", "us-west-2"}, |
| 94 | + {"AWS_DEFAULT_REGION set", "", "eu-west-1", "eu-west-1"}, |
| 95 | + {"Both set, AWS_REGION wins", "us-west-2", "eu-west-1", "us-west-2"}, |
| 96 | + {"Neither set, defaults", "", "", endpoints.UsEast2RegionID}, |
| 97 | + } |
| 98 | + |
| 99 | + for _, tt := range tests { |
| 100 | + t.Run(tt.name, func(t *testing.T) { |
| 101 | + // Save original values |
| 102 | + origRegion := os.Getenv("AWS_REGION") |
| 103 | + origDefault := os.Getenv("AWS_DEFAULT_REGION") |
| 104 | + defer func() { |
| 105 | + // Restore original values |
| 106 | + if origRegion != "" { |
| 107 | + os.Setenv("AWS_REGION", origRegion) |
| 108 | + } else { |
| 109 | + os.Unsetenv("AWS_REGION") |
| 110 | + } |
| 111 | + if origDefault != "" { |
| 112 | + os.Setenv("AWS_DEFAULT_REGION", origDefault) |
| 113 | + } else { |
| 114 | + os.Unsetenv("AWS_DEFAULT_REGION") |
| 115 | + } |
| 116 | + }() |
| 117 | + |
| 118 | + // Set test values |
| 119 | + if tt.awsRegion != "" { |
| 120 | + os.Setenv("AWS_REGION", tt.awsRegion) |
| 121 | + } else { |
| 122 | + os.Unsetenv("AWS_REGION") |
| 123 | + } |
| 124 | + if tt.awsDefault != "" { |
| 125 | + os.Setenv("AWS_DEFAULT_REGION", tt.awsDefault) |
| 126 | + } else { |
| 127 | + os.Unsetenv("AWS_DEFAULT_REGION") |
| 128 | + } |
| 129 | + |
| 130 | + if got := product.GetAWSRegion(); got != tt.want { |
| 131 | + t.Errorf("GetAWSRegion() = %v, want %v", got, tt.want) |
| 132 | + } |
| 133 | + }) |
| 134 | + } |
| 135 | +} |
0 commit comments