Skip to content

Commit e8a9bd9

Browse files
committed
Fix e2e tests for journal
1 parent 6b47819 commit e8a9bd9

File tree

7 files changed

+47
-30
lines changed

7 files changed

+47
-30
lines changed

driver.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ module.exports = () => {
8989
},
9090
};
9191
try {
92+
console.log('Checking journal token');
9293
await log.client.checkJournalToken();
9394
} catch (err) {
9495
console.error(err);
@@ -108,7 +109,7 @@ module.exports = () => {
108109

109110
stream.on('data', msg => {
110111
log.stats.entry.received += 1;
111-
msg.message =msg.line.toString('utf-8');
112+
msg.message = msg.line.toString('utf-8');
112113
delete msg.line;
113114
msg.tag = tag;
114115
log.buffer.push(msg);
@@ -169,7 +170,7 @@ module.exports = () => {
169170
// }
170171
const client = journalClient(Info.Config);
171172
const stream = await client.read(Config, Info);
172-
return stream.pipe(new ParseJournalStream());
173+
return stream;
173174
};
174175

175176
return driver;

journal.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22
const qs = require('qs');
33
const logger = require('superagent-logger');
44
const WebSocket = require('ws');
5+
const { FilterJournalDockerStream, ParseJournalStream } = require('./transform');
56

67
module.exports = (config) => {
7-
const url = `http://${config['journal-fqdn']}/log`;
8+
const url = `https://${config['journal-fqdn']}/log`;
89
const agent = require('superagent').agent().use(logger);
910
return {
10-
checkJournalToken: () => agent.head(url).query({ follow: false })
11+
checkJournalToken: () => agent
12+
.head(url)
13+
.query({ follow: 'false' })
1114
.set('x-auth-password', config['journal-token']),
1215
send: (messages) => new Promise((resolve, reject) => {
1316
const body = Array.isArray(messages) ? messages : [messages];
@@ -37,14 +40,18 @@ module.exports = (config) => {
3740
}
3841

3942
console.log('query', query);
40-
41-
const ws = new WebSocket(`${url}?${qs.stringify(query)}`, {
43+
const ws_url = `${url}?${qs.stringify(query)}`;
44+
console.log('WS', ws_url);
45+
const ws = new WebSocket(ws_url, {
4246
headers: { 'x-auth-password': config['journal-token'] },
4347
});
4448

4549
ws.on('open', () => {
4650
console.log(config['journal-fqdn'], 'websocket opened');
47-
const stream = WebSocket.createWebSocketStream(ws);
51+
const stream = WebSocket.createWebSocketStream(ws).
52+
pipe(new ParseJournalStream()).
53+
pipe(new FilterJournalDockerStream());
54+
stream.pause();
4855
resolve(stream);
4956
});
5057

tests/driver.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ class LogGenerator extends Readable {
3838
}
3939
}
4040

41-
const defaultInfo = {
41+
const defaultInfo = () => ({
4242
Config: {
4343
'journal-fqdn': `${process.env.JOURNAL_ID}.journal.pl-waw-1.hyperone.cloud`,
4444
'journal-token': process.env.JOURNAL_TOKEN,
4545
},
46-
ContainerID: 'eddeb66fc259',
46+
ContainerID: getRandom(),
4747
ContainerName: '/confident_carson',
4848
ContainerEntrypoint: 'sh',
4949
ContainerArgs: [],
@@ -54,12 +54,12 @@ const defaultInfo = {
5454
ContainerLabels: [],
5555
LogPath: '',
5656
DaemonName: 'docker',
57-
};
57+
});
5858

5959
test.serial('driver.startLogging with credentials starts', hasCredentialEnv(async t => {
6060
const d = driver();
6161
const stream = new LogGenerator();
62-
const resp = await d.startLogging(stream, '/tmp/file.sock', defaultInfo);
62+
const resp = await d.startLogging(stream, '/tmp/file.sock', defaultInfo());
6363
t.true(!!resp);
6464
stream.destroy();
6565
}));
@@ -68,7 +68,7 @@ test.serial('driver.stopLogging', hasCredentialEnv(async t => {
6868
const d = driver();
6969
const stream = new LogGenerator();
7070
const file = '/tmp/file.sock';
71-
await d.startLogging(stream, file, defaultInfo);
71+
await d.startLogging(stream, file, defaultInfo());
7272
const resp = d.stopLogging(file);
7373
stream.destroy();
7474
t.true(!!resp);
@@ -78,14 +78,14 @@ test.serial('driver.stopLogging without credentials raise error', async t => {
7878
const d = driver();
7979
const stream = new LogGenerator();
8080
const file = '/tmp/file.sock';
81-
await t.throwsAsync(() => d.startLogging(stream, file, { ...defaultInfo, Config: {} }));
81+
await t.throwsAsync(() => d.startLogging(stream, file, { ...defaultInfo(), Config: {} }));
8282
});
8383

8484
test.serial('driver.startLogging consume logs', hasCredentialEnv(async t => {
8585
const d = driver();
8686
const stream = new LogGenerator();
8787
const file = '/tmp/file.sock';
88-
await d.startLogging(stream, file, defaultInfo);
88+
await d.startLogging(stream, file, defaultInfo());
8989
await d.stopLogging(file);
9090
t.true(stream._index > 100);
9191
}));
@@ -95,19 +95,21 @@ test.serial('driver.startLogging consume & reads logs from journal', hasCredenti
9595
const token = getRandom();
9696
const instream = new Readable.from([
9797
{
98+
time_nano: +new Date(),
9899
source: 'stdout',
99-
line: token,
100+
line: Buffer.from(token),
100101
},
101102
]);
102103
const file = '/tmp/file.sock';
103-
await d.startLogging(instream, file, defaultInfo);
104+
const info = defaultInfo();
105+
await d.startLogging(instream, file, info);
104106
await d.stopLogging(file);
105-
const outstream = await d.readLogs(defaultInfo, {
107+
const outstream = await d.readLogs(info, {
106108
Follow: false,
107109
});
108110
let found = false;
109111
outstream.on('data', msg => {
110-
found = found || msg.line === token;
112+
found = found || msg.line.toString() === token;
111113
});
112114
await new Promise(resolve => {
113115
outstream.on('end', resolve);

tests/e2e/e2e.bats

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ teardown() {
1616
@test "plugin send logs" {
1717
run docker run \
1818
--log-driver 'h1cr.io/h1-docker-logging-plugin:latest' \
19-
--label dockerbats="$BATS_TEST_NAME" \
19+
--label dockerbats="$BATS_TEST_NAME" \
20+
--log-opt labels=dockerbats \
2021
--log-opt journal-fqdn=${JOURNAL_ID}.journal.pl-waw-1.hyperone.cloud \
2122
--log-opt journal-token=${JOURNAL_TOKEN} \
2223
alpine sh -c 'echo $RANDOM';
@@ -30,7 +31,8 @@ teardown() {
3031
@test "plugin flush logs" {
3132
run docker run -d \
3233
--log-driver 'h1cr.io/h1-docker-logging-plugin:latest' \
33-
--label dockerbats="$BATS_TEST_NAME" \
34+
--label dockerbats="$BATS_TEST_NAME" \
35+
--log-opt labels=dockerbats \
3436
--log-opt journal-fqdn=${JOURNAL_ID}.journal.pl-waw-1.hyperone.cloud \
3537
--log-opt journal-token=${JOURNAL_TOKEN} \
3638
alpine sh -c 'seq 1 10; sleep 30';
@@ -47,13 +49,16 @@ teardown() {
4749
token=${RANDOM};
4850
run docker run \
4951
--log-driver 'h1cr.io/h1-docker-logging-plugin:latest' \
50-
--label dockerbats="$BATS_TEST_NAME" \
52+
--label dockerbats="$BATS_TEST_NAME-${token}" \
53+
--log-opt labels=dockerbats \
5154
--log-opt journal-fqdn=${JOURNAL_ID}.journal.pl-waw-1.hyperone.cloud \
5255
--log-opt journal-token=${JOURNAL_TOKEN} \
5356
alpine sh -c "seq 100 | while read line; do echo \"multiple-\${line}-${token}\"; done;";
5457
[ "$status" -eq 0 ]
55-
containerId=$(docker container ls -a -q --filter label=dockerbats="$BATS_TEST_NAME");
58+
containerId=$(docker container ls -a -q --filter label=dockerbats="${BATS_TEST_NAME}-${token}");
59+
echo "Container id: ${containerId}";
5660
run docker logs "${containerId}";
61+
echo "Output of logs: ${output}";
5762
[[ $output =~ "multiple-1-${token}" ]]
5863
[[ $output =~ "multiple-100-${token}" ]]
5964
[ "$status" -eq 0 ]
@@ -62,6 +67,7 @@ teardown() {
6267
@test "plugin require token" {
6368
run docker run -d \
6469
--log-driver 'h1cr.io/h1-docker-logging-plugin:latest' \
70+
--log-opt labels=dockerbats \
6571
--label dockerbats="$BATS_TEST_NAME" \
6672
alpine id;
6773
[[ $output =~ "Missing 'journal-fqdn option of log driver." ]]

tests/protocol.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ test('parse journal stream', async t => new Promise((resolve, reject) => {
2929
count+=1;
3030
})
3131
.on('error', reject)
32-
.on('end', () => {
32+
.on('finish', () => {
3333
t.true(count == 2);
3434
return resolve();
3535
});

transform.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const { messages } = require('./parse');
55

66
class ParseDockerStream extends stream.Transform {
77
constructor(options = {}) {
8-
super({objectMode: true, ...options});
8+
super({ objectMode: true, ...options });
99
this._buf = Buffer.alloc(0);
1010
this.bytes = 0;
1111
}
@@ -42,16 +42,18 @@ class ParseDockerStream extends stream.Transform {
4242

4343
class ParseJournalStream extends stream.Transform {
4444
constructor(options = {}) {
45-
super({objectMode: true, ...options});
45+
super({ objectMode: true, ...options });
4646
this.chunks = 0;
4747
}
4848

4949
_transform(chunk, encoding, callback) {
5050
this.chunks += 1;
5151
try {
52-
const message = JSON.parse(chunk);
53-
message.line = Buffer.from(message.message);
54-
delete message.message;
52+
const message = JSON.parse(chunk.toString('utf-8'));
53+
if (message.message) {
54+
message.line = Buffer.from(message.message);
55+
delete message.message;
56+
}
5557
return callback(null, message);
5658
} catch (err) {
5759
return callback(err);
@@ -67,7 +69,7 @@ class FilterJournalDockerStream extends stream.Transform {
6769

6870
_transform(chunk, encoding, callback) {
6971
this.chunks += 1;
70-
if (!['source', 'time', 'line'].every(x => Object.keys(chunk).includes(x))) {
72+
if (!['source', 'time_nano', 'line'].every(x => Object.keys(chunk).includes(x))) {
7173
return callback(null);
7274
}
7375
return callback(null, chunk);

utils.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ const extract_tag = (config, info) => {
2424

2525
if (config.env && info.ContainerEnv) {
2626
const env = env_to_obj(info.ContainerEnv);
27-
console.log(env);
2827
config.env.split(',').filter(x => env[x]).forEach(name => {
2928
tag[name] = env[name];
3029
});

0 commit comments

Comments
 (0)