Skip to content

Commit

Permalink
Add test for omrsysinfo_is_running_in_container
Browse files Browse the repository at this point in the history
Add test for omrsysinfo_is_running_in_container and fix cgroup tests when
running in a cgroup v1 container.

Issue: #1281
Signed-off-by: Eric Yang <[email protected]>
  • Loading branch information
EricYangIBM committed Jun 14, 2022
1 parent cc14b3d commit 477b8c0
Showing 1 changed file with 46 additions and 3 deletions.
49 changes: 46 additions & 3 deletions fvtest/porttest/si.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2376,6 +2376,7 @@ TEST(PortSysinfoTest, sysinfo_test_os_kernel_info)
class CgroupTest : public ::testing::Test {
protected:
#if defined(LINUX)
static bool isRunningInContainer;
static bool isV1Available;
static bool isV2Available;
static std::string memCgroup;
Expand All @@ -2395,6 +2396,11 @@ class CgroupTest : public ::testing::Test {
static void
SetUpTestCase()
{
char *omr_running_in_docker = getenv("OMR_RUNNING_IN_DOCKER");
if ((NULL != omr_running_in_docker) && (0 == strcmp(omr_running_in_docker, "1"))) {
isRunningInContainer = true;
}

isV1Available = isCgroupV1Available();
isV2Available = isCgroupV2Available();
ASSERT_TRUE(isV1Available != isV2Available);
Expand Down Expand Up @@ -2426,13 +2432,25 @@ class CgroupTest : public ::testing::Test {
available |= it->second;
switch(it->second) {
case OMR_CGROUP_SUBSYSTEM_CPU:
cpuCgroup = sm[2].str();
if (isV1Available && isRunningInContainer) {
cpuCgroup = "/";
} else {
cpuCgroup = sm[2].str();
}
break;
case OMR_CGROUP_SUBSYSTEM_MEMORY:
memCgroup = sm[2].str();
if (isV1Available && isRunningInContainer) {
memCgroup = "/";
} else {
memCgroup = sm[2].str();
}
break;
case OMR_CGROUP_SUBSYSTEM_CPUSET:
cpusetCgroup = sm[2].str();
if (isV1Available && isRunningInContainer) {
cpusetCgroup = "/";
} else {
cpusetCgroup = sm[2].str();
}
break;
default:
FAIL() << "Unsupported subsystem";
Expand Down Expand Up @@ -2529,6 +2547,7 @@ class CgroupTest : public ::testing::Test {
};

#if defined(LINUX)
bool CgroupTest::isRunningInContainer = false;
bool CgroupTest::isV1Available = false;
bool CgroupTest::isV2Available = false;
std::string CgroupTest::memCgroup;
Expand Down Expand Up @@ -2920,6 +2939,30 @@ TEST_F(CgroupTest, sysinfo_get_number_CPUs_cgroup)
return;
}
#endif /* defined(LINUX) */

/**
* Test omrsysinfo_is_running_in_container.
*/
TEST_F(CgroupTest, sysinfo_is_running_in_container)
{
OMRPORT_ACCESS_FROM_OMRPORT(portTestEnv->getPortLibrary());
const char *testName = "omrsysinfo_is_running_in_container";

reportTestEntry(OMRPORTLIB, testName);

BOOLEAN runningInContainer = omrsysinfo_is_running_in_container();

#if defined(LINUX)
ASSERT_EQ(isRunningInContainer, CgroupTest::isRunningInContainer);
#else /* defined(LINUX) */
if (FALSE != runningInContainer) {
outputErrorMessage(PORTTEST_ERROR_ARGS, "omrsysinfo_is_running_in_container returned TRUE on non-Linux\n");
}
#endif /* defined(LINUX) */

reportTestExit(OMRPORTLIB, testName);
return;
}
#else /* !defined(LINUX) || (GTEST_GCC_VER_ >= 40900) */
#pragma message("Cgroup tests are disabled due to an unsupported compiler.")
#endif /* !defined(LINUX) || (GTEST_GCC_VER_ >= 40900) */
Expand Down

0 comments on commit 477b8c0

Please sign in to comment.