/** * Returns file size limit in bytes */ long parseFileSizeLimit(String fileSizeLimit) { if (StringUtils.isEmpty(fileSizeLimit)) { return 0; } long sizeLimit = parseSize(fileSizeLimit); long byteMultiplier = parseByteMultiplier(fileSizeLimit); return sizeLimit * byteMultiplier; }
@Test public void parseSizeLimit_sizeWith_G_shouldReturnMegabytesSize() { ExportLogsCommand exportCmd = new ExportLogsCommand(); assertThat(exportCmd.parseFileSizeLimit("1000g")).isEqualTo(1000 * GIGABYTE); }
/** * Wrapper to enable stubbing of static method call for unit testing */ ResultCollector estimateLogSize(SizeExportLogsFunction.Args args, DistributedMember member) { return executeFunction(new SizeExportLogsFunction(), args, member); }
final InternalCacheForClientAccess mockCacheFilter = mock(InternalCacheForClientAccess.class); when(mockCache.getCacheForProcessingClientRequests()).thenReturn(mockCacheFilter); final ExportLogsCommand realCmd = new ExportLogsCommand(); ExportLogsCommand spyCmd = spy(realCmd); testResults2.addResult(member2, 60 * MEGABYTE); doReturn(mockCache).when(spyCmd).getCache(); doReturn(testMembers).when(spyCmd).getMembersIncludingLocators(null, null); doReturn(testResults1).when(spyCmd) .estimateLogSize(Matchers.any(SizeExportLogsFunction.Args.class), eq(member1)); doReturn(testResults2).when(spyCmd) .estimateLogSize(Matchers.any(SizeExportLogsFunction.Args.class), eq(member2)); doReturn(10 * MEGABYTE).when(spyCmd).getLocalDiskAvailable(); CommandResult res = (CommandResult) spyCmd.exportLogs("working dir", null, null, logLevel, onlyLogLevel, false, start, end, logsOnly, statsOnly, "125m"); assertThat(res.getStatus()).isEqualTo(Result.Status.ERROR);
InternalCache cache = (InternalCache) getCache(); try { Set<DistributedMember> targetMembers = getMembersIncludingLocators(groups, memberIds); long userSpecifiedLimit = parseFileSizeLimit(fileSizeLimit); if (userSpecifiedLimit > 0) { onlyLogLevel, logsOnly, statsOnly); List<Object> results = (List<Object>) estimateLogSize(args, server).getResult(); if (!results.isEmpty()) { if (results.get(0) instanceof Long) { if (totalEstimatedExportSize > getLocalDiskAvailable()) { return ResultBuilder.createUserErrorResult( "Estimated logs size will exceed the available disk space on the locator."); executeFunction(new ExportLogsFunction(), new ExportLogsFunction.Args(start, end, logLevel, onlyLogLevel, logsOnly, statsOnly), server).getResult();
@Test public void parseByteMultiplier_illegalUnit_shouldThrow() { assertThatThrownBy(() -> ExportLogsCommand.parseByteMultiplier("1000q")) .isInstanceOf(IllegalArgumentException.class); }
@Test public void parseSize_garbage_shouldThrow() { assertThatThrownBy(() -> ExportLogsCommand.parseSize("bizbap")) .isInstanceOf(IllegalArgumentException.class); }
final InternalCacheForClientAccess mockCacheFilter = mock(InternalCacheForClientAccess.class); when(mockCache.getCacheForProcessingClientRequests()).thenReturn(mockCacheFilter); final ExportLogsCommand realCmd = new ExportLogsCommand(); ExportLogsCommand spyCmd = spy(realCmd); testResults2.addResult(member2, 60 * MEGABYTE); doReturn(mockCache).when(spyCmd).getCache(); doReturn(testMembers).when(spyCmd).getMembersIncludingLocators(null, null); doReturn(testResults1).when(spyCmd) .estimateLogSize(Matchers.any(SizeExportLogsFunction.Args.class), eq(member1)); doReturn(testResults2).when(spyCmd) .estimateLogSize(Matchers.any(SizeExportLogsFunction.Args.class), eq(member2)); doReturn(GIGABYTE).when(spyCmd).getLocalDiskAvailable(); CommandResult res = (CommandResult) spyCmd.exportLogs("working dir", null, null, logLevel, onlyLogLevel, false, start, end, logsOnly, statsOnly, "125m"); assertThat(res.getStatus()).isEqualTo(Result.Status.ERROR);
@Test public void parseByteMultiplier_garbage_shouldThrow() { assertThatThrownBy(() -> ExportLogsCommand.parseByteMultiplier("bizbap")) .isInstanceOf(IllegalArgumentException.class); }
@Test public void parseSize_sizeWithUnit_shouldReturnSize() { assertThat(ExportLogsCommand.parseSize("1000m")).isEqualTo(1000); }
final InternalCacheForClientAccess mockCacheFilter = mock(InternalCacheForClientAccess.class); when(mockCache.getCacheForProcessingClientRequests()).thenReturn(mockCacheFilter); final ExportLogsCommand realCmd = new ExportLogsCommand(); ExportLogsCommand spyCmd = spy(realCmd); testResults1.addResult(member1, new ManagementException(sb.toString())); doReturn(mockCache).when(spyCmd).getCache(); doReturn(testMembers).when(spyCmd).getMembersIncludingLocators(null, null); doReturn(testResults1).when(spyCmd) .estimateLogSize(Matchers.any(SizeExportLogsFunction.Args.class), eq(member1)); CommandResult res = (CommandResult) spyCmd.exportLogs("working dir", null, null, logLevel, onlyLogLevel, false, start, end, logsOnly, statsOnly, "125m"); assertThat(res.getStatus()).isEqualTo(Result.Status.ERROR);
@Test public void parseSizeLimit_sizeWith_M_shouldReturnMegabytesSize() { ExportLogsCommand exportCmd = new ExportLogsCommand(); assertThat(exportCmd.parseFileSizeLimit("1000m")).isEqualTo(1000 * MEGABYTE); }
@Test public void parseByteMultiplier_sizeWith_M_shouldReturnUnit() { assertThat(ExportLogsCommand.parseByteMultiplier("1000M")).isEqualTo(MEGABYTE); }
@Test public void parseSize_sizeWithoutUnit_shouldReturnSize() { assertThat(ExportLogsCommand.parseSize("1000")).isEqualTo(1000); }
@Test public void parseSizeLimit_sizeWith_K_shouldReturnKilobytesSize() { ExportLogsCommand exportCmd = new ExportLogsCommand(); assertThat(exportCmd.parseFileSizeLimit("1000k")).isEqualTo(1000 * KILOBYTE); }
@Test public void parseByteMultiplier_sizeWith_G_shouldReturnUnit() { assertThat(ExportLogsCommand.parseByteMultiplier("1000G")).isEqualTo(GIGABYTE); }
@Test public void parseSizeLimit_sizeWith_T_shouldReturnMegabytesSize() { ExportLogsCommand exportCmd = new ExportLogsCommand(); assertThat(exportCmd.parseFileSizeLimit("1000t")).isEqualTo(1000 * TERABYTE); }
@Test public void parseByteMultiplier_sizeWith_T_shouldReturnUnit() { assertThat(ExportLogsCommand.parseByteMultiplier("1000T")).isEqualTo(TERABYTE); }
@Test public void parseSizeLimit_sizeWithoutUnit_shouldReturnMegabytesSize() { ExportLogsCommand exportCmd = new ExportLogsCommand(); assertThat(exportCmd.parseFileSizeLimit("1000")).isEqualTo(1000 * MEGABYTE); }
@Test public void parseByteMultiplier_sizeWithoutUnit_shouldReturnDefaultUnit() { assertThat(ExportLogsCommand.parseByteMultiplier("1000")).isEqualTo(MEGABYTE); }