Skip to content

Commit 321d3b7

Browse files
committed
tests/test_vagrant.py: Don't use shell to run vagrant
There's no need to have an overhead of a shell and it's often considered bad practice, so convert all subprocess calls to not use a shell. Signed-off-by: Arnaud Patard <apatard@hupstream.com>
1 parent c7db442 commit 321d3b7

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

tests/test_vagrant.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,16 @@ def fixture_test_dir() -> Generator[str, None, None]:
7373
sys.stderr.write("test temp dir: {}\n".format(my_dir))
7474
boxes = list_box_names()
7575
if TEST_BOX_NAME not in boxes:
76-
cmd = f"vagrant box add --provider {TEST_PROVIDER} {TEST_BOX_URL}"
77-
subprocess.check_call(cmd, shell=True)
76+
cmd = ["vagrant", "box", "add", "--provider", TEST_PROVIDER, TEST_BOX_URL]
77+
subprocess.check_call(cmd)
7878

7979
yield my_dir
8080
# Removes the directory created initially, runs once after the last test
8181
sys.stderr.write("module teardown()\n")
8282
if my_dir is not None:
8383
try:
84-
subprocess.check_call("vagrant destroy -f", cwd=my_dir, shell=True)
84+
cmd = ["vagrant", "destroy", "-f"]
85+
subprocess.check_call(cmd, cwd=my_dir)
8586
except subprocess.CalledProcessError:
8687
pass
8788

@@ -95,7 +96,7 @@ def list_box_names():
9596
even if the `Vagrant.box_list()` implementation is broken.
9697
"""
9798
listing = compat.decode(
98-
subprocess.check_output("vagrant box list --machine-readable", shell=True)
99+
subprocess.check_output(["vagrant", "box", "list", "--machine-readable"])
99100
)
100101
box_names = []
101102
for line in listing.splitlines():
@@ -128,7 +129,7 @@ def fixture_vm_dir(request: FixtureRequest, test_dir) -> Generator[str, None, No
128129
# It is not an error if a VM has already been destroyed.
129130
try:
130131
# Try to destroy any vagrant box that might be running.
131-
subprocess.check_call("vagrant destroy -f", cwd=test_dir, shell=True)
132+
subprocess.check_call(["vagrant", "destroy", "-f"], cwd=test_dir)
132133
except subprocess.CalledProcessError:
133134
pass
134135
finally:
@@ -245,20 +246,20 @@ def test_vm_status(vm_dir):
245246
assert (
246247
v.NOT_CREATED == v.status()[0].state
247248
), "Before going up status should be vagrant.NOT_CREATED"
248-
command = "vagrant up"
249-
subprocess.check_call(command, cwd=vm_dir, shell=True)
249+
command = ["vagrant", "up"]
250+
subprocess.check_call(command, cwd=vm_dir)
250251
assert (
251252
v.RUNNING in v.status()[0].state
252253
), "After going up status should be vagrant.RUNNING"
253254

254-
command = "vagrant halt"
255-
subprocess.check_call(command, cwd=vm_dir, shell=True)
255+
command = ["vagrant", "halt"]
256+
subprocess.check_call(command, cwd=vm_dir)
256257
assert (
257258
v.POWEROFF in v.status()[0].state
258259
), "After halting status should be vagrant.POWEROFF"
259260

260-
command = "vagrant destroy -f"
261-
subprocess.check_call(command, cwd=vm_dir, shell=True)
261+
command = ["vagrant", "destroy", "-f"]
262+
subprocess.check_call(command, cwd=vm_dir)
262263
assert (
263264
v.NOT_CREATED in v.status()[0].state
264265
), "After destroying status should be vagrant.NOT_CREATED"
@@ -296,8 +297,8 @@ def test_vm_config(vm_dir):
296297
"""
297298
v = vagrant.Vagrant(vm_dir)
298299
v.up()
299-
command = "vagrant ssh-config"
300-
ssh_config = compat.decode(subprocess.check_output(command, cwd=vm_dir, shell=True))
300+
command = ["vagrant", "ssh-config"]
301+
ssh_config = compat.decode(subprocess.check_output(command, cwd=vm_dir))
301302
parsed_config = dict(
302303
line.strip().split(None, 1)
303304
for line in ssh_config.splitlines()
@@ -536,8 +537,8 @@ def test_multivm_config(vm_dir):
536537
"""
537538
v = vagrant.Vagrant(vm_dir, quiet_stdout=False, quiet_stderr=False)
538539
v.up(vm_name=VM_1)
539-
command = "vagrant ssh-config " + VM_1
540-
ssh_config = compat.decode(subprocess.check_output(command, cwd=vm_dir, shell=True))
540+
command = ["vagrant", "ssh-config", VM_1]
541+
ssh_config = compat.decode(subprocess.check_output(command, cwd=vm_dir))
541542
parsed_config = dict(
542543
line.strip().split(None, 1)
543544
for line in ssh_config.splitlines()

0 commit comments

Comments
 (0)