1
1
import fs from "fs" ;
2
2
import mockFs from "mock-fs" ;
3
3
import Serverless from "serverless" ;
4
- import { ServerlessAzureOptions , ServerlessAzureConfig } from "../models/serverless" ;
4
+ import { ServerlessAzureOptions } from "../models/serverless" ;
5
5
import { MockFactory } from "../test/mockFactory" ;
6
6
import { BaseService } from "./baseService" ;
7
- import { AzureNamingService } from "./namingService" ;
8
7
9
8
jest . mock ( "axios" , ( ) => jest . fn ( ) ) ;
10
9
import axios from "axios" ;
@@ -40,11 +39,9 @@ class MockService extends BaseService {
40
39
describe ( "Base Service" , ( ) => {
41
40
let service : MockService ;
42
41
let serverless : Serverless ;
43
- const serviceName = "my-custom-service"
44
- const loginResultSubscriptionId = "ABC123" ;
45
- const envVarSubscriptionId = "env var sub id" ;
46
-
47
42
43
+ const serviceName = "my-custom-service" ;
44
+
48
45
const slsConfig = {
49
46
service : serviceName ,
50
47
provider : {
@@ -59,7 +56,6 @@ describe("Base Service", () => {
59
56
60
57
function createMockService ( sls : Serverless , options ?: Serverless . Options ) {
61
58
sls . variables [ "azureCredentials" ] = MockFactory . createTestAzureCredentials ( ) ;
62
- sls . variables [ "subscriptionId" ] = loginResultSubscriptionId ;
63
59
Object . assign ( sls . service , slsConfig ) ;
64
60
65
61
return new MockService ( sls , options ) ;
@@ -87,65 +83,7 @@ describe("Base Service", () => {
87
83
expect ( mockService ) . not . toBeNull ( ) ;
88
84
expect ( serverless . service . provider . region ) . toEqual ( "westus" ) ;
89
85
expect ( serverless . service . provider . stage ) . toEqual ( "dev" ) ;
90
- } ) ;
91
-
92
- it ( "returns region and stage based on CLI options" , ( ) => {
93
- const cliOptions = {
94
- stage : "prod" ,
95
- region : "eastus2" ,
96
- } ;
97
- const mockService = new MockService ( serverless , cliOptions ) ;
98
-
99
- expect ( mockService . getRegion ( ) ) . toEqual ( cliOptions . region ) ;
100
- expect ( mockService . getStage ( ) ) . toEqual ( cliOptions . stage ) ;
101
- } ) ;
102
-
103
- it ( "use the resource group name specified in CLI" , ( ) => {
104
- const resourceGroupName = "cliResourceGroupName"
105
- const cliOptions = {
106
- stage : "prod" ,
107
- region : "eastus2" ,
108
- resourceGroup : resourceGroupName
109
- } ;
110
-
111
- const mockService = new MockService ( serverless , cliOptions ) ;
112
- const actualResourceGroupName = mockService . getResourceGroupName ( ) ;
113
-
114
- expect ( actualResourceGroupName ) . toEqual ( resourceGroupName ) ;
115
- } ) ;
116
-
117
- it ( "use the resource group name from sls yaml config" , ( ) => {
118
- const mockService = new MockService ( serverless ) ;
119
- const actualResourceGroupName = mockService . getResourceGroupName ( ) ;
120
-
121
- expect ( actualResourceGroupName ) . toEqual ( serverless . service . provider [ "resourceGroup" ] ) ;
122
- } ) ;
123
-
124
- it ( "Generates resource group from convention when NOT defined in sls yaml" , ( ) => {
125
- serverless . service . provider [ "resourceGroup" ] = null ;
126
- const mockService = new MockService ( serverless ) ;
127
- const actualResourceGroupName = mockService . getResourceGroupName ( ) ;
128
- const expectedRegion = AzureNamingService . createShortAzureRegionName ( mockService . getRegion ( ) ) ;
129
- const expectedStage = AzureNamingService . createShortStageName ( mockService . getStage ( ) ) ;
130
- const expectedResourceGroupName = `sls-${ expectedRegion } -${ expectedStage } -${ serverless . service [ "service" ] } -rg` ;
131
-
132
- expect ( actualResourceGroupName ) . toEqual ( expectedResourceGroupName ) ;
133
- } ) ;
134
-
135
- it ( "set default prefix when one is not defined in yaml config" , ( ) => {
136
- const mockService = new MockService ( serverless ) ;
137
- const actualPrefix = mockService . getPrefix ( ) ;
138
- expect ( actualPrefix ) . toEqual ( "sls" ) ;
139
- } ) ;
140
-
141
- it ( "use the prefix defined in sls yaml config" , ( ) => {
142
- const expectedPrefix = "testPrefix"
143
- serverless . service . provider [ "prefix" ] = expectedPrefix ;
144
- const mockService = new MockService ( serverless ) ;
145
- const actualPrefix = mockService . getPrefix ( ) ;
146
-
147
- expect ( actualPrefix ) . toEqual ( expectedPrefix ) ;
148
- } ) ;
86
+ } ) ;
149
87
150
88
it ( "Fails if credentials have not been set in serverless config" , ( ) => {
151
89
serverless . variables [ "azureCredentials" ] = null ;
@@ -202,91 +140,4 @@ describe("Base Service", () => {
202
140
203
141
readStreamSpy . mockRestore ( ) ;
204
142
} ) ;
205
-
206
- it ( "sets stage name from CLI" , async ( ) => {
207
- const stage = "test" ;
208
- delete ( serverless . service as any as ServerlessAzureConfig ) . provider . resourceGroup ;
209
- expect ( serverless . service . provider . stage ) . not . toEqual ( stage ) ;
210
- service = new MockService ( serverless , { stage } as any ) ;
211
- expect ( service . getStage ( ) ) . toEqual ( stage ) ;
212
- expect ( service . getResourceGroupName ( ) ) . toEqual ( `sls-wus-${ stage } -${ serviceName } -rg` ) ;
213
- } ) ;
214
-
215
- it ( "sets region name from CLI" , async ( ) => {
216
- const region = "East US" ;
217
- delete ( serverless . service as any as ServerlessAzureConfig ) . provider . resourceGroup ;
218
- expect ( serverless . service . provider . region ) . not . toEqual ( region ) ;
219
- service = new MockService ( serverless , { region } as any ) ;
220
- expect ( service . getRegion ( ) ) . toEqual ( region ) ;
221
- expect ( service . getResourceGroupName ( ) ) . toEqual ( `sls-eus-dev-${ serviceName } -rg` ) ;
222
- } ) ;
223
-
224
- it ( "sets prefix from CLI" , async ( ) => {
225
- const prefix = "prefix" ;
226
- delete ( serverless . service as any as ServerlessAzureConfig ) . provider . resourceGroup ;
227
- expect ( serverless . service . provider [ "prefix" ] ) . not . toEqual ( prefix ) ;
228
- service = new MockService ( serverless , { prefix } as any ) ;
229
- expect ( service . getPrefix ( ) ) . toEqual ( prefix ) ;
230
- expect ( service . getResourceGroupName ( ) ) . toEqual ( `${ prefix } -wus-dev-${ serviceName } -rg` ) ;
231
- } ) ;
232
-
233
- it ( "sets resource group from CLI" , async ( ) => {
234
- const resourceGroup = "resourceGroup" ;
235
- delete ( serverless . service as any as ServerlessAzureConfig ) . provider . resourceGroup ;
236
- expect ( serverless . service . provider [ "resourceGroup" ] ) . not . toEqual ( resourceGroup ) ;
237
- service = new MockService ( serverless , { resourceGroup } as any ) ;
238
- expect ( service . getResourceGroupName ( ) ) . toEqual ( resourceGroup ) ;
239
- } ) ;
240
-
241
- const cliSubscriptionId = "cli sub id" ;
242
- const configSubscriptionId = "config sub id" ;
243
-
244
- it ( "sets subscription ID from CLI" , async ( ) => {
245
- process . env . AZURE_SUBSCRIPTION_ID = envVarSubscriptionId ;
246
- serverless . service . provider [ "subscriptionId" ] = configSubscriptionId ;
247
- serverless . variables [ "subscriptionId" ] = loginResultSubscriptionId
248
- service = new MockService ( serverless , { subscriptionId : cliSubscriptionId } as any ) ;
249
- expect ( service . getSubscriptionId ( ) ) . toEqual ( cliSubscriptionId ) ;
250
- expect ( serverless . service . provider [ "subscriptionId" ] ) . toEqual ( cliSubscriptionId ) ;
251
- } ) ;
252
-
253
- it ( "sets subscription ID from environment variable" , async ( ) => {
254
- process . env . AZURE_SUBSCRIPTION_ID = envVarSubscriptionId ;
255
- serverless . service . provider [ "subscriptionId" ] = configSubscriptionId ;
256
- serverless . variables [ "subscriptionId" ] = loginResultSubscriptionId
257
- service = new MockService ( serverless , { } as any ) ;
258
- expect ( service . getSubscriptionId ( ) ) . toEqual ( envVarSubscriptionId ) ;
259
- expect ( serverless . service . provider [ "subscriptionId" ] ) . toEqual ( envVarSubscriptionId ) ;
260
- } ) ;
261
-
262
- it ( "sets subscription ID from config" , async ( ) => {
263
- delete process . env . AZURE_SUBSCRIPTION_ID ;
264
- serverless . service . provider [ "subscriptionId" ] = configSubscriptionId ;
265
- serverless . variables [ "subscriptionId" ] = loginResultSubscriptionId
266
- service = new MockService ( serverless , { } as any ) ;
267
- expect ( service . getSubscriptionId ( ) ) . toEqual ( configSubscriptionId ) ;
268
- expect ( serverless . service . provider [ "subscriptionId" ] ) . toEqual ( configSubscriptionId ) ;
269
- } ) ;
270
-
271
- it ( "sets subscription ID from login result" , async ( ) => {
272
- delete process . env . AZURE_SUBSCRIPTION_ID ;
273
- serverless . variables [ "subscriptionId" ] = loginResultSubscriptionId
274
- service = new MockService ( serverless , { } as any ) ;
275
- expect ( service . getSubscriptionId ( ) ) . toEqual ( loginResultSubscriptionId ) ;
276
- expect ( serverless . service . provider [ "subscriptionId" ] ) . toEqual ( loginResultSubscriptionId ) ;
277
- } ) ;
278
-
279
- it ( "sets region to be value from location property if region not set" , ( ) => {
280
- const slsService = MockFactory . createTestService ( ) ;
281
- delete slsService . provider . region ;
282
- const location = "East US" ;
283
- slsService . provider [ "location" ] = location ;
284
-
285
- const sls = MockFactory . createTestServerless ( {
286
- service : slsService
287
- } ) ;
288
-
289
- service = new MockService ( sls , { } as any ) ;
290
- expect ( service . getRegion ( ) ) . toEqual ( location ) ;
291
- } ) ;
292
143
} ) ;
0 commit comments