Skip to content

Commit 5ead5b0

Browse files
mrnicegyu11Dustin Kaiserpcrespov
authored
šŸ› Fix validation of DIRECTOR_V2_SERVICES_CUSTOM_CONSTRAINTS: Make it conform to docker specs (ITISFoundation#3190)
* add underscore * Add test (thx pedro) * make regex according to docker specs * Fix validation Co-authored-by: Dustin Kaiser <[email protected]> Co-authored-by: Pedro Crespo-Valero <[email protected]>
1 parent 06ed666 commit 5ead5b0

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

ā€Žservices/director-v2/src/simcore_service_director_v2/core/settings.pyā€Ž

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@
6767
SUPPORTED_TRAEFIK_LOG_LEVELS: set[str] = {"info", "debug", "warn", "error"}
6868

6969
PlacementConstraintStr = constr(
70-
strip_whitespace=True, regex=r"^[a-zA-Z0-9. ]*(!=|==){1}[a-zA-Z0-9. ]*$"
70+
strip_whitespace=True,
71+
regex=r"^(?!-)(?![.])(?!.*--)(?!.*[.][.])[a-zA-Z0-9.-]*(?<!-)(?<![.])(!=|==){1}[a-zA-Z0-9_. -]*$",
7172
)
7273

7374

ā€Žservices/director-v2/tests/unit/test_core_settings.pyā€Ž

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,12 @@ def test_expected_failure_dynamic_sidecar_settings(
156156
('["one==yes"]', ["one==yes"]),
157157
('["two!=no"]', ["two!=no"]),
158158
('["one==yes", "two!=no"]', ["one==yes", "two!=no"]),
159-
('[" strips.white.spaces == ok "]', ["strips.white.spaces == ok"]),
159+
('["strips.white.spaces== ok "]', ["strips.white.spaces== ok"]),
160+
(
161+
# Bug from https://github.com/ITISFoundation/osparc-simcore/pull/3190
162+
'["node.labels.standard-worker==true"]',
163+
["node.labels.standard-worker==true"],
164+
),
160165
),
161166
)
162167
def test_services_custom_constraints(
@@ -171,6 +176,37 @@ def test_services_custom_constraints(
171176
assert settings.DIRECTOR_V2_SERVICES_CUSTOM_CONSTRAINTS == expected
172177

173178

179+
@pytest.mark.parametrize(
180+
"custom_constraints, expected",
181+
(
182+
# Whitespaces in key are not allowed https://docs.docker.com/config/labels-custom-metadata/#label-keys-and-values
183+
('["strips.white spaces==ok "]', ["strips.white spaces==ok"]),
184+
('[".starting.trailing.dot.==forbidden"]', [".starting.dot==forbidden"]),
185+
('["double...dot==forbidden"]', ["double..dot==forbidden"]),
186+
('["double--hyphen==forbidden"]', ["double--hyphen==forbidden"]),
187+
(
188+
'["-starting.trailing.hyphen-==forbidden"]',
189+
["-starting.trailing.hyphen-==forbidden"],
190+
),
191+
(
192+
# Bug from https://github.com/ITISFoundation/osparc-simcore/pull/
193+
# Underscores forbidden
194+
'["node.labels.standard_worker==true"]',
195+
["node.labels.standard_worker==true"],
196+
),
197+
),
198+
)
199+
def test_services_custom_constraint_failures(
200+
custom_constraints: str,
201+
expected: list[str],
202+
project_env_devel_environment: EnvVarsDict,
203+
monkeypatch: MonkeyPatch,
204+
) -> None:
205+
monkeypatch.setenv("DIRECTOR_V2_SERVICES_CUSTOM_CONSTRAINTS", custom_constraints)
206+
with pytest.raises(Exception) as excinfo:
207+
settings = AppSettings.create_from_envs()
208+
209+
174210
def test_services_custom_constraints_default_empty_list(
175211
project_env_devel_environment: EnvVarsDict,
176212
) -> None:

0 commit comments

Comments
Ā (0)