From 3be264c9a9521642910726241a54b8f976621139 Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Fri, 29 Nov 2019 23:47:22 +0100 Subject: [PATCH] Add integration testing of commands Add testing of the clusterctl commands against the master and slave code. --- client.js | 1 + clusterctl.js | 2 + lib/link/connectors.js | 54 ++++++++++++++ lib/link/link.js | 3 +- master.js | 4 ++ test/integration/clusterio.js | 129 ++++++++++++++++++++++++++++++++++ 6 files changed, 192 insertions(+), 1 deletion(-) create mode 100644 test/integration/clusterio.js diff --git a/client.js b/client.js index 18f47d86..2196fb39 100644 --- a/client.js +++ b/client.js @@ -820,6 +820,7 @@ module.exports = { _Instance: Instance, _checkFilename: checkFilename, _symlinkMods: symlinkMods, + _Slave: Slave, }; if (module === require.main) { diff --git a/clusterctl.js b/clusterctl.js index 1aa0a952..193629e4 100644 --- a/clusterctl.js +++ b/clusterctl.js @@ -438,6 +438,8 @@ module.exports = { _formatOutput: formatOutput, _resolveInstance: resolveInstance, _Control: Control, + + _commands: commands, } diff --git a/lib/link/connectors.js b/lib/link/connectors.js index c124484d..93458546 100644 --- a/lib/link/connectors.js +++ b/lib/link/connectors.js @@ -170,6 +170,60 @@ class SocketIOClientConnector extends events.EventEmitter { } } +class VirtualConnector extends events.EventEmitter { + constructor() { + super(); + + this.other = this; + this._seq = 1; + } + + /** + * Create a pair of virtual connector hooked into each other + * + * Creates two virtual connectors that are liked to each othes such that + * a message sent on one is received by the other. + * + * @returns {Array} two virtuarl connectors. + */ + static makePair() { + let first = new this(); + let second = new this(); + first.other = second; + second.other = first; + return [first, second]; + } + + /** + * Send a message to the other end of the connector + * + * @returns the sequence number of the message sent + */ + send(type, data) { + this.other.emit('message', { seq: this._seq, type, data }); + return this._seq++; + } + + /** + * Close the connection with the given reason. + * + * Sends a close message to the other end of the connector with the + * given reason. Doesn't actually disconnect as virtual connectors + * doen't have a concept of disconnecting. + */ + close(reason) { + this.send('close', { reason }); + } + + /** + * Does nothing + * + * Virtual connectors do not have a concept of disconnecting. + */ + disconnect() { } +} + module.exports = { SocketIOClientConnector, + VirtualConnector, }; diff --git a/lib/link/link.js b/lib/link/link.js index 3260b430..d6b7432d 100644 --- a/lib/link/link.js +++ b/lib/link/link.js @@ -40,7 +40,8 @@ class Link { }); this.setHandler('close', message => { - this.connector.disconnect(message.data.reason); + console.log(`SOCKET | Received close: ${message.data.reason}`); + this.connector.disconnect(); }, schema.close); } diff --git a/master.js b/master.js index 5a6e67c9..b6978d97 100644 --- a/master.js +++ b/master.js @@ -665,6 +665,10 @@ module.exports = { _db: db, _config: config, _SocketIOServerConnector: SocketIOServerConnector, + _controlConnections: controlConnections, + _ControlConnection: ControlConnection, + _slaveConnections: slaveConnections, + _SlaveConnection: SlaveConnection, } if (module === require.main) { diff --git a/test/integration/clusterio.js b/test/integration/clusterio.js new file mode 100644 index 00000000..4620690d --- /dev/null +++ b/test/integration/clusterio.js @@ -0,0 +1,129 @@ +const assert = require("assert").strict; +const fs = require("fs-extra"); +const path = require("path"); + +const link = require("lib/link"); +const master = require("../../master"); +const client = require("../../client"); +const clusterctl = require("../../clusterctl"); +const { version } = require("../../package"); + + +describe("Integration of Clusterio", function() { + let testControl; + let testControlConnection; + + let testSlave; + let testSlaveConnection; + + let instancesDir = path.join("test", "temp", "instances"); + + // Mark that this test takes a lot of time, or depeneds on a test + // that takes a lot of time. + function slowTest(test) { + if (process.env.FAST_TEST) { + test.skip(); + } + + test.timeout(20000); + } + + before(async function() { + master._db.slaves = new Map(); + master._db.instances = new Map([ + [11, { id: 11, name: "foo", slaveId: 4 }], + [21, { id: 21, name: "bar", slaveId: 5 }], + ]); + + Object.assign(master._config, { + description: "test", + visibility: { public: false, lan: false }, + username: "test", + token: "", + game_password: "", + verify_user_identity: false, + allow_commands: "admins-only", + auto_pause: false, + }); + + let [controlClient, controlServer] = link.VirtualConnector.makePair(); + testControl = new clusterctl._Control(controlClient); + testControlConnection = new master._ControlConnection({ agent: "test", version }, controlServer); + master._controlConnections.push(testControlConnection); + + await fs.remove(instancesDir); + await fs.ensureDir(instancesDir) + let [slaveClient, slaveServer] = link.VirtualConnector.makePair(); + testSlave = new client._Slave(slaveClient, { + id: 4, + name: "slave", + instanceDirectory: instancesDir, + factorioDirectory: "factorio", + masterURL: "http://invalid", + masterAuthToken: "invalid", + publicIP: "invalid", + }); + testSlave.instances = await testSlave.findInstances(); + testSlaveConnection = new master._SlaveConnection({ agent: "test", version, name: "slave", id: 4}, slaveServer); + master._slaveConnections.set(4, testSlaveConnection); + }); + + describe("clusterctl", function() { + describe("list-slaves", function() { + it("does not throw", async function() { + await clusterctl._commands.get("list-slaves").run({}, testControl); + }); + }); + describe("list-instances", function() { + it("does not throw", async function() { + await clusterctl._commands.get("list-instances").run({}, testControl); + }); + }); + + describe("create-instances", function() { + it("creates the instance", async function() { + await clusterctl._commands.get("create-instance").run({name: "test", slave: "slave"}, testControl); + assert(await fs.exists(path.join(instancesDir, "test")), "Instance was not created"); + }); + }); + + describe("create-save", function() { + slowTest(this); + it("creates a save", async function() { + await clusterctl._commands.get("create-save").run({instance: "test"}, testControl); + }); + }); + + describe("start-instance", function() { + slowTest(this); + it("starts the instance", async function() { + await clusterctl._commands.get("start-instance").run({instance: "test"}, testControl); + // TODO check that the instance actually stopped + }); + }); + + describe("send-rcon", function() { + slowTest(this); + it("sends the command", async function() { + await clusterctl._commands.get("send-rcon").run({instance: "test", command: "test"}, testControl); + // TODO check that the command was received + }); + }); + + describe("stop-instance", function() { + slowTest(this); + it("stops the instance", async function() { + await clusterctl._commands.get("stop-instance").run({instance: "test"}, testControl); + // TODO check that the instance actually stopped + }); + }); + + describe("delete-instance", function() { + slowTest(this); + it("deletes the instance", async function() { + await clusterctl._commands.get("delete-instance").run({instance: "test"}, testControl); + assert(!await fs.exists(path.join(instancesDir, "test")), "Instance was not deleted"); + }); + }); + }); +});