← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] ~lgp171188/lpcraft:fix-package-repositories-leak-to-following-jobs into lpcraft:main

 

Guruprasad has proposed merging ~lgp171188/lpcraft:fix-package-repositories-leak-to-following-jobs into lpcraft:main.

Commit message:
Fix the leakage of package repositories from a job to the next

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~lgp171188/lpcraft/+git/lpcraft/+merge/435255
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of ~lgp171188/lpcraft:fix-package-repositories-leak-to-following-jobs into lpcraft:main.
diff --git a/NEWS.rst b/NEWS.rst
index 11e8d82..2b01545 100644
--- a/NEWS.rst
+++ b/NEWS.rst
@@ -2,6 +2,11 @@
 Version history
 ===============
 
+0.0.40 (unreleased)
+===================
+
+- Fix the leakage of package repositories from a job to the next
+
 0.0.39 (2023-01-06)
 ===================
 
diff --git a/lpcraft/commands/run.py b/lpcraft/commands/run.py
index 72bc3a4..eed9ef6 100644
--- a/lpcraft/commands/run.py
+++ b/lpcraft/commands/run.py
@@ -712,7 +712,8 @@ class RunCommand(BaseCommand):
                         # we prefer package repositories via CLI more
                         # so they need to come first
                         # also see sources.list(5)
-                        package_repositories = args.package_repositories
+                        package_repositories = []
+                        package_repositories += args.package_repositories
                         for group in job.package_repositories:
                             for repository in group.sources_list_lines():
                                 package_repositories.append(repository)
diff --git a/lpcraft/commands/tests/test_run.py b/lpcraft/commands/tests/test_run.py
index df51380..aaa2c02 100644
--- a/lpcraft/commands/tests/test_run.py
+++ b/lpcraft/commands/tests/test_run.py
@@ -2807,6 +2807,93 @@ class TestRun(RunBaseTestCase):
             file_contents,
         )
 
+    @patch("lpcraft.commands.run._import_signing_keys_for_ppas")
+    @patch("lpcraft.commands.run.get_provider")
+    @patch("lpcraft.commands.run.get_host_architecture", return_value="amd64")
+    def test_per_job_package_repositories_are_isolated_to_corresponding_jobs(
+        self,
+        mock_get_host_architecture,
+        mock_get_provider,
+        mock_import_signing_keys_for_ppas,
+    ):
+        existing_repositories = [
+            "deb http://archive.ubuntu.com/ubuntu/ focal main restricted",
+            "deb-src http://archive.ubuntu.com/ubuntu/ focal main restricted",
+        ]
+
+        def fake_pull_file(source: Path, destination: Path) -> None:
+            destination.write_text("\n".join(existing_repositories))
+
+        launcher = Mock(spec=launch)
+        provider = makeLXDProvider(lxd_launcher=launcher)
+        mock_get_provider.return_value = provider
+        execute_run = launcher.return_value.execute_run
+        execute_run.return_value = subprocess.CompletedProcess([], 0)
+        launcher.return_value.pull_file.side_effect = fake_pull_file
+
+        config = dedent(
+            """
+            pipeline:
+                - job1
+                - job2
+            jobs:
+                job1:
+                    series: focal
+                    architectures: amd64
+                    run: ls -la
+                    packages: [example-package]
+                    package-repositories:
+                        - type: apt
+                          ppa: example/ppa
+                          formats: [deb]
+                          suites: [focal]
+                job2:
+                    series: focal
+                    architectures: amd64
+                    run: ls -la
+                    packages: [example-package]
+                    package-repositories:
+                        - type: apt
+                          url: https://canonical.example.org/repo
+                          components: [main]
+                          formats: [deb]
+                          suites: [focal]
+            """
+        )
+        Path(".launchpad.yaml").write_text(config)
+        result = self.run_command("run")
+        mock_info = launcher.return_value.push_file_io.call_args_list
+
+        self.assertEqual(0, result.exit_code)
+        self.assertEqual(
+            Path("/etc/apt/sources.list"), mock_info[0][1]["destination"]
+        )
+        self.assertEqual(
+            Path("/etc/apt/sources.list"), mock_info[1][1]["destination"]
+        )
+        job1_sources_list = mock_info[0][1]["content"].read().decode()
+        self.assertEqual(
+            dedent(
+                """\
+            deb http://archive.ubuntu.com/ubuntu/ focal main restricted
+            deb-src http://archive.ubuntu.com/ubuntu/ focal main restricted
+            deb https://ppa.launchpadcontent.net/example/ppa/ubuntu focal main
+            """  # noqa: E501
+            ),
+            job1_sources_list,
+        )
+        job2_sources_list = mock_info[1][1]["content"].read().decode()
+        self.assertEqual(
+            dedent(
+                """\
+            deb http://archive.ubuntu.com/ubuntu/ focal main restricted
+            deb-src http://archive.ubuntu.com/ubuntu/ focal main restricted
+            deb https://canonical.example.org/repo focal main
+            """  # noqa: E501
+            ),
+            job2_sources_list,
+        )
+
     @patch("lpcraft.env.get_managed_environment_project_path")
     @patch("lpcraft.commands.run.get_provider")
     @patch("lpcraft.commands.run.get_host_architecture", return_value="amd64")
diff --git a/setup.cfg b/setup.cfg
index 008537b..d115049 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -1,6 +1,6 @@
 [metadata]
 name = lpcraft
-version = 0.0.39
+version = 0.0.40.dev0
 description = Runner for Launchpad CI jobs
 long_description = file: README.rst
 long_description_content_type = text/x-rst