Skip to content

Commit

Permalink
[Shared] Defined AWS domains as constants and added a test to validat…
Browse files Browse the repository at this point in the history
…e that the correct domain is returned for a given region.

Signed-off-by: Giacomo Marciani <mgiacomo@amazon.com>
  • Loading branch information
gmarciani committed Jun 12, 2024
1 parent bfcae4f commit debe75b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
13 changes: 9 additions & 4 deletions cookbooks/aws-parallelcluster-shared/libraries/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@ def aws_region
node['cluster']['region']
end

CLASSIC_AWS_DOMAIN = "amazonaws.com".freeze
CHINA_AWS_DOMAIN = "amazonaws.com.cn".freeze
US_ISO_AWS_DOMAIN = "c2s.ic.gov".freeze
US_ISOB_AWS_DOMAIN = "sc2s.sgov.gov".freeze

def aws_domain
# Get the aws domain name
region = aws_region
if region.start_with?("cn-")
"amazonaws.com.cn"
CHINA_AWS_DOMAIN
elsif region.start_with?("us-iso-")
"c2s.ic.gov"
US_ISO_AWS_DOMAIN
elsif region.start_with?("us-isob-")
"sc2s.sgov.gov"
US_ISOB_AWS_DOMAIN
else
"amazonaws.com"
CLASSIC_AWS_DOMAIN
end
end

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require_relative '../../../libraries/environment'
require 'spec_helper'

describe 'aws_domain' do
shared_examples 'a valid aws_domain function' do |region, expected_aws_domain|
it 'returns the correct AWS domain' do
allow_any_instance_of(Object).to receive(:aws_region).and_return(region)
# We must force aws_domain to call the original function because
# the spec_helper configures aws_domain to return a mocked value for all rspec tests.
allow_any_instance_of(Object).to receive(:aws_domain).and_call_original

expect(aws_domain).to eq(expected_aws_domain)
end
end

context 'when in CN region' do
include_examples 'a valid aws_domain function', 'cn-WHATEVER', 'amazonaws.com.cn'
end

context 'when in US-ISO region' do
include_examples 'a valid aws_domain function', 'us-iso-WHATEVER', 'c2s.ic.gov'
end

context 'when in US-ISOB region' do
include_examples 'a valid aws_domain function', 'us-isob-', 'sc2s.sgov.gov'
end

context 'when in GovCloud region' do
include_examples 'a valid aws_domain function', 'us-gov-WHATEVER', 'amazonaws.com'
end

context 'when in whatever else region' do
include_examples 'a valid aws_domain function', 'WHATEVER-ELSE', 'amazonaws.com'
end
end

0 comments on commit debe75b

Please sign in to comment.