ProcessStats processStats = mock(ProcessStats.class);
when(nodeStats.getProcess()).thenReturn(processStats);
when(processStats.getOpenFileDescriptors()).thenReturn(42L);
NodeInfo nodeInfo = mock(NodeInfo.class);
when(nodeService.info()).thenReturn(nodeInfo);
ProcessInfo processInfo = mock(ProcessInfo.class);
when(nodeInfo.getProcess()).thenReturn(processInfo);
when(processInfo.getMaxFileDescriptors()).thenReturn(1000L);
Discovery discovery = mock(Discovery.class);
bind(Discovery.class).toInstance(discovery);
when(discovery.localNode()).thenReturn(node);
when(node.getId()).thenReturn("node-id-1");
when(node.getName()).thenReturn("node 1");
TransportAddress transportAddress = new InetSocketTransportAddress("localhost", 44300);
when(node.address()).thenReturn(transportAddress);
NetworkStats.Tcp tcp = mock(NetworkStats.Tcp.class, new Answer<Long>() {
@Override
public Long answer(InvocationOnMock invocation) throws Throwable {
return 42L;
}
});
NetworkStats networkStats = mock(NetworkStats.class);
when(networkStats.tcp()).thenReturn(tcp);
NetworkService networkService = mock(NetworkService.class);
when(networkService.stats()).thenReturn(networkStats);
bind(NetworkService.class).toInstance(networkService);
bind(NodeService.class).toInstance(nodeService);
NodeEnvironment nodeEnv = mock(NodeEnvironment.class);
File[] dataLocations = new File[]{ new File("/foo"), new File("/bar") };
when(nodeEnv.hasNodeFile()).then(new Answer<Boolean>() {
@Override
public Boolean answer(InvocationOnMock invocation) throws Throwable {
return isDataNode;
}
});
when(nodeEnv.nodeDataLocations()).thenReturn(dataLocations);
bind(NodeEnvironment.class).toInstance(nodeEnv);
Sigar sigar = mock(Sigar.class);
SigarService sigarService = mock(SigarService.class);
when(sigarService.sigarAvailable()).then(new Answer<Boolean>() {
@Override
public Boolean answer(InvocationOnMock invocation) throws Throwable {
return sigarAvailable;
}
});
FileSystem fsFoo = mock(FileSystem.class);
when(fsFoo.getDevName()).thenReturn("/dev/sda1");
when(fsFoo.getDirName()).thenReturn("/foo");
when(fsFoo.getType()).thenReturn(FileSystem.TYPE_LOCAL_DISK);
FileSystem fsBar = mock(FileSystem.class);
when(fsBar.getDevName()).thenReturn("/dev/sda2");
when(fsBar.getDirName()).thenReturn("/bar");
when(fsBar.getType()).thenReturn(FileSystem.TYPE_LOCAL_DISK);
FileSystem fsFiltered = mock(FileSystem.class);
when(fsFiltered.getType()).thenReturn(FileSystem.TYPE_UNKNOWN);
when(fsFiltered.getDevName()).thenReturn(("/dev/filtered"));
when(fsFiltered.getDirName()).thenReturn(("/filtered"));
FileSystemMap map = mock(FileSystemMap.class);
when(map.getMountPoint("/foo")).thenReturn(fsFoo);
when(map.getMountPoint("/bar")).thenReturn(fsBar);
when(map.getMountPoint("/filtered")).thenReturn(fsFiltered);
FileSystemUsage usage = mock(FileSystemUsage.class, new Answer<Long>() {
@Override
public Long answer(InvocationOnMock invocation) throws Throwable {
return 42L;
}
});
try {
when(sigar.getFileSystemList()).thenReturn(new FileSystem[]{fsFoo, fsBar, fsFiltered});
when(sigar.getFileSystemMap()).thenReturn(map);
when(sigar.getFileSystemUsage(anyString())).thenReturn(usage);
assertThat(sigar.getFileSystemUsage("/"), is(usage));
} catch (SigarException e) {
e.printStackTrace();
}
when(sigarService.sigar()).thenReturn(sigar);
bind(SigarService.class).toInstance(sigarService);
try {
assertThat(sigarService.sigar().getFileSystemMap(), is(map));
} catch (SigarException e) {
e.printStackTrace();
}
HttpInfo httpInfo = mock(HttpInfo.class);
when(nodeInfo.getHttp()).thenReturn(httpInfo);
BoundTransportAddress boundTransportAddress = new BoundTransportAddress(
new InetSocketTransportAddress("localhost", 44200),
new InetSocketTransportAddress("localhost", 44200)
);
when(httpInfo.address()).thenReturn(boundTransportAddress);