diff options
| author | Oskar Nyberg <oskar@mullvad.net> | 2020-11-19 16:02:24 +0100 |
|---|---|---|
| committer | Oskar Nyberg <oskar@mullvad.net> | 2020-11-23 12:23:55 +0100 |
| commit | 86af950a735ad5aef786a92fa0b0f26bb482c38e (patch) | |
| tree | 7d0cd5d27ab4f46208ef0979e0943a9f7bff18f8 | |
| parent | 3d7de19b46aff9258864837a783780796c7c1b90 (diff) | |
| download | mullvadvpn-86af950a735ad5aef786a92fa0b0f26bb482c38e.tar.xz mullvadvpn-86af950a735ad5aef786a92fa0b0f26bb482c38e.zip | |
Add tests for log file rotation and clean up
| -rw-r--r-- | gui/test/logging.spec.ts | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/gui/test/logging.spec.ts b/gui/test/logging.spec.ts new file mode 100644 index 0000000000..9572c6ed24 --- /dev/null +++ b/gui/test/logging.spec.ts @@ -0,0 +1,81 @@ +import { expect } from 'chai'; +import fs from 'fs'; +import sinon from 'sinon'; +import { it, describe, before, beforeEach, after } from 'mocha'; +import * as logging from '../src/shared/logging'; + +const aPath = 'log-directory/a.log'; +const oldAPath = 'log-directory/a.old.log'; +const bPath = 'log-directory/b.log'; +const oldBPath = 'log-directory/b.old.log'; + +const initialFileState = { + [aPath]: 'a', + [bPath]: 'b', + [oldBPath]: 'old b', +}; + +describe('Logging', () => { + let files: Record<string, string>; + let sandbox: sinon.SinonSandbox; + + before(() => { + sandbox = sinon.createSandbox(); + + sandbox.stub(fs, 'accessSync').callsFake((path) => { + if (files[path as string] === undefined) { + throw Error('File not found'); + } + }); + + sandbox.stub(fs, 'renameSync').callsFake((oldPath, newPath) => { + files[newPath as string] = files[oldPath as string]; + fs.unlinkSync(oldPath); + }); + + sandbox.stub(fs, 'unlinkSync').callsFake((path) => { + delete files[path as string]; + }); + + sandbox.stub(fs, 'readFileSync').callsFake((path) => { + return files[path as string] ?? ''; + }); + }); + + after(() => { + sandbox.restore(); + }); + + beforeEach(() => { + files = { ...initialFileState }; + }); + + it('should backup log file', () => { + logging.backupLogFile(aPath); + const oldA = fs.readFileSync(oldAPath).toString(); + + expect(fs.accessSync.bind(null, aPath)).to.throw(); + expect(oldA).to.equal(initialFileState[aPath]); + }); + + it('should replace backup file', () => { + logging.backupLogFile(bPath); + const oldB = fs.readFileSync(oldBPath).toString(); + + expect(fs.accessSync.bind(null, bPath)).to.throw(); + expect(oldB).to.equal(initialFileState[bPath]); + }); + + it('should clean up old log files', () => { + logging.rotateOrDeleteFile(bPath); + const oldB = fs.readFileSync(oldBPath).toString(); + + expect(fs.accessSync.bind(null, bPath)).to.throw(); + expect(oldB).to.equal(initialFileState[bPath]); + + logging.rotateOrDeleteFile(bPath); + + expect(fs.accessSync.bind(null, bPath)).to.throw(); + expect(fs.accessSync.bind(null, oldBPath)).to.throw(); + }); +}); |
