Skip to content

Commit debe75b

Browse files
committed
[Shared] Defined AWS domains as constants and added a test to validate that the correct domain is returned for a given region.
Signed-off-by: Giacomo Marciani <[email protected]>
1 parent bfcae4f commit debe75b

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

cookbooks/aws-parallelcluster-shared/libraries/environment.rb

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,22 @@ def aws_region
22
node['cluster']['region']
33
end
44

5+
CLASSIC_AWS_DOMAIN = "amazonaws.com".freeze
6+
CHINA_AWS_DOMAIN = "amazonaws.com.cn".freeze
7+
US_ISO_AWS_DOMAIN = "c2s.ic.gov".freeze
8+
US_ISOB_AWS_DOMAIN = "sc2s.sgov.gov".freeze
9+
510
def aws_domain
611
# Get the aws domain name
712
region = aws_region
813
if region.start_with?("cn-")
9-
"amazonaws.com.cn"
14+
CHINA_AWS_DOMAIN
1015
elsif region.start_with?("us-iso-")
11-
"c2s.ic.gov"
16+
US_ISO_AWS_DOMAIN
1217
elsif region.start_with?("us-isob-")
13-
"sc2s.sgov.gov"
18+
US_ISOB_AWS_DOMAIN
1419
else
15-
"amazonaws.com"
20+
CLASSIC_AWS_DOMAIN
1621
end
1722
end
1823

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
require_relative '../../../libraries/environment'
2+
require 'spec_helper'
3+
4+
describe 'aws_domain' do
5+
shared_examples 'a valid aws_domain function' do |region, expected_aws_domain|
6+
it 'returns the correct AWS domain' do
7+
allow_any_instance_of(Object).to receive(:aws_region).and_return(region)
8+
# We must force aws_domain to call the original function because
9+
# the spec_helper configures aws_domain to return a mocked value for all rspec tests.
10+
allow_any_instance_of(Object).to receive(:aws_domain).and_call_original
11+
12+
expect(aws_domain).to eq(expected_aws_domain)
13+
end
14+
end
15+
16+
context 'when in CN region' do
17+
include_examples 'a valid aws_domain function', 'cn-WHATEVER', 'amazonaws.com.cn'
18+
end
19+
20+
context 'when in US-ISO region' do
21+
include_examples 'a valid aws_domain function', 'us-iso-WHATEVER', 'c2s.ic.gov'
22+
end
23+
24+
context 'when in US-ISOB region' do
25+
include_examples 'a valid aws_domain function', 'us-isob-', 'sc2s.sgov.gov'
26+
end
27+
28+
context 'when in GovCloud region' do
29+
include_examples 'a valid aws_domain function', 'us-gov-WHATEVER', 'amazonaws.com'
30+
end
31+
32+
context 'when in whatever else region' do
33+
include_examples 'a valid aws_domain function', 'WHATEVER-ELSE', 'amazonaws.com'
34+
end
35+
end

0 commit comments

Comments
 (0)