From f8aa947a9824e2fb3ea92304150d8391f1f0bc5f Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Fri, 29 Nov 2019 22:13:25 +0100 Subject: [PATCH] Add tests for resolveInstance Add tests for the resolveInstance function in clusterctl. --- clusterctl.js | 2 ++ test/clusterctl.js | 31 +++++++++++++++++++++++++++++++ test/mock.js | 9 ++++++++- 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/clusterctl.js b/clusterctl.js index 7ba03f25..75ddcd8c 100644 --- a/clusterctl.js +++ b/clusterctl.js @@ -436,6 +436,8 @@ module.exports = { // for testing only _formatOutputColored: formatOutputColored, _formatOutput: formatOutput, + _resolveInstance: resolveInstance, + _Control: Control, } diff --git a/test/clusterctl.js b/test/clusterctl.js index 0359efb5..37887918 100644 --- a/test/clusterctl.js +++ b/test/clusterctl.js @@ -1,8 +1,10 @@ const assert = require('assert').strict; const chalk = require('chalk'); +const errors = require('lib/errors'); const { testLines } = require('./lib/factorio/lines'); const clusterctl = require('../clusterctl.js'); +const mock = require('./mock'); describe("clusterctl", function() { @@ -28,4 +30,33 @@ describe("clusterctl", function() { } }); }); + + let mockConnector = new mock.MockConnector(); + mockConnector.on('send', function(message) { + if (message.type === 'list_instances_request') { + this.emit('message', { + seq: 1, type: 'list_instances_response', + data: { + seq: message.seq, + list: [{ id: 57, slave_id: 4, name: 'Test Instance' }], + }, + }); + } + }); + let testControl = new clusterctl._Control(mockConnector); + + describe("resolveInstance", function() { + it("should pass an integer like string back", async function() { + assert.equal(await clusterctl._resolveInstance(null, "123"), 123); + }); + it("should resolve an instance name with the master server", async function() { + assert.equal(await clusterctl._resolveInstance(testControl, "Test Instance"), 57); + }); + it("should throw if instance is not found", async function() { + await assert.rejects( + clusterctl._resolveInstance(testControl, "invalid"), + new errors.CommandError("No instance named invalid") + ); + }); + }); }); diff --git a/test/mock.js b/test/mock.js index 8c230e44..d771a48a 100644 --- a/test/mock.js +++ b/test/mock.js @@ -36,10 +36,17 @@ class MockConnector extends events.EventEmitter { } send(type, data) { - this.sentMessages.push({ seq: this._seq, type, data }); + let message = { seq: this._seq, type, data }; + this.sentMessages.push(message); + setImmediate(() => this.emit('send', message)); return this._seq++; } + close(reason) { + this.send('close', { reason }); + this.disconnect(); + } + disconnect() { this.disconnectCalled = true; }