|
56 | 56 | import static org.hamcrest.Matchers.lessThanOrEqualTo; |
57 | 57 | import static org.hamcrest.Matchers.notNullValue; |
58 | 58 | import static org.junit.Assume.assumeThat; |
| 59 | +import static org.mockito.ArgumentMatchers.any; |
59 | 60 | import static org.mockito.ArgumentMatchers.anyString; |
| 61 | +import static org.mockito.ArgumentMatchers.eq; |
60 | 62 | import static org.mockito.Mockito.mock; |
61 | 63 | import static org.mockito.Mockito.never; |
62 | 64 | import static org.mockito.Mockito.reset; |
@@ -310,6 +312,120 @@ List<String> readSysFsCgroupCpuAcctCpuStat(String controlGroup) throws IOExcepti |
310 | 312 | verify(logger, never()).warn(anyString()); |
311 | 313 | } |
312 | 314 |
|
| 315 | + public void testGetProcessRssAnonNotLinuxReturnsNegativeOne() { |
| 316 | + assumeFalse("test runs on non-Linux only", Constants.LINUX); |
| 317 | + final OsProbe probe = new OsProbe() { |
| 318 | + @Override |
| 319 | + List<String> readProcSelfStatus() { |
| 320 | + throw new AssertionError("readProcSelfStatus should not be called on non-Linux"); |
| 321 | + } |
| 322 | + }; |
| 323 | + assertThat(probe.getProcessRssAnon(), equalTo(-1L)); |
| 324 | + } |
| 325 | + |
| 326 | + public void testGetProcessRssAnonReturnsBytes() { |
| 327 | + assumeTrue("test runs on Linux only", Constants.LINUX); |
| 328 | + final OsProbe probe = new OsProbe() { |
| 329 | + @Override |
| 330 | + List<String> readProcSelfStatus() { |
| 331 | + return Arrays.asList("Name:\topensearch", "VmRSS:\t 98304 kB", "RssAnon:\t 4096 kB", "RssFile:\t 2048 kB"); |
| 332 | + } |
| 333 | + }; |
| 334 | + assertThat(probe.getProcessRssAnon(), equalTo(4096L * 1024L)); |
| 335 | + } |
| 336 | + |
| 337 | + public void testGetProcessRssAnonHandlesIOException() { |
| 338 | + assumeTrue("test runs on Linux only", Constants.LINUX); |
| 339 | + final Logger logger = mock(Logger.class); |
| 340 | + final OsProbe probe = new OsProbe(logger) { |
| 341 | + @Override |
| 342 | + List<String> readProcSelfStatus() throws IOException { |
| 343 | + throw new IOException("simulated failure"); |
| 344 | + } |
| 345 | + }; |
| 346 | + assertThat(probe.getProcessRssAnon(), equalTo(-1L)); |
| 347 | + verify(logger, times(1)).warn(eq("failed to read /proc/self/status"), any(IOException.class)); |
| 348 | + } |
| 349 | + |
| 350 | + public void testReadRssAnonFromProcSelfStatusValidValue() throws IOException { |
| 351 | + final OsProbe probe = new OsProbe() { |
| 352 | + @Override |
| 353 | + List<String> readProcSelfStatus() { |
| 354 | + return Arrays.asList("Name:\topensearch", "VmPeak:\t 131072 kB", "RssAnon:\t 12345 kB", "RssShmem:\t 64 kB"); |
| 355 | + } |
| 356 | + }; |
| 357 | + assertThat(probe.readRssAnonFromProcSelfStatus(), equalTo(12345L * 1024L)); |
| 358 | + } |
| 359 | + |
| 360 | + public void testReadRssAnonFromProcSelfStatusZeroValue() throws IOException { |
| 361 | + final OsProbe probe = new OsProbe() { |
| 362 | + @Override |
| 363 | + List<String> readProcSelfStatus() { |
| 364 | + return Collections.singletonList("RssAnon:\t 0 kB"); |
| 365 | + } |
| 366 | + }; |
| 367 | + assertThat(probe.readRssAnonFromProcSelfStatus(), equalTo(0L)); |
| 368 | + } |
| 369 | + |
| 370 | + public void testReadRssAnonFromProcSelfStatusNegativeValue() throws IOException { |
| 371 | + final OsProbe probe = new OsProbe() { |
| 372 | + @Override |
| 373 | + List<String> readProcSelfStatus() { |
| 374 | + return Collections.singletonList("RssAnon:\t -1 kB"); |
| 375 | + } |
| 376 | + }; |
| 377 | + assertThat(probe.readRssAnonFromProcSelfStatus(), equalTo(-1L)); |
| 378 | + } |
| 379 | + |
| 380 | + public void testReadRssAnonFromProcSelfStatusMalformedValue() throws IOException { |
| 381 | + final Logger logger = mock(Logger.class); |
| 382 | + final OsProbe probe = new OsProbe(logger) { |
| 383 | + @Override |
| 384 | + List<String> readProcSelfStatus() { |
| 385 | + return Collections.singletonList("RssAnon:\tnotanumber kB"); |
| 386 | + } |
| 387 | + }; |
| 388 | + assertThat(probe.readRssAnonFromProcSelfStatus(), equalTo(-1L)); |
| 389 | + verify(logger, times(1)).warn(eq("malformed RssAnon value in /proc/self/status"), any(NumberFormatException.class)); |
| 390 | + } |
| 391 | + |
| 392 | + public void testReadRssAnonFromProcSelfStatusUnexpectedShape() throws IOException { |
| 393 | + final Logger logger = mock(Logger.class); |
| 394 | + final OsProbe probe = new OsProbe(logger) { |
| 395 | + @Override |
| 396 | + List<String> readProcSelfStatus() { |
| 397 | + // No whitespace after the label, so split produces a single token |
| 398 | + return Collections.singletonList("RssAnon:"); |
| 399 | + } |
| 400 | + }; |
| 401 | + assertThat(probe.readRssAnonFromProcSelfStatus(), equalTo(-1L)); |
| 402 | + verify(logger, times(1)).warn("RssAnon line has unexpected shape: [{}]", "RssAnon:"); |
| 403 | + } |
| 404 | + |
| 405 | + public void testReadRssAnonFromProcSelfStatusMissingLine() throws IOException { |
| 406 | + final Logger logger = mock(Logger.class); |
| 407 | + final OsProbe probe = new OsProbe(logger) { |
| 408 | + @Override |
| 409 | + List<String> readProcSelfStatus() { |
| 410 | + return Arrays.asList("Name:\topensearch", "VmRSS:\t 98304 kB"); |
| 411 | + } |
| 412 | + }; |
| 413 | + assertThat(probe.readRssAnonFromProcSelfStatus(), equalTo(-1L)); |
| 414 | + verify(logger, times(1)).warn("RssAnon line not found in /proc/self/status"); |
| 415 | + } |
| 416 | + |
| 417 | + public void testReadRssAnonFromProcSelfStatusEmptyFile() throws IOException { |
| 418 | + final Logger logger = mock(Logger.class); |
| 419 | + final OsProbe probe = new OsProbe(logger) { |
| 420 | + @Override |
| 421 | + List<String> readProcSelfStatus() { |
| 422 | + return Collections.emptyList(); |
| 423 | + } |
| 424 | + }; |
| 425 | + assertThat(probe.readRssAnonFromProcSelfStatus(), equalTo(-1L)); |
| 426 | + verify(logger, times(1)).warn("RssAnon line not found in /proc/self/status"); |
| 427 | + } |
| 428 | + |
313 | 429 | private static List<String> getProcSelfGroupLines(String hierarchy) { |
314 | 430 | return Arrays.asList( |
315 | 431 | "10:freezer:/", |
|
0 commit comments