pmbootstrap-meow/test/test_proxy.py
Oliver Smith 37a7f3924d
Fix preserving proxy variables (MR 2237)
Fix "pmbootstrap chroot" and others not passing the proxy environment
variables correctly. Thanks to notfound405 for pointing this out!

Instead of only preserving proxy environment variables in
pmb.helpers.run_core, which should never be called directly, do it in
the calling functions:

* pmb.helpers.run.user
* pmb.helpers.run.root
* pmb.chroot.root
* pmb.chroot.user

This fixes that the environment variables were only really passed by
pmb.helpers.run.user, because the other functions would result in
something like:
  HTTP_PROXY=mytestproxy sudo env -i /usr/bin/sh -c '…'
This is needed to either elevate to root, or to elevate to root first
and then enter the chroot as root or user. Due to the "env -i", the
environment intentionally gets cleaned, but unintentionally also removes
the proxy environment variables that were explicitly set.

By adjusting the functions, they now run a variant of:
  sudo env -i /usr/bin/sh -c 'HTTP_PROXY=mytestproxy …'

The escaping is simplified in this example, run "pmbootstrap -v" to see
the not very readable, but proper escaping with shutil.quote().

Remove the previous test for preserving the environment variables in
pmb.helpers.run_core (as it should never be called directly), and test
instead the new behavior.

Fixes: issue 2299
Fixes: 13c4ac42 ("pmb.helpers.run_core: fix proxy env var logic")
2024-01-25 20:12:53 +00:00

52 lines
1.6 KiB
Python

# Copyright 2024 Oliver Smith
# SPDX-License-Identifier: GPL-3.0-or-later
""" Test preserving HTTP_PROXY and other proxy env vars with all pmbootstrap
run functions. """
import os
import pytest
import sys
import pmb_test # noqa
import pmb.chroot.root
import pmb.chroot.user
import pmb.helpers.run
import pmb.helpers.run_core
@pytest.fixture
def args(request):
import pmb.parse
sys.argv = ["pmbootstrap.py", "chroot"]
args = pmb.parse.arguments()
args.log = args.work + "/log_testsuite.txt"
pmb.helpers.logging.init(args)
request.addfinalizer(pmb.helpers.logging.logfd.close)
return args
def test_proxy_user(args, monkeypatch):
func = pmb.helpers.run.user
monkeypatch.setattr(os, "environ", {"HTTP_PROXY": "testproxy"})
ret = func(args, ["sh", "-c", 'echo "$HTTP_PROXY"'], output_return=True)
assert ret == "testproxy\n"
def test_proxy_root(args, monkeypatch):
func = pmb.helpers.run.root
monkeypatch.setattr(os, "environ", {"HTTP_PROXY": "testproxy"})
ret = func(args, ["sh", "-c", 'echo "$HTTP_PROXY"'], output_return=True)
assert ret == "testproxy\n"
def test_proxy_chroot_user(args, monkeypatch):
func = pmb.chroot.user
monkeypatch.setattr(os, "environ", {"HTTP_PROXY": "testproxy"})
ret = func(args, ["sh", "-c", 'echo "$HTTP_PROXY"'], output_return=True)
assert ret == "testproxy\n"
def test_proxy_chroot_root(args, monkeypatch):
func = pmb.chroot.root
monkeypatch.setattr(os, "environ", {"HTTP_PROXY": "testproxy"})
ret = func(args, ["sh", "-c", 'echo "$HTTP_PROXY"'], output_return=True)
assert ret == "testproxy\n"