Skip to content

Commit 3c5cc83

Browse files
authored
RUBY-3152 FaaS environment detection spec changes (#2724)
* VERCEL_URL field was removed in mongodb/specifications@27f9a69 * if vercel & lambda are both present, prefer vercel
1 parent dd3b99b commit 3c5cc83

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

lib/mongo/server/app_metadata/environment.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ class ValueTooLong < Mongo::Error; end
7878
},
7979

8080
'vercel' => {
81-
'VERCEL_URL' => { field: :url, type: :string },
8281
'VERCEL_REGION' => { field: :region, type: :string },
8382
},
8483
}.freeze
@@ -183,6 +182,11 @@ def detect_environment
183182
matches = DISCRIMINATORS.keys.select { |k| discriminator_matches?(k) }
184183
names = matches.map { |m| DISCRIMINATORS[m][:name] }.uniq
185184

185+
# From the spec:
186+
# When variables for multiple ``client.env.name`` values are present,
187+
# ``vercel`` takes precedence over ``aws.lambda``; any other
188+
# combination MUST cause ``client.env`` to be entirely omitted.
189+
return 'vercel' if names.sort == %w[ aws.lambda vercel ]
186190
raise TooManyEnvironments, names.join(', ') if names.length > 1
187191

188192
names.first

spec/integration/connection/faas_env_spec.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323

2424
'Valid Vercel' => {
2525
'VERCEL' => '1',
26-
'VERCEL_URL' => '*.vercel.app',
2726
'VERCEL_REGION' => 'cdg1',
2827
},
2928

spec/mongo/server/app_metadata/environment_spec.rb

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,24 @@
6565
end
6666
end
6767

68+
context 'when VERCEL and AWS are both given' do
69+
local_env(
70+
'AWS_EXECUTION_ENV' => 'AWS_Lambda_ruby2.7',
71+
'AWS_REGION' => 'us-east-2',
72+
'AWS_LAMBDA_FUNCTION_MEMORY_SIZE' => '1024',
73+
'VERCEL' => '1',
74+
'VERCEL_REGION' => 'cdg1'
75+
)
76+
77+
it_behaves_like 'running in a FaaS environment'
78+
79+
it 'prefers vercel' do
80+
expect(env.aws?).to be false
81+
expect(env.vercel?).to be true
82+
expect(env.fields[:region]).to be == 'cdg1'
83+
end
84+
end
85+
6886
context 'when environment is invalid due to missing variable' do
6987
local_env(
7088
'AWS_EXECUTION_ENV' => 'AWS_Lambda_ruby2.7',
@@ -161,15 +179,13 @@
161179
context 'when FaaS environment is Vercel' do
162180
local_env(
163181
'VERCEL' => '1',
164-
'VERCEL_URL' => '*.vercel.app',
165182
'VERCEL_REGION' => 'cdg1'
166183
)
167184

168185
it_behaves_like 'running in a FaaS environment'
169186

170187
it 'recognizes Vercel' do
171188
expect(env.name).to be == 'vercel'
172-
expect(env.fields[:url]).to be == '*.vercel.app'
173189
expect(env.fields[:region]).to be == 'cdg1'
174190
end
175191
end

0 commit comments

Comments
 (0)