Merge ~jugmac00/lpci:add-golang-plugin into lpci:main

Proposed by Jürgen Gmach
Status: Merged
Merged at revision: ff5b53ac785092fb4d79fbba9fd119240d90badc
Proposed branch: ~jugmac00/lpci:add-golang-plugin
Merge into: lpci:main
Diff against target: 231 lines (+178/-2)
4 files modified
NEWS.rst (+5/-0)
lpcraft/plugin/tests/test_plugins.py (+120/-1)
lpcraft/plugins/plugins.py (+52/-0)
setup.cfg (+1/-1)
Reviewer Review Type Date Requested Status
Colin Watson (community) Approve
Review via email: mp+427068@code.launchpad.net

Commit message

Add Golang plugin

To post a comment you must log in.
Revision history for this message
Jürgen Gmach (jugmac00) wrote :

I was not sure whether it makes sense to enter any invalid value to the version field, as the validator converts everything to string and in case that was not a valid golang version, the user gets a proper message in the error log.

I could add e.g. a value of `[1.18]` to the version field, which is not valid, and which would result in lpcraft trying to install `golang-[1.18]`...

Revision history for this message
Colin Watson (cjwatson) :
review: Approve
Revision history for this message
Jürgen Gmach (jugmac00) wrote :

Thanks for the review!

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
diff --git a/NEWS.rst b/NEWS.rst
index 924486c..3c3b4d6 100644
--- a/NEWS.rst
+++ b/NEWS.rst
@@ -2,6 +2,11 @@
2Version history2Version history
3===============3===============
44
50.0.21 (unreleased)
6===================
7
8- Add Golang plugin.
9
50.0.20 (2022-07-15)100.0.20 (2022-07-15)
6===================11===================
712
diff --git a/lpcraft/plugin/tests/test_plugins.py b/lpcraft/plugin/tests/test_plugins.py
index 5b28fd7..3a9a357 100644
--- a/lpcraft/plugin/tests/test_plugins.py
+++ b/lpcraft/plugin/tests/test_plugins.py
@@ -10,7 +10,7 @@ from unittest.mock import ANY, Mock, call, patch
1010
11from craft_providers.lxd import launch11from craft_providers.lxd import launch
12from fixtures import TempDir12from fixtures import TempDir
13from pydantic import StrictStr, validator13from pydantic import StrictStr, ValidationError, validator
1414
15import lpcraft.config15import lpcraft.config
16from lpcraft.commands.tests import CommandBaseTestCase16from lpcraft.commands.tests import CommandBaseTestCase
@@ -961,3 +961,122 @@ class TestPlugins(CommandBaseTestCase):
961 )961 )
962962
963 self.assertEqual(0, result.exit_code)963 self.assertEqual(0, result.exit_code)
964
965 @patch("lpcraft.commands.run.get_provider")
966 @patch("lpcraft.commands.run.get_host_architecture", return_value="amd64")
967 def test_golang_plugin(
968 self, mock_get_host_architecture, mock_get_provider
969 ):
970 launcher = Mock(spec=launch)
971 provider = makeLXDProvider(lxd_launcher=launcher)
972 mock_get_provider.return_value = provider
973 execute_run = launcher.return_value.execute_run
974 execute_run.return_value = subprocess.CompletedProcess([], 0)
975 config = dedent(
976 """
977 pipeline:
978 - build
979
980 jobs:
981 build:
982 plugin: golang
983 golang-version: "1.17"
984 series: focal
985 architectures: amd64
986 packages: [file, git]
987 run: go build -x examples/go-top.go
988 """
989 )
990 Path(".launchpad.yaml").write_text(config)
991
992 self.run_command("run")
993 self.assertEqual(
994 [
995 call(
996 ["apt", "update"],
997 cwd=PosixPath("/root/lpcraft/project"),
998 env={},
999 stdout=ANY,
1000 stderr=ANY,
1001 ),
1002 call(
1003 ["apt", "install", "-y", "golang-1.17", "file", "git"],
1004 cwd=PosixPath("/root/lpcraft/project"),
1005 env={},
1006 stdout=ANY,
1007 stderr=ANY,
1008 ),
1009 call(
1010 [
1011 "bash",
1012 "--noprofile",
1013 "--norc",
1014 "-ec",
1015 "\nexport PATH=/usr/lib/go-1.17/bin/:$PATH\ngo build -x examples/go-top.go", # noqa: E501
1016 ],
1017 cwd=PosixPath("/root/lpcraft/project"),
1018 env={},
1019 stdout=ANY,
1020 stderr=ANY,
1021 ),
1022 ],
1023 execute_run.call_args_list,
1024 )
1025
1026 @patch("lpcraft.commands.run.get_provider")
1027 @patch("lpcraft.commands.run.get_host_architecture", return_value="amd64")
1028 def test_golang_plugin_with_illegal_version(
1029 self, mock_get_host_architecture, mock_get_provider
1030 ):
1031 launcher = Mock(spec=launch)
1032 provider = makeLXDProvider(lxd_launcher=launcher)
1033 mock_get_provider.return_value = provider
1034 execute_run = launcher.return_value.execute_run
1035 execute_run.return_value = subprocess.CompletedProcess([], 0)
1036 # we do not allow floats as e.g. `1.20` is a problematic value in YAML
1037 config = dedent(
1038 """
1039 pipeline:
1040 - build
1041 jobs:
1042 build:
1043 plugin: golang
1044 golang-version: 1.18
1045 series: focal
1046 architectures: amd64
1047 packages: [file, git]
1048 run: go build -x examples/go-top.go
1049 """
1050 )
1051 Path(".launchpad.yaml").write_text(config)
1052
1053 result = self.run_command("run")
1054
1055 self.assertEqual(1, result.exit_code)
1056
1057 [error] = result.errors
1058 self.assertIn("str type expected", str(error))
1059
1060 def test_load_golang_plugin_configuration_with_invalid_version(self):
1061 config = dedent(
1062 """
1063 pipeline:
1064 - build
1065 jobs:
1066 build:
1067 plugin: golang
1068 golang-version: 1.18
1069 series: focal
1070 architectures: amd64
1071 packages: [file, git]
1072 run: go build -x examples/go-top.go
1073 """
1074 )
1075 config_path = Path(".launchpad.yaml")
1076 config_path.write_text(config)
1077
1078 self.assertRaises(
1079 ValidationError,
1080 lpcraft.config.Config.load,
1081 config_path,
1082 )
diff --git a/lpcraft/plugins/plugins.py b/lpcraft/plugins/plugins.py
index 833c461..d4464ee 100644
--- a/lpcraft/plugins/plugins.py
+++ b/lpcraft/plugins/plugins.py
@@ -8,6 +8,7 @@ __all__ = [
8 "PyProjectBuildPlugin",8 "PyProjectBuildPlugin",
9 "MiniCondaPlugin",9 "MiniCondaPlugin",
10 "CondaBuildPlugin",10 "CondaBuildPlugin",
11 "GolangPlugin",
11]12]
1213
13import textwrap14import textwrap
@@ -399,3 +400,54 @@ class CondaBuildPlugin(MiniCondaPlugin):
399 {build_command}{conda_channels}{configs} {self.build_target}400 {build_command}{conda_channels}{configs} {self.build_target}
400 {run_command}"""401 {run_command}"""
401 )402 )
403
404
405@register(name="golang")
406class GolangPlugin(BasePlugin):
407 """Installs the required `golang` version.
408
409 Usage:
410 In `.launchpad.yaml`, create the following structure. Please note that
411 the `golang-version` has to be a string.
412
413 .. code-block:: yaml
414
415 pipeline:
416 - build
417
418 jobs:
419 build:
420 plugin: golang
421 golang-version: "1.17"
422 series: focal
423 architectures: amd64
424 packages: [file, git]
425 run: go build -x examples/go-top.go
426
427 Please note that the requested golang package needs to be available
428 either in the standard repository or in a repository specified in
429 `package-repositories`.
430 """
431
432 class Config(BaseConfig):
433 golang_version: StrictStr
434
435 INTERPOLATES_RUN_COMMAND = True
436
437 def get_plugin_config(self) -> "GolangPlugin.Config":
438 return cast(GolangPlugin.Config, self.config.plugin_config)
439
440 @hookimpl # type: ignore
441 def lpcraft_install_packages(self) -> list[str]:
442 version = self.get_plugin_config().golang_version
443 return [f"golang-{version}"]
444
445 @hookimpl # type: ignore
446 def lpcraft_execute_run(self) -> str:
447 version = self.get_plugin_config().golang_version
448 run_command = self.config.run or ""
449 return textwrap.dedent(
450 f"""
451 export PATH=/usr/lib/go-{version}/bin/:$PATH
452 {run_command}"""
453 )
diff --git a/setup.cfg b/setup.cfg
index 620de17..76d03ce 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -1,6 +1,6 @@
1[metadata]1[metadata]
2name = lpcraft2name = lpcraft
3version = 0.0.203version = 0.0.21.dev0
4description = Runner for Launchpad CI jobs4description = Runner for Launchpad CI jobs
5long_description = file: README.rst5long_description = file: README.rst
6long_description_content_type = text/x-rst6long_description_content_type = text/x-rst

Subscribers

People subscribed via source and target branches