-
Notifications
You must be signed in to change notification settings - Fork 376
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
check for unexpected process in agent cgroups before cgroups enabled #3103
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5558a78
check for unexpected process in cgroup before enable
nagworld9 ae32cf3
agent restart
nagworld9 e837868
move the process check
nagworld9 a1dbe21
fix unit tests
nagworld9 fd13729
address comments
nagworld9 27941cc
pylint
nagworld9 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
# | ||
# The test suite verify the agent running in expected cgroups and also, checks agent tracking the cgroups for polling resource metrics. Also, it verifies the agent cpu quota is set as expected. | ||
# The test suite verify the agent running in expected cgroups and also, checks agent tracking the cgroups for polling resource metrics, | ||
# checks unexpected processes in the agent cgroups, and it verifies the agent cpu quota is set as expected. | ||
# | ||
name: "AgentCgroups" | ||
tests: | ||
- "agent_cgroups/agent_cgroups.py" | ||
- "agent_cgroups/agent_cpu_quota.py" | ||
- "agent_cgroups/agent_cgroups_process_check.py" | ||
images: "cgroups-endorsed" | ||
owns_vm: true |
77 changes: 77 additions & 0 deletions
77
tests_e2e/tests/agent_cgroups/agent_cgroups_process_check.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# Microsoft Azure Linux Agent | ||
# | ||
# Copyright 2018 Microsoft Corporation | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
from typing import List, Dict, Any | ||
|
||
from tests_e2e.tests.lib.agent_test import AgentVmTest | ||
from tests_e2e.tests.lib.agent_test_context import AgentVmTestContext | ||
from tests_e2e.tests.lib.logging import log | ||
from tests_e2e.tests.lib.virtual_machine_extension_client import VirtualMachineExtensionClient | ||
from tests_e2e.tests.lib.vm_extension_identifier import VmExtensionIds | ||
|
||
|
||
class AgentCgroupsProcessCheck(AgentVmTest): | ||
""" | ||
Tests the agent's ability to detect processes that do not belong to the agent's cgroup | ||
""" | ||
def __init__(self, context: AgentVmTestContext): | ||
super().__init__(context) | ||
self._ssh_client = self._context.create_ssh_client() | ||
|
||
def run(self): | ||
""" | ||
Steps: | ||
1. Verify that agent detects processes that do not belong to the agent's cgroup and disable the cgroups | ||
2. Run the extension, so that they are run in the agent's cgroup | ||
3. Restart the ext_handler process to re-initialize the cgroups setup | ||
4. Verify that agent detects extension processes and will not enable the cgroups | ||
""" | ||
|
||
log.info("=====Validating agent cgroups process check") | ||
self._run_remote_test(self._ssh_client, "agent_cgroups_process_check-unknown_process_check.py", use_sudo=True) | ||
|
||
self._install_ama_extension() | ||
|
||
log.info("=====Validating agent cgroups not enabled") | ||
self._run_remote_test(self._ssh_client, "agent_cgroups_process_check-cgroups_not_enabled.py", use_sudo=True) | ||
|
||
def _install_ama_extension(self): | ||
ama_extension = VirtualMachineExtensionClient( | ||
self._context.vm, VmExtensionIds.AzureMonitorLinuxAgent, | ||
resource_name="AMAAgent") | ||
log.info("Installing %s", ama_extension) | ||
ama_extension.enable() | ||
ama_extension.assert_instance_view() | ||
|
||
def get_ignore_error_rules(self) -> List[Dict[str, Any]]: | ||
|
||
ignore_rules = [ | ||
# This is produced by the test, so it is expected | ||
# Examples: | ||
# 2024-04-01T19:16:11.929000Z INFO MonitorHandler ExtHandler [CGW] Disabling resource usage monitoring. Reason: Check on cgroups failed: | ||
# [CGroupsException] The agent's cgroup includes unexpected processes: ['[PID: 2957] dd\x00if=/dev/zero\x00of=/dev/null\x00 '] | ||
# 2024-04-01T19:17:04.995276Z WARNING ExtHandler ExtHandler [CGroupsException] The agent's cgroup includes unexpected processes: ['[PID: 3285] /usr/bin/python3\x00/var/lib/waagent/Microsoft.Azure.Monitor.AzureM', '[PID: 3286] /usr/bin/python3\x00/var/lib/waagent/Microsoft.Azure.Monitor.AzureM'] | ||
{'message': r"The agent's cgroup includes unexpected processes"}, | ||
{'message': r"Found unexpected processes in the agent cgroup before agent enable cgroups"} | ||
] | ||
return ignore_rules | ||
|
||
|
||
if __name__ == "__main__": | ||
AgentCgroupsProcessCheck.run_from_command_line() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
tests_e2e/tests/scripts/agent_cgroups_process_check-cgroups_not_enabled.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#!/usr/bin/env pypy3 | ||
|
||
# Microsoft Azure Linux Agent | ||
# | ||
# Copyright 2018 Microsoft Corporation | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
# This script verifies agent detected unexpected processes in the agent cgroup before cgroup initialization | ||
|
||
from assertpy import fail | ||
|
||
from azurelinuxagent.common.utils import shellutil | ||
from tests_e2e.tests.lib.cgroup_helpers import check_agent_quota_disabled, check_log_message | ||
from tests_e2e.tests.lib.logging import log | ||
from tests_e2e.tests.lib.retry import retry_if_false | ||
|
||
|
||
def restart_ext_handler(): | ||
log.info("Restarting the extension handler") | ||
shellutil.run_command(["pkill", "-f", "WALinuxAgent.*run-exthandler"]) | ||
|
||
|
||
def verify_agent_cgroups_not_enabled(): | ||
""" | ||
Verifies that the agent cgroups not enabled when ama extension(unexpected) processes are found in the agent cgroup | ||
""" | ||
log.info("Verifying agent cgroups are not enabled") | ||
|
||
ama_process_found: bool = retry_if_false(lambda: check_log_message("The agent's cgroup includes unexpected processes:.+/var/lib/waagent/Microsoft.Azure.Monitor")) | ||
if not ama_process_found: | ||
fail("Agent failed to found ama extension processes in the agent cgroup") | ||
|
||
found: bool = retry_if_false(lambda: check_log_message("Found unexpected processes in the agent cgroup before agent enable cgroups")) | ||
if not found: | ||
fail("Agent failed to found unknown processes in the agent cgroup") | ||
|
||
disabled: bool = retry_if_false(check_agent_quota_disabled) | ||
if not disabled: | ||
fail("The agent failed to disable its CPUQuota when cgroups were not enabled") | ||
|
||
|
||
def main(): | ||
restart_ext_handler() | ||
verify_agent_cgroups_not_enabled() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It feels unintuitive that this function returns True when the check fails. Can we rename the function or switch the return value?