@@ -12,6 +12,8 @@ const {
1212const { ValidLifecycleRules : supportedLifecycleRules } = require ( 'arsenal' ) . models ;
1313
1414describe ( 'Config' , ( ) => {
15+ const defaultConfig = JSON . parse ( fs . readFileSync ( 'config.json' ) , { encoding : 'utf-8' } ) ;
16+
1517 const envToRestore = [ ] ;
1618 const setEnv = ( key , value ) => {
1719 if ( key in process . env ) {
@@ -446,7 +448,6 @@ describe('Config', () => {
446448 } ) ;
447449
448450 describe ( 'multiObjectDeleteEnableOptimizations' , ( ) => {
449- const defaultConfig = JSON . parse ( fs . readFileSync ( 'config.json' ) , { encoding : 'utf-8' } ) ;
450451 let sandbox ;
451452 let readFileStub ;
452453
@@ -679,4 +680,134 @@ describe('Config', () => {
679680 assert . deepStrictEqual ( parsedRules , rules ) ;
680681 } ) ;
681682 } ) ;
683+
684+ describe ( 'port and internalPort configuration' , ( ) => {
685+ // same as `defaultConfig` (i.e. config.json) but without the port settings
686+ const baseConfig = ( ( ) => {
687+ const config = { ...defaultConfig } ;
688+ delete config . port ;
689+ delete config . listenOn ;
690+ delete config . internalPort ;
691+ delete config . internalListenOn ;
692+ delete config . metricsPort ;
693+ delete config . metricsListenOn ;
694+ return config ;
695+ } ) ( ) ;
696+
697+ let sandbox ;
698+ let readFileStub ;
699+
700+ beforeEach ( ( ) => {
701+ sandbox = sinon . createSandbox ( ) ;
702+ readFileStub = sandbox . stub ( fs , 'readFileSync' ) ;
703+ readFileStub . callThrough ( ) ;
704+ } ) ;
705+
706+ afterEach ( ( ) => {
707+ sandbox . restore ( ) ;
708+ } ) ;
709+
710+ it ( 'should throw an error for negative port number' , ( ) => {
711+ const testConfig = { ...baseConfig , port : - 1 } ;
712+ readFileStub . withArgs ( sinon . match ( / c o n f i g .j s o n $ / ) ) . returns ( JSON . stringify ( testConfig ) ) ;
713+
714+ assert . throws ( ( ) => new ConfigObject ( ) , / b a d c o n f i g : p o r t m u s t b e a p o s i t i v e i n t e g e r / ) ;
715+ } ) ;
716+
717+ it ( 'should throw an error for non-integer port number' , ( ) => {
718+ const testConfig = { ...baseConfig , port : 8000.5 } ;
719+ readFileStub . withArgs ( sinon . match ( / c o n f i g .j s o n $ / ) ) . returns ( JSON . stringify ( testConfig ) ) ;
720+
721+ assert . throws ( ( ) => new ConfigObject ( ) , / b a d c o n f i g : p o r t m u s t b e a p o s i t i v e i n t e g e r / ) ;
722+ } ) ;
723+
724+ it ( 'should throw an error for negative internalPort number' , ( ) => {
725+ const testConfig = { ...baseConfig , internalPort : - 1 } ;
726+ readFileStub . withArgs ( sinon . match ( / c o n f i g .j s o n $ / ) ) . returns ( JSON . stringify ( testConfig ) ) ;
727+
728+ assert . throws ( ( ) => new ConfigObject ( ) , / b a d c o n f i g : i n t e r n a l P o r t m u s t b e a p o s i t i v e i n t e g e r / ) ;
729+ } ) ;
730+
731+ it ( 'should throw an error for non-integer internalPort number' , ( ) => {
732+ const testConfig = { ...baseConfig , internalPort : 9000.5 } ;
733+ readFileStub . withArgs ( sinon . match ( / c o n f i g .j s o n $ / ) ) . returns ( JSON . stringify ( testConfig ) ) ;
734+
735+ assert . throws ( ( ) => new ConfigObject ( ) , / b a d c o n f i g : i n t e r n a l P o r t m u s t b e a p o s i t i v e i n t e g e r / ) ;
736+ } ) ;
737+
738+ it ( 'should accept a valid port number' , ( ) => {
739+ const testConfig = { ...baseConfig , port : 8765 } ;
740+ readFileStub . withArgs ( sinon . match ( / c o n f i g .j s o n $ / ) ) . returns ( JSON . stringify ( testConfig ) ) ;
741+
742+ const config = new ConfigObject ( ) ;
743+ assert . strictEqual ( config . port , 8765 ) ;
744+ assert . strictEqual ( config . internalPort , undefined ) ;
745+ } ) ;
746+
747+ it ( 'should accept a valid internalPort number' , ( ) => {
748+ const testConfig = { ...baseConfig , internalPort : 9000 } ;
749+ readFileStub . withArgs ( sinon . match ( / c o n f i g .j s o n $ / ) ) . returns ( JSON . stringify ( testConfig ) ) ;
750+
751+ const config = new ConfigObject ( ) ;
752+ assert . strictEqual ( config . port , undefined ) ;
753+ assert . strictEqual ( config . internalPort , 9000 ) ;
754+ } ) ;
755+
756+ it ( 'should accept both port and internalPort when provided' , ( ) => {
757+ const testConfig = { ...baseConfig , port : 8000 , internalPort : 9000 } ;
758+ readFileStub . withArgs ( sinon . match ( / c o n f i g .j s o n $ / ) ) . returns ( JSON . stringify ( testConfig ) ) ;
759+
760+ const config = new ConfigObject ( ) ;
761+ assert . strictEqual ( config . port , 8000 ) ;
762+ assert . strictEqual ( config . internalPort , 9000 ) ;
763+ } ) ;
764+
765+ it ( 'should use default port 8000 if no port or internalPort specified' , ( ) => {
766+ readFileStub . withArgs ( sinon . match ( / c o n f i g .j s o n $ / ) ) . returns ( JSON . stringify ( baseConfig ) ) ;
767+
768+ const config = new ConfigObject ( ) ;
769+ assert . strictEqual ( config . port , 8000 ) ;
770+ assert . strictEqual ( config . internalPort , undefined ) ;
771+ } ) ;
772+
773+ it ( 'should default port if listenOn is provided' , ( ) => {
774+ const testConfig = {
775+ ...baseConfig ,
776+ listenOn : [ '127.0.0.1:9000' ] ,
777+ } ;
778+ readFileStub . withArgs ( sinon . match ( / c o n f i g .j s o n $ / ) ) . returns ( JSON . stringify ( testConfig ) ) ;
779+
780+ const config = new ConfigObject ( ) ;
781+ assert . strictEqual ( config . port , 8000 ) ;
782+ assert . strictEqual ( config . internalPort , undefined ) ;
783+ assert . deepEqual ( config . listenOn , [ { ip : '127.0.0.1' , port : 9000 } ] ) ;
784+ } ) ;
785+
786+ it ( 'should not default port if internalListenOn is provided' , ( ) => {
787+ const testConfig = {
788+ ...baseConfig ,
789+ internalListenOn : [ '127.0.0.1:9000' ] ,
790+ } ;
791+ readFileStub . withArgs ( sinon . match ( / c o n f i g .j s o n $ / ) ) . returns ( JSON . stringify ( testConfig ) ) ;
792+
793+ const config = new ConfigObject ( ) ;
794+ assert . strictEqual ( config . port , undefined ) ;
795+ assert . strictEqual ( config . internalPort , undefined ) ;
796+ assert . deepEqual ( config . internalListenOn , [ { ip : '127.0.0.1' , port : 9000 } ] ) ;
797+ } ) ;
798+
799+ it ( 'should properly handle listenOn and internalListenOn configuration' , ( ) => {
800+ const testConfig = {
801+ ...baseConfig ,
802+ listenOn : [ '0.0.0.0:8000' ] ,
803+ internalListenOn : [ '127.0.0.1:9000' ] ,
804+ } ;
805+ readFileStub . withArgs ( sinon . match ( / c o n f i g .j s o n $ / ) ) . returns ( JSON . stringify ( testConfig ) ) ;
806+
807+ const config = new ConfigObject ( ) ;
808+ assert . strictEqual ( config . port , 8000 ) ;
809+ assert . deepEqual ( config . listenOn , [ { ip : '0.0.0.0' , port : 8000 } ] ) ;
810+ assert . deepEqual ( config . internalListenOn , [ { ip : '127.0.0.1' , port : 9000 } ] ) ;
811+ } ) ;
812+ } ) ;
682813} ) ;
0 commit comments