DockerBasedSystemRamInfoProvider implements SystemRamInfoProvider { @Override public SystemRamInfo getSystemRamInfo() throws ServerException { SystemInfo systemInfo; try { systemInfo = dockerConnector.getSystemInfo(); } catch (IOException e) { LOG.error("Failed to retrieve system information from docker.", e); throw new ServerException(SYSTEM_RAM_INFO_ERROR, e); } String[][] driverStatus = systemInfo.getDriverStatus(); String[][] systemStatus = systemInfo.getSystemStatus(); String[][] statusOutput = driverStatus == null ? systemStatus : driverStatus; if (statusOutput == null) { LOG.error( "Empty system information was received from docker. Whole system info from docker {}", systemInfo); throw new ServerException(SYSTEM_RAM_INFO_ERROR); } List<String> allNodesRamUsage = new ArrayList<>(); for (String[] systemInfoEntry : statusOutput) { if (systemInfoEntry.length == 2 && systemInfoEntry[0] != null && systemInfoEntry[0].endsWith("Reserved Memory")) { allNodesRamUsage.add(systemInfoEntry[1]); } } if (allNodesRamUsage.isEmpty()) { LOG.error( "System RAM values was not found in docker system info response. All system values from docker: {}", deepToString(statusOutput)); throw new ServerException(SYSTEM_RAM_INFO_ERROR); } long systemRamUsed = 0; long systemRamTotal = 0; for (String nodeRamUsage : allNodesRamUsage) { String[] ramValues = nodeRamUsage.split(" / "); if (ramValues.length != 2) { LOG.error( "A problem occurred while parsing system information from docker. " + "Expected: <used RAM size> / <total RAM size> but got: " + nodeRamUsage); throw new ServerException(SYSTEM_RAM_INFO_ERROR); } systemRamUsed += parseSize(ramValues[0]); systemRamTotal += parseSize(ramValues[1]); } return new SystemRamInfo(systemRamUsed, systemRamTotal); } @Inject  DockerBasedSystemRamInfoProvider(DockerConnectorProvider dockerConnectorProvider); @Override SystemRamInfo getSystemRamInfo();  }
@Test public void shouldReturnRamNotExceededStatusWhenSystemRamLimitIsNotExceeded() throws Exception { when(systemInfo.getDriverStatus()) .thenReturn(new String[][] {{" └ Reserved Memory", "0 B / 3 GiB"}}); assertFalse(systemRamInfoProvider.getSystemRamInfo().isSystemRamLimitExceeded()); }
@Test public void shouldReturnRamExceededStatusWhenSystemRamLimitIsExceeded() throws Exception { when(systemInfo.getDriverStatus()) .thenReturn( new String[][] { {" └ Reserved Memory", "800 MiB / 1 GiB"}, {" └ Reserved Memory", "0.99 GiB / 1 GiB"}, {" └ Reserved Memory", "0.95 GiB / 1 GiB"} }); assertTrue(systemRamInfoProvider.getSystemRamInfo().isSystemRamLimitExceeded()); }
@Test public void shouldReturnRamValues() throws Exception { when(systemInfo.getDriverStatus()) .thenReturn(new String[][] {{" └ Reserved Memory", "1 GiB / 3 GiB"}}); SystemRamInfo systemRamInfo = systemRamInfoProvider.getSystemRamInfo(); assertEquals(systemRamInfo.getSystemRamUsed(), 1024 * 1024 * 1024); assertEquals(systemRamInfo.getSystemRamTotal(), 3L * 1024 * 1024 * 1024); }
@Test( expectedExceptions = ServerException.class, expectedExceptionsMessageRegExp = "An error occurred while getting system RAM info." ) public void shouldThrowExceptionIfFailedToRecognizeDockerSystemInfo() throws Exception { when(systemInfo.getDriverStatus()) .thenReturn(new String[][] {{"Unrecognized value", "Unrecognized value"}}); systemRamInfoProvider.getSystemRamInfo(); }
@Test( expectedExceptions = ServerException.class, expectedExceptionsMessageRegExp = "An error occurred while getting system RAM info." ) public void shouldThrowExceptionIfFailedToRecognizeDockerSystemRamValues() throws Exception { when(systemInfo.getDriverStatus()) .thenReturn(new String[][] {{" └ Reserved Memory", "Unrecognized value"}}); systemRamInfoProvider.getSystemRamInfo(); }
@Test( expectedExceptions = ServerException.class, expectedExceptionsMessageRegExp = "An error occurred while getting system RAM info." ) public void shouldThrowExceptionIfDockerSystemRamValuesArrayFromResponseIsNull() throws Exception { systemRamInfoProvider.getSystemRamInfo(); }