Skip to content

Commit 34a7b87

Browse files
fix FAAS provider determination when multiple providers are present
1 parent 3b8d1d0 commit 34a7b87

File tree

3 files changed

+26
-19
lines changed

3 files changed

+26
-19
lines changed

src/cmap/handshake/faas_provider.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,23 @@ import type { ClientMetadata } from './client_metadata';
44
export type FAASProvider = 'aws' | 'gcp' | 'azure' | 'vercel' | 'none';
55

66
export function determineCloudProvider(): FAASProvider {
7-
if (process.env.AWS_EXECUTION_ENV || process.env.AWS_LAMBDA_RUNTIME_API) {
8-
return 'aws';
9-
}
10-
11-
if (process.env.FUNCTIONS_WORKER_RUNTIME) {
12-
return 'azure';
13-
}
7+
const awsPresent = process.env.AWS_EXECUTION_ENV || process.env.AWS_LAMBDA_RUNTIME_API;
8+
const azurePresent = process.env.FUNCTIONS_WORKER_RUNTIME;
9+
const gcpPresent = process.env.K_SERVICE || process.env.FUNCTION_NAME;
10+
const vercelPresent = process.env.VERCEL;
1411

15-
if (process.env.K_SERVICE || process.env.FUNCTION_NAME) {
16-
return 'gcp';
17-
}
12+
const numberOfProvidersPresent = [awsPresent, azurePresent, gcpPresent, vercelPresent].filter(
13+
identity
14+
).length;
1815

19-
if (process.env.VERCEL) {
20-
return 'vercel';
16+
if (numberOfProvidersPresent !== 1) {
17+
return 'none';
2118
}
2219

23-
return 'none';
20+
if (awsPresent) return 'aws';
21+
if (azurePresent) return 'azure';
22+
if (gcpPresent) return 'gcp';
23+
return 'vercel';
2424
}
2525

2626
function applyAzureMetadata(m: ClientMetadata): ClientMetadata {

test/integration/mongodb-handshake/mongodb-handshake.prose.test.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,7 @@ context('FAAS Environment Prose Tests', function () {
4949
]
5050
},
5151
{
52-
// TODO - clarify behavior here
53-
expectedProvider: 'aws',
54-
52+
expectedProvider: 'none',
5553
context: '5. Invalid - multiple providers',
5654
env: [
5755
['AWS_EXECUTION_ENV', 'AWS_Lambda_java8'],
@@ -67,7 +65,6 @@ context('FAAS Environment Prose Tests', function () {
6765
]
6866
},
6967
{
70-
// TODO - clarify behavior here
7168
expectedProvider: 'aws',
7269
context: '7. Invalid - wrong types',
7370
env: [
@@ -94,7 +91,7 @@ context('FAAS Environment Prose Tests', function () {
9491
before(`metadata confirmation test for ${name}`, function () {
9592
expect(determineCloudProvider()).to.equal(
9693
expectedProvider,
97-
'determined the wrong cloud provider based'
94+
'determined the wrong cloud provider'
9895
);
9996
});
10097

test/unit/cmap/handshake/client_metadata.test.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,17 @@ describe('client metadata module', () => {
4444
});
4545

4646
context('when there is data from multiple cloud providers in the env', () => {
47-
it.skip('TODO');
47+
before(() => {
48+
process.env.AWS_EXECUTION_ENV = 'non-empty-string';
49+
process.env.FUNCTIONS_WORKER_RUNTIME = 'non-empty-string';
50+
});
51+
after(() => {
52+
delete process.env.AWS_EXECUTION_ENV;
53+
delete process.env.FUNCTIONS_WORKER_RUNTIME;
54+
});
55+
it('parses no FAAS provider', () => {
56+
expect(determineCloudProvider()).to.equal('none');
57+
});
4858
});
4959
});
5060

0 commit comments

Comments
 (0)