From 3a42195bba1a7db5a9927043b2022bf486da02e0 Mon Sep 17 00:00:00 2001 From: Reckless_Satoshi Date: Tue, 14 Nov 2023 01:42:04 +0000 Subject: [PATCH] Fix tests failures when LND node is not used --- .github/workflows/django-test.yml | 17 ++++++++--------- api/lightning/cln.py | 2 +- api/lightning/lnd.py | 4 ++-- api/tests/test_utils.py | 17 +++++++++++------ api/utils.py | 26 ++++++++++++++++---------- tests/node_utils.py | 3 ++- 6 files changed, 40 insertions(+), 29 deletions(-) diff --git a/.github/workflows/django-test.yml b/.github/workflows/django-test.yml index 48171b00..588ff539 100644 --- a/.github/workflows/django-test.yml +++ b/.github/workflows/django-test.yml @@ -18,26 +18,27 @@ jobs: test: runs-on: ubuntu-latest strategy: - max-parallel: 2 + max-parallel: 4 matrix: python-tag: ['3.11.6-slim-bookworm', '3.12-slim-bookworm'] - lnd-version: ["v0.17.0-beta"] # , "v0.17.0-beta.rc1"] - cln-version: ["v23.08.1"] - ln-vendor: ["LND", "CLN"] + lnd-version: ['v0.17.0-beta'] # , 'v0.17.0-beta.rc1'] + cln-version: ['v23.08.1'] + ln-vendor: ['LND', 'CLN'] steps: - name: 'Checkout' uses: actions/checkout@v4 - - name: Update Python version in Dockerfile + - name: Patch Dockerfile and .env-sample run: | sed -i "1s/FROM python:.*/FROM python:${{ matrix.python-tag }}/" Dockerfile sed -i '/RUN pip install --no-cache-dir -r requirements.txt/a COPY requirements_dev.txt .\nRUN pip install --no-cache-dir -r requirements_dev.txt' Dockerfile + sed -i "s/^LNVENDOR=.*/LNVENDOR='${{ matrix.ln-vendor }}'/" .env-sample - uses: satackey/action-docker-layer-caching@v0.0.11 continue-on-error: true with: - key: coordinator-docker-cache-${{ hashFiles('./Dockerfile') }} + key: coordinator-docker-cache-${{ hashFiles('Dockerfile', 'requirements.txt', 'requirements_dev.txt') }} restore-keys: | coordinator-docker-cache- @@ -46,13 +47,11 @@ jobs: with: compose-file: "./docker-tests.yml" down-flags: "--volumes" - # Ideally we run only coordinator-${{ matrix.ln-vendor }} , at the moment some tests fail if LND is not around. services: | bitcoind postgres redis - coordinator-CLN - coordinator-LND + coordinator-${{ matrix.ln-vendor }} robot-LND coordinator env: diff --git a/api/lightning/cln.py b/api/lightning/cln.py index adb609e5..ef81f7d8 100755 --- a/api/lightning/cln.py +++ b/api/lightning/cln.py @@ -67,7 +67,7 @@ class CLNNode: return response.version except Exception as e: print(f"Cannot get CLN version: {e}") - return None + return "Not installed" @classmethod def get_info(cls): diff --git a/api/lightning/lnd.py b/api/lightning/lnd.py index 92336d9f..bf831fa9 100644 --- a/api/lightning/lnd.py +++ b/api/lightning/lnd.py @@ -87,8 +87,8 @@ class LNDNode: log("verstub.GetVersion", request, response) return "v" + response.version except Exception as e: - print(e) - return None + print(f"Cannot get CLN version: {e}") + return "Not installed" @classmethod def decode_payreq(cls, invoice): diff --git a/api/tests/test_utils.py b/api/tests/test_utils.py index 55ba5bca..e5c4e7dd 100644 --- a/api/tests/test_utils.py +++ b/api/tests/test_utils.py @@ -1,6 +1,7 @@ from unittest.mock import MagicMock, Mock, mock_open, patch import numpy as np +from decouple import config from django.test import TestCase from api.models import Order @@ -94,13 +95,17 @@ class TestUtils(TestCase): mock_response_blockchain.json.assert_called_once() mock_response_yadio.json.assert_called_once() - def test_get_lnd_version(self): - version = get_lnd_version() - self.assertTrue(isinstance(version, str)) + if config("LNVENDOR", cast=str) == "LND": - def test_get_cln_version(self): - version = get_cln_version() - self.assertTrue(isinstance(version, str)) + def test_get_lnd_version(self): + version = get_lnd_version() + self.assertTrue(isinstance(version, str)) + + elif config("LNVENDOR", cast=str) == "CLN": + + def test_get_cln_version(self): + version = get_cln_version() + self.assertTrue(isinstance(version, str)) @patch( "builtins.open", new_callable=mock_open, read_data="00000000000000000000 dev" diff --git a/api/utils.py b/api/utils.py index e9075b70..4348892b 100644 --- a/api/utils.py +++ b/api/utils.py @@ -176,12 +176,15 @@ lnd_version_cache = {} @ring.dict(lnd_version_cache, expire=3600) def get_lnd_version(): - try: - from api.lightning.lnd import LNDNode + if LNVENDOR == "LND": + try: + from api.lightning.lnd import LNDNode - return LNDNode.get_version() - except Exception: - return "No LND" + return LNDNode.get_version() + except Exception: + return "Not installed" + else: + return "Not installed" cln_version_cache = {} @@ -189,12 +192,15 @@ cln_version_cache = {} @ring.dict(cln_version_cache, expire=3600) def get_cln_version(): - try: - from api.lightning.cln import CLNNode + if LNVENDOR == "CLN": + try: + from api.lightning.cln import CLNNode - return CLNNode.get_version() - except Exception: - return "No CLN" + return CLNNode.get_version() + except Exception: + return "Not installed" + else: + return "Not installed" robosats_commit_cache = {} diff --git a/tests/node_utils.py b/tests/node_utils.py index c5db258b..5987a069 100644 --- a/tests/node_utils.py +++ b/tests/node_utils.py @@ -125,11 +125,12 @@ def connect_to_node(node_name, node_id, ip_port): headers=node["headers"], ) if response.json() == {}: + print("Peered robot node to coordinator node!") return response.json() else: if "already connected to peer" in response.json()["message"]: return response.json() - print(f"Could not connect to coordinator node: {response.json()}") + print(f"Could not peer coordinator node: {response.json()}") time.sleep(wait_step)