} else {
return new double[]{1, 5, 15}; // unix behaviour
}
}
});
ByteSizeValue byteSizeValue = mock(ByteSizeValue.class);
when(byteSizeValue.bytes()).thenReturn(12345342234L);
when(byteSizeValue.toString()).thenReturn("11.4gb");
OsStats.Mem mem = mock(OsStats.Mem.class);
when(osStats.mem()).thenReturn(mem);
when(mem.actualFree()).thenReturn(byteSizeValue);
when(mem.actualUsed()).thenReturn(byteSizeValue);
when(mem.usedPercent()).thenReturn((short) 22);
when(mem.freePercent()).thenReturn((short) 78);
bind(OsService.class).toInstance(osService);
NodeService nodeService = mock(NodeService.class);
NodeStats nodeStats = mock(NodeStats.class);
when(nodeService.stats()).thenReturn(nodeStats);
when(nodeStats.getHostname()).thenReturn("localhost");
DiscoveryNode node = mock(DiscoveryNode.class);
when(nodeStats.getNode()).thenReturn(node);
when(nodeStats.getOs()).thenReturn(osStats);
when(osStats.uptime()).thenReturn(new TimeValue(3600000));
OsStats.Cpu cpu = mock(OsStats.Cpu.class);
when(osStats.cpu()).thenReturn(cpu);
when(cpu.sys()).thenReturn((short) 2);
when(cpu.user()).thenReturn((short) 4);
when(cpu.idle()).thenReturn((short) 94);
when(cpu.stolen()).thenReturn((short) 10);
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);
JvmService jvmService = mock(JvmService.class);
JvmStats jvmStats = mock(JvmStats.class);
JvmStats.Mem jvmStatsMem = mock(JvmStats.Mem.class);
ByteSizeValue heapByteSizeValueMax = mock(ByteSizeValue.class);
when(heapByteSizeValueMax.bytes()).thenReturn(123456L);
when(jvmStatsMem.getHeapMax()).thenReturn(heapByteSizeValueMax);
when(jvmStatsMem.getHeapUsed()).thenReturn(heapByteSizeValueMax);
when(jvmStats.mem()).thenReturn(jvmStatsMem);
when(jvmService.stats()).thenReturn(jvmStats);