Skip to content

Commit a88c203

Browse files
From a live coding session to get this in running different environment. (#647)
Running the test environment lead to problem with the docker network type `host`. Instead the redis and mongo container are not available via hostname instad of 127.0.0.1. On the CI server both services still needs to be used from 127.0.01
1 parent 51a2edc commit a88c203

File tree

7 files changed

+55
-17
lines changed

7 files changed

+55
-17
lines changed

.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export STEAM_API_KEY=YOUR_KEY_HERE
2+
export STEAM_ID=YOUR_ID_HERE

doc/Testing.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# Testing
22

3-
Following tests are automatically executed on a new PR and new commits on master.
4-
3+
### if you want to test in a local environment:
4+
- create a .env file in root see .env.example
5+
6+
### Following tests are automatically executed on a new PR and new commits on master:
57
- Checking Code style
68
- Starting a private server and checking for controller upgrade with a certain time.
79

8-
Unit tests would be nice.
10+
Unit tests would be nice.

docker-compose-setup.yml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,17 @@ services:
2020
environment:
2121
- STEAM_API_KEY
2222
- STEAM_ID
23+
- MONGO_HOST=mongo
24+
- REDIS_HOST=redis
25+
env_file:
26+
- .env
2327
depends_on:
2428
- redis
2529
- mongo
26-
network_mode: "host"
30+
#network_mode: "host"
31+
ports:
32+
- 21025:21025
33+
- 21026:21026
2734
logs:
2835
image: node:12
2936
working_dir: /mnt
@@ -37,4 +44,4 @@ services:
3744
- server
3845
network_mode: "host"
3946
profiles:
40-
- donotstart
47+
- donotstart

docker-compose.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,14 @@ services:
2020
environment:
2121
- STEAM_API_KEY
2222
- STEAM_ID
23+
- MONGO_HOST=mongo
24+
- REDIS_HOST=redis
25+
env_file:
26+
- .env
2327
depends_on:
2428
- redis
2529
- mongo
26-
network_mode: "host"
30+
#network_mode: "host"
31+
ports:
32+
- 21025:21025
33+
- 21026:21026

utils/test.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const _ = require('lodash');
44

55
const {setPassword, sleep, initServer, startServer, spawnBots, helpers, logConsole, followLog} = require('./testHelpers');
66

7-
const {cliPort, verbose, tickDuration, playerRoom, players, rooms, milestones} = require('./testConfig');
7+
const {cliPort, verbose, tickDuration, waitForConnection, playerRoom, players, rooms, milestones} = require('./testConfig');
88

99
const controllerRooms = {};
1010
const status = {};
@@ -52,7 +52,7 @@ class Tester {
5252
* @param {object} defer
5353
* @return {undefined}
5454
*/
55-
async checkForSucces(line, defer) {
55+
async checkForSuccess(line, defer) {
5656
if (botsSpawned && line.startsWith(`'OK'`)) {
5757
let appendix = '';
5858
if (this.maxRuntime > 0) {
@@ -97,7 +97,7 @@ class Tester {
9797
const defer = q.defer();
9898
const socket = net.connect(cliPort, '127.0.0.1');
9999

100-
socket.on('data', async (raw) => {
100+
socket.on('data', async(raw) => {
101101
const data = raw.toString('utf8');
102102
const line = data.replace(/^< /, '').replace(/\n< /, '');
103103
if (await spawnBots(line, socket, rooms, players, tickDuration)) {
@@ -115,11 +115,11 @@ class Tester {
115115
return;
116116
}
117117

118-
await this.checkForSucces(line, defer);
118+
await this.checkForSuccess(line, defer);
119119
});
120120

121121
socket.on('connect', () => {
122-
console.log('connected');
122+
console.log(new Date().toString(), `${lastTick} connected`);
123123
});
124124
socket.on('error', (error) => {
125125
defer.reject(error);
@@ -132,7 +132,8 @@ class Tester {
132132
const start = Date.now();
133133
await initServer();
134134
await startServer();
135-
await sleep(5);
135+
console.log(new Date().toString(), 'waiting for connection');
136+
await sleep(waitForConnection);
136137
let exitCode = 0;
137138
try {
138139
await this.execute();

utils/testConfig.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ module.exports.verbose = false;
44

55
module.exports.tickDuration = 10;
66

7+
// todo for local-testing
8+
// if your machine is slow try increment this
9+
module.exports.waitForConnection = 10;
10+
711
module.exports.playerRoom = 'W7N4';
812
const players = {
913
'W1N7': {x: 43, y: 35},

utils/testHelpers.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ let hostname = '127.0.0.1';
1313
function setHostname(newHostname) {
1414
hostname = newHostname;
1515
}
16+
1617
module.exports.setHostname = setHostname;
1718

1819
/**
@@ -43,14 +44,16 @@ async function followLog(rooms, logConsole, statusUpdater, restrictToRoom) {
4344

4445
await api.auth();
4546

46-
api.socket.connect();
47+
await api.socket.connect();
4748
api.socket.on('connected', ()=> {});
4849
api.socket.on('auth', (event)=> {});
49-
api.socket.subscribe('console', logConsole(room));
50-
api.socket.subscribe('room:' + room, statusUpdater);
50+
await api.socket.subscribe('console', logConsole(room));
51+
await api.socket.subscribe('room:' + room, statusUpdater);
5152
}
52-
return new Promise(() => {});
53+
return new Promise(() => {
54+
});
5355
}
56+
5457
module.exports.followLog = followLog;
5558

5659
/**
@@ -92,6 +95,7 @@ module.exports.setPassword = setPassword;
9295
function sleep(seconds) {
9396
return new Promise((resolve) => setTimeout(resolve, seconds * 1000));
9497
}
98+
9599
module.exports.sleep = sleep;
96100

97101
async function initServer() {
@@ -107,6 +111,15 @@ async function initServer() {
107111
const configFilename = path.resolve(dir, '.screepsrc');
108112
let config = fs.readFileSync(configFilename, {encoding: 'utf8'});
109113
config = config.replace(/{{STEAM_KEY}}/, process.env.STEAM_API_KEY);
114+
config += '\n\n';
115+
if (process.env.MONGO_HOST) {
116+
config += '[mongo]\n';
117+
config += `host = ${process.env.MONGO_HOST}\n\n`;
118+
}
119+
if (process.env.REDIS_HOST) {
120+
config += '[redis]\n';
121+
config += `host = ${process.env.REDIS_HOST}\n\n`;
122+
}
110123
fs.writeFileSync(configFilename, config);
111124
fs.chmodSync(path.resolve(dir, 'node_modules/.hooks/install'), '755');
112125
fs.chmodSync(path.resolve(dir, 'node_modules/.hooks/uninstall'), '755');
@@ -127,6 +140,7 @@ async function initServer() {
127140
console.log(e);
128141
}
129142
}
143+
130144
module.exports.initServer = initServer;
131145

132146
/**
@@ -139,13 +153,14 @@ async function startServer() {
139153
process.chdir(dir);
140154
return lib.start({}, process.stdout);
141155
}
156+
142157
module.exports.startServer = startServer;
143158

144159
/**
145160
* logs event
146161
*
147162
* @param {string} room
148-
* @return {void}
163+
* @return {function}
149164
*/
150165
const logConsole = function(room) {
151166
return (event) => {

0 commit comments

Comments
 (0)