Make platform specific assumptions in OS & Process probes tests

This commit is contained in:
Tanguy Leroux 2015-08-17 12:22:24 +02:00
parent e424701819
commit 8e052f0da2
3 changed files with 44 additions and 12 deletions

View File

@ -45,18 +45,34 @@ public class OsProbeTests extends ESTestCase {
OsStats stats = probe.osStats();
assertNotNull(stats);
assertThat(stats.getTimestamp(), greaterThan(0L));
assertThat(stats.getLoadAverage(), anyOf(equalTo((double) -1), greaterThanOrEqualTo((double) 0)));
if (Constants.WINDOWS) {
// Load average is always -1 on Windows platforms
assertThat(stats.getLoadAverage(), equalTo((double) -1));
} else {
// Load average can be negative if not available or not computed yet, otherwise it should be >= 0
assertThat(stats.getLoadAverage(), anyOf(lessThan((double) 0), greaterThanOrEqualTo((double) 0)));
}
assertNotNull(stats.getMem());
assertThat(stats.getMem().getTotal().bytes(), anyOf(equalTo(-1L), greaterThan(0L)));
assertThat(stats.getMem().getFree().bytes(), anyOf(equalTo(-1L), greaterThan(0L)));
assertThat(stats.getMem().getTotal().bytes(), greaterThan(0L));
assertThat(stats.getMem().getFree().bytes(), greaterThan(0L));
assertThat(stats.getMem().getFreePercent(), allOf(greaterThanOrEqualTo((short) 0), lessThanOrEqualTo((short) 100)));
assertThat(stats.getMem().getUsed().bytes(), anyOf(equalTo(-1L), greaterThanOrEqualTo(0L)));
assertThat(stats.getMem().getUsed().bytes(), greaterThan(0L));
assertThat(stats.getMem().getUsedPercent(), allOf(greaterThanOrEqualTo((short) 0), lessThanOrEqualTo((short) 100)));
assertNotNull(stats.getSwap());
assertThat(stats.getSwap().getTotal().bytes(), anyOf(equalTo(-1L), greaterThanOrEqualTo(0L)));
assertThat(stats.getSwap().getFree().bytes(), anyOf(equalTo(-1L), greaterThanOrEqualTo(0L)));
assertThat(stats.getSwap().getUsed().bytes(), anyOf(equalTo(-1L), greaterThanOrEqualTo(0L)));
assertNotNull(stats.getSwap().getTotal());
long total = stats.getSwap().getTotal().bytes();
if (total > 0) {
assertThat(stats.getSwap().getTotal().bytes(), greaterThan(0L));
assertThat(stats.getSwap().getFree().bytes(), greaterThan(0L));
assertThat(stats.getSwap().getUsed().bytes(), greaterThanOrEqualTo(0L));
} else {
// On platforms with no swap
assertThat(stats.getSwap().getTotal().bytes(), equalTo(0L));
assertThat(stats.getSwap().getFree().bytes(), equalTo(0L));
assertThat(stats.getSwap().getUsed().bytes(), equalTo(0L));
}
}
}

View File

@ -19,6 +19,7 @@
package org.elasticsearch.monitor.process;
import org.apache.lucene.util.Constants;
import org.elasticsearch.bootstrap.Bootstrap;
import org.elasticsearch.test.ESTestCase;
import org.junit.Test;
@ -43,14 +44,29 @@ public class ProcessProbeTests extends ESTestCase {
public void testProcessStats() {
ProcessStats stats = probe.processStats();
assertNotNull(stats);
assertThat(stats.getTimestamp(), greaterThan(0L));
if (Constants.WINDOWS) {
// Open/Max files descriptors are not supported on Windows platforms
assertThat(stats.getOpenFileDescriptors(), equalTo(-1L));
assertThat(stats.getMaxFileDescriptors(), equalTo(-1L));
} else {
assertThat(stats.getOpenFileDescriptors(), greaterThan(0L));
assertThat(stats.getMaxFileDescriptors(), greaterThan(0L));
}
ProcessStats.Cpu cpu = stats.getCpu();
assertNotNull(cpu);
assertThat(cpu.getPercent(), greaterThanOrEqualTo((short) 0));
assertThat(cpu.total, anyOf(equalTo(-1L), greaterThan(0L)));
// CPU percent can be negative if the system recent cpu usage is not available
assertThat(cpu.getPercent(), anyOf(lessThan((short) 0), allOf(greaterThanOrEqualTo((short) 0), lessThanOrEqualTo((short) 100))));
// CPU time can return -1 if the the platform does not support this operation, let's see which platforms fail
assertThat(cpu.total, greaterThan(0L));
ProcessStats.Mem mem = stats.getMem();
assertNotNull(mem);
assertThat(mem.totalVirtual, anyOf(equalTo(-1L), greaterThan(0L)));
// Commited total virtual memory can return -1 if not supported, let's see which platforms fail
assertThat(mem.totalVirtual, greaterThan(0L));
}
}

View File

@ -126,7 +126,7 @@ the operating system:
Last time the operating system statistics have been refreshed
`os.load_average`::
System load average for the last minute
System load average for the last minute, or -1 if not supported
`os.mem.total_in_bytes`::
Total amount of physical memory in bytes
@ -170,7 +170,7 @@ the current running process:
Maximum number of file descriptors allowed on the system, or -1 if not supported
`process.cpu.percent`::
CPU usage in percent
CPU usage in percent, or -1 if not known at the time the stats are computed
`process.cpu.total_in_millis`::
CPU time (in milliseconds) used by the process on which the Java virtual machine is running, or -1 if not supported