Skip to content

Commit

Permalink
add comparisons keyword for 'assert_contains' and 'assert_not_contain…
Browse files Browse the repository at this point in the history
…s' property which is used to specify list of assertions.

the 'mode' key is added to allow status check to be logical OR/AND.
update buildspec file.
update json schema definition
  • Loading branch information
shahzebsiddiqui committed Oct 30, 2023
1 parent 56392e9 commit c5f095e
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 46 deletions.
18 changes: 11 additions & 7 deletions buildtest/buildsystem/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ def contains_check(builder):
metric_names = list(builder.metadata["metrics"].keys())

# iterate over each metric in buildspec and determine reference check for each metric
for metric in builder.status["contains"]:
for metric in builder.status["contains"]["comparisons"]:
name = metric["name"]
ref_value = metric["ref"]

Expand Down Expand Up @@ -810,9 +810,11 @@ def contains_check(builder):
console.print(
f"[blue]{builder}[/]: testing metric: [red]{name}[/red] if [yellow]{conv_value}[/yellow] in [yellow]{ref_value}[/yellow] - Check: {bool_check}"
)

# perform a logical AND on the list and return the boolean result
bool_check = all(assert_check)
# perform logical OR if mode is set to 'or' or 'OR' otherwise do logical AND
if builder.status["contains"].get("mode") in ["or", "OR"]:
bool_check = any(assert_check)
else:
bool_check = all(assert_check)

console.print(f"[blue]{builder}[/]: Contains Check: {bool_check}")
return bool_check
Expand All @@ -836,7 +838,7 @@ def notcontains_check(builder):
metric_names = list(builder.metadata["metrics"].keys())

# iterate over each metric in buildspec and determine reference check for each metric
for metric in builder.status["not_contains"]:
for metric in builder.status["not_contains"]["comparisons"]:
name = metric["name"]
ref_value = metric["ref"]

Expand Down Expand Up @@ -873,8 +875,10 @@ def notcontains_check(builder):
f"[blue]{builder}[/]: testing metric: [red]{name}[/red] if [yellow]{conv_value}[/yellow] not in [yellow]{ref_value}[/yellow] - Check: {bool_check}"
)

# perform a logical AND on the list and return the boolean result
bool_check = all(assert_check)
if builder.status["not_contains"].get("mode") in ["or", "OR"]:
bool_check = any(assert_check)
else:
bool_check = all(assert_check)

console.print(f"[blue]{builder}[/]: Not Contains Check: {bool_check}")
return bool_check
Expand Down
74 changes: 35 additions & 39 deletions buildtest/schemas/definitions.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -353,48 +353,12 @@
}
},
"contains": {
"description": "Check if metric value is in a list of reference values ",
"items": {
"type": "object",
"additionalProperties": false,
"required": [
"name",
"ref"
],
"properties": {
"name": {
"type": "string",
"description": "Name of metric to use for comparison"
},
"ref": {
"type": "array",
"minItems": 1,
"description": "Specify a list of reference value"
}
}
}
"description": "Check if metric value is in a list of reference values",
"$ref": "#/definitions/status_check_contains_not_contains"
},
"not_contains": {
"description": "Check if metric value not in a list of reference values ",
"items": {
"type": "object",
"additionalProperties": false,
"required": [
"name",
"ref"
],
"properties": {
"name": {
"type": "string",
"description": "Name of metric to use for comparison"
},
"ref": {
"type": "array",
"minItems": 1,
"description": "Specify a list of reference value"
}
}
}
"$ref": "#/definitions/status_check_contains_not_contains"
},
"is_symlink": {
"description": "Check for list of files or directory paths that are symbolic links",
Expand Down Expand Up @@ -544,6 +508,38 @@
}
}
},
"status_check_contains_not_contains": {
"type": "object",
"required": ["comparisons"],
"additionalProperties": false,
"properties": {
"mode": {
"$ref": "#/definitions/mode"
},
"comparisons": {
"type": "array",
"items": {
"type": "object",
"additionalProperties": false,
"required": [
"name",
"ref"
],
"properties": {
"name": {
"type": "string",
"description": "Name of metric to use for comparison"
},
"ref": {
"type": "array",
"minItems": 1,
"description": "Specify a list of reference value"
}
}
}
}
}
},
"mode": {
"description": "Determine how the status check is resolved, for instance it can be logical AND or OR",
"type": "string",
Expand Down
3 changes: 3 additions & 0 deletions tutorials/perf_checks/contains.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ buildspecs:
item: 1
status:
contains:
comparisons:
- name: x
ref: [1, 2, 4, 8]
not_contains:
comparisons:
- name: x
ref: [2, 4]
assert_contains_fail:
Expand All @@ -38,5 +40,6 @@ buildspecs:
item: 1
status:
contains:
comparisons:
- name: x
ref: ['1', 2, 4, 8]

0 comments on commit c5f095e

Please sign in to comment.