From 2100a7cfdbb5b768136c5c5c01a087f8541229db Mon Sep 17 00:00:00 2001 From: nepoz Date: Thu, 8 Jul 2021 12:10:46 -0500 Subject: [PATCH] Set up initial testing framework for network module --- bumblebee_status/modules/contrib/network.py | 2 +- tests/modules/contrib/test_network.py | 65 +++++++++++++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 tests/modules/contrib/test_network.py diff --git a/bumblebee_status/modules/contrib/network.py b/bumblebee_status/modules/contrib/network.py index d90a21b..26c7889 100644 --- a/bumblebee_status/modules/contrib/network.py +++ b/bumblebee_status/modules/contrib/network.py @@ -33,7 +33,7 @@ class Module(core.module.Module): try: socket.create_connection(("1.1.1.1", 53)) self.__is_connected = True - except OSError: + except: self.__is_connected = False # Attempt to extract a valid network interface device diff --git a/tests/modules/contrib/test_network.py b/tests/modules/contrib/test_network.py new file mode 100644 index 0000000..e171d69 --- /dev/null +++ b/tests/modules/contrib/test_network.py @@ -0,0 +1,65 @@ +import pytest +from unittest import TestCase, mock + +import core.config +import core.widget +import modules.contrib.network + +def build_module(): + config = core.config.Config([]) + return modules.contrib.network.Module(config=config, theme=None) + +def wireless_default(): + return { + "default": { + 1: ('10.0.1.12', 'wlan0') + } + } + +def wired_default(): + return { + 'default': { + 18: ('10.0.1.12', 'eth0') + } + } + +def exec_side_effect(*args, **kwargs): + if args[0] == "iwgetid": + return "ESSID: bumblefoo" + elif "iwconfig" in args[0]: + return "level=-30" + + return "default" + + +class TestNetworkUnit(TestCase): + def test_load_module(self): + __import__("modules.contrib.network") + + @pytest.mark.allow_hosts(['127.0.0.1']) + def test_no_internet(self): + module = build_module() + assert module.widgets()[0].full_text() == "No connection" + + @mock.patch('util.cli.execute') + @mock.patch('netifaces.gateways') + @mock.patch('netifaces.AF_INET', 1) + def test_wireless_connection(self, gateways_mock, execute_mock): + fake_ssid = "bumblefoo" + gateways_mock.return_value = wireless_default() + execute_mock.side_effect = exec_side_effect + + module = build_module() + + assert fake_ssid in module.widgets()[0].full_text() + + @mock.patch('util.cli.execute') + @mock.patch('netifaces.gateways') + @mock.patch('netifaces.AF_INET', 18) + def test_wired_connection(self, gateways_mock, execute_mock): + gateways_mock.return_value = wired_default() + execute_mock.side_effect = exec_side_effect + + module = build_module() + + assert module.widgets()[0].full_text() == "Ethernet"