-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The jsonpatch tests now follow a similar format as the conditions tests, where we define them in a declarative way in a yaml format. Moreover, we define tests for specific schema versions. This allows us to keep running old tests for old schemas and for schemas with backward compatibility and introduce new tests that are only executed for the new schemas.
- Loading branch information
1 parent
0e561ae
commit dc7ccd6
Showing
5 changed files
with
184 additions
and
177 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import os | ||
|
||
import pytest | ||
import yaml | ||
from test_utils import expand_schemas | ||
|
||
from generic_k8s_webhook.config_parser.entrypoint import GenericWebhookConfigManifest | ||
|
||
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) | ||
JSONPATCH_YAML = os.path.join(SCRIPT_DIR, "jsonpatch_test.yaml") | ||
|
||
|
||
def _parse_tests() -> list[tuple]: | ||
with open(JSONPATCH_YAML, "r") as f: | ||
raw_tests = yaml.safe_load(f) | ||
|
||
parsed_tests = [] | ||
for test_suite in raw_tests["test_suites"]: | ||
for test in test_suite["tests"]: | ||
for schema in expand_schemas(raw_tests["schemas_subsets"], test["schemas"]): | ||
for i, case in enumerate(test["cases"]): | ||
parsed_tests.append( | ||
( | ||
f"{test_suite['name']}_{i}", # name | ||
schema, # schema | ||
case["patch"], # patch | ||
case["payload"], # payload | ||
case["expected_result"], # expected_result | ||
) | ||
) | ||
return parsed_tests | ||
|
||
|
||
@pytest.mark.parametrize(("name", "schema", "patch", "payload", "expected_result"), _parse_tests()) | ||
def test_all(name, schema, patch, payload, expected_result): | ||
raw_config = { | ||
"apiVersion": f"generic-webhook/{schema}", | ||
"kind": "GenericWebhookConfig", | ||
"webhooks": [{"name": "test-webhook", "path": "test-path", "actions": [{"patch": [patch]}]}], | ||
} | ||
gwcm = GenericWebhookConfigManifest(raw_config) | ||
action = gwcm.list_webhook_config[0].list_actions[0] | ||
json_patch = action.get_patches(payload) | ||
result = json_patch.apply(payload) | ||
assert result == expected_result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
schemas_subsets: | ||
# v1alpha1 is a subset of the features from v1beta1 | ||
# For this reason, any test that uses v1alpha1 will also be executed for v1beta1 | ||
v1alpha1: | ||
- v1beta1 | ||
|
||
# Each suite stresses a specific jsonpatch operator. | ||
# Each test specifies a patch operation to be applied on a json payload | ||
# and the expected result. | ||
test_suites: | ||
- name: ADD | ||
tests: | ||
- schemas: [v1alpha1] | ||
cases: | ||
# Change the value of a simple key | ||
- patch: | ||
op: add | ||
path: .spec | ||
value: foo | ||
payload: { spec: {}, metadata: {} } | ||
expected_result: { spec: foo, metadata: {} } | ||
# Add a subkey that doesn't exist | ||
- patch: | ||
op: add | ||
path: .spec.subkey | ||
value: foo | ||
payload: { spec: {}, metadata: {} } | ||
expected_result: { spec: { subkey: foo }, metadata: {} } | ||
# Add a 2 subkeys that don't exist | ||
- patch: | ||
op: add | ||
path: .spec.subkey1.subkey2 | ||
value: foo | ||
payload: { spec: {}, metadata: {} } | ||
expected_result: | ||
{ spec: { subkey1: { subkey2: foo } }, metadata: {} } | ||
# Add an element to an existing empty list | ||
- patch: | ||
op: add | ||
path: .spec.containers.- | ||
value: { name: main } | ||
payload: { spec: { containers: [] }, metadata: {} } | ||
expected_result: | ||
{ spec: { containers: [{ name: main }] }, metadata: {} } | ||
# Add an element to an existing non-empty list | ||
- patch: | ||
op: add | ||
path: .spec.containers.- | ||
value: { name: main } | ||
payload: { spec: { containers: [{ name: sidecar }] }, metadata: {} } | ||
expected_result: | ||
spec: { containers: [{ name: sidecar }, { name: main }] } | ||
metadata: {} | ||
# Add an element to a non-existing list | ||
- patch: | ||
op: add | ||
path: .spec.containers.- | ||
value: { name: main } | ||
payload: { spec: {}, metadata: {} } | ||
expected_result: | ||
{ spec: { containers: [{ name: main }] }, metadata: {} } | ||
# Add a new entry on the second element of the list | ||
- patch: | ||
op: add | ||
path: .spec.containers.0.metadata | ||
value: {} | ||
payload: { spec: { containers: [{ name: main }] }, metadata: {} } | ||
expected_result: | ||
spec: { containers: [{ name: main, metadata: {} }] } | ||
metadata: {} | ||
- name: REMOVE | ||
tests: | ||
- schemas: [v1alpha1] | ||
cases: | ||
# Remove the value of a simple key | ||
- patch: | ||
op: remove | ||
path: .spec | ||
payload: { spec: {}, metadata: {} } | ||
expected_result: { metadata: {} } | ||
- name: REPLACE | ||
tests: | ||
- schemas: [v1alpha1] | ||
cases: | ||
# Replace the value of a simple key | ||
- patch: | ||
op: replace | ||
path: .metadata.name | ||
value: bar | ||
payload: { spec: {}, metadata: { name: foo } } | ||
expected_result: { spec: {}, metadata: { name: bar } } | ||
- name: COPY | ||
tests: | ||
- schemas: [v1alpha1] | ||
cases: | ||
# Copy the value from a simple key to another | ||
- patch: | ||
op: copy | ||
path: .metadata.name | ||
from: .spec.containers.0.name | ||
payload: | ||
{ spec: { containers: [{ name: bar }] }, metadata: { name: foo } } | ||
expected_result: | ||
{ spec: { containers: [{ name: bar }] }, metadata: { name: bar } } | ||
- name: MOVE | ||
tests: | ||
- schemas: [v1alpha1] | ||
cases: | ||
# Move the value from a simple key to another | ||
- patch: | ||
op: move | ||
path: .metadata.name | ||
from: .spec.containers.0.name | ||
payload: | ||
{ spec: { containers: [{ name: bar }] }, metadata: { name: foo } } | ||
expected_result: | ||
{ spec: { containers: [{}] }, metadata: { name: bar } } | ||
- name: TEST | ||
tests: | ||
- schemas: [v1alpha1] | ||
cases: | ||
# Test the value of a simple key | ||
- patch: | ||
op: test | ||
path: .metadata.name | ||
value: foo | ||
payload: { spec: {}, metadata: { name: foo } } | ||
expected_result: { spec: {}, metadata: { name: foo } } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters