|
1 | 1 | /* eslint-disable no-control-regex */
|
2 | 2 | import { expect } from 'chai';
|
3 | 3 | import type { Db } from 'mongodb';
|
4 |
| -import { MongoClient } from 'mongodb'; |
| 4 | +import { MongoClient, ObjectId } from 'mongodb'; |
5 | 5 |
|
6 | 6 | import { eventually } from '../../../testing/eventually';
|
7 | 7 | import { TestShell } from './test-shell';
|
@@ -1674,6 +1674,74 @@ describe('e2e', function () {
|
1674 | 1674 | });
|
1675 | 1675 | });
|
1676 | 1676 |
|
| 1677 | + /** Helper to visualize the existence of files, 1 if it exists, 0 otherwise. */ |
| 1678 | + const existingFiles = async (paths: string[]) => { |
| 1679 | + return ( |
| 1680 | + await Promise.all( |
| 1681 | + paths.map((path) => |
| 1682 | + fs.stat(path).then( |
| 1683 | + () => 1, |
| 1684 | + () => 0 |
| 1685 | + ) |
| 1686 | + ) |
| 1687 | + ) |
| 1688 | + ).join(''); |
| 1689 | + }; |
| 1690 | + |
| 1691 | + describe('with custom log retention days', function () { |
| 1692 | + const customLogDir = useTmpdir(); |
| 1693 | + |
| 1694 | + it('should delete older files according to the setting', async function () { |
| 1695 | + const paths: string[] = []; |
| 1696 | + const today = Math.floor(Date.now() / 1000); |
| 1697 | + const tenDaysAgo = today - 10 * 24 * 60 * 60; |
| 1698 | + // Create 6 files older than 7 days |
| 1699 | + for (let i = 5; i >= 0; i--) { |
| 1700 | + const filename = path.join( |
| 1701 | + customLogDir.path, |
| 1702 | + ObjectId.createFromTime(tenDaysAgo - i).toHexString() + '_log' |
| 1703 | + ); |
| 1704 | + await fs.writeFile(filename, ''); |
| 1705 | + paths.push(filename); |
| 1706 | + } |
| 1707 | + // Create 4 files newer than 10 days |
| 1708 | + for (let i = 3; i >= 0; i--) { |
| 1709 | + const filename = path.join( |
| 1710 | + customLogDir.path, |
| 1711 | + ObjectId.createFromTime(today - i).toHexString() + '_log' |
| 1712 | + ); |
| 1713 | + await fs.writeFile(filename, ''); |
| 1714 | + paths.push(filename); |
| 1715 | + } |
| 1716 | + |
| 1717 | + const retentionDays = 7; |
| 1718 | + |
| 1719 | + const globalConfig = path.join(homedir, 'globalconfig.conf'); |
| 1720 | + await fs.writeFile( |
| 1721 | + globalConfig, |
| 1722 | + `mongosh:\n logLocation: "${customLogDir.path}"\n logRetentionDays: ${retentionDays}` |
| 1723 | + ); |
| 1724 | + |
| 1725 | + expect(await existingFiles(paths)).equals('1111111111'); |
| 1726 | + |
| 1727 | + shell = this.startTestShell({ |
| 1728 | + args: ['--nodb'], |
| 1729 | + env: { |
| 1730 | + ...env, |
| 1731 | + MONGOSH_GLOBAL_CONFIG_FILE_FOR_TESTING: globalConfig, |
| 1732 | + }, |
| 1733 | + forceTerminal: true, |
| 1734 | + }); |
| 1735 | + |
| 1736 | + await shell.waitForPrompt(); |
| 1737 | + |
| 1738 | + // Add the newly created log file |
| 1739 | + paths.push(path.join(customLogDir.path, `${shell.logId}_log`)); |
| 1740 | + // Expect 6 files to be deleted and 5 to remain (including the new log file) |
| 1741 | + expect(await existingFiles(paths)).equals('00000011111'); |
| 1742 | + }); |
| 1743 | + }); |
| 1744 | + |
1677 | 1745 | it('creates a log file that keeps track of session events', async function () {
|
1678 | 1746 | expect(await shell.executeLine('print(123 + 456)')).to.include('579');
|
1679 | 1747 | const log = await readLogFile();
|
|
0 commit comments