Skip to content

Commit

Permalink
fixed config tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fengtality committed Feb 7, 2023
1 parent 19ab410 commit e7410d8
Show file tree
Hide file tree
Showing 14 changed files with 58 additions and 88 deletions.
4 changes: 2 additions & 2 deletions src/services/config-manager-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 6 additions & 8 deletions test/config.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
);
}

Expand All @@ -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
);
}
Expand Down
47 changes: 24 additions & 23 deletions test/services/config-manager-v2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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')
Expand All @@ -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(() => {
Expand All @@ -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,
Expand All @@ -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
);
Expand All @@ -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', {});
Expand All @@ -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) => {
Expand Down Expand Up @@ -215,24 +216,24 @@ 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();
});
});

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();
});
});
6 changes: 3 additions & 3 deletions test/services/data/config-manager-v2/test1/invalid-root-2.yml
Original file line number Diff line number Diff line change
@@ -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
6 changes: 3 additions & 3 deletions test/services/data/config-manager-v2/test1/invalid-root-3.yml
Original file line number Diff line number Diff line change
@@ -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
6 changes: 3 additions & 3 deletions test/services/data/config-manager-v2/test1/invalid-root-4.yml
Original file line number Diff line number Diff line change
@@ -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
6 changes: 3 additions & 3 deletions test/services/data/config-manager-v2/test1/invalid-root.yml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
caCertificatePath: ca.crt
certificatePath: gateway.crt
keyPath: gateway.key
funnyKeyPath: funny.key
28 changes: 4 additions & 24 deletions test/services/data/config-manager-v2/test1/root.yml
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
12 changes: 3 additions & 9 deletions test/services/data/config-manager-v2/test1/root2.yml
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions test/services/data/config-manager-v2/test1/server.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
certificatePath: gateway.crt

# If true it will collect data about API usage.
telemetry_enabled: false
3 changes: 0 additions & 3 deletions test/services/data/config-manager-v2/test1/ssl.yml

This file was deleted.

2 changes: 0 additions & 2 deletions test/services/data/config-manager-v2/test1/telemetry.yml

This file was deleted.

6 changes: 3 additions & 3 deletions test/services/logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
Expand Down

0 comments on commit e7410d8

Please sign in to comment.