|
| 1 | +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +"""Regression tests: Jobs carrying deferred (run-time-resolved) FormatStrings |
| 3 | +must be picklable and copyable. |
| 4 | +
|
| 5 | +FormatString.__new__ requires a keyword-only parsing context, but |
| 6 | +str.__getnewargs__ supplies only the string value, so pickle.loads, |
| 7 | +copy.copy/deepcopy, and model_copy(deep=True) raised TypeError — and |
| 8 | +subclasses that default the context (e.g. ArgString) silently re-parsed |
| 9 | +EXPR-grammar strings with the legacy parser (or failed pickling on the |
| 10 | +engine-backed parse tree). FormatString now records a parse-context snapshot |
| 11 | +and reconstructs through __getnewargs_ex__, re-parsing under the same grammar |
| 12 | +(see FormatString.__getstate__/__getnewargs_ex__). |
| 13 | +""" |
| 14 | + |
| 15 | +import copy |
| 16 | +import pickle |
| 17 | + |
| 18 | +from openjd.model import SymbolTable, create_job, decode_job_template |
| 19 | +from openjd.model._format_strings import FormatString |
| 20 | +from openjd.model._types import ParameterValue, ParameterValueType |
| 21 | +from openjd.model.v2023_09 import ArgString, ModelParsingContext |
| 22 | + |
| 23 | + |
| 24 | +def _job_with_deferred_fields(extensions: list[str], *, timeout: str): |
| 25 | + """An instantiated Job whose onRun action carries a deferred (unresolved) |
| 26 | + FormatString timeout and a deferred cancelation (mode + notifyPeriod).""" |
| 27 | + template = decode_job_template( |
| 28 | + template={ |
| 29 | + "specificationVersion": "jobtemplate-2023-09", |
| 30 | + "extensions": list(extensions), |
| 31 | + "name": "PickleTest", |
| 32 | + "parameterDefinitions": [ |
| 33 | + {"name": "TO", "type": "INT", "default": 30}, |
| 34 | + {"name": "Mode", "type": "STRING", "default": "TERMINATE"}, |
| 35 | + ], |
| 36 | + "steps": [ |
| 37 | + { |
| 38 | + "name": "Step1", |
| 39 | + "script": { |
| 40 | + "actions": { |
| 41 | + "onRun": { |
| 42 | + "command": "echo", |
| 43 | + "timeout": timeout, |
| 44 | + "cancelation": { |
| 45 | + "mode": "{{Param.Mode}}", |
| 46 | + "notifyPeriodInSeconds": "{{Param.TO}}", |
| 47 | + }, |
| 48 | + } |
| 49 | + } |
| 50 | + }, |
| 51 | + } |
| 52 | + ], |
| 53 | + }, |
| 54 | + supported_extensions=extensions, |
| 55 | + ) |
| 56 | + return create_job( |
| 57 | + job_template=template, |
| 58 | + job_parameter_values={ |
| 59 | + "TO": ParameterValue(type=ParameterValueType.INT, value="30"), |
| 60 | + "Mode": ParameterValue(type=ParameterValueType.STRING, value="TERMINATE"), |
| 61 | + }, |
| 62 | + ) |
| 63 | + |
| 64 | + |
| 65 | +def _deferred_symtab() -> SymbolTable: |
| 66 | + symtab = SymbolTable() |
| 67 | + symtab["Param.TO"] = 30 |
| 68 | + symtab["Param.Mode"] = "TERMINATE" |
| 69 | + return symtab |
| 70 | + |
| 71 | + |
| 72 | +def _assert_deferred_fields_equivalent(original, reconstructed) -> None: |
| 73 | + """The reconstructed action's deferred FormatStrings must be FormatStrings |
| 74 | + that evaluate identically to the originals against the same symtab.""" |
| 75 | + symtab = _deferred_symtab() |
| 76 | + for attr_path in ("timeout", "cancelation.mode", "cancelation.notifyPeriodInSeconds"): |
| 77 | + orig_value = original |
| 78 | + recon_value = reconstructed |
| 79 | + for part in attr_path.split("."): |
| 80 | + orig_value = getattr(orig_value, part) |
| 81 | + recon_value = getattr(recon_value, part) |
| 82 | + assert isinstance(recon_value, FormatString), attr_path |
| 83 | + assert type(recon_value) is type(orig_value), attr_path |
| 84 | + assert str(recon_value) == str(orig_value), attr_path |
| 85 | + assert recon_value.resolve(symtab=symtab) == orig_value.resolve(symtab=symtab), attr_path |
| 86 | + |
| 87 | + |
| 88 | +class TestJobPickleRoundTrip: |
| 89 | + def test_expr_job_pickle_round_trip(self) -> None: |
| 90 | + # GIVEN an EXPR + FEATURE_BUNDLE_1 job with deferred timeout and |
| 91 | + # cancelation, where the timeout uses EXPR-only arithmetic (the |
| 92 | + # legacy grammar cannot parse it — a reconstruction under a default |
| 93 | + # context would fail or change semantics). |
| 94 | + job = _job_with_deferred_fields(["EXPR", "FEATURE_BUNDLE_1"], timeout="{{ Param.TO * 2 }}") |
| 95 | + |
| 96 | + # WHEN |
| 97 | + reconstructed = pickle.loads(pickle.dumps(job)) |
| 98 | + |
| 99 | + # THEN |
| 100 | + original_action = job.steps[0].script.actions.onRun |
| 101 | + recon_action = reconstructed.steps[0].script.actions.onRun |
| 102 | + _assert_deferred_fields_equivalent(original_action, recon_action) |
| 103 | + symtab = _deferred_symtab() |
| 104 | + assert recon_action.timeout.resolve(symtab=symtab) == "60" |
| 105 | + |
| 106 | + def test_legacy_fb1_job_pickle_round_trip(self) -> None: |
| 107 | + # GIVEN a legacy (non-EXPR) FEATURE_BUNDLE_1 job with a deferred |
| 108 | + # FormatString timeout. |
| 109 | + job = _job_with_deferred_fields(["FEATURE_BUNDLE_1"], timeout="{{Param.TO}}") |
| 110 | + |
| 111 | + # WHEN |
| 112 | + reconstructed = pickle.loads(pickle.dumps(job)) |
| 113 | + |
| 114 | + # THEN |
| 115 | + original_action = job.steps[0].script.actions.onRun |
| 116 | + recon_action = reconstructed.steps[0].script.actions.onRun |
| 117 | + _assert_deferred_fields_equivalent(original_action, recon_action) |
| 118 | + symtab = _deferred_symtab() |
| 119 | + assert recon_action.timeout.resolve(symtab=symtab) == "30" |
| 120 | + |
| 121 | + def test_expr_job_deepcopy_and_model_copy(self) -> None: |
| 122 | + # GIVEN |
| 123 | + job = _job_with_deferred_fields(["EXPR", "FEATURE_BUNDLE_1"], timeout="{{ Param.TO * 2 }}") |
| 124 | + original_action = job.steps[0].script.actions.onRun |
| 125 | + |
| 126 | + # WHEN / THEN: both deep-copy paths reconstruct working FormatStrings. |
| 127 | + for job_copy in (copy.deepcopy(job), job.model_copy(deep=True)): |
| 128 | + copied_action = job_copy.steps[0].script.actions.onRun |
| 129 | + _assert_deferred_fields_equivalent(original_action, copied_action) |
| 130 | + |
| 131 | + |
| 132 | +class TestFormatStringPickleFaithfulness: |
| 133 | + """The reconstruction context must reproduce the original parse mode: |
| 134 | + re-parsing an EXPR-grammar string under a default (legacy) context would |
| 135 | + reject it outright or silently change its evaluation semantics.""" |
| 136 | + |
| 137 | + def test_expr_arg_string_typed_round_trip(self) -> None: |
| 138 | + # GIVEN an ArgString whose whole-field EXPR list expression only |
| 139 | + # parses under the EXPR grammar. |
| 140 | + context = ModelParsingContext(supported_extensions=["EXPR", "FEATURE_BUNDLE_1"]) |
| 141 | + original = ArgString('{{ ["a","b c"] }}', context=context) |
| 142 | + |
| 143 | + # WHEN |
| 144 | + reconstructed = pickle.loads(pickle.dumps(original)) |
| 145 | + |
| 146 | + # THEN: still an EXPR whole-field expression with identical typed |
| 147 | + # (RFC 0005) evaluation, not a legacy re-parse. |
| 148 | + assert type(reconstructed) is ArgString |
| 149 | + assert reconstructed.whole_field_expression() is not None |
| 150 | + symtab = SymbolTable() |
| 151 | + original_value = original.resolve_value(symtab=symtab) |
| 152 | + reconstructed_value = reconstructed.resolve_value(symtab=symtab) |
| 153 | + assert type(reconstructed_value) is type(original_value) |
| 154 | + assert str(reconstructed_value) == str(original_value) |
| 155 | + assert reconstructed.resolve(symtab=symtab) == original.resolve(symtab=symtab) |
| 156 | + |
| 157 | + def test_legacy_format_string_copy(self) -> None: |
| 158 | + # A plain legacy-grammar FormatString survives copy.copy and deepcopy. |
| 159 | + context = ModelParsingContext() |
| 160 | + original = FormatString("prefix {{ Task.Param.Frame }} suffix", context=context) |
| 161 | + symtab = SymbolTable() |
| 162 | + symtab["Task.Param.Frame"] = 7 |
| 163 | + for reconstructed in (copy.copy(original), copy.deepcopy(original)): |
| 164 | + assert str(reconstructed) == str(original) |
| 165 | + assert reconstructed.resolve(symtab=symtab) == "prefix 7 suffix" |
0 commit comments