summaryrefslogtreecommitdiff
path: root/tests/cloud_tests/instances
diff options
context:
space:
mode:
authorJoshua Powers <josh.powers@canonical.com>2017-09-11 10:29:19 -0700
committerScott Moser <smoser@brickies.net>2017-09-13 21:16:43 -0400
commit1ac4bc2a4758d330bb94cd1b2391121cf461ff6a (patch)
tree40f2e93a89a7d3b830fecf5b1d6c2208ffaf25d5 /tests/cloud_tests/instances
parented8f1b159174715403cb1ffa200ff6d080770152 (diff)
downloadvyos-cloud-init-1ac4bc2a4758d330bb94cd1b2391121cf461ff6a.tar.gz
vyos-cloud-init-1ac4bc2a4758d330bb94cd1b2391121cf461ff6a.zip
tests: execute: support command as string
If a string is passed to execute, then invoke 'bash', '-c', 'string'. That allows the less verbose execution of simple commands: image.execute("ls /run") compared to the more explicit but longer winded: image.execute(["ls", "/run"]) If 'env' was ever modified in execute or a method that it called, then the next invocation's default value would be changed. Instead use None and then set to a new empty dict in the method.
Diffstat (limited to 'tests/cloud_tests/instances')
-rw-r--r--tests/cloud_tests/instances/base.py10
-rw-r--r--tests/cloud_tests/instances/lxd.py10
2 files changed, 15 insertions, 5 deletions
diff --git a/tests/cloud_tests/instances/base.py b/tests/cloud_tests/instances/base.py
index 959e9cce..58f45b14 100644
--- a/tests/cloud_tests/instances/base.py
+++ b/tests/cloud_tests/instances/base.py
@@ -23,7 +23,7 @@ class Instance(object):
self.config = config
self.features = features
- def execute(self, command, stdout=None, stderr=None, env={},
+ def execute(self, command, stdout=None, stderr=None, env=None,
rcs=None, description=None):
"""Execute command in instance, recording output, error and exit code.
@@ -31,6 +31,8 @@ class Instance(object):
target filesystem being available at /.
@param command: the command to execute as root inside the image
+ if command is a string, then it will be executed as:
+ ['sh', '-c', command]
@param stdout, stderr: file handles to write output and error to
@param env: environment variables
@param rcs: allowed return codes from command
@@ -137,9 +139,9 @@ class Instance(object):
tests.append(self.config['cloud_init_ready_script'])
formatted_tests = ' && '.join(clean_test(t) for t in tests)
- test_cmd = ('for ((i=0;i<{time};i++)); do {test} && exit 0; sleep 1; '
- 'done; exit 1;').format(time=time, test=formatted_tests)
- cmd = ['/bin/bash', '-c', test_cmd]
+ cmd = ('i=0; while [ $i -lt {time} ] && i=$(($i+1)); do {test} && '
+ 'exit 0; sleep 1; done; exit 1').format(time=time,
+ test=formatted_tests)
if self.execute(cmd, rcs=(0, 1))[-1] != 0:
raise OSError('timeout: after {}s system not started'.format(time))
diff --git a/tests/cloud_tests/instances/lxd.py b/tests/cloud_tests/instances/lxd.py
index b9c2cc6b..a43918c2 100644
--- a/tests/cloud_tests/instances/lxd.py
+++ b/tests/cloud_tests/instances/lxd.py
@@ -31,7 +31,7 @@ class LXDInstance(base.Instance):
self._pylxd_container.sync()
return self._pylxd_container
- def execute(self, command, stdout=None, stderr=None, env={},
+ def execute(self, command, stdout=None, stderr=None, env=None,
rcs=None, description=None):
"""Execute command in instance, recording output, error and exit code.
@@ -39,6 +39,8 @@ class LXDInstance(base.Instance):
target filesystem being available at /.
@param command: the command to execute as root inside the image
+ if command is a string, then it will be executed as:
+ ['sh', '-c', command]
@param stdout: file handler to write output
@param stderr: file handler to write error
@param env: environment variables
@@ -46,6 +48,12 @@ class LXDInstance(base.Instance):
@param description: purpose of command
@return_value: tuple containing stdout data, stderr data, exit code
"""
+ if env is None:
+ env = {}
+
+ if isinstance(command, str):
+ command = ['sh', '-c', command]
+
# ensure instance is running and execute the command
self.start()
res = self.pylxd_container.execute(command, environment=env)