Skip to content

Commit 4d7f9b4

Browse files
committed
fix: check resolved host requirement capability names at job creation
The capability name constraints and the uniqueness of names within hostRequirements amounts and attributes apply to the names after their format strings are resolved (OpenJobDescription/openjd-specifications#189). Template validation only compares the raw names, so two different format strings that resolved to the same name, or a name that resolved to more than 100 characters, created a job. Check the 100-character limit on the resolved amount and attribute names, and the case-insensitive uniqueness of the resolved names, when the job is created. The messages match the ones for literal names. Signed-off-by: Mark <399551+mwiebe@users.noreply.github.com>
1 parent 0ca67e1 commit 4d7f9b4

2 files changed

Lines changed: 166 additions & 0 deletions

File tree

‎src/openjd/model/v2023_09/_model.py‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3066,6 +3066,18 @@ def _check_constraints(self, value: Any) -> None:
30663066
_STANDARD_AMOUNT_CAPABILITIES_NAMES = list(STANDARD_AMOUNT_CAPABILITIES.keys())
30673067

30683068

3069+
_CAPABILITY_NAME_MAX_LENGTH = 100
3070+
3071+
3072+
def _validate_resolved_capability_name_length(name: str) -> None:
3073+
"""Apply the 100-character limit of §3.3.1.1 / §3.3.2.1 to a resolved
3074+
capability name. The limit applies after the name's format string has
3075+
been resolved, so a name that is a format string is checked at job
3076+
creation."""
3077+
if len(name) > _CAPABILITY_NAME_MAX_LENGTH:
3078+
raise ValueError(f"String must be at most {_CAPABILITY_NAME_MAX_LENGTH} characters long")
3079+
3080+
30693081
class AmountCapabilityName(FormatString):
30703082
"""The name of an amount capability."""
30713083

@@ -3121,6 +3133,10 @@ class AmountRequirement(OpenJDModel_v2023_09):
31213133
@field_validator("name")
31223134
@classmethod
31233135
def _validate_name(cls, v: str, info: ValidationInfo) -> str:
3136+
# The name is the resolved <AmountCapabilityName>, so its §3.3.1.1
3137+
# length limit applies here even when the template's name was a
3138+
# format string.
3139+
_validate_resolved_capability_name_length(v)
31243140
validate_amount_capability_name(
31253141
capability_name=v, standard_capabilities=_STANDARD_AMOUNT_CAPABILITIES_NAMES
31263142
)
@@ -3274,6 +3290,10 @@ class AttributeRequirement(OpenJDModel_v2023_09):
32743290
@field_validator("name")
32753291
@classmethod
32763292
def _validate_name(cls, v: str) -> str:
3293+
# The name is the resolved <AttributeCapabilityName>, so its §3.3.2.1
3294+
# length limit applies here even when the template's name was a
3295+
# format string.
3296+
_validate_resolved_capability_name_length(v)
32773297
validate_attribute_capability_name(
32783298
capability_name=v, standard_capabilities=_STANDARD_ATTRIBUTE_CAPABILITIES_NAMES
32793299
)
@@ -3410,6 +3430,28 @@ class HostRequirements(OpenJDModel_v2023_09):
34103430
amounts: Optional[list[AmountRequirement]] = None
34113431
attributes: Optional[list[AttributeRequirement]] = None
34123432

3433+
# §3.3: no two amounts, and no two attributes, may have the same name
3434+
# after the name format strings have been resolved. The template only
3435+
# compares the raw names, so two different format strings that resolve
3436+
# to the same capability are caught here.
3437+
@field_validator("amounts")
3438+
@classmethod
3439+
def _validate_amounts(
3440+
cls, v: Optional[list[AmountRequirement]]
3441+
) -> Optional[list[AmountRequirement]]:
3442+
if v is None:
3443+
return v
3444+
return validate_unique_elements(v, item_value=lambda v: v.name.lower(), property="name")
3445+
3446+
@field_validator("attributes")
3447+
@classmethod
3448+
def _validate_attributes(
3449+
cls, v: Optional[list[AttributeRequirement]]
3450+
) -> Optional[list[AttributeRequirement]]:
3451+
if v is None:
3452+
return v
3453+
return validate_unique_elements(v, item_value=lambda v: v.name.lower(), property="name")
3454+
34133455

34143456
class HostRequirementsTemplate(OpenJDModel_v2023_09):
34153457
amounts: Optional[list[AmountRequirementTemplate]] = None

‎test/openjd/model_v0/test_create_job.py‎

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,3 +1068,127 @@ def test_queue_environment_parameters_reach_the_tables(self) -> None:
10681068
"string",
10691069
"fromQueueEnv",
10701070
)
1071+
1072+
1073+
class TestCreateJobResolvedCapabilityNames:
1074+
"""The §3.3.1.1 / §3.3.2.1 capability name constraints and the §3.3
1075+
uniqueness constraints apply to the names after their format strings have
1076+
been resolved, so job creation checks the resolved names."""
1077+
1078+
@staticmethod
1079+
def _create_job(host_requirements: dict[str, Any], a: str, b: str) -> None:
1080+
job_template = decode_job_template(
1081+
template={
1082+
"specificationVersion": "jobtemplate-2023-09",
1083+
"name": "Job",
1084+
"parameterDefinitions": [
1085+
{"name": "A", "type": "STRING"},
1086+
{"name": "B", "type": "STRING"},
1087+
],
1088+
"steps": [
1089+
{
1090+
"name": "Step",
1091+
"hostRequirements": host_requirements,
1092+
"script": {"actions": {"onRun": {"command": "do something"}}},
1093+
}
1094+
],
1095+
},
1096+
)
1097+
parameter_values = {
1098+
"A": ParameterValue(type=ParameterValueType.STRING, value=a),
1099+
"B": ParameterValue(type=ParameterValueType.STRING, value=b),
1100+
}
1101+
create_job(job_template=job_template, job_parameter_values=parameter_values)
1102+
1103+
@pytest.mark.parametrize(
1104+
"host_requirements,a,b,expected_message",
1105+
[
1106+
pytest.param(
1107+
{
1108+
"attributes": [
1109+
{"name": "{{Param.A}}", "anyOf": ["v"]},
1110+
{"name": "{{Param.B}}", "anyOf": ["v"]},
1111+
]
1112+
},
1113+
"attr.custom.x",
1114+
"ATTR.CUSTOM.X",
1115+
"1 validation errors for JobTemplate\n"
1116+
"steps[0] -> hostRequirements -> attributes:\n"
1117+
"\tDuplicate values for name are not allowed. Duplicate values: attr.custom.x",
1118+
id="attribute names resolve to the same name, case-insensitively",
1119+
),
1120+
pytest.param(
1121+
{
1122+
"amounts": [
1123+
{"name": "{{Param.A}}", "min": 1},
1124+
{"name": "{{Param.B}}", "min": 1},
1125+
]
1126+
},
1127+
"amount.custom.x",
1128+
"amount.custom.x",
1129+
"1 validation errors for JobTemplate\n"
1130+
"steps[0] -> hostRequirements -> amounts:\n"
1131+
"\tDuplicate values for name are not allowed. Duplicate values: amount.custom.x",
1132+
id="amount names resolve to the same name",
1133+
),
1134+
pytest.param(
1135+
{"attributes": [{"name": "{{Param.A}}", "anyOf": ["v"]}]},
1136+
"attr.custom." + "a" * 89,
1137+
"unused",
1138+
"1 validation errors for JobTemplate\n"
1139+
"steps[0] -> hostRequirements -> attributes[0] -> name:\n"
1140+
"\tString must be at most 100 characters long",
1141+
id="attribute name resolves to 101 characters",
1142+
),
1143+
pytest.param(
1144+
{"amounts": [{"name": "{{Param.A}}", "min": 1}]},
1145+
"amount.custom." + "a" * 87,
1146+
"unused",
1147+
"1 validation errors for JobTemplate\n"
1148+
"steps[0] -> hostRequirements -> amounts[0] -> name:\n"
1149+
"\tString must be at most 100 characters long",
1150+
id="amount name resolves to 101 characters",
1151+
),
1152+
],
1153+
)
1154+
def test_resolved_names_checked(
1155+
self, host_requirements: dict[str, Any], a: str, b: str, expected_message: str
1156+
) -> None:
1157+
# WHEN
1158+
with pytest.raises(DecodeValidationError) as excinfo:
1159+
self._create_job(host_requirements, a, b)
1160+
1161+
# THEN
1162+
assert str(excinfo.value) == expected_message
1163+
1164+
@pytest.mark.parametrize(
1165+
"host_requirements,a,b",
1166+
[
1167+
pytest.param(
1168+
{
1169+
"attributes": [
1170+
{"name": "{{Param.A}}", "anyOf": ["v"]},
1171+
{"name": "{{Param.B}}", "anyOf": ["v"]},
1172+
]
1173+
},
1174+
"attr.custom.x",
1175+
"attr.custom.y",
1176+
id="attribute names resolve to different names",
1177+
),
1178+
pytest.param(
1179+
{"attributes": [{"name": "{{Param.A}}", "anyOf": ["v"]}]},
1180+
"attr.custom." + "a" * 88,
1181+
"unused",
1182+
id="attribute name resolves to exactly 100 characters",
1183+
),
1184+
pytest.param(
1185+
{"amounts": [{"name": "{{Param.A}}", "min": 1}]},
1186+
"amount.custom." + "a" * 86,
1187+
"unused",
1188+
id="amount name resolves to exactly 100 characters",
1189+
),
1190+
],
1191+
)
1192+
def test_valid_resolved_names(self, host_requirements: dict[str, Any], a: str, b: str) -> None:
1193+
# WHEN / THEN (no error)
1194+
self._create_job(host_requirements, a, b)

0 commit comments

Comments
 (0)