|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const { expect } = require('chai'); |
| 4 | +const _ = require('lodash'); |
| 5 | + |
| 6 | +const resolveMeta = require('../../../../../../../lib/configuration/variables/resolve-meta'); |
| 7 | +const resolve = require('../../../../../../../lib/configuration/variables/resolve'); |
| 8 | +const selfSource = require('../../../../../../../lib/configuration/variables/sources/self'); |
| 9 | +const getParamSource = require('../../../../../../../lib/configuration/variables/sources/instance-dependent/param'); |
| 10 | +const Serverless = require('../../../../../../../lib/serverless'); |
| 11 | + |
| 12 | +describe('test/unit/lib/configuration/variables/sources/instance-dependent/param.test.js', () => { |
| 13 | + let configuration; |
| 14 | + let variablesMeta; |
| 15 | + let serverlessInstance; |
| 16 | + |
| 17 | + const initializeServerless = async ({ configExt, options, setupOptions = {} } = {}) => { |
| 18 | + configuration = { |
| 19 | + service: 'foo', |
| 20 | + provider: { |
| 21 | + name: 'aws', |
| 22 | + deploymentBucket: '${param:bucket}', |
| 23 | + timeout: '${param:timeout}', |
| 24 | + }, |
| 25 | + custom: { |
| 26 | + missingAddress: '${param:}', |
| 27 | + unsupportedAddress: '${param:foo}', |
| 28 | + nonStringAddress: '${param:${self:custom.someObject}}', |
| 29 | + someObject: {}, |
| 30 | + }, |
| 31 | + params: { |
| 32 | + default: { |
| 33 | + bucket: 'global.bucket', |
| 34 | + timeout: 10, |
| 35 | + }, |
| 36 | + dev: { |
| 37 | + bucket: 'my.bucket', |
| 38 | + }, |
| 39 | + }, |
| 40 | + }; |
| 41 | + if (configExt) { |
| 42 | + configuration = _.merge(configuration, configExt); |
| 43 | + } |
| 44 | + variablesMeta = resolveMeta(configuration); |
| 45 | + serverlessInstance = new Serverless({ |
| 46 | + configuration, |
| 47 | + serviceDir: process.cwd(), |
| 48 | + configurationFilename: 'serverless.yml', |
| 49 | + commands: ['package'], |
| 50 | + options: options || {}, |
| 51 | + }); |
| 52 | + serverlessInstance.init(); |
| 53 | + await resolve({ |
| 54 | + serviceDir: process.cwd(), |
| 55 | + configuration, |
| 56 | + variablesMeta, |
| 57 | + sources: { |
| 58 | + self: selfSource, |
| 59 | + param: getParamSource(setupOptions.withoutInstance ? null : serverlessInstance), |
| 60 | + }, |
| 61 | + options: options || {}, |
| 62 | + fulfilledSources: new Set(['self', 'param']), |
| 63 | + }); |
| 64 | + }; |
| 65 | + |
| 66 | + it('should resolve ${param:timeout}', async () => { |
| 67 | + await initializeServerless(); |
| 68 | + if (variablesMeta.get('param\0timeout')) throw variablesMeta.get('param\0timeout').error; |
| 69 | + expect(configuration.provider.timeout).to.equal(10); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should resolve ${param:bucket} for different stages', async () => { |
| 73 | + // Dev by default |
| 74 | + await initializeServerless(); |
| 75 | + expect(configuration.provider.deploymentBucket).to.equal('my.bucket'); |
| 76 | + |
| 77 | + // Forced prod |
| 78 | + await initializeServerless({ |
| 79 | + configExt: { |
| 80 | + provider: { |
| 81 | + stage: 'prod', |
| 82 | + }, |
| 83 | + }, |
| 84 | + }); |
| 85 | + expect(configuration.provider.deploymentBucket).to.equal('global.bucket'); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should resolve ${param:bucket} when no serverless instance available', async () => { |
| 89 | + await initializeServerless({ setupOptions: { withoutInstance: true } }); |
| 90 | + expect(variablesMeta.get('provider\0timeout')).to.have.property('variables'); |
| 91 | + expect(variablesMeta.get('provider\0timeout')).to.not.have.property('error'); |
| 92 | + }); |
| 93 | + |
| 94 | + it('should report with an error missing address', async () => { |
| 95 | + await initializeServerless(); |
| 96 | + expect(variablesMeta.get('custom\0missingAddress').error.code).to.equal( |
| 97 | + 'VARIABLE_RESOLUTION_ERROR' |
| 98 | + ); |
| 99 | + }); |
| 100 | + |
| 101 | + it('should report with an error unsupported address', async () => { |
| 102 | + await initializeServerless(); |
| 103 | + expect(variablesMeta.get('custom\0unsupportedAddress').error.code).to.equal( |
| 104 | + 'VARIABLE_RESOLUTION_ERROR' |
| 105 | + ); |
| 106 | + }); |
| 107 | + |
| 108 | + it('should report with an error a non-string address', async () => { |
| 109 | + await initializeServerless(); |
| 110 | + expect(variablesMeta.get('custom\0nonStringAddress').error.code).to.equal( |
| 111 | + 'VARIABLE_RESOLUTION_ERROR' |
| 112 | + ); |
| 113 | + }); |
| 114 | +}); |
0 commit comments