HMSDebuggerComponent { public boolean cleanHmsDebugLogs( String logArchiveDirectoryName ) throws HmsException { logger.debug( "In cleanHmsDebugLogs(), trying to delete logs in this directory [ " + logArchiveDirectoryName + " ]" ); if ( logArchiveDirectoryName != null ) { File logArchiveName = new File( logArchiveDirectoryName ); if ( logArchiveName.exists() && logArchiveName.isDirectory() && logArchiveName.canWrite() ) { for ( File archive : logArchiveName.listFiles() ) { if ( archive.getName().matches( HMS_ARCHIVE_REGEX ) ) { try { if ( isFileDeleteable( archive.getName() ) ) { archive.delete(); } } catch ( Exception e ) { logger.error( "Unable to delete log archive [ " + archive.getName() + " ] from filesystem.", e ); } } } return true; } else { String err = "[ " + logArchiveDirectoryName + " ] doesnot exist or is not a directory."; logger.error( err ); throw new HmsException( err ); } } else { String err = "Absolute path for log directory [ " + logArchiveDirectoryName + " ] cannot be null"; logger.error( err ); throw new HmsException( err ); } }  String archiveHmsDebugLogs( final String host_id, final String hmsIpAddr, final String hmsOobUsername,
                                       final String hmsLogArchiverScript, final String hmsLogArchiveLocation,
                                       final String hmsOobLogLocation, final String hmsIbLogLocation,
                                       final int noOfLines, final EventComponent component ); boolean writeAllEventsToFile( ServerNode node, String fileName, String logDirectoryPath ); boolean writeNBSwitchInfoToFile( NBSwitchInfo node, String fileName, String logDirectoryPath ); boolean writeServerInfoToFile( ServerInfo host, String fileName, String logDirectoryPath ); boolean writeFile( String eventsLogFilePath, String content ); String getAbsoluteFilePath( String directoryLocation, String filename ); String getEventsDataAsString( ServerNode node ); String getSwitchEventsDataAsString( SwitchNode node ); boolean writeHmsLogs( String node_id, String hmsLogArchiverScript, String hmsLogArchiveLocation,
                                 String timestamp, int noOfLines, String nodeInfoLogFileName, String hmsArchiveName,
                                 String hmsOobLogLocation, String hmsIbLogLocation,
                                 final EventComponent isHostOrSwitch ); boolean callLogArchiverShellScript( String node_id, String hmsLogArchiverScript,
                                               String hmsLogArchiveLocation, String eventsLogFileName,
                                               String hmsArchiveName, String host_id, String timestamp,
                                               String hmsOobLogFileName, String hmsIbLogLocation, int noOfLines ); boolean callServerLogArchiverShellScript( String node_id, String hmsLogArchiverScript,
                                                     String hmsLogArchiveLocation, String serverInfoFileName,
                                                     String hmsArchiveName, String host_id, String timestamp,
                                                     String hmsOobLogLocation, String hmsIbLogLocation, int noOfLines,
                                                     String eventLogFileName ); boolean isParameterNotNullNonEmpty( String param ); String getCurrentTimestamp(); String getServerInfoOrNBSwitchInfoLogFilename( String host_id, String timestamp,
                                                          EventComponent isHostOrSwitch ); String getHmsEventsLogFilename( String host_id, String timestamp ); String getHmsOobLogFilename( String host_id, String timestamp ); String getHmsLogArchiveName( String host_id, String timestamp ); boolean cleanHmsDebugLogs( String logArchiveDirectoryName ); boolean isFileDeleteable( String logArchiveName ); Date parseDateFromFileName( String logArchiveName ); void setHmsSwitchManager( HmsSwitchManager hmsSwitchManager ); final String TIMESTAMP_FORMAT; final String HMS_ARCHIVE_REGEX; final String HMS_TIMESTAMP_EXTRACTION_REGEX; final long logClearDurationInHours; }
@Test( expected = HmsException.class ) public void cleanHmsLogs_invalidInput() throws HmsException { assertTrue( debuggerUtil.cleanHmsDebugLogs( null ) ); }
@Test( expected = HmsException.class ) public void cleanHmsLogs_validInput_dirNotExist() throws HmsException { debuggerUtil.cleanHmsDebugLogs( "sampleDir" ); }
@Test public void cleanHmsLogs_validInput() throws Exception { File fileMock = mock( File.class ); when( fileMock.canWrite() ).thenReturn( true ); when( fileMock.getParentFile() ).thenReturn( fileMock ); when( fileMock.isDirectory() ).thenReturn( true ); when( fileMock.exists() ).thenReturn( true ); File[] dummyFiles = { fileMock }; when( fileMock.listFiles() ).thenReturn( dummyFiles ); when( fileMock.getName() ).thenReturn( "N1_hms_logs_2015-03-03_00-00-00.zip" ); when( fileMock.delete() ).thenReturn( true ); whenNew( File.class ).withArguments( anyString() ).thenReturn( fileMock ); debuggerUtil.cleanHmsDebugLogs( "testFile" ); }