Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding missing tests #75

Merged
merged 2 commits into from
Sep 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions wgkex/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ def mac2eui64(mac: str, prefix=None) -> str:

if not prefix:
return ":".join(re.findall(r".{4}", eui64))
else:
net = ipaddress.ip_network(prefix, strict=False)
euil = int(f"0x{eui64:16}", 16)
return f"{net[euil]}/{net.prefixlen}"
net = ipaddress.ip_network(prefix, strict=False)
euil = int(f"0x{eui64:16}", 16)
return f"{net[euil]}/{net.prefixlen}"
22 changes: 22 additions & 0 deletions wgkex/worker/app_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,28 @@ def test_main_fails_no_domain(self, connect_mock, config_mock):
with self.assertRaises(app.DomainsNotInConfig):
app.main()

@mock.patch.object(app.config, "load_config")
@mock.patch.object(app.mqtt, "connect", autospec=True)
def test_main_fails_bad_domain(self, connect_mock, config_mock):
"""Ensure we fail when domains are badly formatted."""
test_prefix = "TEST_PREFIX_"
config_mock.return_value = dict(
domains=[f"cant_split_domain"], domain_prefix=test_prefix
)
connect_mock.return_value = None
with mock.patch("app.flush_workers", return_value=None):
app.main()
connect_mock.assert_called_with()

@mock.patch("time.sleep", side_effect=InterruptedError)
@mock.patch("app.wg_flush_stale_peers")
def test_flush_workers(self, flush_mock, sleep_mock):
"""Ensure we fail when domains are badly formatted."""
flush_mock.return_value = ""
# Infinite loop in flush_workers has no exit value, so test will generate one, and test for that.
with self.assertRaises(InterruptedError):
app.flush_workers("test_domain")


if __name__ == "__main__":
unittest.main()