Skip to content

Commit

Permalink
tests: add end to end deletion test
Browse files Browse the repository at this point in the history
  • Loading branch information
gagik committed Feb 6, 2025
1 parent cf012a0 commit 539dbde
Showing 1 changed file with 55 additions and 4 deletions.
59 changes: 55 additions & 4 deletions packages/e2e-tests/test/e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1674,8 +1674,9 @@ describe('e2e', function () {
});
});

/** Helper to visualize the existence of files, 1 if it exists, 0 otherwise. */
const existingFiles = async (paths: string[]) => {
/** Helper to visualize and compare the existence of files in a specific order.
* Returns a string comprised of: 1 if a given file exists, 0 otherwise. */
const getFilesState = async (paths: string[]) => {
return (
await Promise.all(
paths.map((path) =>
Expand Down Expand Up @@ -1722,7 +1723,7 @@ describe('e2e', function () {
`mongosh:\n logLocation: "${customLogDir.path}"\n logRetentionDays: ${retentionDays}`
);

expect(await existingFiles(paths)).equals('1111111111');
expect(await getFilesState(paths)).equals('1111111111');

shell = this.startTestShell({
args: ['--nodb'],
Expand All @@ -1738,7 +1739,57 @@ describe('e2e', function () {
// Add the newly created log file
paths.push(path.join(customLogDir.path, `${shell.logId}_log`));
// Expect 6 files to be deleted and 5 to remain (including the new log file)
expect(await existingFiles(paths)).equals('00000011111');
expect(await getFilesState(paths)).equals('00000011111');
});
});

describe('with custom log retention max file count', function () {
const customLogDir = useTmpdir();

it('should delete files once it is above the max file limit', async function () {
const globalConfig = path.join(homedir, 'globalconfig.conf');
await fs.writeFile(
globalConfig,
`mongosh:\n logLocation: "${customLogDir.path}"\n logMaxFileCount: 4`
);
const paths: string[] = [];
const offset = Math.floor(Date.now() / 1000);

// Create 10 log files
for (let i = 9; i >= 0; i--) {
const filename = path.join(
customLogDir.path,
ObjectId.createFromTime(offset - i).toHexString() + '_log'
);
await fs.writeFile(filename, '');
paths.push(filename);
}

// All 10 existing log files exist.
expect(await getFilesState(paths)).to.equal('1111111111');
shell = this.startTestShell({
args: ['--nodb'],
env: {
...env,
MONGOSH_TEST_ONLY_MAX_LOG_FILE_COUNT: '',
MONGOSH_GLOBAL_CONFIG_FILE_FOR_TESTING: globalConfig,
},
forceTerminal: true,
});

await shell.waitForPrompt();

// Add the newly created log to the file list.
paths.push(
path.join(customLogDir.path, `${shell.logId as string}_log`)
);

expect(
await shell.executeLine('config.get("logMaxFileCount")')
).contains('4');

// Expect 7 files to be deleted and 4 to remain (including the new log file)
expect(await getFilesState(paths)).to.equal('00000001111');
});
});

Expand Down

0 comments on commit 539dbde

Please sign in to comment.