From 140e33b13a422411a16d6bc3fc131e2f4bac5cea Mon Sep 17 00:00:00 2001 From: Alex Myers Date: Fri, 26 Jan 2024 12:32:47 -0600 Subject: [PATCH] pytest: add tests for pip and poetry installed virtual envs Also test checkout of a git tag on installation. --- .../lightningd/testplugpass/requirements.txt | 2 + .../lightningd/testplugpass/testplugpass.py | 8 +++ .../lightningd/testplugpyproj/pyproject.toml | 17 +++++ .../testplugpyproj/testplugpyproj.py | 17 +++++ .../rkls_api_lightningd_plugins.json | 9 +++ tests/test_reckless.py | 66 ++++++++++++++++++- 6 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 tests/data/recklessrepo/lightningd/testplugpyproj/pyproject.toml create mode 100755 tests/data/recklessrepo/lightningd/testplugpyproj/testplugpyproj.py diff --git a/tests/data/recklessrepo/lightningd/testplugpass/requirements.txt b/tests/data/recklessrepo/lightningd/testplugpass/requirements.txt index e69de29bb..7b19e6771 100644 --- a/tests/data/recklessrepo/lightningd/testplugpass/requirements.txt +++ b/tests/data/recklessrepo/lightningd/testplugpass/requirements.txt @@ -0,0 +1,2 @@ +pyln-client + diff --git a/tests/data/recklessrepo/lightningd/testplugpass/testplugpass.py b/tests/data/recklessrepo/lightningd/testplugpass/testplugpass.py index 444043531..435f979f2 100755 --- a/tests/data/recklessrepo/lightningd/testplugpass/testplugpass.py +++ b/tests/data/recklessrepo/lightningd/testplugpass/testplugpass.py @@ -3,6 +3,8 @@ from pyln.client import Plugin plugin = Plugin() +__version__ = 'v1' + @plugin.init() def init(options, configuration, plugin, **kwargs): @@ -14,4 +16,10 @@ def testmethod(plugin): return ("I live.") +@plugin.method("gettestplugversion") +def gettestplugversion(plugin): + "to test commit/tag checkout" + return __version__ + + plugin.run() diff --git a/tests/data/recklessrepo/lightningd/testplugpyproj/pyproject.toml b/tests/data/recklessrepo/lightningd/testplugpyproj/pyproject.toml new file mode 100644 index 000000000..738c943c0 --- /dev/null +++ b/tests/data/recklessrepo/lightningd/testplugpyproj/pyproject.toml @@ -0,0 +1,17 @@ +[project] +dependencies = ["pyln-client"] + +[tool.poetry] +name = "testplugpyproj" +version = "0.1.0" +description = "testing poetry installation of python plugins" +authors = ["Alex Myers "] + +[tool.poetry.dependencies] +# Build dependencies belong here +python = "^3.8" +pyln-client = "^23.11" + +[build-system] +requires = ["poetry-core>=1.0.0"] +build-backend = "poetry.core.masonry.api" diff --git a/tests/data/recklessrepo/lightningd/testplugpyproj/testplugpyproj.py b/tests/data/recklessrepo/lightningd/testplugpyproj/testplugpyproj.py new file mode 100755 index 000000000..444043531 --- /dev/null +++ b/tests/data/recklessrepo/lightningd/testplugpyproj/testplugpyproj.py @@ -0,0 +1,17 @@ +#!/usr/bin/env python3 +from pyln.client import Plugin + +plugin = Plugin() + + +@plugin.init() +def init(options, configuration, plugin, **kwargs): + plugin.log("testplug initialized") + + +@plugin.method("testmethod") +def testmethod(plugin): + return ("I live.") + + +plugin.run() diff --git a/tests/data/recklessrepo/rkls_api_lightningd_plugins.json b/tests/data/recklessrepo/rkls_api_lightningd_plugins.json index 5c30a0232..3a0484f4f 100644 --- a/tests/data/recklessrepo/rkls_api_lightningd_plugins.json +++ b/tests/data/recklessrepo/rkls_api_lightningd_plugins.json @@ -16,5 +16,14 @@ "git_url": "https://api.github.com/repos/lightningd/plugins/git/trees/testplugfail", "download_url": null, "type": "dir" + }, + { + "name": "testplugpyproj", + "path": "testplugpyproj", + "url": "https://api.github.com/repos/lightningd/plugins/contents/webhook?ref=master", + "html_url": "https://github.com/lightningd/plugins/tree/master/testplugpyproj", + "git_url": "https://api.github.com/repos/lightningd/plugins/git/trees/testplugpyproj", + "download_url": null, + "type": "dir" } ] diff --git a/tests/test_reckless.py b/tests/test_reckless.py index ee1d38f02..bca922e01 100644 --- a/tests/test_reckless.py +++ b/tests/test_reckless.py @@ -61,8 +61,15 @@ def canned_github_server(directory): repo_initialization = (f'cp -r {plugins_path}/* .;' 'git add --all;' 'git commit -m "initial commit - autogenerated by test_reckless.py";') + tag_and_update = ('git tag v1;' + "sed -i 's/v1/v2/g' testplugpass/testplugpass.py;" + 'git add testplugpass/testplugpass.py;' + 'git commit -m "update to v2";' + 'git tag v2;') subprocess.check_output([repo_initialization], env=my_env, shell=True, cwd=repo_dir) + subprocess.check_output([tag_and_update], env=my_env, + shell=True, cwd=repo_dir) del my_env['HOME'] del my_env['GIT_DIR'] del my_env['GIT_WORK_TREE'] @@ -191,6 +198,25 @@ def test_install(node_factory): assert os.path.exists(plugin_path) +@unittest.skipIf(VALGRIND, "virtual environment triggers memleak detection") +def test_poetry_install(node_factory): + """test search, git clone, and installation to folder.""" + n = get_reckless_node(node_factory) + r = reckless([f"--network={NETWORK}", "-v", "install", "testplugpyproj"], dir=n.lightning_dir) + assert r.returncode == 0 + assert 'dependencies installed successfully' in r.stdout + assert 'plugin installed:' in r.stdout + assert 'testplugpyproj enabled' in r.stdout + check_stderr(r.stderr) + plugin_path = Path(n.lightning_dir) / 'reckless/testplugpyproj' + print(plugin_path) + assert os.path.exists(plugin_path) + n.start() + print(n.rpc.testmethod()) + assert n.daemon.is_in_log(r'plugin-manager: started\([0-9].*\) /tmp/ltests-[a-z0-9_].*/test_poetry_install_1/lightning-1/reckless/testplugpyproj/testplugpyproj.py') + assert n.rpc.testmethod() == 'I live.' + + @unittest.skipIf(VALGRIND, "virtual environment triggers memleak detection") def test_local_dir_install(node_factory): """Test search and install from local directory source.""" @@ -236,4 +262,42 @@ def test_disable_enable(node_factory): 'active': True, 'dynamic': True} time.sleep(1) print(n.rpc.plugin_list()['plugins']) - assert(test_plugin in n.rpc.plugin_list()['plugins']) + assert test_plugin in n.rpc.plugin_list()['plugins'] + + +@unittest.skipIf(VALGRIND, "virtual environment triggers memleak detection") +def test_tag_install(node_factory): + "install a plugin from a specific commit hash or tag" + node = get_reckless_node(node_factory) + node.start() + r = reckless([f"--network={NETWORK}", "-v", "install", "testPlugPass"], + dir=node.lightning_dir) + assert r.returncode == 0 + metadata = node.lightning_dir / "reckless/testplugpass/.metadata" + with open(metadata, "r") as md: + header = '' + for line in md.readlines(): + line = line.strip() + if header == 'requested commit': + assert line == 'None' + header = line + # should install v2 (latest) without specifying + version = node.rpc.gettestplugversion() + assert version == 'v2' + r = reckless([f"--network={NETWORK}", "-v", "uninstall", "testplugpass"], + dir=node.lightning_dir) + r = reckless([f"--network={NETWORK}", "-v", "install", "testplugpass@v1"], + dir=node.lightning_dir) + assert r.returncode == 0 + # v1 should now be checked out. + version = node.rpc.gettestplugversion() + assert version == 'v1' + installed_path = Path(node.lightning_dir) / 'reckless/testplugpass' + assert installed_path.is_dir() + with open(metadata, "r") as md: + header = '' + for line in md.readlines(): + line = line.strip() + if header == 'requested commit': + assert line == 'v1' + header = line