@@ -1842,6 +1842,59 @@ describe('e2e', function () {
18421842 } ) ;
18431843 } ) ;
18441844
1845+ describe ( 'with custom log retention max logs size' , function ( ) {
1846+ const customLogDir = useTmpdir ( ) ;
1847+
1848+ it ( 'should delete files once it is above the logs retention GB' , async function ( ) {
1849+ const globalConfig = path . join ( homedir , 'globalconfig.conf' ) ;
1850+ await fs . writeFile (
1851+ globalConfig ,
1852+ // Set logRetentionGB to 4 KB
1853+ `mongosh:\n logLocation: ${ JSON . stringify (
1854+ customLogDir . path
1855+ ) } \n logRetentionGB: ${ 4 / 1024 / 1024 } `
1856+ ) ;
1857+ const paths : string [ ] = [ ] ;
1858+ const offset = Math . floor ( Date . now ( ) / 1000 ) ;
1859+
1860+ // Create 10 log files, 1kb each
1861+ for ( let i = 9 ; i >= 0 ; i -- ) {
1862+ const filename = path . join (
1863+ customLogDir . path ,
1864+ ObjectId . createFromTime ( offset - i ) . toHexString ( ) + '_log'
1865+ ) ;
1866+ await fs . writeFile ( filename , '0' . repeat ( 1024 ) ) ;
1867+ paths . push ( filename ) ;
1868+ }
1869+
1870+ // All 10 existing log files exist.
1871+ expect ( await getFilesState ( paths ) ) . to . equal ( '1111111111' ) ;
1872+ shell = this . startTestShell ( {
1873+ args : [ '--nodb' ] ,
1874+ env : {
1875+ ...env ,
1876+ MONGOSH_TEST_ONLY_MAX_LOG_FILE_COUNT : '' ,
1877+ MONGOSH_GLOBAL_CONFIG_FILE_FOR_TESTING : globalConfig ,
1878+ } ,
1879+ forceTerminal : true ,
1880+ } ) ;
1881+
1882+ await shell . waitForPrompt ( ) ;
1883+
1884+ // Add the newly created log to the file list.
1885+ paths . push (
1886+ path . join ( customLogDir . path , `${ shell . logId as string } _log` )
1887+ ) ;
1888+
1889+ expect (
1890+ await shell . executeLine ( 'config.get("logRetentionGB")' )
1891+ ) . contains ( `${ 4 / 1024 / 1024 } ` ) ;
1892+
1893+ // Expect 6 files to be deleted and 5 to remain (including the new log file)
1894+ expect ( await getFilesState ( paths ) ) . to . equal ( '00000011111' ) ;
1895+ } ) ;
1896+ } ) ;
1897+
18451898 it ( 'creates a log file that keeps track of session events' , async function ( ) {
18461899 expect ( await shell . executeLine ( 'print(123 + 456)' ) ) . to . include ( '579' ) ;
18471900 const log = await readLogFile ( ) ;
0 commit comments