|
| 1 | +import { OpenFeature } from '@openfeature/server-sdk'; |
| 2 | +import { AwsSsmProvider } from '../lib/aws-ssm-provider'; |
| 3 | +import { GetParameterCommand, GetParameterCommandOutput, SSMClient } from '@aws-sdk/client-ssm'; |
| 4 | +import { mockClient } from 'aws-sdk-client-mock'; |
| 5 | + |
| 6 | +const ssmMock = mockClient(SSMClient); |
| 7 | + |
| 8 | +describe('AWS SSM Provider E2E', () => { |
| 9 | + const featureFlags = OpenFeature.getClient(); |
| 10 | + OpenFeature.setProvider( |
| 11 | + new AwsSsmProvider({ |
| 12 | + ssmClientConfig: { |
| 13 | + region: 'eu-west-1', |
| 14 | + }, |
| 15 | + cacheOpts: { |
| 16 | + enabled: true, |
| 17 | + size: 1, |
| 18 | + ttl: 10, |
| 19 | + }, |
| 20 | + }), |
| 21 | + ); |
| 22 | + |
| 23 | + describe('when using OpenFeature with AWS SSM Provider to retrieve a boolean', () => { |
| 24 | + it('should use AWS SSM in order to retrieve the value', async () => { |
| 25 | + const res: GetParameterCommandOutput = { |
| 26 | + Parameter: { |
| 27 | + Name: '/lambda/loggingEnabled', |
| 28 | + Value: 'true', |
| 29 | + }, |
| 30 | + $metadata: {}, |
| 31 | + }; |
| 32 | + ssmMock.on(GetParameterCommand).resolves(res); |
| 33 | + const flagValue = await featureFlags.getBooleanValue('/lambda/loggingEnabled', false); |
| 34 | + expect(flagValue).toBe(true); |
| 35 | + }); |
| 36 | + }); |
| 37 | + describe('when using OpenFeature with AWS SSM Provider to retrieve a string', () => { |
| 38 | + it('should use AWS SSM in order to retrieve the value', async () => { |
| 39 | + const res: GetParameterCommandOutput = { |
| 40 | + Parameter: { |
| 41 | + Name: '/lambda/logLevel', |
| 42 | + Value: 'ERROR', |
| 43 | + }, |
| 44 | + $metadata: {}, |
| 45 | + }; |
| 46 | + ssmMock.on(GetParameterCommand).resolves(res); |
| 47 | + const flagValue = await featureFlags.getStringValue('/lambda/logLevel', 'INFO'); |
| 48 | + expect(flagValue).toBe('ERROR'); |
| 49 | + }); |
| 50 | + }); |
| 51 | + describe('when using OpenFeature with AWS SSM Provider to retrieve a number', () => { |
| 52 | + it('should use AWS SSM in order to retrieve the value', async () => { |
| 53 | + const res: GetParameterCommandOutput = { |
| 54 | + Parameter: { |
| 55 | + Name: '/lambda/logRetentionInDays', |
| 56 | + Value: '3', |
| 57 | + }, |
| 58 | + $metadata: {}, |
| 59 | + }; |
| 60 | + ssmMock.on(GetParameterCommand).resolves(res); |
| 61 | + const flagValue = await featureFlags.getNumberValue('/lambda/logRetentionInDays', 14); |
| 62 | + expect(flagValue).toBe(3); |
| 63 | + }); |
| 64 | + }); |
| 65 | + describe('when using OpenFeature with AWS SSM Provider to retrieve an object', () => { |
| 66 | + it('should use AWS SSM in order to retrieve the value', async () => { |
| 67 | + const res: GetParameterCommandOutput = { |
| 68 | + Parameter: { |
| 69 | + Name: '/lambda/env', |
| 70 | + Value: JSON.stringify({ |
| 71 | + PROCESS_NUMBER: 3, |
| 72 | + SOME_ENV_VAR: 4, |
| 73 | + }), |
| 74 | + }, |
| 75 | + $metadata: {}, |
| 76 | + }; |
| 77 | + ssmMock.on(GetParameterCommand).resolves(res); |
| 78 | + const flagValue = await featureFlags.getObjectValue('/lambda/env', {}); |
| 79 | + expect(flagValue).toStrictEqual({ |
| 80 | + PROCESS_NUMBER: 3, |
| 81 | + SOME_ENV_VAR: 4, |
| 82 | + }); |
| 83 | + }); |
| 84 | + }); |
| 85 | +}); |
0 commit comments