From 9a6c81c9f182adfe24b051fe2e50e51d5a1c6c14 Mon Sep 17 00:00:00 2001 From: Jason Tedor Date: Mon, 24 Oct 2016 19:50:45 -0400 Subject: [PATCH] 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. --- .../org/elasticsearch/monitor/os/OsProbe.java | 2 +- .../monitor/os/OsProbeTests.java | 28 +++++++++++++------ 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/monitor/os/OsProbe.java b/core/src/main/java/org/elasticsearch/monitor/os/OsProbe.java index e893c1a3ecb..6d264afb6c7 100644 --- a/core/src/main/java/org/elasticsearch/monitor/os/OsProbe.java +++ b/core/src/main/java/org/elasticsearch/monitor/os/OsProbe.java @@ -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; } diff --git a/core/src/test/java/org/elasticsearch/monitor/os/OsProbeTests.java b/core/src/test/java/org/elasticsearch/monitor/os/OsProbeTests.java index f477ef3214f..9bd4ab3b90f 100644 --- a/core/src/test/java/org/elasticsearch/monitor/os/OsProbeTests.java +++ b/core/src/test/java/org/elasticsearch/monitor/os/OsProbeTests.java @@ -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); + } } }