Skip to content

Commit

Permalink
Fix get/update_lab_from_api (#322)
Browse files Browse the repository at this point in the history
Fix the interfaces number while getting (updating) the network scenario from the API
  • Loading branch information
tcaiazzi committed Nov 20, 2024
1 parent ae0e65a commit 461f61d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
19 changes: 13 additions & 6 deletions src/Kathara/manager/docker/DockerManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -707,11 +707,15 @@ def get_lab_from_api(self, lab_hash: Optional[str] = None, lab_name: Optional[st
device.meta["sysctls"] = container.attrs["HostConfig"]["Sysctls"]

if "none" not in container.attrs["NetworkSettings"]["Networks"]:
for network_name, network_options in container.attrs["NetworkSettings"]["Networks"].items():
if network_name == "bridge":
device.add_meta("bridged", True)
continue

if "bridge" in container.attrs["NetworkSettings"]["Networks"].keys():
device.add_meta("bridged", True)
container.attrs["NetworkSettings"]["Networks"].pop("bridge")

networks = sorted(container.attrs["NetworkSettings"]["Networks"].items(),
key=lambda x: x[1]["DriverOpts"]["kathara.iface"])

for network_name, network_options in networks:
network = lab_networks[network_name]
link = reconstructed_lab.get_or_new_link(network.attrs["Labels"]["name"])
link.api_object = network
Expand Down Expand Up @@ -756,10 +760,13 @@ def update_lab_from_api(self, lab: Lab) -> None:
# Collision domains declared in the network scenario
static_links = set([x.link for x in device.interfaces.values()])
# Interfaces currently attached to the device
if "bridge" in container.attrs["NetworkSettings"]["Networks"].keys():
container.attrs["NetworkSettings"]["Networks"].pop("bridge")

current_ifaces = [
(lab.get_or_new_link(deployed_networks[name].attrs["Labels"]["name"]), options)
for name, options in container.attrs["NetworkSettings"]["Networks"].items()
if name != "bridge"
for name, options in sorted(container.attrs["NetworkSettings"]["Networks"].items(),
key=lambda x: x[1]["DriverOpts"]["kathara.iface"])
]

# Collision domains currently attached to the device
Expand Down
7 changes: 6 additions & 1 deletion tests/manager/docker/docker_manager_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ def docker_container(mock_container):
"kathara_user_hash_test_network": {
"Links": None,
"DriverOpts": {
'kathara.iface': '0', 'kathara.link': 'A',
"kathara.mac_addr": "00:00:00:00:00:01"
}
},
Expand Down Expand Up @@ -1371,11 +1372,14 @@ def test_update_lab_from_api_add_link(mock_get_links_api_objects, mock_get_machi
docker_container.attrs["NetworkSettings"]["Networks"] = {
"kathara_user_hash_test_network": {
"Links": None,
"DriverOpts": None
"DriverOpts": {
'kathara.iface': '0', 'kathara.link': 'A',
}
},
"kathara_user_hash_test_network_b": {
"Links": None,
"DriverOpts": {
'kathara.iface': '1', 'kathara.link': 'B',
"kathara.mac_addr": "00:00:00:00:00:02"
}
}
Expand Down Expand Up @@ -1427,6 +1431,7 @@ def test_update_lab_from_api_add_remove_link(mock_get_links_api_objects, mock_ge
"kathara_user_hash_test_network_b": {
"Links": None,
"DriverOpts": {
'kathara.iface': '1', 'kathara.link': 'B',
"kathara.mac_addr": "00:00:00:00:00:02"
}
}
Expand Down

0 comments on commit 461f61d

Please sign in to comment.