Mock areCgroupStatsAvailable in OsProbeTests

When acquiring cgroup stats, we check if such stats are available by
invoking a method areCgroupStatsAvailable. This method checks
availability by looking for existence of some virtual files in
/proc/self/cgroup and /sys/fs/cgroups. If these stats are not available,
the getCgroup method returns null. The OsProbeTests#testCgroupProbe did
not account for this. On some systems where tests run, the cgroup stats
might not be available yet this test method was expecting them to be (we
mock the relevant virtual file reads). This commit handles the execution
of this test on such systems by overriding the behavior of
OsProbe#areCgroupStatsAvailable. We test both the possibility of this
method returning true as well as false.
This commit is contained in:
Jason Tedor 2016-10-24 19:50:45 -04:00
parent de241f441d
commit 9a6c81c9f1
2 changed files with 21 additions and 9 deletions

View File

@ -433,7 +433,7 @@ public class OsProbe {
* {@code false}
*/
@SuppressForbidden(reason = "access /proc/self/cgroup, /sys/fs/cgroup/cpu, and /sys/fs/cgroup/cpuacct")
private boolean areCgroupStatsAvailable() {
protected boolean areCgroupStatsAvailable() {
if (!Files.exists(PathUtils.get("/proc/self/cgroup"))) {
return false;
}

View File

@ -147,6 +147,7 @@ public class OsProbeTests extends ESTestCase {
public void testCgroupProbe() {
assumeTrue("test runs on Linux only", Constants.LINUX);
final boolean areCgroupStatsAvailable = randomBoolean();
final String hierarchy = randomAsciiOfLength(16);
final OsProbe probe = new OsProbe() {
@ -193,17 +194,28 @@ public class OsProbeTests extends ESTestCase {
"throttled_time 139298645489");
}
@Override
protected boolean areCgroupStatsAvailable() {
return areCgroupStatsAvailable;
}
};
final OsStats.Cgroup cgroup = probe.osStats().getCgroup();
assertThat(cgroup.getCpuAcctControlGroup(), equalTo("/" + hierarchy));
assertThat(cgroup.getCpuAcctUsageNanos(), equalTo(364869866063112L));
assertThat(cgroup.getCpuControlGroup(), equalTo("/" + hierarchy));
assertThat(cgroup.getCpuCfsPeriodMicros(), equalTo(100000L));
assertThat(cgroup.getCpuCfsQuotaMicros(), equalTo(50000L));
assertThat(cgroup.getCpuStat().getNumberOfElapsedPeriods(), equalTo(17992L));
assertThat(cgroup.getCpuStat().getNumberOfTimesThrottled(), equalTo(1311L));
assertThat(cgroup.getCpuStat().getTimeThrottledNanos(), equalTo(139298645489L));
if (areCgroupStatsAvailable) {
assertNotNull(cgroup);
assertThat(cgroup.getCpuAcctControlGroup(), equalTo("/" + hierarchy));
assertThat(cgroup.getCpuAcctUsageNanos(), equalTo(364869866063112L));
assertThat(cgroup.getCpuControlGroup(), equalTo("/" + hierarchy));
assertThat(cgroup.getCpuCfsPeriodMicros(), equalTo(100000L));
assertThat(cgroup.getCpuCfsQuotaMicros(), equalTo(50000L));
assertThat(cgroup.getCpuStat().getNumberOfElapsedPeriods(), equalTo(17992L));
assertThat(cgroup.getCpuStat().getNumberOfTimesThrottled(), equalTo(1311L));
assertThat(cgroup.getCpuStat().getTimeThrottledNanos(), equalTo(139298645489L));
} else {
assertNull(cgroup);
}
}
}