Skip to content

Commit

Permalink
fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nagworld9 committed Apr 2, 2024
1 parent e837868 commit a1dbe21
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
13 changes: 13 additions & 0 deletions tests/data/cgroups/proc_self_cgroup_azure_slice
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
12:blkio:/azure.slice/walinuxagent.service
11:cpu,cpuacct:/azure.slice/walinuxagent.service
10:devices:/azure.slice/walinuxagent.service
9:pids:/azure.slice/walinuxagent.service
8:memory:/azure.slice/walinuxagent.service
7:freezer:/
6:hugetlb:/
5:perf_event:/
4:net_cls,net_prio:/
3:cpuset:/
2:rdma:/
1:name=systemd:/azure.slice/walinuxagent.service
0::/azure.slice/walinuxagent.service
33 changes: 23 additions & 10 deletions tests/ga/test_cgroupconfigurator.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ def tearDownClass(cls):
CGroupConfigurator._instance = None
AgentTestCase.tearDownClass()

def tearDown(self):
CGroupConfigurator._instance = None
AgentTestCase.tearDown(self)

@contextlib.contextmanager
def _get_cgroup_configurator(self, initialize=True, enable=True, mock_commands=None):
CGroupConfigurator._instance = None
Expand Down Expand Up @@ -911,17 +915,26 @@ def test_agent_not_enable_cgroups_if_unexpected_process_already_in_agent_cgroups
command_mocks = [MockCommand(r"^systemctl show walinuxagent\.service --property Slice",
'''Slice=azure.slice
''')]
with self._get_cgroup_configurator(mock_commands=command_mocks) as configurator:
self.assertFalse(configurator.enabled(), "Cgroups should not be enabled")
original_read_file = fileutil.read_file

disable_events = [kwargs for _, kwargs in add_event.call_args_list if kwargs["op"] == WALAEventOperation.CGroupsDisabled]
self.assertTrue(
len(disable_events) == 1,
"Exactly 1 event should have been emitted. Got: {0}".format(disable_events))
self.assertIn(
"Found unexpected processes in the agent cgroup before agent enable cgroups",
disable_events[0]["message"],
"The error message is not correct when process check failed")
def mock_read_file(filepath, **args):
if filepath == "/proc/self/cgroup":
filepath = os.path.join(data_dir, "cgroups", "proc_self_cgroup_azure_slice")
return original_read_file(filepath, **args)

with self._get_cgroup_configurator(initialize=False, mock_commands=command_mocks) as configurator:
with patch("azurelinuxagent.common.utils.fileutil.read_file", side_effect=mock_read_file):
configurator.initialize()

self.assertFalse(configurator.enabled(), "Cgroups should not be enabled")
disable_events = [kwargs for _, kwargs in add_event.call_args_list if kwargs["op"] == WALAEventOperation.CGroupsDisabled]
self.assertTrue(
len(disable_events) == 1,
"Exactly 1 event should have been emitted. Got: {0}".format(disable_events))
self.assertIn(
"Found unexpected processes in the agent cgroup before agent enable cgroups",
disable_events[0]["message"],
"The error message is not correct when process check failed")

def test_check_agent_memory_usage_should_raise_a_cgroups_exception_when_the_limit_is_exceeded(self):
metrics = [MetricValue(MetricsCategory.MEMORY_CATEGORY, MetricsCounter.TOTAL_MEM_USAGE, AGENT_NAME_TELEMETRY, conf.get_agent_memory_quota() + 1),
Expand Down

0 comments on commit a1dbe21

Please sign in to comment.