Add integration testing of commands

Add testing of the clusterctl commands against the master and slave
code.
This commit is contained in:
Hornwitser committed 2019-11-30 00:08:10 +01:00
1 parent b24c129e38
commit 3be264c9a9
6 files changed
+192 -1

No files matched your search

+1
View File
@@ -820,6 +820,7 @@ module.exports = {
_Instance: Instance,
_checkFilename: checkFilename,
_symlinkMods: symlinkMods,
_Slave: Slave,
};
if (module === require.main) {
+2
View File
@@ -438,6 +438,8 @@ module.exports = {
_formatOutput: formatOutput,
_resolveInstance: resolveInstance,
_Control: Control,
_commands: commands,
}
+54
View File
@@ -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,
};
+2 -1
View File
@@ -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);
}
+4
View File
@@ -665,6 +665,10 @@ module.exports = {
_db: db,
_config: config,
_SocketIOServerConnector: SocketIOServerConnector,
_controlConnections: controlConnections,
_ControlConnection: ControlConnection,
_slaveConnections: slaveConnections,
_SlaveConnection: SlaveConnection,
}
if (module === require.main) {
+129
View File
@@ -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");
});
});
});
});