11const assert = require ( 'assert' ) ;
2+ const sinon = require ( 'sinon' ) ;
3+ const fs = require ( 'fs' ) ;
4+ const path = require ( 'path' ) ;
25const {
36 azureArchiveLocationConstraintAssert,
4- ConfigObject : ConfigObjectForTest ,
7+ ConfigObject,
58} = require ( '../../lib/Config' ) ;
69
710describe ( 'Config' , ( ) => {
@@ -25,7 +28,6 @@ describe('Config', () => {
2528 } ) ;
2629
2730 it ( 'should emit an event when auth data is updated' , done => {
28- const { ConfigObject } = require ( '../../lib/Config' ) ;
2931 const config = new ConfigObject ( ) ;
3032 let emitted = false ;
3133 config . on ( 'authdata-update' , ( ) => {
@@ -209,8 +211,6 @@ describe('Config', () => {
209211 } ) ;
210212
211213 describe ( 'getAzureStorageAccountName' , ( ) => {
212- const { ConfigObject } = require ( '../../lib/Config' ) ;
213-
214214 it ( 'should return account name from config' , ( ) => {
215215 setEnv ( 'azurebackend_AZURE_STORAGE_ACCOUNT_NAME' , '' ) ;
216216 const config = new ConfigObject ( ) ;
@@ -259,7 +259,7 @@ describe('Config', () => {
259259
260260 describe ( 'time options' , ( ) => {
261261 it ( 'should getTimeOptions' , ( ) => {
262- const config = new ConfigObjectForTest ( ) ;
262+ const config = new ConfigObject ( ) ;
263263 const expectedOptions = {
264264 expireOneDayEarlier : false ,
265265 transitionOneDayEarlier : false ,
@@ -273,7 +273,7 @@ describe('Config', () => {
273273 it ( 'should getTimeOptions with TIME_PROGRESSION_FACTOR' , ( ) => {
274274 setEnv ( 'TIME_PROGRESSION_FACTOR' , 2 ) ;
275275
276- const config = new ConfigObjectForTest ( ) ;
276+ const config = new ConfigObject ( ) ;
277277 const expectedOptions = {
278278 expireOneDayEarlier : false ,
279279 transitionOneDayEarlier : false ,
@@ -287,7 +287,7 @@ describe('Config', () => {
287287 it ( 'should getTimeOptions with EXPIRE_ONE_DAY_EARLIER' , ( ) => {
288288 setEnv ( 'EXPIRE_ONE_DAY_EARLIER' , true ) ;
289289
290- const config = new ConfigObjectForTest ( ) ;
290+ const config = new ConfigObject ( ) ;
291291 const expectedOptions = {
292292 expireOneDayEarlier : true ,
293293 transitionOneDayEarlier : false ,
@@ -301,7 +301,7 @@ describe('Config', () => {
301301 it ( 'should getTimeOptions with TRANSITION_ONE_DAY_EARLIER' , ( ) => {
302302 setEnv ( 'TRANSITION_ONE_DAY_EARLIER' , true ) ;
303303
304- const config = new ConfigObjectForTest ( ) ;
304+ const config = new ConfigObject ( ) ;
305305 const expectedOptions = {
306306 expireOneDayEarlier : false ,
307307 transitionOneDayEarlier : true ,
@@ -316,7 +316,7 @@ describe('Config', () => {
316316 setEnv ( 'EXPIRE_ONE_DAY_EARLIER' , true ) ;
317317 setEnv ( 'TRANSITION_ONE_DAY_EARLIER' , true ) ;
318318
319- const config = new ConfigObjectForTest ( ) ;
319+ const config = new ConfigObject ( ) ;
320320 const expectedOptions = {
321321 expireOneDayEarlier : true ,
322322 transitionOneDayEarlier : true ,
@@ -331,22 +331,22 @@ describe('Config', () => {
331331 setEnv ( 'EXPIRE_ONE_DAY_EARLIER' , true ) ;
332332 setEnv ( 'TIME_PROGRESSION_FACTOR' , 2 ) ;
333333
334- assert . throws ( ( ) => new ConfigObjectForTest ( ) ) ;
334+ assert . throws ( ( ) => new ConfigObject ( ) ) ;
335335 } ) ;
336336
337337 it ( 'should throw error if TRANSITION_ONE_DAY_EARLIER and TIME_PROGRESSION_FACTOR' , ( ) => {
338338 setEnv ( 'TRANSITION_ONE_DAY_EARLIER' , true ) ;
339339 setEnv ( 'TIME_PROGRESSION_FACTOR' , 2 ) ;
340340
341- assert . throws ( ( ) => new ConfigObjectForTest ( ) ) ;
341+ assert . throws ( ( ) => new ConfigObject ( ) ) ;
342342 } ) ;
343343
344344 it ( 'should throw error if both EXPIRE/TRANSITION_ONE_DAY_EARLIER and TIME_PROGRESSION_FACTOR' , ( ) => {
345345 setEnv ( 'EXPIRE_ONE_DAY_EARLIER' , true ) ;
346346 setEnv ( 'TRANSITION_ONE_DAY_EARLIER' , true ) ;
347347 setEnv ( 'TIME_PROGRESSION_FACTOR' , 2 ) ;
348348
349- assert . throws ( ( ) => new ConfigObjectForTest ( ) ) ;
349+ assert . throws ( ( ) => new ConfigObject ( ) ) ;
350350 } ) ;
351351 } ) ;
352352
@@ -364,7 +364,6 @@ describe('Config', () => {
364364 } ) ;
365365
366366 it ( 'should set up scuba' , ( ) => {
367- const { ConfigObject } = require ( '../../lib/Config' ) ;
368367 const config = new ConfigObject ( ) ;
369368
370369 assert . deepStrictEqual (
@@ -380,7 +379,6 @@ describe('Config', () => {
380379 setEnv ( 'SCUBA_HOST' , 'scubahost' ) ;
381380 setEnv ( 'SCUBA_PORT' , 1234 ) ;
382381
383- const { ConfigObject } = require ( '../../lib/Config' ) ;
384382 const config = new ConfigObject ( ) ;
385383
386384 assert . deepStrictEqual (
@@ -407,7 +405,6 @@ describe('Config', () => {
407405 } ) ;
408406
409407 it ( 'should set up quota' , ( ) => {
410- const { ConfigObject } = require ( '../../lib/Config' ) ;
411408 const config = new ConfigObject ( ) ;
412409
413410 assert . deepStrictEqual (
@@ -423,7 +420,6 @@ describe('Config', () => {
423420 setEnv ( 'QUOTA_MAX_STALENESS_MS' , 1234 ) ;
424421 setEnv ( 'QUOTA_ENABLE_INFLIGHTS' , 'true' ) ;
425422
426- const { ConfigObject } = require ( '../../lib/Config' ) ;
427423 const config = new ConfigObject ( ) ;
428424
429425 assert . deepStrictEqual (
@@ -439,7 +435,6 @@ describe('Config', () => {
439435 setEnv ( 'QUOTA_MAX_STALENESS_MS' , 'notanumber' ) ;
440436 setEnv ( 'QUOTA_ENABLE_INFLIGHTS' , 'true' ) ;
441437
442- const { ConfigObject } = require ( '../../lib/Config' ) ;
443438 const config = new ConfigObject ( ) ;
444439
445440 assert . deepStrictEqual (
@@ -466,7 +461,6 @@ describe('Config', () => {
466461 } ) ;
467462
468463 it ( 'should set up utapi local cache' , ( ) => {
469- const { ConfigObject } = require ( '../../lib/Config' ) ;
470464 const config = new ConfigObject ( ) ;
471465
472466 assert . deepStrictEqual (
@@ -480,7 +474,6 @@ describe('Config', () => {
480474 } ) ;
481475
482476 it ( 'should set up utapi redis' , ( ) => {
483- const { ConfigObject } = require ( '../../lib/Config' ) ;
484477 const config = new ConfigObject ( ) ;
485478
486479 assert . deepStrictEqual (
@@ -668,4 +661,82 @@ describe('Config', () => {
668661 assert . throws ( ( ) => azureArchiveLocationConstraintAssert ( locationObj ) ) ;
669662 } ) ;
670663 } ) ;
664+
665+ describe ( 'instanceId' , ( ) => {
666+ let defaultConfig ;
667+
668+ before ( ( ) => {
669+ setEnv ( 'S3_CONFIG_FILE' , 'tests/unit/testConfigs/allOptsConfig/config.json' ) ;
670+ // Import config
671+ const defaultConfigPath = path . join ( __dirname , './testConfigs/allOptsConfig/config.json' ) ;
672+ defaultConfig = require ( defaultConfigPath ) ;
673+ } ) ;
674+
675+ afterEach ( ( ) => {
676+ sinon . restore ( ) ;
677+ } ) ;
678+
679+ it ( 'should use the instance id set in the config' , ( ) => {
680+ // Stub fs.readFileSync to return a specific config.json content that includes instanceId
681+ const originalReadFileSync = fs . readFileSync ;
682+ const readFileSyncStub = sinon . stub ( fs , 'readFileSync' ) ;
683+ // Mock only config.json file
684+ readFileSyncStub
685+ . withArgs ( sinon . match ( / \/ c o n f i g \. j s o n $ / ) )
686+ . returns ( JSON . stringify ( { ...defaultConfig , instanceId : 'test' } ) ) ;
687+ // For all other files, use the original readFileSync
688+ readFileSyncStub
689+ . callsFake ( ( filePath , ...args ) => originalReadFileSync ( filePath , ...args ) ) ;
690+ // Create a new ConfigObject instance
691+ const config = new ConfigObject ( ) ;
692+ assert . strictEqual ( config . instanceId , 'test' ) ;
693+ } ) ;
694+
695+ it ( 'should assert if instanceId is not a string' , ( ) => {
696+ // Stub fs.readFileSync to return a specific config.json content that includes instanceId
697+ const originalReadFileSync = fs . readFileSync ;
698+ const readFileSyncStub = sinon . stub ( fs , 'readFileSync' ) ;
699+ // Mock only config.json file
700+ readFileSyncStub
701+ . withArgs ( sinon . match ( / \/ c o n f i g \. j s o n $ / ) )
702+ . returns ( JSON . stringify ( { ...defaultConfig , instanceId : 1234 } ) ) ;
703+ // For all other files, use the original readFileSync
704+ readFileSyncStub
705+ . callsFake ( ( filePath , ...args ) => originalReadFileSync ( filePath , ...args ) ) ;
706+ // Create a new ConfigObject instance
707+ assert . throws ( ( ) => new ConfigObject ( ) ) ;
708+ } ) ;
709+
710+ it ( 'should use the instance id set in the env var' , ( ) => {
711+ setEnv ( 'CLOUDSERVER_INSTANCE_ID' , '123456' ) ;
712+ const config = new ConfigObject ( ) ;
713+ assert . strictEqual ( config . instanceId , '123456' ) ;
714+ } ) ;
715+
716+ it ( 'should assert if instanceId is longer than 6 characters' , ( ) => {
717+ setEnv ( 'CLOUDSERVER_INSTANCE_ID' , '1234567' ) ;
718+ assert . throws ( ( ) => new ConfigObject ( ) ) ;
719+ } ) ;
720+
721+ it ( 'should generate a new instanceId for external cloudserver' , ( ) => {
722+ setEnv ( 'HOSTNAME' , 'connector-cloudserver-69958b4697-bs5pn' ) ;
723+ const config = new ConfigObject ( ) ;
724+ assert . strictEqual ( config . instanceId , 'ebs5pn' ) ;
725+ } ) ;
726+
727+ it ( 'should generate a new instanceId for internal cloudserver' , ( ) => {
728+ setEnv ( 'HOSTNAME' , 'internal-cloudserver-54dc76b796-48m2x' ) ;
729+ const config = new ConfigObject ( ) ;
730+ assert . strictEqual ( config . instanceId , 'i48m2x' ) ;
731+ } ) ;
732+ it ( 'should generate a random instanceId if HOSTNAME has an unexpected format' , ( ) => {
733+ setEnv ( 'HOSTNAME' , 'cldsrv1' ) ;
734+ const config = new ConfigObject ( ) ;
735+ assert . strictEqual ( config . instanceId . length , 6 ) ;
736+ } ) ;
737+ it ( 'should generate a random instanceId if HOSTNAME is not set' , ( ) => {
738+ const config = new ConfigObject ( ) ;
739+ assert . strictEqual ( config . instanceId . length , 6 ) ;
740+ } ) ;
741+ } ) ;
671742} ) ;
0 commit comments