From 394c434de4cfad025f7cd50d553d4c76bf55a5f9 Mon Sep 17 00:00:00 2001 From: Albrecht Lohofener Date: Wed, 2 Oct 2024 19:44:26 +0200 Subject: [PATCH] Add more tests --- test/requirements.txt | 3 +- test/tests/openwrt_docker_test.py | 64 ++++++++++++++++++++++++++++--- 2 files changed, 61 insertions(+), 6 deletions(-) diff --git a/test/requirements.txt b/test/requirements.txt index 49ef4eb..8b16ed7 100644 --- a/test/requirements.txt +++ b/test/requirements.txt @@ -3,4 +3,5 @@ pytest pytest-docker requests[security] pyyaml -pytest-html \ No newline at end of file +pytest-html +paramiko \ No newline at end of file diff --git a/test/tests/openwrt_docker_test.py b/test/tests/openwrt_docker_test.py index 1139a7f..85bd244 100644 --- a/test/tests/openwrt_docker_test.py +++ b/test/tests/openwrt_docker_test.py @@ -9,6 +9,7 @@ import base64 import time import yaml import re +import paramiko # Workaround to make parameters global accessible @pytest.fixture(scope='session') @@ -235,8 +236,8 @@ def test_openwrt_booted(docker_services): @pytest.mark.parametrize("parameter", - [('LAN_IF','veth'),('LAN_IF','ens5')], indirect=True, - ids=['LAN_IF=veth', 'LAN_IF=ens5']) + [('LAN_IF','veth'),('LAN_IF','ens5'),('LAN_IF','')], indirect=True, + ids=['LAN_IF=veth', 'LAN_IF=ens5', 'LAN_IF=""']) def test_openwrt_lan(docker_services, parameter): wait_for_openwrt_startup(docker_services) @@ -248,6 +249,14 @@ def test_openwrt_lan(docker_services, parameter): assert True, 'ping timeout' return + case "": + response = run_openwrt_shell_command("ip", "addr") + assert response['exitcode'] == 0 + + # Look for eth0 + assert ('eth0' in response['out-data']) == True + return + case _: # Usage of real Ethernet interface e.g. 'eth0' # This test is most likely only working in a github action environment because multiple VM are necessary to test it. See the action file, please. # Try to ping LAN-VM @@ -259,8 +268,8 @@ def test_openwrt_lan(docker_services, parameter): @pytest.mark.parametrize("parameter", - [('WAN_IF','host'),('WAN_IF','none'),('WAN_IF','ens4')], indirect=True, - ids=['WAN_IF=host', 'WAN_IF=none', 'WAN_IF=ens4']) + [('WAN_IF','host'),('WAN_IF','none'),('WAN_IF','ens4'),('WAN_IF','')], indirect=True, + ids=['WAN_IF=host', 'WAN_IF=none', 'WAN_IF=ens4', 'WAN_IF=""']) def test_openwrt_wan(docker_services, parameter): wait_for_openwrt_startup(docker_services) @@ -270,6 +279,12 @@ def test_openwrt_wan(docker_services, parameter): response = run_openwrt_shell_command("nslookup", "google.com") assert response['exitcode'] == 0 return + + case '': + # For some reason ping is not working at github actions, so use nslookup to test internet connection + response = run_openwrt_shell_command("nslookup", "google.com") + assert response['exitcode'] == 0 + return case 'none': # We are looking for eth1. It should not existing @@ -445,4 +460,43 @@ def test_mdns(docker_services): polling2.poll(lambda: os.system("ping -c 1 openwrt.local >/dev/null") == 0, step=1, timeout=240) except polling2.TimeoutException: assert True, 'ping timeout' - return \ No newline at end of file + return + + +@pytest.mark.parametrize("parameter", + [('WAN_IF','host'),('WAN_IF','')], indirect=True, + ids=['WAN_IF=host', 'WAN_IF=""']) +def test_port_8000_luci(docker_services): + wait_for_openwrt_startup(docker_services) + + response = run_openwrt_shell_command("fw_wan_open_http", "") + assert response['exitcode'] == 0 + + response = requests.get("http://localhost:8000") + assert ('LuCI - Lua Configuration Interface' in response.content.decode()) == True + + +@pytest.mark.parametrize("parameter", + [('WAN_IF','host'),('WAN_IF','')], indirect=True, + ids=['WAN_IF=host', 'WAN_IF=""']) +def test_port_8022_ssh(docker_services): + wait_for_openwrt_startup(docker_services) + + response = run_openwrt_shell_command("fw_wan_open_ssh", "") + assert response['exitcode'] == 0 + + try: + try: + ssh = paramiko.client.SSHClient() + user = "root" + ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) + + ssh.connect("127.0.0.1", 8022, username=user, password=None, look_for_keys=False) + except paramiko.ssh_exception.AuthenticationException as e: # Paramiko can't handle password less login, we have to use a workaround + ssh.get_transport().auth_none(user) + stdout = ssh.exec_command("cat /etc/banner")[1].readlines() + assert ('OpenWrt' in stdout[6]) == True + return + except Exception as excinfo: + print(get_logs()) + pytest.fail(f"Unexpected exception raised: {excinfo}")