← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] ~jugmac00/launchpad-buildd:add-support-for-craft-platform-for-snap-builds into launchpad-buildd:master

 

Jürgen Gmach has proposed merging ~jugmac00/launchpad-buildd:add-support-for-craft-platform-for-snap-builds into launchpad-buildd:master.

Commit message:
Add support for passing 'craft-platform' for snap builds

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~jugmac00/launchpad-buildd/+git/launchpad-buildd/+merge/484965
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of ~jugmac00/launchpad-buildd:add-support-for-craft-platform-for-snap-builds into launchpad-buildd:master.
diff --git a/debian/changelog b/debian/changelog
index 5eeb5d8..8f65098 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -1,3 +1,9 @@
+launchpad-buildd (255) UNRELEASED; urgency=medium
+
+  * Add support for craft-platforms for snap builds.
+
+ -- Jürgen Gmach <juergen.gmach@xxxxxxxxxxxxx>  Thu, 24 Apr 2025 17:48:04 +0200
+
 launchpad-buildd (254) noble; urgency=medium
   
   [ John Chittum ]
diff --git a/lpbuildd/snap.py b/lpbuildd/snap.py
index 2a3b72e..76f61c4 100644
--- a/lpbuildd/snap.py
+++ b/lpbuildd/snap.py
@@ -50,6 +50,7 @@ class SnapBuildManager(BuildManagerProxyMixin, DebianBuildManager):
         )
         self.private = extra_args.get("private", False)
         self.proxy_service = None
+        self.craft_platform = extra_args.get("craft_platform")
         self.target_architectures = extra_args.get("target_architectures")
         self.disable_proxy_after_pull = extra_args.get(
             "disable_proxy_after_pull"
@@ -120,6 +121,8 @@ class SnapBuildManager(BuildManagerProxyMixin, DebianBuildManager):
             args.extend(["--launchpad-instance", self.launchpad_instance])
         if self.launchpad_server_url:
             args.extend(["--launchpad-server-url", self.launchpad_server_url])
+        if self.craft_platform:
+            args.extend(["--craft-platform", self.craft_platform])
         args.append(self.name)
         self.runTargetSubProcess("buildsnap", *args)
 
diff --git a/lpbuildd/target/build_snap.py b/lpbuildd/target/build_snap.py
index 205a3e7..5d957ae 100644
--- a/lpbuildd/target/build_snap.py
+++ b/lpbuildd/target/build_snap.py
@@ -122,6 +122,11 @@ class BuildSnap(
             help="launchpad server url.",
         )
         parser.add_argument("name", help="name of snap to build")
+        parser.add_argument(
+            "--craft-platform",
+            type=str,
+            help="craft platform name used by the craft tool",
+        )
 
     def install(self):
         logger.info("Running install phase...")
@@ -282,6 +287,8 @@ class BuildSnap(
         env["SNAPCRAFT_BUILD_ENVIRONMENT"] = "host"
         if self.args.target_architectures:
             env["SNAPCRAFT_BUILD_FOR"] = self.args.target_architectures[0]
+        if self.args.craft_platform:
+            env["CRAFT_PLATFORM"] = self.args.craft_platform
         output_path = os.path.join("/build", self.args.name)
         self.run_build_command(["snapcraft"], cwd=output_path, env=env)
         for entry in sorted(self.backend.listdir(output_path)):
diff --git a/lpbuildd/target/tests/test_build_snap.py b/lpbuildd/target/tests/test_build_snap.py
index 08a04b1..9f56b08 100644
--- a/lpbuildd/target/tests/test_build_snap.py
+++ b/lpbuildd/target/tests/test_build_snap.py
@@ -1194,6 +1194,37 @@ class TestBuildSnap(TestCase):
             ),
         )
 
+    def test_build_with_craft_platform(self):
+        args = [
+            "buildsnap",
+            "--backend=fake",
+            "--series=xenial",
+            "--arch=amd64",
+            "1",
+            "--branch",
+            "lp:foo",
+            "--craft-platform",
+            "ubuntu-22.04-amd64",
+            "test-image",
+        ]
+        build_snap = parse_args(args=args).operation
+        build_snap.build()
+        self.assertThat(
+            build_snap.backend.run.calls,
+            MatchesListwise(
+                [
+                    RanBuildCommand(
+                        ["snapcraft"],
+                        cwd="/build/test-image",
+                        SNAPCRAFT_BUILD_ENVIRONMENT="host",
+                        SNAPCRAFT_BUILD_INFO="1",
+                        SNAPCRAFT_IMAGE_INFO="{}",
+                        CRAFT_PLATFORM="ubuntu-22.04-amd64",
+                    ),
+                ]
+            ),
+        )
+
     def test_build_private(self):
         args = [
             "buildsnap",
diff --git a/lpbuildd/tests/test_snap.py b/lpbuildd/tests/test_snap.py
index 9a4988e..d234183 100644
--- a/lpbuildd/tests/test_snap.py
+++ b/lpbuildd/tests/test_snap.py
@@ -931,3 +931,82 @@ class TestSnapBuildManagerIteration(TestCase):
             f"http://control.fetch-service.example/{session_id}/token";,
             request.url,
         )
+
+    @defer.inlineCallbacks
+    def test_iterate_craft_platform(self):
+        # Test that craft_platform is correctly passed through.
+        args = {
+            "build_request_id": 13,
+            "build_request_timestamp": "2018-04-13T14:50:02Z",
+            "build_url": "https://launchpad.example/build";,
+            "git_repository": "https://git.launchpad.dev/~example/+git/snap";,
+            "git_path": "master",
+            "craft_platform": "ubuntu-22.04-amd64",
+        }
+        expected_options = [
+            "--build-request-id",
+            "13",
+            "--build-request-timestamp",
+            "2018-04-13T14:50:02Z",
+            "--build-url",
+            "https://launchpad.example/build";,
+            "--git-repository",
+            "https://git.launchpad.dev/~example/+git/snap";,
+            "--git-path",
+            "master",
+            "--craft-platform",
+            "ubuntu-22.04-amd64",
+        ]
+        yield self.startBuild(args, expected_options)
+
+        log_path = os.path.join(self.buildmanager._cachepath, "buildlog")
+        with open(log_path, "w") as log:
+            log.write("Build log for craft platform name test.")
+
+        self.buildmanager.backend.add_file(
+            "/build/test-snap/test-snap_0_all.snap", b"I am a snap package."
+        )
+
+        # After building the package, reap processes.
+        yield self.buildmanager.iterate(0)
+        expected_command = [
+            "sharepath/bin/in-target",
+            "in-target",
+            "scan-for-processes",
+            "--backend=lxd",
+            "--series=xenial",
+            "--arch=i386",
+            self.buildid,
+        ]
+        self.assertEqual(SnapBuildState.BUILD_SNAP, self.getState())
+        self.assertEqual(expected_command, self.buildmanager.commands[-1])
+        self.assertNotEqual(
+            self.buildmanager.iterate, self.buildmanager.iterators[-1]
+        )
+        self.assertFalse(self.builder.wasCalled("buildFail"))
+        self.assertThat(
+            self.builder,
+            HasWaitingFiles.byEquality(
+                {
+                    "test-snap_0_all.snap": b"I am a snap package.",
+                }
+            ),
+        )
+
+        # Control returns to the DebianBuildManager in the UMOUNT state.
+        self.buildmanager.iterateReap(self.getState(), 0)
+        expected_command = [
+            "sharepath/bin/in-target",
+            "in-target",
+            "umount-chroot",
+            "--backend=lxd",
+            "--series=xenial",
+            "--arch=i386",
+            self.buildid,
+        ]
+        self.assertEqual(SnapBuildState.UMOUNT, self.getState())
+        self.assertEqual(expected_command, self.buildmanager.commands[-1])
+        self.assertEqual(
+            self.buildmanager.iterate, self.buildmanager.iterators[-1]
+        )
+        self.assertFalse(self.builder.wasCalled("buildFail"))