Skip to content

Commit

Permalink
Merge branch 'main' into Fix/Change_the_behavior_in_the_to_fail_if_an…
Browse files Browse the repository at this point in the history
…_argument_is_present_in_the_settings_but_not_in_the_agent_definition_
  • Loading branch information
amine3 authored Nov 3, 2023
2 parents 1bff2ab + 45a4fd1 commit b34f405
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
9 changes: 8 additions & 1 deletion src/ostorlab/runtimes/lite_local/agent_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import logging
import uuid
import base64
import random
from typing import List, Optional

import docker
Expand All @@ -33,6 +34,8 @@
HEALTHCHECK_TIMEOUT = 30 * SECOND
HEALTHCHECK_START_PERIOD = 2 * SECOND
HEALTHCHECK_INTERVAL = 30 * SECOND
MAX_SERVICE_NAME_LEN = 63
MAX_RANDOM_NAME_LEN = 5


class Error(exceptions.OstorlabError):
Expand Down Expand Up @@ -345,11 +348,15 @@ def create_agent_service(

service_name = (
agent_definition.service_name
or self.agent.container_image.replace(":", "_").replace(".", "")
or self.agent.container_image.split(":")[0].replace(".", "")
+ "_"
+ self.runtime_name
)

# We apply the random str only if it will not break the max docker service name characters (63)
if len(service_name) + MAX_RANDOM_NAME_LEN < MAX_SERVICE_NAME_LEN:
service_name = service_name + "_" + str(random.randrange(0, 9999))

env = [
f"UNIVERSE={self.runtime_name}",
]
Expand Down
7 changes: 7 additions & 0 deletions src/ostorlab/runtimes/local/agent_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import hashlib
import uuid
import base64
import random
from typing import List, Optional

import docker
Expand All @@ -35,6 +36,8 @@
HEALTHCHECK_TIMEOUT = 30 * SECOND
HEALTHCHECK_START_PERIOD = 2 * SECOND
HEALTHCHECK_INTERVAL = 30 * SECOND
MAX_SERVICE_NAME_LEN = 63
MAX_RANDOM_NAME_LEN = 5


class Error(exceptions.OstorlabError):
Expand Down Expand Up @@ -341,6 +344,10 @@ def create_agent_service(
+ self.runtime_name
)

# We apply the random str only if it will not break the max docker service name characters (63)
if len(service_name) + MAX_RANDOM_NAME_LEN < MAX_SERVICE_NAME_LEN:
service_name = service_name + "_" + str(random.randrange(0, 9999))

env = [
f"UNIVERSE={self.runtime_name}",
]
Expand Down
9 changes: 6 additions & 3 deletions tests/runtimes/lite_local/runtime_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def testLiteLocalCreateAgentService_whenAgentDefAndAgentSettingsAreNotEmpty_serv
name="agent_name_from_def",
mounts=["def_mount1", "def_mount2"],
mem_limit=420000,
service_name="my_service",
service_name="complex_long_name_special_duplicate_duplicate_name_agent_def",
restart_policy="",
)
mocker.patch(
Expand Down Expand Up @@ -185,7 +185,10 @@ def testLiteLocalCreateAgentService_whenAgentDefAndAgentSettingsAreNotEmpty_serv
assert kwargs["resources"]["Limits"]["MemoryBytes"] == 700000
assert kwargs["mounts"] == ["settings_mount1"]
assert kwargs["restart_policy"]["Condition"] == "on-failure"
assert kwargs["name"] == "my_service"
assert len(kwargs["name"]) < 63
assert (
kwargs["name"] == "complex_long_name_special_duplicate_duplicate_name_agent_def"
)


def testLiteLocalCreateAgentService_whenAgentDefAndAgentSettingsCapsAreNotEmpty_serviceCreatedwithAgentSettings(
Expand Down Expand Up @@ -255,5 +258,5 @@ def testLiteLocalCreateAgentService_whenAgentDefAndAgentSettingsCapsAreNotEmpty_
assert kwargs["resources"]["Limits"]["MemoryBytes"] == 700000
assert kwargs["mounts"] == ["settings_mount1"]
assert kwargs["restart_policy"]["Condition"] == "on-failure"
assert kwargs["name"] == "my_service"
assert "my_service_" in kwargs["name"]
assert kwargs["cap_add"] == ["NET_ADMIN"]
4 changes: 3 additions & 1 deletion tests/runtimes/local/agent_runtime_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

def container_name_mock(name):
del name
return "name"
return "complex_long_name_special_duplicate_agent:v1026"


def testCreateAgentService_whenAgentDefAndAgentSettingsAreNotEmpty_serviceCreatedwithAgentSettings(
Expand Down Expand Up @@ -83,6 +83,8 @@ def testCreateAgentService_whenAgentDefAndAgentSettingsAreNotEmpty_serviceCreate

# assert arguments were overridden by the agent settings.
assert kwargs["resources"]["Limits"]["MemoryBytes"] == 700000
assert len(kwargs["name"]) < 63
assert "complex_long_name_special_duplicate_agent_42" in kwargs["name"]
assert kwargs["mounts"] == ["settings_mount1"]
assert kwargs["endpoint_spec"]["Ports"][0]["PublishedPort"] == 40000
assert kwargs["restart_policy"]["Condition"] == "on-failure"
Expand Down

0 comments on commit b34f405

Please sign in to comment.