2021-01-27 21:13:33 +00:00
|
|
|
// Copyright 2018-2021 Signal Messenger, LLC
|
2020-10-30 20:34:04 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2018-02-22 18:21:53 +00:00
|
|
|
// NOTE: Temporarily allow `then` until we convert the entire file to `async` / `await`:
|
|
|
|
/* eslint-disable more/no-then */
|
|
|
|
|
2021-01-27 21:13:33 +00:00
|
|
|
import * as fs from 'fs';
|
|
|
|
import * as fse from 'fs-extra';
|
|
|
|
import * as os from 'os';
|
|
|
|
import * as path from 'path';
|
|
|
|
import { expect } from 'chai';
|
2018-01-08 21:19:25 +00:00
|
|
|
|
2021-01-27 21:13:33 +00:00
|
|
|
import {
|
2018-01-08 21:19:25 +00:00
|
|
|
eliminateOutOfDateFiles,
|
|
|
|
eliminateOldEntries,
|
|
|
|
isLineAfterDate,
|
|
|
|
fetchLog,
|
2021-07-30 16:43:16 +00:00
|
|
|
fetchLogs,
|
2021-01-27 21:13:33 +00:00
|
|
|
} from '../logging/main_process_logging';
|
2018-01-08 21:19:25 +00:00
|
|
|
|
2021-01-27 21:13:33 +00:00
|
|
|
describe('logging', () => {
|
|
|
|
const fakeLogEntry = ({
|
|
|
|
level = 30,
|
|
|
|
msg = 'hello world',
|
|
|
|
time = new Date().toISOString(),
|
|
|
|
}: {
|
|
|
|
level?: number;
|
|
|
|
msg?: string;
|
|
|
|
time?: string;
|
|
|
|
}): Record<string, unknown> => ({
|
|
|
|
level,
|
|
|
|
msg,
|
|
|
|
time,
|
|
|
|
});
|
2018-01-08 21:19:25 +00:00
|
|
|
|
2021-01-27 21:13:33 +00:00
|
|
|
const fakeLogLine = (...args: Parameters<typeof fakeLogEntry>): string =>
|
|
|
|
JSON.stringify(fakeLogEntry(...args));
|
|
|
|
|
|
|
|
let tmpDir: string;
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
tmpDir = await fs.promises.mkdtemp(path.join(os.tmpdir(), 'signal-test-'));
|
2018-01-08 21:19:25 +00:00
|
|
|
});
|
|
|
|
|
2021-01-27 21:13:33 +00:00
|
|
|
afterEach(async () => {
|
|
|
|
await fse.remove(tmpDir);
|
2018-01-08 21:19:25 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('#isLineAfterDate', () => {
|
|
|
|
it('returns false if falsy', () => {
|
|
|
|
const actual = isLineAfterDate('', new Date());
|
|
|
|
expect(actual).to.equal(false);
|
|
|
|
});
|
|
|
|
it('returns false if invalid JSON', () => {
|
|
|
|
const actual = isLineAfterDate('{{}', new Date());
|
|
|
|
expect(actual).to.equal(false);
|
|
|
|
});
|
|
|
|
it('returns false if date is invalid', () => {
|
|
|
|
const line = JSON.stringify({ time: '2018-01-04T19:17:05.014Z' });
|
|
|
|
const actual = isLineAfterDate(line, new Date('try6'));
|
|
|
|
expect(actual).to.equal(false);
|
|
|
|
});
|
|
|
|
it('returns false if log time is invalid', () => {
|
|
|
|
const line = JSON.stringify({ time: 'try7' });
|
|
|
|
const date = new Date('2018-01-04T19:17:00.000Z');
|
|
|
|
const actual = isLineAfterDate(line, date);
|
|
|
|
expect(actual).to.equal(false);
|
|
|
|
});
|
|
|
|
it('returns false if date before provided date', () => {
|
|
|
|
const line = JSON.stringify({ time: '2018-01-04T19:17:00.000Z' });
|
|
|
|
const date = new Date('2018-01-04T19:17:05.014Z');
|
|
|
|
const actual = isLineAfterDate(line, date);
|
|
|
|
expect(actual).to.equal(false);
|
|
|
|
});
|
|
|
|
it('returns true if date is after provided date', () => {
|
|
|
|
const line = JSON.stringify({ time: '2018-01-04T19:17:05.014Z' });
|
|
|
|
const date = new Date('2018-01-04T19:17:00.000Z');
|
|
|
|
const actual = isLineAfterDate(line, date);
|
|
|
|
expect(actual).to.equal(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#eliminateOutOfDateFiles', () => {
|
|
|
|
it('deletes an empty file', () => {
|
|
|
|
const date = new Date();
|
|
|
|
const log = '\n';
|
2021-01-27 21:13:33 +00:00
|
|
|
const target = path.join(tmpDir, 'log.log');
|
2018-01-08 21:19:25 +00:00
|
|
|
fs.writeFileSync(target, log);
|
|
|
|
|
2021-01-27 21:13:33 +00:00
|
|
|
return eliminateOutOfDateFiles(tmpDir, date).then(() => {
|
2018-01-08 21:19:25 +00:00
|
|
|
expect(fs.existsSync(target)).to.equal(false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('deletes a file with invalid JSON lines', () => {
|
|
|
|
const date = new Date();
|
|
|
|
const log = '{{}\n';
|
2021-01-27 21:13:33 +00:00
|
|
|
const target = path.join(tmpDir, 'log.log');
|
2018-01-08 21:19:25 +00:00
|
|
|
fs.writeFileSync(target, log);
|
|
|
|
|
2021-01-27 21:13:33 +00:00
|
|
|
return eliminateOutOfDateFiles(tmpDir, date).then(() => {
|
2018-01-08 21:19:25 +00:00
|
|
|
expect(fs.existsSync(target)).to.equal(false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('deletes a file with all dates before provided date', () => {
|
|
|
|
const date = new Date('2018-01-04T19:17:05.014Z');
|
|
|
|
const contents = [
|
|
|
|
JSON.stringify({ time: '2018-01-04T19:17:00.014Z' }),
|
|
|
|
JSON.stringify({ time: '2018-01-04T19:17:01.014Z' }),
|
|
|
|
JSON.stringify({ time: '2018-01-04T19:17:02.014Z' }),
|
|
|
|
JSON.stringify({ time: '2018-01-04T19:17:03.014Z' }),
|
|
|
|
].join('\n');
|
2021-01-27 21:13:33 +00:00
|
|
|
const target = path.join(tmpDir, 'log.log');
|
2018-01-08 21:19:25 +00:00
|
|
|
fs.writeFileSync(target, contents);
|
|
|
|
|
2021-01-27 21:13:33 +00:00
|
|
|
return eliminateOutOfDateFiles(tmpDir, date).then(() => {
|
2018-01-08 21:19:25 +00:00
|
|
|
expect(fs.existsSync(target)).to.equal(false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('keeps a file with first line date before provided date', () => {
|
|
|
|
const date = new Date('2018-01-04T19:16:00.000Z');
|
|
|
|
const contents = [
|
|
|
|
JSON.stringify({ time: '2018-01-04T19:17:00.014Z' }),
|
|
|
|
JSON.stringify({ time: '2018-01-04T19:17:01.014Z' }),
|
|
|
|
JSON.stringify({ time: '2018-01-04T19:17:02.014Z' }),
|
|
|
|
JSON.stringify({ time: '2018-01-04T19:17:03.014Z' }),
|
|
|
|
].join('\n');
|
2021-01-27 21:13:33 +00:00
|
|
|
const target = path.join(tmpDir, 'log.log');
|
2018-01-08 21:19:25 +00:00
|
|
|
fs.writeFileSync(target, contents);
|
|
|
|
|
2021-01-27 21:13:33 +00:00
|
|
|
return eliminateOutOfDateFiles(tmpDir, date).then(() => {
|
2018-01-08 21:19:25 +00:00
|
|
|
expect(fs.existsSync(target)).to.equal(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('keeps a file with last line date before provided date', () => {
|
|
|
|
const date = new Date('2018-01-04T19:17:01.000Z');
|
|
|
|
const contents = [
|
|
|
|
JSON.stringify({ time: '2018-01-04T19:17:00.014Z' }),
|
|
|
|
JSON.stringify({ time: '2018-01-04T19:17:01.014Z' }),
|
|
|
|
JSON.stringify({ time: '2018-01-04T19:17:02.014Z' }),
|
|
|
|
JSON.stringify({ time: '2018-01-04T19:17:03.014Z' }),
|
|
|
|
].join('\n');
|
2021-01-27 21:13:33 +00:00
|
|
|
const target = path.join(tmpDir, 'log.log');
|
2018-01-08 21:19:25 +00:00
|
|
|
fs.writeFileSync(target, contents);
|
|
|
|
|
2021-01-27 21:13:33 +00:00
|
|
|
return eliminateOutOfDateFiles(tmpDir, date).then(() => {
|
2018-01-08 21:19:25 +00:00
|
|
|
expect(fs.existsSync(target)).to.equal(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#eliminateOldEntries', () => {
|
|
|
|
it('eliminates all non-parsing entries', () => {
|
|
|
|
const date = new Date('2018-01-04T19:17:01.000Z');
|
|
|
|
const contents = [
|
|
|
|
'random line',
|
2021-01-27 21:13:33 +00:00
|
|
|
fakeLogLine({ time: '2018-01-04T19:17:01.014Z' }),
|
|
|
|
fakeLogLine({ time: '2018-01-04T19:17:02.014Z' }),
|
|
|
|
fakeLogLine({ time: '2018-01-04T19:17:03.014Z' }),
|
2018-01-08 21:19:25 +00:00
|
|
|
].join('\n');
|
|
|
|
const expected = [
|
2021-01-27 21:13:33 +00:00
|
|
|
fakeLogEntry({ time: '2018-01-04T19:17:01.014Z' }),
|
|
|
|
fakeLogEntry({ time: '2018-01-04T19:17:02.014Z' }),
|
|
|
|
fakeLogEntry({ time: '2018-01-04T19:17:03.014Z' }),
|
|
|
|
];
|
2018-01-08 21:19:25 +00:00
|
|
|
|
2021-01-27 21:13:33 +00:00
|
|
|
const target = path.join(tmpDir, 'log.log');
|
2018-04-27 21:25:04 +00:00
|
|
|
const files = [
|
|
|
|
{
|
|
|
|
path: target,
|
|
|
|
},
|
|
|
|
];
|
2018-01-08 21:19:25 +00:00
|
|
|
|
|
|
|
fs.writeFileSync(target, contents);
|
|
|
|
|
|
|
|
return eliminateOldEntries(files, date).then(() => {
|
2021-01-27 21:13:33 +00:00
|
|
|
const actualEntries = fs
|
|
|
|
.readFileSync(target, 'utf8')
|
|
|
|
.split('\n')
|
|
|
|
.map(line => line.trim())
|
|
|
|
.filter(Boolean)
|
|
|
|
.map(line => JSON.parse(line));
|
|
|
|
expect(actualEntries).to.deep.equal(expected);
|
2018-01-08 21:19:25 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
it('preserves all lines if before target date', () => {
|
|
|
|
const date = new Date('2018-01-04T19:17:03.000Z');
|
|
|
|
const contents = [
|
|
|
|
'random line',
|
2021-01-27 21:13:33 +00:00
|
|
|
fakeLogLine({ time: '2018-01-04T19:17:01.014Z' }),
|
|
|
|
fakeLogLine({ time: '2018-01-04T19:17:02.014Z' }),
|
|
|
|
fakeLogLine({ time: '2018-01-04T19:17:03.014Z' }),
|
2018-01-08 21:19:25 +00:00
|
|
|
].join('\n');
|
2021-01-27 21:13:33 +00:00
|
|
|
const expected = fakeLogEntry({ time: '2018-01-04T19:17:03.014Z' });
|
2018-01-08 21:19:25 +00:00
|
|
|
|
2021-01-27 21:13:33 +00:00
|
|
|
const target = path.join(tmpDir, 'log.log');
|
2018-04-27 21:25:04 +00:00
|
|
|
const files = [
|
|
|
|
{
|
|
|
|
path: target,
|
|
|
|
},
|
|
|
|
];
|
2018-01-08 21:19:25 +00:00
|
|
|
|
|
|
|
fs.writeFileSync(target, contents);
|
|
|
|
|
|
|
|
return eliminateOldEntries(files, date).then(() => {
|
2021-01-27 21:13:33 +00:00
|
|
|
// There should only be 1 line, so we can parse it safely.
|
|
|
|
expect(JSON.parse(fs.readFileSync(target, 'utf8'))).to.deep.equal(
|
|
|
|
expected
|
|
|
|
);
|
2018-01-08 21:19:25 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#fetchLog', () => {
|
|
|
|
it('returns error if file does not exist', () => {
|
|
|
|
const target = 'random_file';
|
2018-04-27 21:25:04 +00:00
|
|
|
return fetchLog(target).then(
|
|
|
|
() => {
|
|
|
|
throw new Error('Expected an error!');
|
|
|
|
},
|
|
|
|
error => {
|
|
|
|
expect(error)
|
|
|
|
.to.have.property('message')
|
|
|
|
.that.match(/random_file/);
|
|
|
|
}
|
|
|
|
);
|
2018-01-08 21:19:25 +00:00
|
|
|
});
|
|
|
|
it('returns empty array if file has no valid JSON lines', () => {
|
|
|
|
const contents = 'line 1\nline2\n';
|
2021-01-27 21:13:33 +00:00
|
|
|
const target = path.join(tmpDir, 'test.log');
|
2018-01-08 21:19:25 +00:00
|
|
|
|
|
|
|
fs.writeFileSync(target, contents);
|
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
return fetchLog(target).then(result => {
|
2021-01-27 21:13:33 +00:00
|
|
|
expect(result).to.deep.equal([]);
|
2018-01-08 21:19:25 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
it('returns just three fields in each returned line', () => {
|
|
|
|
const contents = [
|
|
|
|
JSON.stringify({
|
|
|
|
one: 1,
|
|
|
|
two: 2,
|
2021-01-27 21:13:33 +00:00
|
|
|
level: 30,
|
|
|
|
time: '2020-04-20T06:09:08.000Z',
|
|
|
|
msg: 'message 1',
|
2018-01-08 21:19:25 +00:00
|
|
|
}),
|
|
|
|
JSON.stringify({
|
|
|
|
one: 1,
|
|
|
|
two: 2,
|
2021-01-27 21:13:33 +00:00
|
|
|
level: 40,
|
|
|
|
time: '2021-04-20T06:09:08.000Z',
|
|
|
|
msg: 'message 2',
|
2018-01-08 21:19:25 +00:00
|
|
|
}),
|
|
|
|
'',
|
|
|
|
].join('\n');
|
2018-04-27 21:25:04 +00:00
|
|
|
const expected = [
|
|
|
|
{
|
2021-01-27 21:13:33 +00:00
|
|
|
level: 30,
|
|
|
|
time: '2020-04-20T06:09:08.000Z',
|
|
|
|
msg: 'message 1',
|
2018-04-27 21:25:04 +00:00
|
|
|
},
|
|
|
|
{
|
2021-01-27 21:13:33 +00:00
|
|
|
level: 40,
|
|
|
|
time: '2021-04-20T06:09:08.000Z',
|
|
|
|
msg: 'message 2',
|
2018-04-27 21:25:04 +00:00
|
|
|
},
|
|
|
|
];
|
2018-01-08 21:19:25 +00:00
|
|
|
|
2021-01-27 21:13:33 +00:00
|
|
|
const target = path.join(tmpDir, 'test.log');
|
2018-01-08 21:19:25 +00:00
|
|
|
|
|
|
|
fs.writeFileSync(target, contents);
|
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
return fetchLog(target).then(result => {
|
2018-01-08 21:19:25 +00:00
|
|
|
expect(result).to.deep.equal(expected);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-07-30 16:43:16 +00:00
|
|
|
describe('#fetchLogs', () => {
|
2018-01-08 21:19:25 +00:00
|
|
|
it('returns single entry if no files', () => {
|
2021-07-30 16:43:16 +00:00
|
|
|
return fetchLogs(tmpDir).then(results => {
|
2018-01-08 21:19:25 +00:00
|
|
|
expect(results).to.have.length(1);
|
|
|
|
expect(results[0].msg).to.match(/Loaded this list/);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('returns sorted entries from all files', () => {
|
|
|
|
const first = [
|
2021-01-27 21:13:33 +00:00
|
|
|
fakeLogLine({ msg: '2', time: '2018-01-04T19:17:05.014Z' }),
|
2018-01-08 21:19:25 +00:00
|
|
|
'',
|
|
|
|
].join('\n');
|
|
|
|
const second = [
|
2021-01-27 21:13:33 +00:00
|
|
|
fakeLogLine({ msg: '1', time: '2018-01-04T19:17:00.014Z' }),
|
|
|
|
fakeLogLine({ msg: '3', time: '2018-01-04T19:18:00.014Z' }),
|
2018-01-08 21:19:25 +00:00
|
|
|
'',
|
|
|
|
].join('\n');
|
|
|
|
|
2021-01-27 21:13:33 +00:00
|
|
|
fs.writeFileSync(path.join(tmpDir, 'first.log'), first);
|
|
|
|
fs.writeFileSync(path.join(tmpDir, 'second.log'), second);
|
2018-01-08 21:19:25 +00:00
|
|
|
|
2021-07-30 16:43:16 +00:00
|
|
|
return fetchLogs(tmpDir).then(results => {
|
2018-01-08 21:19:25 +00:00
|
|
|
expect(results).to.have.length(4);
|
2021-01-27 21:13:33 +00:00
|
|
|
expect(results[0].msg).to.equal('1');
|
|
|
|
expect(results[1].msg).to.equal('2');
|
|
|
|
expect(results[2].msg).to.equal('3');
|
2018-01-08 21:19:25 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|