← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] ~jugmac00/lpcraft:add-lpcraft-environment-configuration into lpcraft:main

 

Jürgen Gmach has proposed merging ~jugmac00/lpcraft:add-lpcraft-environment-configuration into lpcraft:main.

Commit message:
Jobs can now have an environment configuration

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~jugmac00/lpcraft/+git/lpcraft/+merge/412409
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of ~jugmac00/lpcraft:add-lpcraft-environment-configuration into lpcraft:main.
diff --git a/lpcraft/commands/run.py b/lpcraft/commands/run.py
index e5a5fcc..6975436 100644
--- a/lpcraft/commands/run.py
+++ b/lpcraft/commands/run.py
@@ -96,6 +96,7 @@ def _run_pipeline(args: Namespace) -> None:
                     proc = instance.execute_run(
                         cmd,
                         cwd=env.get_managed_environment_project_path(),
+                        env=job.environment,
                         stdout=stream,
                         stderr=stream,
                     )
diff --git a/lpcraft/commands/tests/test_run.py b/lpcraft/commands/tests/test_run.py
index b31b75c..6dec643 100644
--- a/lpcraft/commands/tests/test_run.py
+++ b/lpcraft/commands/tests/test_run.py
@@ -3,7 +3,7 @@
 
 import os
 import subprocess
-from pathlib import Path
+from pathlib import Path, PosixPath
 from textwrap import dedent
 from typing import Optional
 from unittest.mock import ANY, Mock, call, patch
@@ -362,6 +362,7 @@ class RunPipelineTestCase(CommandBaseTestCase):
                 call(
                     ["lpcraft", "run", "--series", "focal", "test"],
                     cwd=Path("/root/project"),
+                    env=None,
                     stdout=ANY,
                     stderr=ANY,
                 )
@@ -416,6 +417,7 @@ class RunPipelineTestCase(CommandBaseTestCase):
         execute_run.assert_called_once_with(
             ["lpcraft", "run", "--series", "focal", "test"],
             cwd=Path("/root/project"),
+            env=None,
             stdout=ANY,
             stderr=ANY,
         )
@@ -457,12 +459,14 @@ class RunPipelineTestCase(CommandBaseTestCase):
                 call(
                     ["lpcraft", "run", "--series", "focal", "test"],
                     cwd=Path("/root/project"),
+                    env=None,
                     stdout=ANY,
                     stderr=ANY,
                 ),
                 call(
                     ["lpcraft", "run", "--series", "bionic", "build-wheel"],
                     cwd=Path("/root/project"),
+                    env=None,
                     stdout=ANY,
                     stderr=ANY,
                 ),
@@ -510,21 +514,66 @@ class RunPipelineTestCase(CommandBaseTestCase):
                 call(
                     ["lpcraft", "run", "--series", "bionic", "test"],
                     cwd=Path("/root/project"),
+                    env=None,
                     stdout=ANY,
                     stderr=ANY,
                 ),
                 call(
                     ["lpcraft", "run", "--series", "focal", "test"],
                     cwd=Path("/root/project"),
+                    env=None,
                     stdout=ANY,
                     stderr=ANY,
                 ),
                 call(
                     ["lpcraft", "run", "--series", "bionic", "build-wheel"],
                     cwd=Path("/root/project"),
+                    env=None,
                     stdout=ANY,
                     stderr=ANY,
                 ),
             ],
             execute_run.call_args_list,
         )
+
+    @patch("lpcraft.commands.run.get_provider")
+    @patch("lpcraft.commands.run.get_host_architecture", return_value="amd64")
+    def test_pass_in_environment_variables(
+        self, mock_get_host_architecture, mock_get_provider
+    ):
+        launcher = Mock(spec=launch)
+        provider = self.makeLXDProvider(lxd_launcher=launcher)
+        mock_get_provider.return_value = provider
+        execute_run = launcher.return_value.execute_run
+        execute_run.return_value = subprocess.CompletedProcess([], 0)
+        config = dedent(
+            """
+            pipeline:
+                - test
+
+            jobs:
+                test:
+                    series: focal
+                    architectures: amd64
+                    run: tox
+                    environment:
+                        TOX_SKIP_ENV: '^(?!lint-)'
+            """
+        )
+        Path(".launchpad.yaml").write_text(config)
+
+        result = self.run_command("run")
+        self.assertEqual(0, result.exit_code)
+
+        self.assertEqual(
+            [
+                call(
+                    ["lpcraft", "run", "--series", "focal", "test"],
+                    cwd=PosixPath("/root/project"),
+                    env={"TOX_SKIP_ENV": "^(?!lint-)"},
+                    stdout=ANY,
+                    stderr=ANY,
+                )
+            ],
+            execute_run.call_args_list,
+        )
diff --git a/lpcraft/config.py b/lpcraft/config.py
index 0a6d0af..e4011f3 100644
--- a/lpcraft/config.py
+++ b/lpcraft/config.py
@@ -26,6 +26,7 @@ class Job(ModelConfigDefaults):
     series: StrictStr
     architectures: List[StrictStr]
     run: Optional[StrictStr]
+    environment: Optional[Dict[str, Optional[str]]]
 
     @pydantic.validator("architectures", pre=True)
     def validate_architectures(
diff --git a/lpcraft/tests/test_config.py b/lpcraft/tests/test_config.py
index 6fb98f9..16649ad 100644
--- a/lpcraft/tests/test_config.py
+++ b/lpcraft/tests/test_config.py
@@ -127,3 +127,26 @@ class TestConfig(TestCase):
                 ),
             ),
         )
+
+    def test_load_environment(self):
+        path = self.create_config(
+            dedent(
+                """
+                pipeline:
+                    - test
+
+                jobs:
+                    test:
+                        series: focal
+                        architectures: amd64
+                        environment:
+                            ACTIVE: 1
+                            SKIP: 0
+
+                """
+            )
+        )
+        config = Config.load(path)
+        self.assertEqual(
+            {"ACTIVE": "1", "SKIP": "0"}, config.jobs["test"][0].environment
+        )