From 2c8dafec701d80ddd9a3d1855a14a8eef0c44790 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thayn=C3=A3=20Moretti?= Date: Sat, 29 Aug 2020 20:17:39 -0300 Subject: [PATCH 1/3] Add Network Traffic module tests --- tests/modules/contrib/test_network_traffic.py | 54 +++++++++++++++++-- 1 file changed, 51 insertions(+), 3 deletions(-) diff --git a/tests/modules/contrib/test_network_traffic.py b/tests/modules/contrib/test_network_traffic.py index 0dd52a9..256aac1 100644 --- a/tests/modules/contrib/test_network_traffic.py +++ b/tests/modules/contrib/test_network_traffic.py @@ -1,9 +1,57 @@ import pytest +from unittest import TestCase, mock + +import core.config +import core.widget +import modules.contrib.network_traffic + +from types import SimpleNamespace pytest.importorskip("psutil") - pytest.importorskip("netifaces") -def test_load_module(): - __import__("modules.contrib.network_traffic") +def io_counters_mock(recv, sent): + return { + 'lo': SimpleNamespace( + bytes_sent = sent, + bytes_recv = recv + ) + } + +def gateways_response(): + return { + 'default': { + 1: ('10.0.0.10', 'lo') + } + } + +def build_module(): + return modules.contrib.network_traffic.Module(config=core.config.Config([]), theme=None) + +class TestNetworkTrafficUnit(TestCase): + def test_load_module(self): + __import__("modules.contrib.network_traffic") + + @mock.patch('psutil.net_io_counters') + @mock.patch('netifaces.gateways') + @mock.patch('netifaces.AF_INET', 1) + def test_update_rates(self, gateways_mock, net_io_counters_mock): + net_io_counters_mock.return_value = io_counters_mock(0, 0) + gateways_mock.return_value = gateways_response() + + module = build_module() + + net_io_counters_mock.return_value = io_counters_mock(2842135, 1932215) + module.update() + + assert module.widgets()[1].full_text() == '1.84MiB/s' + assert module.widgets()[0].full_text() == '2.71MiB/s' + + def test_initial_download_rate(self): + module = build_module() + assert module.widgets()[0].full_text() == '0.00B/s' + + def test_initial_upload_rate(self): + module = build_module() + assert module.widgets()[1].full_text() == '0.00B/s' From dff187252a44a6d44cbae4a26e3aec26ef51f800 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thayn=C3=A3=20Moretti?= Date: Sat, 29 Aug 2020 21:09:59 -0300 Subject: [PATCH 2/3] Fix RECV/SENT start values --- bumblebee_status/modules/contrib/network_traffic.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bumblebee_status/modules/contrib/network_traffic.py b/bumblebee_status/modules/contrib/network_traffic.py index bd1a9b6..f9c8c98 100644 --- a/bumblebee_status/modules/contrib/network_traffic.py +++ b/bumblebee_status/modules/contrib/network_traffic.py @@ -38,8 +38,8 @@ class Module(core.module.Module): try: self._bandwidth = BandwidthInfo() - self._rate_recv = "?" - self._rate_sent = "?" + self._rate_recv = 0 + self._rate_sent = 0 self._bytes_recv = self._bandwidth.bytes_recv() self._bytes_sent = self._bandwidth.bytes_sent() except Exception: From 22ddcf42bddca59f2028ed61665594eafadad746 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thayn=C3=A3=20Moretti?= Date: Sat, 29 Aug 2020 21:49:41 -0300 Subject: [PATCH 3/3] Improve tests --- tests/modules/contrib/test_network_traffic.py | 60 +++++++++++++++---- 1 file changed, 47 insertions(+), 13 deletions(-) diff --git a/tests/modules/contrib/test_network_traffic.py b/tests/modules/contrib/test_network_traffic.py index 256aac1..1abae72 100644 --- a/tests/modules/contrib/test_network_traffic.py +++ b/tests/modules/contrib/test_network_traffic.py @@ -26,12 +26,48 @@ def gateways_response(): } def build_module(): - return modules.contrib.network_traffic.Module(config=core.config.Config([]), theme=None) + config = core.config.Config([]) + return modules.contrib.network_traffic.Module(config=config, theme=None) + +def download_widget(module): + return module.widgets()[0] + +def upload_widget(module): + return module.widgets()[1] + +def mb_to_bytes(value): + return value*1024**2 class TestNetworkTrafficUnit(TestCase): def test_load_module(self): __import__("modules.contrib.network_traffic") + def test_initial_download_rate(self): + module = build_module() + assert download_widget(module).full_text() == '0.00B/s' + + def test_initial_upload_rate(self): + module = build_module() + assert upload_widget(module).full_text() == '0.00B/s' + + @mock.patch('netifaces.gateways') + def test_invalid_gateways(self, gateways_mock): + gateways_mock.return_value = { 'invalid': 'gateways' } + + module = build_module() + + assert download_widget(module).full_text() == '0.00B/s' + assert upload_widget(module).full_text() == '0.00B/s' + + @mock.patch('psutil.net_io_counters') + def test_invalid_io_counters(self, net_io_counters_mock): + net_io_counters_mock.return_value = { 'invalid': 'io_counters' } + + module = build_module() + + assert download_widget(module).full_text() == '0.00B/s' + assert upload_widget(module).full_text() == '0.00B/s' + @mock.patch('psutil.net_io_counters') @mock.patch('netifaces.gateways') @mock.patch('netifaces.AF_INET', 1) @@ -41,17 +77,15 @@ class TestNetworkTrafficUnit(TestCase): module = build_module() - net_io_counters_mock.return_value = io_counters_mock(2842135, 1932215) + assert download_widget(module).full_text() == '0.00B/s' + assert upload_widget(module).full_text() == '0.00B/s' + + net_io_counters_mock.return_value = io_counters_mock( + mb_to_bytes(30), + mb_to_bytes(0.5) + ) + module.update() - assert module.widgets()[1].full_text() == '1.84MiB/s' - assert module.widgets()[0].full_text() == '2.71MiB/s' - - def test_initial_download_rate(self): - module = build_module() - assert module.widgets()[0].full_text() == '0.00B/s' - - def test_initial_upload_rate(self): - module = build_module() - assert module.widgets()[1].full_text() == '0.00B/s' - + assert download_widget(module).full_text() == '30.00MiB/s' + assert upload_widget(module).full_text() == '512.00KiB/s'