-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathtest_round_trip.py
More file actions
235 lines (176 loc) · 8.04 KB
/
test_round_trip.py
File metadata and controls
235 lines (176 loc) · 8.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
"""Round-trip tests for the HCL2 → JSON → HCL2 pipeline.
Every test starts from the source HCL files in test/integration/hcl2_original/
and runs the pipeline forward from there, comparing actuals against expected
outputs at each stage:
1. HCL → JSON serialization (parse + transform + serialize)
2. JSON → JSON reserialization (serialize + deserialize + reserialize)
3. JSON → HCL reconstruction (serialize + deserialize + format + reconstruct)
4. Full round-trip (HCL → JSON → HCL → JSON produces identical JSON)
"""
# pylint: disable=C0103,C0114,C0115,C0116
import json
from enum import Enum
from pathlib import Path
from typing import List
from unittest import TestCase
from hcl2.api import parses_to_tree
from hcl2.deserializer import BaseDeserializer
from hcl2.formatter import BaseFormatter
from hcl2.reconstructor import HCLReconstructor
from hcl2.transformer import RuleTransformer
INTEGRATION_DIR = Path(__file__).absolute().parent
HCL2_ORIGINAL_DIR = INTEGRATION_DIR / "hcl2_original"
_STEP_DIRS = {
"hcl2_original": HCL2_ORIGINAL_DIR,
"hcl2_reconstructed": INTEGRATION_DIR / "hcl2_reconstructed",
"json_serialized": INTEGRATION_DIR / "json_serialized",
"json_reserialized": INTEGRATION_DIR / "json_reserialized",
}
_STEP_SUFFIXES = {
"hcl2_original": ".tf",
"hcl2_reconstructed": ".tf",
"json_serialized": ".json",
"json_reserialized": ".json",
}
class SuiteStep(Enum):
ORIGINAL = "hcl2_original"
RECONSTRUCTED = "hcl2_reconstructed"
JSON_SERIALIZED = "json_serialized"
JSON_RESERIALIZED = "json_reserialized"
def _get_suites() -> List[str]:
"""
Get a list of the test suites.
Names of a test suite is a name of file in `test/integration/hcl2_original/` without the .tf suffix.
Override SUITES to run a specific subset, e.g. SUITES = ["config"]
"""
return SUITES or sorted(
file.stem for file in HCL2_ORIGINAL_DIR.iterdir() if file.is_file()
)
# set this to arbitrary list of test suites to run,
# e.g. `SUITES = ["smoke"]` to run the tests only for `test/integration/hcl2_original/smoke.tf`
SUITES: List[str] = []
def _get_suite_file(suite_name: str, step: SuiteStep) -> Path:
"""Return the path for a given suite name and pipeline step."""
return _STEP_DIRS[step.value] / (suite_name + _STEP_SUFFIXES[step.value])
def _parse_and_serialize(hcl_text: str, options=None) -> dict:
"""Parse HCL text and serialize to a Python dict."""
parsed_tree = parses_to_tree(hcl_text)
rules = RuleTransformer().transform(parsed_tree)
if options:
return rules.serialize(options=options)
return rules.serialize()
def _direct_reconstruct(hcl_text: str) -> str:
"""Parse HCL text, transform to IR, convert to Lark tree, and reconstruct."""
parsed_tree = parses_to_tree(hcl_text)
rules = RuleTransformer().transform(parsed_tree)
lark_tree = rules.to_lark()
return HCLReconstructor().reconstruct(lark_tree)
def _deserialize_and_reserialize(serialized: dict) -> dict:
"""Deserialize a Python dict back through the rule tree and reserialize."""
deserializer = BaseDeserializer()
formatter = BaseFormatter()
deserialized = deserializer.load_python(serialized)
formatter.format_tree(deserialized)
return deserialized.serialize()
def _deserialize_and_reconstruct(serialized: dict) -> str:
"""Deserialize a Python dict and reconstruct HCL text."""
deserializer = BaseDeserializer()
formatter = BaseFormatter()
reconstructor = HCLReconstructor()
deserialized = deserializer.load_python(serialized)
formatter.format_tree(deserialized)
lark_tree = deserialized.to_lark()
return reconstructor.reconstruct(lark_tree)
class TestRoundTripSerialization(TestCase):
"""Test HCL2 → JSON serialization: parse HCL, transform, serialize, compare with expected JSON."""
maxDiff = None
def test_hcl_to_json(self):
for suite in _get_suites():
with self.subTest(suite=suite):
hcl_path = _get_suite_file(suite, SuiteStep.ORIGINAL)
json_path = _get_suite_file(suite, SuiteStep.JSON_SERIALIZED)
actual = _parse_and_serialize(hcl_path.read_text())
expected = json.loads(json_path.read_text())
self.assertEqual(
actual,
expected,
f"HCL → JSON serialization mismatch for suite {suite}",
)
class TestDirectReconstruction(TestCase):
"""Test HCL2 → IR → HCL2 direct pipeline.
Parse HCL, transform to IR, convert directly to Lark tree (skipping
serialization to dict), reconstruct HCL, then verify the result
re-parses to the same JSON as the original.
"""
maxDiff = None
def test_direct_reconstruct(self):
for suite in _get_suites():
with self.subTest(suite=suite):
hcl_path = _get_suite_file(suite, SuiteStep.ORIGINAL)
original_hcl = hcl_path.read_text()
# Direct: HCL → IR → Lark → HCL
reconstructed_hcl = _direct_reconstruct(original_hcl)
self.assertMultiLineEqual(
reconstructed_hcl,
original_hcl,
f"Direct reconstruction mismatch for suite {suite}: "
f"HCL → IR → HCL did not match original HCL",
)
class TestRoundTripReserialization(TestCase):
"""Test JSON → JSON reserialization.
Parse HCL, serialize, deserialize, reserialize, compare with expected.
"""
maxDiff = None
def test_json_reserialization(self):
for suite in _get_suites():
with self.subTest(suite=suite):
hcl_path = _get_suite_file(suite, SuiteStep.ORIGINAL)
json_reserialized_path = _get_suite_file(
suite, SuiteStep.JSON_RESERIALIZED
)
serialized = _parse_and_serialize(hcl_path.read_text())
actual = _deserialize_and_reserialize(serialized)
expected = json.loads(json_reserialized_path.read_text())
self.assertEqual(
actual,
expected,
f"JSON reserialization mismatch for suite {suite}",
)
class TestRoundTripReconstruction(TestCase):
"""Test JSON → HCL reconstruction.
Parse HCL, serialize, deserialize, format, reconstruct, compare with expected HCL.
"""
maxDiff = None
def test_json_to_hcl(self):
for suite in _get_suites():
with self.subTest(suite=suite):
hcl_path = _get_suite_file(suite, SuiteStep.ORIGINAL)
hcl_reconstructed_path = _get_suite_file(suite, SuiteStep.RECONSTRUCTED)
serialized = _parse_and_serialize(hcl_path.read_text())
actual = _deserialize_and_reconstruct(serialized)
expected = hcl_reconstructed_path.read_text()
self.assertMultiLineEqual(
actual,
expected,
f"HCL reconstruction mismatch for suite {suite}",
)
class TestRoundTripFull(TestCase):
"""Test full round-trip: HCL → JSON → HCL → JSON should produce matching JSON."""
maxDiff = None
def test_full_round_trip(self):
for suite in _get_suites():
with self.subTest(suite=suite):
hcl_path = _get_suite_file(suite, SuiteStep.ORIGINAL)
original_hcl = hcl_path.read_text()
# Forward: HCL → JSON
serialized = _parse_and_serialize(original_hcl)
# Reconstruct: JSON → HCL
reconstructed_hcl = _deserialize_and_reconstruct(serialized)
# Reparse: reconstructed HCL → JSON
reserialized = _parse_and_serialize(reconstructed_hcl)
self.assertEqual(
reserialized,
serialized,
f"Full round-trip mismatch for suite {suite}: "
f"HCL → JSON → HCL → JSON did not produce identical JSON",
)