VEDeviceDiscoverer { public Set<Device> getDevicesFromPath(String path) throws IOException { MutableInt counter = new MutableInt(0); return Files.walk(Paths.get(path), 1) .filter(p -> p.toFile().getName().startsWith("veslot")) .map(p -> toDevice(p, counter)) .collect(Collectors.toSet()); } VEDeviceDiscoverer(UdevUtil udevUtil); Set<Device> getDevicesFromPath(String path);  }
@Test public void testDetectSingleOnlineDevice() throws IOException { createVeSlotFile(0); createOsStateFile(0); when(mockCommandExecutor.getOutput()) .thenReturn("8:1:character special file"); when(udevUtil.getSysPath(anyInt(), anyChar())).thenReturn(testFolder); Set<Device> devices = discoverer.getDevicesFromPath(testFolder); assertEquals("Number of devices", 1, devices.size()); Device device = devices.iterator().next(); assertEquals("Device ID", 0, device.getId()); assertEquals("Major number", 8, device.getMajorNumber()); assertEquals("Minor number", 1, device.getMinorNumber()); assertEquals("Status", "ONLINE", device.getStatus()); assertTrue("Device is not healthy", device.isHealthy()); }
@Test public void testDetectMultipleOnlineDevices() throws IOException { createVeSlotFile(0); createVeSlotFile(1); createVeSlotFile(2); createOsStateFile(0); when(mockCommandExecutor.getOutput()).thenReturn( "8:1:character special file", "9:1:character special file", "a:1:character special file"); when(udevUtil.getSysPath(anyInt(), anyChar())).thenReturn(testFolder); Set<Device> devices = discoverer.getDevicesFromPath(testFolder); assertEquals("Number of devices", 3, devices.size()); List<Device> devicesList = Lists.newArrayList(devices); devicesList.sort(DEVICE_COMPARATOR); Device device0 = devicesList.get(0); assertEquals("Device ID", 0, device0.getId()); assertEquals("Major number", 8, device0.getMajorNumber()); assertEquals("Minor number", 1, device0.getMinorNumber()); assertEquals("Status", "ONLINE", device0.getStatus()); assertTrue("Device is not healthy", device0.isHealthy()); Device device1 = devicesList.get(1); assertEquals("Device ID", 1, device1.getId()); assertEquals("Major number", 9, device1.getMajorNumber()); assertEquals("Minor number", 1, device1.getMinorNumber()); assertEquals("Status", "ONLINE", device1.getStatus()); assertTrue("Device is not healthy", device1.isHealthy()); Device device2 = devicesList.get(2); assertEquals("Device ID", 2, device2.getId()); assertEquals("Major number", 10, device2.getMajorNumber()); assertEquals("Minor number", 1, device2.getMinorNumber()); assertEquals("Status", "ONLINE", device2.getStatus()); assertTrue("Device is not healthy", device2.isHealthy()); }
@Test public void testNegativeDeviceStateNumber() throws IOException { createVeSlotFile(0); createOsStateFile(-1); when(mockCommandExecutor.getOutput()) .thenReturn("8:1:character special file"); when(udevUtil.getSysPath(anyInt(), anyChar())).thenReturn(testFolder); Set<Device> devices = discoverer.getDevicesFromPath(testFolder); assertEquals("Number of devices", 1, devices.size()); Device device = devices.iterator().next(); assertEquals("Device ID", 0, device.getId()); assertEquals("Major number", 8, device.getMajorNumber()); assertEquals("Minor number", 1, device.getMinorNumber()); assertEquals("Status", "Unknown (-1)", device.getStatus()); assertFalse("Device should not be healthy", device.isHealthy()); }
@Test public void testDeviceStateNumberTooHigh() throws IOException { createVeSlotFile(0); createOsStateFile(5); when(mockCommandExecutor.getOutput()) .thenReturn("8:1:character special file"); when(udevUtil.getSysPath(anyInt(), anyChar())).thenReturn(testFolder); Set<Device> devices = discoverer.getDevicesFromPath(testFolder); assertEquals("Number of devices", 1, devices.size()); Device device = devices.iterator().next(); assertEquals("Device ID", 0, device.getId()); assertEquals("Major number", 8, device.getMajorNumber()); assertEquals("Minor number", 1, device.getMinorNumber()); assertEquals("Status", "Unknown (5)", device.getStatus()); assertFalse("Device should not be healthy", device.isHealthy()); }
@Test public void testDeviceNumberFromMajorAndMinor() throws IOException { createVeSlotFile(0); createVeSlotFile(1); createVeSlotFile(2); createOsStateFile(0); when(mockCommandExecutor.getOutput()).thenReturn( "10:1:character special file", "1d:2:character special file", "4:3c:character special file"); when(udevUtil.getSysPath(anyInt(), anyChar())).thenReturn(testFolder); Set<Device> devices = discoverer.getDevicesFromPath(testFolder); List<Device> devicesList = Lists.newArrayList(devices); devicesList.sort(DEVICE_COMPARATOR); Device device0 = devicesList.get(0); assertEquals("Major number", 16, device0.getMajorNumber()); assertEquals("Minor number", 1, device0.getMinorNumber()); Device device1 = devicesList.get(1); assertEquals("Major number", 29, device1.getMajorNumber()); assertEquals("Minor number", 2, device1.getMinorNumber()); Device device2 = devicesList.get(2); assertEquals("Major number", 4, device2.getMajorNumber()); assertEquals("Minor number", 60, device2.getMinorNumber()); }
@Test public void testNonVESlotFilesAreSkipped() throws IOException { createVeSlotFile(0); createOsStateFile(0); createFile("abcde"); createFile("vexlot"); createFile("xyzveslot"); when(mockCommandExecutor.getOutput()).thenReturn( "8:1:character special file", "9:1:character special file", "10:1:character special file", "11:1:character special file", "12:1:character special file"); when(udevUtil.getSysPath(anyInt(), anyChar())).thenReturn(testFolder); Set<Device> devices = discoverer.getDevicesFromPath(testFolder); assertEquals("Number of devices", 1, devices.size()); Device device = devices.iterator().next(); assertEquals("Device ID", 0, device.getId()); assertEquals("Major number", 8, device.getMajorNumber()); assertEquals("Minor number", 1, device.getMinorNumber()); assertEquals("Status", "ONLINE", device.getStatus()); assertTrue("Device is not healthy", device.isHealthy()); }
@Test public void testNonBlockOrCharFilesAreRejected() throws IOException { expected.expect(IllegalArgumentException.class); expected.expectMessage("File is neither a char nor block device"); createVeSlotFile(0); when(mockCommandExecutor.getOutput()).thenReturn( "0:0:regular file"); discoverer.getDevicesFromPath(testFolder); }