From e7410d873f1aa93bd76005a4dcd0d17afaece9f8 Mon Sep 17 00:00:00 2001 From: Michael Feng Date: Tue, 7 Feb 2023 10:19:28 -0800 Subject: [PATCH] fixed config tests --- src/services/config-manager-v2.ts | 4 +- test/config.util.ts | 14 +++--- test/services/config-manager-v2.test.ts | 47 ++++++++++--------- .../test1/invalid-root-2.yml | 6 +-- .../test1/invalid-root-3.yml | 6 +-- .../test1/invalid-root-4.yml | 6 +-- .../config-manager-v2/test1/invalid-root.yml | 6 +-- .../{invalid-ssl.yml => invalid-server.yml} | 2 - .../data/config-manager-v2/test1/root.yml | 28 ++--------- .../data/config-manager-v2/test1/root2.yml | 12 ++--- .../data/config-manager-v2/test1/server.yml | 4 ++ .../data/config-manager-v2/test1/ssl.yml | 3 -- .../config-manager-v2/test1/telemetry.yml | 2 - test/services/logger.test.ts | 6 +-- 14 files changed, 58 insertions(+), 88 deletions(-) rename test/services/data/config-manager-v2/test1/{invalid-ssl.yml => invalid-server.yml} (50%) create mode 100644 test/services/data/config-manager-v2/test1/server.yml delete mode 100644 test/services/data/config-manager-v2/test1/ssl.yml delete mode 100644 test/services/data/config-manager-v2/test1/telemetry.yml diff --git a/src/services/config-manager-v2.ts b/src/services/config-manager-v2.ts index ba82eae0f8..7b6a012fe2 100644 --- a/src/services/config-manager-v2.ts +++ b/src/services/config-manager-v2.ts @@ -71,13 +71,13 @@ export class ConfigurationNamespace { /** * This class encapsulates a namespace under the configuration tree. * A namespace represents the top-level component of a configuration path. - * e.g. if the config path is "ssl.certificatePath", then "ssl" is the + * e.g. if the config path is "server.certificatePath", then "server" is the * namespace. * * Each namespace contains a JSON schema and a YAML configuration file. * * The JSON schema specifies the properties and data types allowed within the - * namespace. e.g. you may specify that the "ssl" namespace has a few + * namespace. e.g. you may specify that the "server" namespace has a few * mandatory properties dealing with certificates and private keys. This means * any missing properties or any properties outsides of the JSON schema would * cause a failure to initialize the namespace, and also cannot be set into diff --git a/test/config.util.ts b/test/config.util.ts index 333c1e1d27..fbe18d136e 100644 --- a/test/config.util.ts +++ b/test/config.util.ts @@ -8,11 +8,9 @@ export class OverrideConfigs { #initialized: boolean = false; public constructor() { - this.nonceDbPath = ConfigManagerV2.getInstance().get( - 'database.nonceDbPath' - ); + this.nonceDbPath = ConfigManagerV2.getInstance().get('server.nonceDbPath'); this.transactionDbPath = ConfigManagerV2.getInstance().get( - 'database.transactionDbPath' + 'server.transactionDbPath' ); } @@ -26,19 +24,19 @@ export class OverrideConfigs { updateConfigs(): void { ConfigManagerV2.getInstance().set( - 'database.nonceDbPath', + 'server.nonceDbPath', this.#testNonceDbPath ); ConfigManagerV2.getInstance().set( - 'database.transactionDbPath', + 'server.transactionDbPath', this.#testTransactionDbPath ); } resetConfigs(): void { - ConfigManagerV2.getInstance().set('database.nonceDbPath', this.nonceDbPath); + ConfigManagerV2.getInstance().set('server.nonceDbPath', this.nonceDbPath); ConfigManagerV2.getInstance().set( - 'database.transactionDbPath', + 'server.transactionDbPath', this.transactionDbPath ); } diff --git a/test/services/config-manager-v2.test.ts b/test/services/config-manager-v2.test.ts index 4155fc4abf..51d56034e2 100644 --- a/test/services/config-manager-v2.test.ts +++ b/test/services/config-manager-v2.test.ts @@ -43,7 +43,7 @@ describe('Configuration manager v2 tests', () => { }); it('loading a valid configuration root', (done) => { - expect(configManager.get('ssl.caCertificatePath')).toBeDefined(); + expect(configManager.get('server.certificatePath')).toBeDefined(); expect(configManager.get('ethereum.networks')).toBeDefined(); expect(configManager.get('defira.contractAddresses')).toBeDefined(); done(); @@ -75,7 +75,7 @@ describe('Configuration manager v2 tests', () => { }); it('reading from config file', (done) => { - expect(configManager.get('ssl.keyPath')).toEqual('gateway.key'); + expect(configManager.get('server.certificatePath')).toEqual('gateway.crt'); expect(configManager.get('ethereum.networks.kovan.chainID')).toEqual(42); expect( configManager.get('ethereum.networks.kovan.nativeCurrencySymbol') @@ -90,13 +90,13 @@ describe('Configuration manager v2 tests', () => { it('reading a non-existent config entry', (done) => { expect(configManager.get('ethereum.kovan.chainID')).toBeUndefined(); - expect(configManager.get('ssl.keyPath.keyPath')).toBeUndefined(); + expect(configManager.get('server.keyPath.keyPath')).toBeUndefined(); done(); }); it('reading invalid config keys', (done) => { expect(() => { - configManager.get('ssl'); + configManager.get('server'); }).toThrow(); done(); expect(() => { @@ -105,8 +105,8 @@ describe('Configuration manager v2 tests', () => { }); it('writing a valid configuration', (done) => { - const newKeyPath: string = 'new-gateway.key'; - configManager.set('ssl.keyPath', newKeyPath); + const newKeyPath: string = 'new-gateway.crt'; + configManager.set('server.certificatePath', newKeyPath); configManager.set('ethereum.networks.kovan.chainID', 970); configManager.set('ethereum.networks.mainnet', { chainID: 61, @@ -116,12 +116,14 @@ describe('Configuration manager v2 tests', () => { 'https://wispy-bird-88a7.uniswap.workers.dev/?url=http://tokens.1inch.eth.link', nativeCurrencySymbol: 'ETH', }); - expect(configManager.get('ssl.keyPath')).toEqual(newKeyPath); + expect(configManager.get('server.certificatePath')).toEqual(newKeyPath); const verifyConfigManager: ConfigManagerV2 = new ConfigManagerV2( path.join(tempDirPath, 'test1/root.yml') ); - expect(verifyConfigManager.get('ssl.keyPath')).toEqual(newKeyPath); + expect(verifyConfigManager.get('server.certificatePath')).toEqual( + newKeyPath + ); expect(verifyConfigManager.get('ethereum.networks.kovan.chainID')).toEqual( 970 ); @@ -133,7 +135,7 @@ describe('Configuration manager v2 tests', () => { it('writing an invalid configuration', (done) => { expect(() => { - configManager.set('ssl.nonKeyPath', 'noSuchFile.txt'); + configManager.set('server.nonKeyPath', 'noSuchFile.txt'); }).toThrow(); expect(() => { configManager.set('ethereum', {}); @@ -155,30 +157,29 @@ describe('Configuration manager v2 tests', () => { }); it('getting namespace objects', (done) => { - const sslNamespace: ConfigurationNamespace = configManager.getNamespace( - 'ssl' + const serverNamespace: ConfigurationNamespace = configManager.getNamespace( + 'server' ) as ConfigurationNamespace; - expect(path.basename(sslNamespace.schemaPath)).toEqual('ssl-schema.json'); - expect(path.dirname(sslNamespace.schemaPath)).toEqual( + expect(path.basename(serverNamespace.schemaPath)).toEqual('server-schema.json'); + expect(path.dirname(serverNamespace.schemaPath)).toEqual( path.dirname(ConfigRootSchemaPath) ); - expect(sslNamespace.configurationPath).toEqual( - path.join(tempDirPath, 'test1/ssl.yml') + expect(serverNamespace.configurationPath).toEqual( + path.join(tempDirPath, 'test1/server.yml') ); done(); }); it('Test upgradability', () => { - expect(configManager.get('logging.logPath')).toEqual('./logs'); - expect(configManager.get('telemetry.allowed')).toEqual(false); - expect(configManager.get('telemetry.enabled')).toEqual(false); + expect(configManager.get('server.logPath')).toEqual('./logs'); + expect(configManager.get('server.telemetry_enabled')).toEqual(false); }); it('Dummy test to attempt migration', () => { const configManager2 = new ConfigManagerV2( path.join(tempDirPath, 'test1/root2.yml') ); - expect(configManager2.get('ssl.caCertificatePath')).toBeDefined(); + expect(configManager2.get('server.certificatePath')).toBeDefined(); }); it('Test deep copy', (done) => { @@ -215,16 +216,16 @@ describe('Configuration manager v2 tests', () => { it('Get all configuration', (done) => { const allConfigs = configManager.allConfigurations; - expect(allConfigs.ssl.keyPath).toEqual('gateway.key'); + expect(allConfigs.server.certificatePath).toEqual('gateway.crt'); expect(allConfigs.ethereum.networks.kovan.chainID).toEqual(42); done(); }); it('Get instance', (done) => { let configManager = ConfigManagerV2.getInstance(); - expect(configManager.allConfigurations.telemetry.enabled).toEqual(false); + expect(configManager.allConfigurations.server.telemetry_enabled).toEqual(false); configManager = ConfigManagerV2.getInstance(); - expect(configManager.allConfigurations.telemetry.enabled).toEqual(false); + expect(configManager.allConfigurations.server.telemetry_enabled).toEqual(false); done(); }); }); @@ -232,7 +233,7 @@ describe('Configuration manager v2 tests', () => { describe('Sample configurations', () => { it('Read sample schemas', (done) => { const sampleConfigManager = new ConfigManagerV2('./src/templates/root.yml'); - expect(sampleConfigManager.get('ssl.caCertificatePath')).toBeDefined(); + expect(sampleConfigManager.get('server.certificatePath')).toBeDefined(); done(); }); }); diff --git a/test/services/data/config-manager-v2/test1/invalid-root-2.yml b/test/services/data/config-manager-v2/test1/invalid-root-2.yml index a1f2098fc5..88016277b7 100644 --- a/test/services/data/config-manager-v2/test1/invalid-root-2.yml +++ b/test/services/data/config-manager-v2/test1/invalid-root-2.yml @@ -1,8 +1,8 @@ version: 1 configurations: - $namespace ssl: - configurationPath: invalid-ssl.yml - schemaPath: ssl-schema.json + $namespace server: + configurationPath: invalid-server.yml + schemaPath: server-schema.json $namespace ethereum: configurationPath: ethereum.yml schemaPath: ethereum-schema.json diff --git a/test/services/data/config-manager-v2/test1/invalid-root-3.yml b/test/services/data/config-manager-v2/test1/invalid-root-3.yml index 0df3ba42fe..ad46fe4665 100644 --- a/test/services/data/config-manager-v2/test1/invalid-root-3.yml +++ b/test/services/data/config-manager-v2/test1/invalid-root-3.yml @@ -1,8 +1,8 @@ version: 1 configurations: - $namespace ssl: - configurationPath: ssl.yml - schemaPath: ssl-schema.json + $namespace server: + configurationPath: server.yml + schemaPath: server-schema.json $namespace ethereum: configurationPath: ethereum.yml schemaPath: schema-nonexist.json diff --git a/test/services/data/config-manager-v2/test1/invalid-root-4.yml b/test/services/data/config-manager-v2/test1/invalid-root-4.yml index 0df3ba42fe..ad46fe4665 100644 --- a/test/services/data/config-manager-v2/test1/invalid-root-4.yml +++ b/test/services/data/config-manager-v2/test1/invalid-root-4.yml @@ -1,8 +1,8 @@ version: 1 configurations: - $namespace ssl: - configurationPath: ssl.yml - schemaPath: ssl-schema.json + $namespace server: + configurationPath: server.yml + schemaPath: server-schema.json $namespace ethereum: configurationPath: ethereum.yml schemaPath: schema-nonexist.json diff --git a/test/services/data/config-manager-v2/test1/invalid-root.yml b/test/services/data/config-manager-v2/test1/invalid-root.yml index a7039d5da7..00342d4cd1 100644 --- a/test/services/data/config-manager-v2/test1/invalid-root.yml +++ b/test/services/data/config-manager-v2/test1/invalid-root.yml @@ -1,8 +1,8 @@ version: 1 configurations: - $namespace ssl: - configurationPath: ssl.yml - schemaPath: ssl-schema.json + $namespace server: + configurationPath: server.yml + schemaPath: server-schema.json $namespace ethereum: configurationPath: ethereum.yml schemaPath: ethereum-schema.json diff --git a/test/services/data/config-manager-v2/test1/invalid-ssl.yml b/test/services/data/config-manager-v2/test1/invalid-server.yml similarity index 50% rename from test/services/data/config-manager-v2/test1/invalid-ssl.yml rename to test/services/data/config-manager-v2/test1/invalid-server.yml index 6a983227e5..ef86ff79e8 100644 --- a/test/services/data/config-manager-v2/test1/invalid-ssl.yml +++ b/test/services/data/config-manager-v2/test1/invalid-server.yml @@ -1,4 +1,2 @@ -caCertificatePath: ca.crt certificatePath: gateway.crt -keyPath: gateway.key funnyKeyPath: funny.key \ No newline at end of file diff --git a/test/services/data/config-manager-v2/test1/root.yml b/test/services/data/config-manager-v2/test1/root.yml index fbdd34f595..40bc421bf0 100644 --- a/test/services/data/config-manager-v2/test1/root.yml +++ b/test/services/data/config-manager-v2/test1/root.yml @@ -1,5 +1,9 @@ version: 1 configurations: + $namespace server: + configurationPath: server.yml + schemaPath: server-schema.json + $namespace harmony: configurationPath: harmony.yml schemaPath: harmony-schema.json @@ -8,10 +12,6 @@ configurations: configurationPath: avalanche.yml schemaPath: ethereum-schema.json - $namespace database: - configurationPath: database.yml - schemaPath: database-schema.json - $namespace ethereum: configurationPath: ethereum.yml schemaPath: ethereum-schema.json @@ -20,22 +20,10 @@ configurations: configurationPath: polygon.yml schemaPath: ethereum-schema.json - $namespace ethereumGasStation: - configurationPath: ethereum-gas-station.yml - schemaPath: ethereum-gas-station-schema.json - $namespace defira: configurationPath: defira.yml schemaPath: defira-schema.json - $namespace logging: - configurationPath: logging.yml - schemaPath: logging-schema.json - - $namespace ssl: - configurationPath: ssl.yml - schemaPath: ssl-schema.json - $namespace pangolin: configurationPath: pangolin.yml schemaPath: pangolin-schema.json @@ -48,14 +36,6 @@ configurations: configurationPath: traderjoe.yml schemaPath: traderjoe-schema.json - $namespace server: - configurationPath: server.yml - schemaPath: server-schema.json - - $namespace telemetry: - configurationPath: telemetry.yml - schemaPath: telemetry-schema.json - $namespace uniswap: configurationPath: uniswap.yml schemaPath: uniswap-schema.json diff --git a/test/services/data/config-manager-v2/test1/root2.yml b/test/services/data/config-manager-v2/test1/root2.yml index bf92142ed6..9e41601e3c 100644 --- a/test/services/data/config-manager-v2/test1/root2.yml +++ b/test/services/data/config-manager-v2/test1/root2.yml @@ -1,14 +1,8 @@ version: 0 configurations: - $namespace ssl: - configurationPath: ssl.yml - schemaPath: ssl-schema.json + $namespace server: + configurationPath: server.yml + schemaPath: server-schema.json $namespace ethereum: configurationPath: ethereum.yml schemaPath: ethereum-schema.json - $namespace logging: - configurationPath: logging.yml - schemaPath: logging-schema.json - $namespace telemetry: - configurationPath: telemetry.yml - schemaPath: telemetry-schema.json diff --git a/test/services/data/config-manager-v2/test1/server.yml b/test/services/data/config-manager-v2/test1/server.yml new file mode 100644 index 0000000000..400674bf3e --- /dev/null +++ b/test/services/data/config-manager-v2/test1/server.yml @@ -0,0 +1,4 @@ +certificatePath: gateway.crt + +# If true it will collect data about API usage. +telemetry_enabled: false diff --git a/test/services/data/config-manager-v2/test1/ssl.yml b/test/services/data/config-manager-v2/test1/ssl.yml deleted file mode 100644 index 7ab539b6eb..0000000000 --- a/test/services/data/config-manager-v2/test1/ssl.yml +++ /dev/null @@ -1,3 +0,0 @@ -caCertificatePath: ca.crt -certificatePath: gateway.crt -keyPath: gateway.key diff --git a/test/services/data/config-manager-v2/test1/telemetry.yml b/test/services/data/config-manager-v2/test1/telemetry.yml deleted file mode 100644 index 353450877c..0000000000 --- a/test/services/data/config-manager-v2/test1/telemetry.yml +++ /dev/null @@ -1,2 +0,0 @@ -# If true it will collect data about API usage. -allowed: false diff --git a/test/services/logger.test.ts b/test/services/logger.test.ts index 37de22f17f..313ebc83c4 100644 --- a/test/services/logger.test.ts +++ b/test/services/logger.test.ts @@ -22,10 +22,10 @@ describe('Test logger', () => { it('test telemetry transport can be added', () => { const initTransports = logger.transports.length; - if (!ConfigManagerV2.getInstance().get('telemetry.enabled')) { - ConfigManagerV2.getInstance().set('telemetry.enabled', true); + if (!ConfigManagerV2.getInstance().get('server.telemetry_enabled')) { + ConfigManagerV2.getInstance().set('server.telemetry_enabled', true); telemetry(); - ConfigManagerV2.getInstance().set('telemetry.enabled', false); + ConfigManagerV2.getInstance().set('server.telemetry_enabled', false); expect(logger.transports.length).toEqual(initTransports + 1); } });