Skip to content

Commit

Permalink
Fix unit test failures when run on containers
Browse files Browse the repository at this point in the history
  • Loading branch information
narrieta@microsoft committed Jan 21, 2025
1 parent 3ee925c commit f639366
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 35 deletions.
2 changes: 1 addition & 1 deletion azurelinuxagent/common/osutil/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def crypt(password, salt):
IOCTL_SIOCGIFHWADDR = 0x8927
IFNAMSIZ = 16

IP_COMMAND_OUTPUT = re.compile(r'^\d+:\s+(\w+):\s+(.*)$')
IP_COMMAND_OUTPUT = re.compile(r'^\d+:\s+([\w@]+):\s+(.*)$')

STORAGE_DEVICE_PATH = '/sys/bus/vmbus/devices/'
GEN2_DEVICE_ID = 'f8b3781a-1e82-4818-a1c3-63d806ec15bb'
Expand Down
52 changes: 25 additions & 27 deletions tests/ga/test_cgroupconfigurator.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,39 +542,37 @@ def test_start_extension_command_should_disable_cgroups_and_invoke_the_command_d
@patch('time.sleep', side_effect=lambda _: mock_sleep())
def test_start_extension_command_should_capture_only_the_last_subprocess_output(self, _):
with self._get_cgroup_configurator() as configurator:
pass # release the mocks used to create the test CGroupConfigurator so that they do not conflict the mock Popen below
original_popen = subprocess.Popen

original_popen = subprocess.Popen
def mock_popen(command, *args, **kwargs):
# Inject a syntax error to the call

def mock_popen(command, *args, **kwargs):
# Inject a syntax error to the call

# Popen can accept both strings and lists, handle both here.
if isinstance(command, str):
command = command.replace('systemd-run', 'systemd-run syntax_error')
elif isinstance(command, list) and command[0] == 'systemd-run':
command = ['systemd-run', 'syntax_error'] + command[1:]
# Popen can accept both strings and lists, handle both here.
if isinstance(command, str) and command.startswith('systemd-run'):
command = 'systemd-run syntax_error'
elif isinstance(command, list) and command[0] == 'systemd-run':
command = ['systemd-run', 'syntax_error']

return original_popen(command, *args, **kwargs)
return original_popen(command, *args, **kwargs)

expected_output = "[stdout]\n{0}\n\n\n[stderr]\n"
expected_output = "[stdout]\n{0}\n\n\n[stderr]\n"

with tempfile.TemporaryFile(dir=self.tmp_dir, mode="w+b") as stdout:
with tempfile.TemporaryFile(dir=self.tmp_dir, mode="w+b") as stderr:
with patch("azurelinuxagent.ga.cgroupapi.subprocess.Popen", side_effect=mock_popen):
# We expect this call to fail because of the syntax error
process_output = configurator.start_extension_command(
extension_name="Microsoft.Compute.TestExtension-1.2.3",
command="echo 'very specific test message'",
cmd_name="test",
timeout=300,
shell=True,
cwd=self.tmp_dir,
env={}.update(os.environ),
stdout=stdout,
stderr=stderr)
with tempfile.TemporaryFile(dir=self.tmp_dir, mode="w+b") as stdout:
with tempfile.TemporaryFile(dir=self.tmp_dir, mode="w+b") as stderr:
with patch("azurelinuxagent.ga.cgroupapi.subprocess.Popen", side_effect=mock_popen):
# We expect this call to fail because of the syntax errors
process_output = configurator.start_extension_command(
extension_name="Microsoft.Compute.TestExtension-1.2.3",
command="echo 'very specific test message'",
cmd_name="test",
timeout=300,
shell=True,
cwd=self.tmp_dir,
env={}.update(os.environ),
stdout=stdout,
stderr=stderr)

self.assertEqual(expected_output.format("very specific test message"), process_output)
self.assertEqual(expected_output.format("very specific test message"), process_output)

def test_it_should_set_extension_services_cpu_memory_quota(self):
service_list = [
Expand Down
2 changes: 1 addition & 1 deletion tests/python_eol/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ RUN <<..
# Install the Python venv
#
apt-get update
apt-get -y install curl bzip2 sudo
apt-get -y install curl bzip2 sudo iproute2
groupadd waagent
useradd --shell /bin/bash --create-home -g waagent waagent
curl -sSf --retry 5 -o /tmp/python-${PYTHON_VERSION}.tar.bz2 https://dcrdata.blob.core.windows.net/python/python-${PYTHON_VERSION}.tar.bz2
Expand Down
7 changes: 1 addition & 6 deletions tests/python_eol/patch_python_venv.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,7 @@ fi
PYTHON_VERSION=$1

if [[ "${PYTHON_VERSION}" =~ ^2\.6|2\.7$ ]]; then
if [[ "${PYTHON_VERSION}" == "2.6" ]]; then
file_to_patch="/opt/python/2.6.9/lib/python2.6/httplib.py"
else
file_to_patch="/opt/python/2.7.16/lib/python2.7/httplib.py"
fi
cat >> "$file_to_patch" << ...
cat >> /opt/python/${PYTHON_VERSION}/lib/python${PYTHON_VERSION}/httplib.py << ...
# Added by WALinuxAgent dev team to work around the lack of OpenSSL 1.0 shared libraries
class HTTPSConnection(HTTPConnection):
Expand Down

0 comments on commit f639366

Please sign in to comment.