Skip to content

Commit 60c49ce

Browse files
committed
BUGFIX: tell the json parser in stack load to treat jinja templates as strings
our current json parser will attempt to interpret things between double curly braces - some json thing.
1 parent 4adcf0c commit 60c49ce

3 files changed

Lines changed: 27 additions & 2 deletions

File tree

common/src/stack/command/stack/commands/load/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ class command(stack.commands.Command):
2424
MustBeRoot = 0
2525

2626
def _load(self, text):
27-
parser = JsonComment(json) # standard JSON is stupid
27+
parser = JsonComment() # standard JSON is stupid
2828
try:
29-
data = parser.loads(text)
29+
data = parser.loads(text, template=False)
3030
except ValueError as e:
3131
# parse the error message and split the input at the
3232
# syntax error
@@ -106,6 +106,7 @@ def load_file(self, filename):
106106
data = fin.read()
107107
except IOError:
108108
raise CommandError(self, f'cannot read {filename}')
109+
109110
document = self._load(data)
110111
if scope == 'global':
111112
return document
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"network": [
3+
{
4+
"name": "bogus",
5+
"address": "10.35.100.0",
6+
"gateway": null,
7+
"netmask": "255.255.255.0",
8+
"dns": false,
9+
"pxe": false,
10+
"mtu": 65492,
11+
"zone": "{{ fake }}"
12+
}
13+
]
14+
}

test-framework/test-suites/integration/tests/load/test_load.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,13 @@ def test_load_cart(self, host, test_file):
165165
/opt/stack/bin/stack "add box" test os=sles
166166
/opt/stack/bin/stack "enable cart" test box=test
167167
''')
168+
169+
170+
def test_load_json_with_jinja(self, host, test_file):
171+
""" test that the json parser doesn't swallow jinja templates """
172+
json_path = Path(test_file("load/json/network_with_jinja.json"))
173+
174+
result = host.run(f'stack load {json_path}')
175+
assert result.rc == 0
176+
assert result.stdout.lstrip().startswith('''/opt/stack/bin/stack "add network" bogus''')
177+
assert "zone='{{ fake }}'" in result.stdout

0 commit comments

Comments
 (0)