Skip to content
This repository has been archived by the owner on Mar 9, 2022. It is now read-only.

Changes to orchestration data is reflected in response #262

Merged
merged 3 commits into from
Apr 5, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/EngineDiscovery.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ async function discover() {
const engineEntry = new EngineEntry(item, this.updateInterval);
logger.info(`Engine discovered at address: ${item.statusIp}:${engineEntry.properties.engine.port} with key: ${item.key}`);
this.engineMap.add(item.key, engineEntry);
} else {
const engineEntry = this.engineMap.get(item.key);
if (engineEntry) {
engineEntry.updateOrchestrationProperties(item);
}
}
});

Expand Down
10 changes: 10 additions & 0 deletions src/EngineEntry.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ class EngineEntry {
}
}

updateOrchestrationProperties(properties) {
const keys = Object.keys(properties);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer Object.keys(properties).forEach((key) => ...)

for (let i = 0; i < keys.length; i += 1) {
const key = keys[i];
if (key !== 'key' && key !== 'engine' && key !== 'statusIp') {
this.properties[key] = properties[key];
}
}
}

/**
* Starts periodical status checking.
*/
Expand Down
9 changes: 9 additions & 0 deletions src/EngineMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ class EngineMap {
has(key) {
return Object.prototype.hasOwnProperty.call(this.entries, key);
}

/**
* Returns the engine with the supplied key
* @param {string} key - the unique engine identifier.
* @returns the engine entry or undefined if missing
*/
get(key) {
return this.entries[key];
}
}

module.exports = EngineMap;
19 changes: 19 additions & 0 deletions test/unit/EngineEntry.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,25 @@ describe('EngineEntry', () => {
});
});

describe('#updateOrchestrationProperties()', () => {
beforeEach(() => {
healthFetcher = new EngineStatusFetcher({ get: () => { } });
fetchStub = sinon.stub(healthFetcher, 'fetch');
fetchStub.withArgs('10.10.10.10', 9098, '/healthcheck').returns(Promise.resolve(healthOk));
fetchStub.withArgs('10.10.10.10', 9999, '/metrics').returns(Promise.resolve(metrics));
});

it('should properties to be updated', () => {
entry = new EngineEntry({ engine: { labels: { 'qix-engine-api-port': '9998', 'qix-engine-metrics-port': '9999' } }, statusIp: '10.10.10.10' }, 10, healthFetcher);
expect(entry.properties).to.deep.equal({ engine: { port: 9998, metricsPort: 9999, labels: { 'qix-engine-api-port': '9998', 'qix-engine-metrics-port': '9999' } }, statusIp: '10.10.10.10' });

const newProps = { kubernetes: { newInfo: 'yes' } };
entry.updateOrchestrationProperties(newProps);
expect(entry.properties).to.deep.equal({ engine: { port: 9998, metricsPort: 9999, labels: { 'qix-engine-api-port': '9998', 'qix-engine-metrics-port': '9999' } }, statusIp: '10.10.10.10', kubernetes: { newInfo: 'yes' } });
});
});


afterEach(() => {
fetchStub.reset();
});
Expand Down
7 changes: 7 additions & 0 deletions test/unit/EngineMap.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ describe('EngineMap', () => {
expect(engineMap.has('dummy')).to.equal(false);
});

it('should return', () => {
const entry = getEntry();
engineMap.add('foo', entry);
expect(engineMap.get('foo')).to.equal(entry);
expect(engineMap.get('dummy')).to.equal(undefined);
});

it('should return all entries', () => {
const entry1 = getEntry();
const entry2 = getEntry();
Expand Down