Skip to content

Commit

Permalink
salt,tests: Add unit test for metalk8s_network.routes
Browse files Browse the repository at this point in the history
Also fix metalk8s_checks.routes_exists test since
it was mocking network.routes module.

Refs: #3349
  • Loading branch information
alexandre-allard committed May 4, 2021
1 parent 39fec21 commit 335e63b
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
39 changes: 39 additions & 0 deletions salt/tests/unit/modules/files/test_metalk8s_network.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,42 @@ get_listening_processes:
127.0.0.1:
pid: 123
name: likely-something

routes:
# 0. Default route
- ip_route_output: |-
default via 10.200.0.1 dev eth0
result:
- &default_route
addr_family: inet
destination: 0.0.0.0
flags: UG
gateway: 10.200.0.1
interface: eth0
netmask: 0.0.0.0
# 1. A simple route
- ip_route_output: |-
10.200.0.0/16 dev eth0 proto kernel scope link src 10.200.2.41
result:
- &simple_route
addr_family: inet
destination: 10.200.0.0
flags: U
gateway: 0.0.0.0
interface: eth0
netmask: 255.255.0.0
# 2. A blackhole route
- ip_route_output: |-
blackhole 10.233.162.0/26 proto bird
result: []
# 3. Multiple routes
- ip_route_output: |-
default via 10.200.0.1 dev eth0
10.200.0.0/16 dev eth0 proto kernel scope link src 10.200.2.41
blackhole 10.233.162.0/26 proto bird
result:
- *default_route
- *simple_route
# 4. No routes
- ip_route_output: ''
result: []
2 changes: 1 addition & 1 deletion salt/tests/unit/modules/test_metalk8s_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ def test_route_exists(
network_routes_mock = MagicMock(return_value=routes_ret)

patch_dict = {
"network.routes": network_routes_mock,
"metalk8s_network.routes": network_routes_mock,
}

with patch.dict(metalk8s_checks.__salt__, patch_dict):
Expand Down
26 changes: 26 additions & 0 deletions salt/tests/unit/modules/test_metalk8s_network.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import ipaddress
import os.path
from unittest import TestCase
from unittest.mock import MagicMock, patch
Expand Down Expand Up @@ -266,3 +267,28 @@ def test_get_listening_processes(
"psutil.Process", process_mock
):
self.assertEqual(metalk8s_network.get_listening_processes(), result)

@utils.parameterized_from_cases(YAML_TESTS_CASES["routes"])
def test_routes(self, ip_route_output, result):
"""
Tests the return of `routes` function
"""

def _mock_convert_cidr(cidr):
ret = {"network": None, "netmask": None, "broadcast": None}
network_info = ipaddress.ip_network(cidr, strict=False)
ret["network"] = str(network_info.network_address)
ret["netmask"] = str(network_info.netmask)
ret["broadcast"] = str(network_info.broadcast_address)
return ret

mock_convert_cidr = MagicMock(side_effect=_mock_convert_cidr)
mock_ip_cmd = MagicMock(return_value=ip_route_output)
with patch.dict(
metalk8s_network.__salt__,
{"cmd.run": mock_ip_cmd, "network.convert_cidr": mock_convert_cidr},
):
routes = metalk8s_network.routes()
print(routes)
self.assertEqual(routes, result)
mock_ip_cmd.assert_called_once_with("ip -4 route show table main")

0 comments on commit 335e63b

Please sign in to comment.