Skip to content

Commit

Permalink
Minor fixes to labels (#256)
Browse files Browse the repository at this point in the history
  • Loading branch information
Skazza94 committed Dec 23, 2023
1 parent 83c6b34 commit 51e62ae
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/Kathara/cli/ui/setting/DockerOptionsHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def add_items(self, current_menu: ConsoleMenu, menu_formatter: MenuFormatBuilder
FunctionItem(
text="Do not share collision domains",
function=setting_utils.update_setting_value,
args=["shareds_cd", SharedCollisionDomainsOption.NOT_SHARED],
args=["shared_cds", SharedCollisionDomainsOption.NOT_SHARED],
should_exit=True
)
)
Expand Down
18 changes: 10 additions & 8 deletions src/Kathara/manager/docker/DockerLink.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,20 +104,22 @@ def create(self, link: Link) -> None:
else:
network_ipam_config = docker.types.IPAMConfig(driver='null')

user_label = "shared_cd" if Setting.get_instance().shared_cds == SharedCollisionDomainsOption.USERS \
else utils.get_current_user_name()
additional_labels = {}
if Setting.get_instance().shared_cds != SharedCollisionDomainsOption.USERS:
additional_labels["user"] = utils.get_current_user_name()
if Setting.get_instance().shared_cds == SharedCollisionDomainsOption.NOT_SHARED:
additional_labels["lab_hash"] = link.lab.hash

link.api_object = self.client.networks.create(
name=link_name,
driver=f"{Setting.get_instance().network_plugin}:{utils.get_architecture()}",
check_duplicate=True,
ipam=network_ipam_config,
labels={
"lab_hash": link.lab.hash if
Setting.get_instance().shared_cds == SharedCollisionDomainsOption.NOT_SHARED else None,
"name": link.name,
"user": user_label,
"app": "kathara",
"external": ";".join([x.get_full_name() for x in link.external])
"external": ";".join([x.get_full_name() for x in link.external]),
**additional_labels
}
)

Expand Down Expand Up @@ -165,7 +167,7 @@ def wipe(self, user: str = None) -> None:
Returns:
None
"""
user_label = "shared_cd" if Setting.get_instance().shared_cds == SharedCollisionDomainsOption.USERS else user
user_label = user if Setting.get_instance().shared_cds != SharedCollisionDomainsOption.USERS else None
networks = self.get_links_api_objects_by_filters(user=user_label)
for item in networks:
item.reload()
Expand Down Expand Up @@ -372,4 +374,4 @@ def get_network_name(link: Link) -> str:
elif Setting.get_instance().shared_cds == SharedCollisionDomainsOption.USERS:
return f"{Setting.get_instance().net_prefix}_{link.name}"
elif Setting.get_instance().shared_cds == SharedCollisionDomainsOption.NOT_SHARED:
return f"{Setting.get_instance().net_prefix}_{utils.get_current_user_name()}_{link.lab.hash}_{link.name}"
return f"{Setting.get_instance().net_prefix}_{utils.get_current_user_name()}_{link.name}_{link.lab.hash}"
17 changes: 7 additions & 10 deletions tests/manager/docker/docker_link_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def test_get_network_name(mock_get_current_user_name, mock_setting_get_instance,
})
mock_setting_get_instance.return_value = setting_mock
link_name = DockerLink.get_network_name(default_link)
assert link_name == "kathara_user_lab-hash_A"
assert link_name == "kathara_user_A_lab-hash"


@mock.patch("src.Kathara.setting.Setting.Setting.get_instance")
Expand Down Expand Up @@ -121,16 +121,16 @@ def test_create(mock_get_current_user_name, mock_setting_get_instance, docker_li
mock_setting_get_instance.return_value = setting_mock
docker_link.create(default_link)
docker_link.client.networks.create.assert_called_once_with(
name="kathara_user_lab-hash_A",
name="kathara_user_A_lab-hash",
driver=f"{setting_mock.network_plugin}:{utils.get_architecture()}",
check_duplicate=True,
ipam=docker.types.IPAMConfig(driver='null'),
labels={
"lab_hash": default_link.lab.hash,
"name": "A",
"user": "user",
"app": "kathara",
"external": ""
"external": "",
"user": "user",
"lab_hash": default_link.lab.hash,
}
)

Expand All @@ -142,7 +142,7 @@ def test_create_bridge_link(docker_link, bridged_link):
@mock.patch("src.Kathara.setting.Setting.Setting.get_instance")
@mock.patch("src.Kathara.utils.get_current_user_name")
def test_create_shared_cds_between_users(mock_get_current_user_name, mock_setting_get_instance, docker_link,
default_link):
default_link):
docker_link.client.networks.list.return_value = []

mock_get_current_user_name.return_value = 'user'
Expand All @@ -161,9 +161,7 @@ def test_create_shared_cds_between_users(mock_get_current_user_name, mock_settin
check_duplicate=True,
ipam=docker.types.IPAMConfig(driver='null'),
labels={
"lab_hash": None,
"name": "A",
"user": "shared_cd",
"app": "kathara",
"external": ""
}
Expand All @@ -173,7 +171,7 @@ def test_create_shared_cds_between_users(mock_get_current_user_name, mock_settin
@mock.patch("src.Kathara.setting.Setting.Setting.get_instance")
@mock.patch("src.Kathara.utils.get_current_user_name")
def test_create_shared_cds_between_labs(mock_get_current_user_name, mock_setting_get_instance, docker_link,
default_link):
default_link):
docker_link.client.networks.list.return_value = []

mock_get_current_user_name.return_value = 'user'
Expand All @@ -192,7 +190,6 @@ def test_create_shared_cds_between_labs(mock_get_current_user_name, mock_setting
check_duplicate=True,
ipam=docker.types.IPAMConfig(driver='null'),
labels={
"lab_hash": None,
"name": "A",
"user": "user",
"app": "kathara",
Expand Down

0 comments on commit 51e62ae

Please sign in to comment.