Skip to content

Commit

Permalink
autoinstall: Don't use snap env when invoking early and late commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris-Peterson444 committed Sep 28, 2023
1 parent 926bfeb commit a0a7bba
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 8 deletions.
7 changes: 5 additions & 2 deletions subiquity/server/controllers/cmdlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from subiquity.server.controller import NonInteractiveController
from subiquitycore.async_helpers import run_bg_task
from subiquitycore.context import with_context
from subiquitycore.utils import arun_command
from subiquitycore.utils import arun_command, orig_environ


@attr.s(auto_attribs=True)
Expand Down Expand Up @@ -100,6 +100,9 @@ def __init__(self, app):
super().__init__(app)
self.syslog_id = app.echo_syslog_id

def env(self):
return orig_environ(None)


class LateController(CmdListController):
autoinstall_key = "late-commands"
Expand All @@ -124,7 +127,7 @@ def __init__(self, app):
]

def env(self):
env = super().env()
env = orig_environ(None)
if self.app.base_model.target is not None:
env["TARGET_MOUNT_POINT"] = self.app.base_model.target
return env
Expand Down
69 changes: 69 additions & 0 deletions subiquity/server/controllers/tests/test_cmdlist.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Copyright 2023 Canonical, Ltd.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from unittest import IsolatedAsyncioTestCase, mock

from subiquity.server.controllers import cmdlist
from subiquity.server.controllers.cmdlist import (
Command,
EarlyController,
LateController,
)
from subiquitycore.tests.mocks import make_app


class TestCmdListController(IsolatedAsyncioTestCase):
def setUp(self):
self.mocked_arun = mock.patch.object(cmdlist, "arun_command")
snap_env_vars = {
"LD_LIBRARY_PATH": "/var/lib/snapd/lib/gl",
}
self.mocked_os_environ = mock.patch.dict("os.environ", snap_env_vars)


class TestEarlyController(TestCmdListController):
def setUp(self):
super().setUp()
self.controller = EarlyController(make_app())
self.controller.cmds = [Command(args="some-command", check=False)]

async def test_no_library_path_on_run(self):
with (
self.mocked_arun as faked_process_call,
self.mocked_os_environ,
):
await self.controller.run()
_, kwargs = faked_process_call.call_args

call_env = kwargs["env"]
assert "LD_LIBRARY_PATH" not in call_env


class TestLateController(TestCmdListController):
def setUp(self):
super().setUp()
self.controller = LateController(make_app())
self.controller.cmds = [Command(args="some-command", check=False)]

async def test_no_library_path_on_run(self):
with (
self.mocked_arun as faked_process_call,
self.mocked_os_environ,
):
await self.controller.run()
_, kwargs = faked_process_call.call_args

call_env = kwargs["env"]
assert "LD_LIBRARY_PATH" not in call_env
5 changes: 5 additions & 0 deletions subiquitycore/tests/mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,9 @@ def make_app(model=None):
app.opts = mock.Mock()
app.opts.dry_run = True
app.scale_factor = 1000
app.echo_syslog_id = None
app.log_syslog_id = None
app.report_start_event = mock.Mock()
app.report_finish_event = mock.Mock()

return app
15 changes: 9 additions & 6 deletions subiquitycore/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import os
import random
import subprocess
from typing import Any, Dict, List, Sequence
from typing import Any, Dict, List, Optional, Sequence

log = logging.getLogger("subiquitycore.utils")

Expand All @@ -35,21 +35,24 @@ def _clean_env(env, *, locale=True):
return env


def orig_environ(env):
def orig_environ(env: Optional[Dict[str, str]]) -> Dict[str, str]:
"""Generate an environment dict that is suitable for use for running
programs that live outside the snap."""

if env is None:
env = os.environ
ret = env.copy()
env: Dict[str, str] = os.environ

ret: Dict[str, str] = env.copy()

for key, val in env.items():
if key.endswith("_ORIG"):
key_to_restore = key[: -len("_ORIG")]
key_to_restore: str = key[: -len("_ORIG")]
if val:
ret[key_to_restore] = val
else:
del ret[key_to_restore]
del ret[key]
ret.pop("LD_LIBRARY_PATH", None)
_ = ret.pop("LD_LIBRARY_PATH", None)
return ret


Expand Down

0 comments on commit a0a7bba

Please sign in to comment.