1
+ import child_process from "child_process" ;
1
2
jest . mock ( "open" ) ;
2
3
import open from "open" ;
3
4
jest . mock ( "../../config" ) ;
4
5
import { Config } from "../../config" ;
5
6
import { exec } from "../../lib/shell" ;
7
+ import * as shell from "../../lib/shell" ;
6
8
import { validatePrereqs } from "../../lib/validator" ;
9
+ import * as validator from "../../lib/validator" ;
7
10
import {
8
11
disableVerboseLogging ,
9
12
enableVerboseLogging ,
10
13
logger ,
11
14
} from "../../logger" ;
12
15
import {
16
+ cleanDashboardContainers ,
13
17
DashboardConfig ,
14
18
execute ,
15
19
extractManifestRepositoryInformation ,
@@ -21,6 +25,7 @@ import * as dashboard from "./dashboard";
21
25
22
26
import uuid from "uuid/v4" ;
23
27
import { deepClone } from "../../lib/util" ;
28
+ import { getErrorMessage } from "../../lib/errorBuilder" ;
24
29
25
30
const dashboardConf : DashboardConfig = {
26
31
port : 2020 ,
@@ -112,9 +117,7 @@ describe("Test execute function", () => {
112
117
it ( "positive test" , async ( ) => {
113
118
mockConfig ( ) ;
114
119
const exitFn = jest . fn ( ) ;
115
- jest
116
- . spyOn ( dashboard , "launchDashboard" )
117
- . mockReturnValueOnce ( Promise . resolve ( uuid ( ) ) ) ;
120
+ jest . spyOn ( dashboard , "launchDashboard" ) . mockResolvedValueOnce ( uuid ( ) ) ;
118
121
jest . spyOn ( dashboard , "validateValues" ) . mockReturnValueOnce ( dashboardConf ) ;
119
122
( open as jest . Mock ) . mockReturnValueOnce ( Promise . resolve ( ) ) ;
120
123
await execute (
@@ -196,19 +199,61 @@ describe("Validate dashboard clean up", () => {
196
199
} ) ;
197
200
198
201
describe ( "Fallback to azure devops access token" , ( ) => {
199
- test ( "Has repo_access_token specified" , async ( ) => {
200
- const envVars = ( await getEnvVars ( dashboardConf ) ) . toString ( ) ;
201
- logger . info (
202
- `spin: ${ envVars } , act: ${ mockedConf . introspection . azure . source_repo_access_token } `
203
- ) ;
204
- const expectedSubstring = "REACT_APP_SOURCE_REPO_ACCESS_TOKEN=test_token" ;
205
- expect ( envVars . includes ( expectedSubstring ) ) . toBeTruthy ( ) ;
202
+ test ( "with repo_access_token and without sourceRepoAccessToken" , async ( ) => {
203
+ const conf = deepClone ( dashboardConf ) ;
204
+ delete conf . sourceRepoAccessToken ;
205
+ const envVars = getEnvVars ( conf ) . toString ( ) ;
206
+
207
+ expect (
208
+ envVars . includes ( `REACT_APP_PIPELINE_ACCESS_TOKEN=${ conf . accessToken } ` )
209
+ ) . toBeTruthy ( ) ;
210
+ expect (
211
+ envVars . includes ( `REACT_APP_SOURCE_REPO_ACCESS_TOKEN=${ conf . accessToken } ` )
212
+ ) . toBeTruthy ( ) ;
213
+ expect (
214
+ envVars . includes ( `REACT_APP_MANIFEST_ACCESS_TOKEN=${ conf . accessToken } ` )
215
+ ) . toBeTruthy ( ) ;
216
+ } ) ;
217
+ test ( "without repo_access_token and with sourceRepoAccessToken" , async ( ) => {
218
+ const conf = deepClone ( dashboardConf ) ;
219
+ delete conf . accessToken ;
220
+ const envVars = getEnvVars ( conf ) . toString ( ) ;
221
+
222
+ expect ( envVars . includes ( "REACT_APP_PIPELINE_ACCESS_TOKEN" ) ) . toBeFalsy ( ) ;
223
+ expect (
224
+ envVars . includes (
225
+ `REACT_APP_SOURCE_REPO_ACCESS_TOKEN=${ dashboardConf . sourceRepoAccessToken } `
226
+ )
227
+ ) . toBeTruthy ( ) ;
228
+ expect (
229
+ envVars . includes (
230
+ `REACT_APP_MANIFEST_ACCESS_TOKEN=${ dashboardConf . sourceRepoAccessToken } `
231
+ )
232
+ ) . toBeTruthy ( ) ;
206
233
} ) ;
234
+ test ( "with manifest repository information" , async ( ) => {
235
+ jest
236
+ . spyOn ( dashboard , "extractManifestRepositoryInformation" )
237
+ . mockReturnValueOnce ( {
238
+ manifestRepoName : "mName" ,
239
+ githubUsername : "gitUser" ,
240
+ } ) ;
241
+ const envVars = getEnvVars ( dashboardConf ) . toString ( ) ;
207
242
208
- it ( "No repo_access_token was specified" , async ( ) => {
209
- const envVars = ( await getEnvVars ( dashboardConf ) ) . toString ( ) ;
210
- const expectedSubstring = `REACT_APP_SOURCE_REPO_ACCESS_TOKEN=${ dashboardConf . sourceRepoAccessToken } ` ;
211
- expect ( envVars . includes ( expectedSubstring ) ) . toBeTruthy ( ) ;
243
+ expect ( envVars . includes ( "REACT_APP_MANIFEST=mName" ) ) . toBeTruthy ( ) ;
244
+ expect (
245
+ envVars . includes ( "REACT_APP_GITHUB_MANIFEST_USERNAME=gitUser" )
246
+ ) . toBeTruthy ( ) ;
247
+ } ) ;
248
+ test ( "negative test" , async ( ) => {
249
+ jest
250
+ . spyOn ( dashboard , "extractManifestRepositoryInformation" )
251
+ . mockImplementationOnce ( ( ) => {
252
+ throw Error ( "fake" ) ;
253
+ } ) ;
254
+ expect ( ( ) => {
255
+ getEnvVars ( dashboardConf ) ;
256
+ } ) . toThrow ( getErrorMessage ( "introspect-dashboard-cmd-get-env" ) ) ;
212
257
} ) ;
213
258
} ) ;
214
259
@@ -238,3 +283,69 @@ describe("Extract manifest repository information", () => {
238
283
logger . info ( "Verified that manifest repository extraction works" ) ;
239
284
} ) ;
240
285
} ) ;
286
+
287
+ describe ( "test cleanDashboardContainers function" , ( ) => {
288
+ it ( "positive test" , async ( ) => {
289
+ const containerIds = [ "f5ad0bff2448" , "f5ad0bff2449" ] ;
290
+ jest . spyOn ( shell , "exec" ) . mockResolvedValueOnce ( containerIds . join ( "\n" ) ) ;
291
+
292
+ jest . spyOn ( shell , "exec" ) . mockImplementationOnce (
293
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
294
+ async (
295
+ cmd : string ,
296
+ args ?: string [ ] ,
297
+ opts ?: child_process . SpawnOptions
298
+ ) : Promise < string > => {
299
+ expect ( args ) . toStrictEqual ( [ "kill" , ...containerIds ] ) ;
300
+ return "" ;
301
+ }
302
+ ) ;
303
+ await cleanDashboardContainers ( {
304
+ image : "fake" ,
305
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
306
+ } as any ) ;
307
+ } ) ;
308
+ it ( "negative test: cannot get docker image ids" , async ( ) => {
309
+ jest . spyOn ( shell , "exec" ) . mockRejectedValueOnce ( Error ( "fake" ) ) ;
310
+
311
+ await expect (
312
+ cleanDashboardContainers ( {
313
+ image : "fake" ,
314
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
315
+ } as any )
316
+ ) . rejects . toThrow (
317
+ getErrorMessage ( "introspect-dashboard-cmd-kill-docker-container" )
318
+ ) ;
319
+ } ) ;
320
+ it ( "negative test: cannot kill images" , async ( ) => {
321
+ const containerIds = [ "f5ad0bff2448" , "f5ad0bff2449" ] ;
322
+ jest . spyOn ( shell , "exec" ) . mockResolvedValueOnce ( containerIds . join ( "\n" ) ) ;
323
+
324
+ jest . spyOn ( shell , "exec" ) . mockRejectedValueOnce ( Error ( "fake" ) ) ;
325
+ await expect (
326
+ cleanDashboardContainers ( {
327
+ image : "fake" ,
328
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
329
+ } as any )
330
+ ) . rejects . toThrow (
331
+ getErrorMessage ( "introspect-dashboard-cmd-kill-docker-container" )
332
+ ) ;
333
+ } ) ;
334
+ } ) ;
335
+
336
+ describe ( "test launchDashboard function" , ( ) => {
337
+ it ( "postive test" , async ( ) => {
338
+ jest . spyOn ( validator , "validatePrereqs" ) . mockReturnValueOnce ( true ) ;
339
+ jest . spyOn ( dashboard , "cleanDashboardContainers" ) . mockResolvedValueOnce ( ) ;
340
+ jest . spyOn ( shell , "exec" ) . mockResolvedValueOnce ( "ok" ) ;
341
+ jest . spyOn ( shell , "exec" ) . mockResolvedValueOnce ( "container-identifier" ) ;
342
+ const res = await launchDashboard ( dashboardConf , true ) ;
343
+ expect ( res ) . toBe ( "container-identifier" ) ;
344
+ } ) ;
345
+ it ( "negative test" , async ( ) => {
346
+ jest . spyOn ( validator , "validatePrereqs" ) . mockReturnValueOnce ( false ) ;
347
+ await expect ( launchDashboard ( dashboardConf , true ) ) . rejects . toThrow (
348
+ getErrorMessage ( "introspect-dashboard-cmd-launch-err" )
349
+ ) ;
350
+ } ) ;
351
+ } ) ;
0 commit comments