1
1
'use strict' ;
2
2
3
3
const { expect } = require ( 'chai' ) ;
4
- const _ = require ( 'lodash' ) ;
5
4
6
5
const resolveMeta = require ( '../../../../../../../lib/configuration/variables/resolve-meta' ) ;
7
6
const resolve = require ( '../../../../../../../lib/configuration/variables/resolve' ) ;
@@ -10,105 +9,159 @@ const getParamSource = require('../../../../../../../lib/configuration/variables
10
9
const Serverless = require ( '../../../../../../../lib/serverless' ) ;
11
10
12
11
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' ,
12
+ const runServerless = async ( {
13
+ cliParameters = [ ] ,
14
+ stageParameters = { } ,
15
+ stage,
16
+ resolveWithoutInstance = false ,
17
+ } = { } ) => {
18
+ const configuration = {
19
+ service : 'param-test-service' ,
20
20
provider : {
21
+ stage,
21
22
name : 'aws' ,
22
23
deploymentBucket : '${param:bucket}' ,
23
24
timeout : '${param:timeout}' ,
25
+ region : '${param:region}' ,
24
26
} ,
25
27
custom : {
26
28
missingAddress : '${param:}' ,
27
29
unsupportedAddress : '${param:foo}' ,
28
30
nonStringAddress : '${param:${self:custom.someObject}}' ,
29
31
someObject : { } ,
30
32
} ,
31
- params : {
32
- default : {
33
- bucket : 'global.bucket' ,
34
- timeout : 10 ,
35
- } ,
36
- dev : {
37
- bucket : 'my.bucket' ,
38
- } ,
39
- } ,
33
+ params : stageParameters ,
40
34
} ;
41
- if ( configExt ) {
42
- configuration = _ . merge ( configuration , configExt ) ;
43
- }
44
- variablesMeta = resolveMeta ( configuration ) ;
45
- serverlessInstance = new Serverless ( {
35
+
36
+ const variablesMeta = resolveMeta ( configuration ) ;
37
+
38
+ const serverlessInstance = new Serverless ( {
46
39
configuration,
40
+ options : {
41
+ param : cliParameters ,
42
+ } ,
47
43
serviceDir : process . cwd ( ) ,
48
44
configurationFilename : 'serverless.yml' ,
49
45
commands : [ 'package' ] ,
50
- options : options || { } ,
51
46
} ) ;
47
+
52
48
serverlessInstance . init ( ) ;
49
+
53
50
await resolve ( {
54
51
serviceDir : process . cwd ( ) ,
55
52
configuration,
56
53
variablesMeta,
57
54
sources : {
58
55
self : selfSource ,
59
- param : getParamSource ( setupOptions . withoutInstance ? null : serverlessInstance ) ,
56
+ param : getParamSource ( resolveWithoutInstance ? null : serverlessInstance ) ,
57
+ } ,
58
+ options : {
59
+ param : cliParameters ,
60
60
} ,
61
- options : options || { } ,
62
61
fulfilledSources : new Set ( [ 'self' , 'param' ] ) ,
63
62
} ) ;
63
+
64
+ return {
65
+ configuration,
66
+ serverlessInstance,
67
+ variablesMeta,
68
+ } ;
64
69
} ;
65
70
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 ) ;
71
+ it ( 'should resolve parameters from CLI parameters' , async ( ) => {
72
+ const { configuration } = await runServerless ( {
73
+ cliParameters : [ 'region=eu-west-1' ] ,
74
+ } ) ;
75
+ expect ( configuration . provider . region ) . to . equal ( 'eu-west-1' ) ;
70
76
} ) ;
71
77
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' ) ;
78
+ it ( 'should resolve parameter from parameters for the configured stage' , async ( ) => {
79
+ const { configuration } = await runServerless ( {
80
+ stageParameters : {
81
+ staging : {
82
+ timeout : 10 ,
83
+ } ,
84
+ } ,
85
+ stage : 'staging' ,
86
+ } ) ;
87
+ expect ( configuration . provider . timeout ) . to . equal ( 10 ) ;
88
+ } ) ;
76
89
77
- // Forced prod
78
- await initializeServerless ( {
79
- configExt : {
80
- provider : {
81
- stage : 'prod' ,
90
+ it ( 'should resolve parameter from default parameters if the parameter is not set for the configured stage' , async ( ) => {
91
+ const { configuration } = await runServerless ( {
92
+ stageParameters : {
93
+ staging : { } ,
94
+ default : {
95
+ bucket : 'global.bucket' ,
82
96
} ,
83
97
} ,
98
+ stage : 'staging' ,
84
99
} ) ;
85
100
expect ( configuration . provider . deploymentBucket ) . to . equal ( 'global.bucket' ) ;
86
101
} ) ;
87
102
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' ) ;
103
+ it ( 'should resolve parameter from `dev` parameter if the stage is not configured' , async ( ) => {
104
+ const { configuration } = await runServerless ( {
105
+ stageParameters : {
106
+ dev : {
107
+ timeout : 5 ,
108
+ } ,
109
+ staging : {
110
+ timeout : 10 ,
111
+ } ,
112
+ } ,
113
+ } ) ;
114
+ expect ( configuration . provider . timeout ) . to . equal ( 5 ) ;
92
115
} ) ;
93
116
94
- it ( 'should report with an error missing address' , async ( ) => {
95
- await initializeServerless ( ) ;
117
+ it ( 'should treat CLI parameters with a higher precedence than stage parameters' , async ( ) => {
118
+ const { configuration } = await runServerless ( {
119
+ cliParameters : [ 'region=eu-west-2' ] ,
120
+ stageParameters : {
121
+ staging : {
122
+ region : 'eu-west-1' ,
123
+ } ,
124
+ } ,
125
+ stage : 'staging' ,
126
+ } ) ;
127
+ expect ( configuration . provider . region ) . to . equal ( 'eu-west-2' ) ;
128
+ } ) ;
129
+
130
+ it ( 'should report with an error when the CLI parameter is invalid' , async ( ) => {
131
+ const { variablesMeta } = await runServerless ( {
132
+ cliParameters : [ 'region' ] ,
133
+ } ) ;
134
+
135
+ expect ( variablesMeta . get ( 'provider\0region' ) . error . code ) . to . equal ( 'VARIABLE_RESOLUTION_ERROR' ) ;
136
+ } ) ;
137
+
138
+ it ( 'should report with an error when the address is missing' , async ( ) => {
139
+ const { variablesMeta } = await runServerless ( ) ;
96
140
expect ( variablesMeta . get ( 'custom\0missingAddress' ) . error . code ) . to . equal (
97
141
'VARIABLE_RESOLUTION_ERROR'
98
142
) ;
99
143
} ) ;
100
144
101
- it ( 'should report with an error unsupported address' , async ( ) => {
102
- await initializeServerless ( ) ;
145
+ it ( 'should report with an error when the address is not supported ' , async ( ) => {
146
+ const { variablesMeta } = await runServerless ( ) ;
103
147
expect ( variablesMeta . get ( 'custom\0unsupportedAddress' ) . error . code ) . to . equal (
104
148
'VARIABLE_RESOLUTION_ERROR'
105
149
) ;
106
150
} ) ;
107
151
108
- it ( 'should report with an error a non-string address' , async ( ) => {
109
- await initializeServerless ( ) ;
152
+ it ( 'should report with an error when the address it not a string ' , async ( ) => {
153
+ const { variablesMeta } = await runServerless ( ) ;
110
154
expect ( variablesMeta . get ( 'custom\0nonStringAddress' ) . error . code ) . to . equal (
111
155
'VARIABLE_RESOLUTION_ERROR'
112
156
) ;
113
157
} ) ;
158
+
159
+ it ( 'should still resolve variables when no Serverless instance is available' , async ( ) => {
160
+ const { variablesMeta } = await runServerless ( {
161
+ cliParameters : [ 'timeout=10' ] ,
162
+ resolveWithoutInstance : true ,
163
+ } ) ;
164
+ expect ( variablesMeta . get ( 'provider\0timeout' ) ) . to . have . property ( 'variables' ) ;
165
+ expect ( variablesMeta . get ( 'provider\0timeout' ) ) . to . not . have . property ( 'error' ) ;
166
+ } ) ;
114
167
} ) ;
0 commit comments