Skip to content

Commit 6c7f082

Browse files
bgreenbbsanders
authored andcommitted
INTERNAL: Fix check for when custom template file is present
1 parent b2a920c commit 6c7f082

3 files changed

Lines changed: 96 additions & 4 deletions

File tree

common/src/stack/command/stack/commands/report/vm/__init__.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,19 @@ def run(self, param, args):
9191

9292
template_conf = Path(template_loc)
9393
env = Environment()
94-
ast = env.parse(template_conf.read_text())
95-
var_list = meta.find_undeclared_variables(ast)
9694

97-
if template_conf.is_file() and req_vars.issubset(var_list):
95+
# Check if the template exists
96+
# and has the required variables
97+
if template_conf.is_file():
98+
ast = env.parse(template_conf.read_text())
99+
var_list = meta.find_undeclared_variables(ast)
100+
else:
101+
raise CommandError(self, f'Unable to find template file: {template_conf}')
102+
103+
if req_vars.issubset(var_list):
98104
libvirt_template = jinja2.Template(template_conf.read_text(), lstrip_blocks=True, trim_blocks=True)
99105
else:
100-
raise CommandError(self, f'Unable to parse or missing variables for template file: {template_conf}')
106+
raise CommandError(self, f'Missing required template variables for libvirt config file: {template_conf}')
101107

102108
# If the hypervisor param is set
103109
# ignore any VM not belonging to the
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<domain type="kvm">
2+
<name>{{ name }}</name>
3+
<memory>{{ memory }}</memory>
4+
<vcpu>{{ cpucount }}</vcpu>
5+
<os>
6+
<type arch="x86_64">hvm</type>
7+
</os>
8+
<features>
9+
<acpi/>
10+
<apic/>
11+
{% if os == 'sles' %}
12+
<vmport state="off"/>
13+
{% endif %}
14+
</features>
15+
<clock offset="utc">
16+
<timer name="rtc" tickpolicy="catchup"/>
17+
<timer name="pit" tickpolicy="delay"/>
18+
<timer name="hpet" present="no"/>
19+
</clock>
20+
<on_poweroff>destroy</on_poweroff>
21+
<on_reboot>restart</on_reboot>
22+
<on_crash>restart</on_crash>
23+
<pm>
24+
<suspend-to-mem enabled="no"/>
25+
<suspend-to-disk enabled="no"/>
26+
</pm>
27+
<devices>
28+
{% if os == 'redhat' %}
29+
<emulator>/usr/libexec/qemu-kvm</emulator>
30+
{% endif %}
31+
{% if os == 'sles' %}
32+
<emulator>/usr/bin/qemu-kvm</emulator>
33+
{% endif %}
34+
<serial type="pty">
35+
<target port="0"/>
36+
</serial>
37+
<serial type="pty">
38+
<target port="1"/>
39+
</serial>
40+
<input bus="ps2" type="mouse"/>
41+
<graphics autoport="yes" keymap="en-us" type="vnc" port="-1"/>
42+
<video>
43+
<model heads="1" vram="9216" type="cirrus"/>
44+
</video>
45+
<rng model="virtio">
46+
<backend model="random">/dev/random</backend>
47+
<address type="pci" domain="0x0000" bus="0x00" slot="0x0c" function="0x0"/>
48+
</rng>
49+
</devices>
50+
</domain>

test-framework/test-suites/integration/tests/report/test_report_vm.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,11 @@ def test_report_vm_no_kickstartable(
221221
host_os,
222222
test_file
223223
):
224+
"""
225+
Test for a virtual machine with its kickstartable attribute
226+
set to false, the bootorder excludes the network interfaces
227+
"""
228+
224229
expect_output = Path(test_file(f'report/vm_config_no_kickstart_{host_os}.txt')).read_text()
225230

226231
conf = host.run('stack report vm vm-backend-0-3')
@@ -249,6 +254,10 @@ def test_report_vm_custom_template(
249254
host_os,
250255
test_file
251256
):
257+
"""
258+
Test report vm uses the custom jinja2 template set as an attribute
259+
"""
260+
252261
expect_output = Path(test_file(f'report/vm_config_simple_{host_os}.txt')).read_text()
253262
custom_config = Path(test_file(f'report/vm_config_custom_template.j2'))
254263

@@ -277,6 +286,11 @@ def test_report_vm_bad_template(
277286
host_os,
278287
test_file
279288
):
289+
"""
290+
Test report vm raises an error for a template
291+
missing required jinja2 variables
292+
"""
293+
280294
custom_config = Path(test_file(f'report/vm_config_bad_template.j2'))
281295

282296
set_template_attr = host.run(f'stack set host attr vm-backend-0-3 attr=vm_config_location value={custom_config}')
@@ -285,3 +299,25 @@ def test_report_vm_bad_template(
285299
conf = host.run('stack report vm vm-backend-0-3 bare=y')
286300
assert conf.rc != 0
287301

302+
assert "Missing required template variables" in conf.stderr
303+
304+
def test_report_vm_no_template(
305+
self,
306+
add_hypervisor,
307+
add_vm_multiple,
308+
host,
309+
host_os,
310+
test_file
311+
):
312+
"""
313+
Test an error is raised when the custom template file cannot be found
314+
"""
315+
316+
set_template_attr = host.run(f'stack set host attr vm-backend-0-3 attr=vm_config_location value=/export/fake_template.j2')
317+
assert set_template_attr.rc == 0
318+
319+
conf = host.run('stack report vm vm-backend-0-3 bare=y')
320+
assert conf.rc != 0
321+
322+
assert "Unable to find template file" in conf.stderr
323+

0 commit comments

Comments
 (0)