Merge ~powersj/cloud-init:cii-strings into cloud-init:master

Proposed by Joshua Powers
Status: Merged
Merged at revision: 1ac4bc2a4758d330bb94cd1b2391121cf461ff6a
Proposed branch: ~powersj/cloud-init:cii-strings
Merge into: cloud-init:master
Diff against target: 135 lines (+22/-13)
4 files modified
tests/cloud_tests/bddeb.py (+1/-2)
tests/cloud_tests/instances/base.py (+6/-4)
tests/cloud_tests/instances/lxd.py (+9/-1)
tests/cloud_tests/setup_image.py (+6/-6)
Reviewer Review Type Date Requested Status
Server Team CI bot continuous-integration Approve
cloud-init Commiters Pending
Review via email: mp+330535@code.launchpad.net

Commit message

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.

To post a comment you must log in.
Revision history for this message
Server Team CI bot (server-team-bot) wrote :

PASSED: Continuous integration, rev:2f2717cc364a7ab2d7ff129f5b862643ab24af3f
https://jenkins.ubuntu.com/server/job/cloud-init-ci/283/
Executed test runs:
    SUCCESS: Checkout
    SUCCESS: Unit & Style Tests
    SUCCESS: Ubuntu LTS: Build
    SUCCESS: Ubuntu LTS: Integration
    SUCCESS: MAAS Compatability Testing
    IN_PROGRESS: Declarative: Post Actions

Click here to trigger a rebuild:
https://jenkins.ubuntu.com/server/job/cloud-init-ci/283/rebuild

review: Approve (continuous-integration)
Revision history for this message
Scott Moser (smoser) wrote :

I'd like 2 things changed
a.) do not use /bin/bash or require it by (Instance/Image).execute.
   That is not python's default shell, unless /bin/sh == /bin/bash on the system.
b.) lets only fix strings where we were already using sh (via sh -c).

throwing stuff to sh just allows for errors and unexpected behavior.

I realize this is a contrived example, but its the kind of unexpected results you can get when you hand things off to shell interpretation and are not careful about quoting

## pretend tmp=$(mktemp -d) happens to return the literal name '/tmp/foo?'
## simulate this behavior by the 'tmp=; mkdir -p'
$ touch /tmp/foo1 /tmp/foo2
$ tmp=/tmp/foo?; mkdir -p "/tmp/foo?"
# ls /tmp/
foo1 foo2 foo?
# rm -Rvf $tmp
removed `/tmp/foo1'
removed `/tmp/foo2'
removed directory: `/tmp/foo?'

## YIKES!

I'll give you a diff or a MP with the changes i'd like.

Revision history for this message
Scott Moser (smoser) wrote :

Note above, 'mktemp -d' is probably not going to give you 'foo?'. However, nothing that i'm aware of promises that it's returned filenames will not have any characters that may be "special" to /bin/sh (or /usr/bin/perl or /usr/bin/python or gcc).

Revision history for this message
Joshua Powers (powersj) :
Revision history for this message
Server Team CI bot (server-team-bot) wrote :

PASSED: Continuous integration, rev:2be9c0f1418b76b2a65198f35588e3f24d7b5f89
https://jenkins.ubuntu.com/server/job/cloud-init-ci/287/
Executed test runs:
    SUCCESS: Checkout
    SUCCESS: Unit & Style Tests
    SUCCESS: Ubuntu LTS: Build
    SUCCESS: Ubuntu LTS: Integration
    SUCCESS: MAAS Compatability Testing
    IN_PROGRESS: Declarative: Post Actions

Click here to trigger a rebuild:
https://jenkins.ubuntu.com/server/job/cloud-init-ci/287/rebuild

review: Approve (continuous-integration)

There was an error fetching revisions from git servers. Please try again in a few minutes. If the problem persists, contact Launchpad support.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/tests/cloud_tests/bddeb.py b/tests/cloud_tests/bddeb.py
2index fe80535..fba8a0c 100644
3--- a/tests/cloud_tests/bddeb.py
4+++ b/tests/cloud_tests/bddeb.py
5@@ -28,8 +28,7 @@ def build_deb(args, instance):
6 # update remote system package list and install build deps
7 LOG.debug('installing pre-reqs')
8 pkgs = ' '.join(pre_reqs)
9- cmd = 'apt-get update && apt-get install --yes {}'.format(pkgs)
10- instance.execute(['/bin/sh', '-c', cmd])
11+ instance.execute('apt-get update && apt-get install --yes {}'.format(pkgs))
12
13 # local tmpfile that must be deleted
14 local_tarball = tempfile.NamedTemporaryFile().name
15diff --git a/tests/cloud_tests/instances/base.py b/tests/cloud_tests/instances/base.py
16index 959e9cc..58f45b1 100644
17--- a/tests/cloud_tests/instances/base.py
18+++ b/tests/cloud_tests/instances/base.py
19@@ -23,7 +23,7 @@ class Instance(object):
20 self.config = config
21 self.features = features
22
23- def execute(self, command, stdout=None, stderr=None, env={},
24+ def execute(self, command, stdout=None, stderr=None, env=None,
25 rcs=None, description=None):
26 """Execute command in instance, recording output, error and exit code.
27
28@@ -31,6 +31,8 @@ class Instance(object):
29 target filesystem being available at /.
30
31 @param command: the command to execute as root inside the image
32+ if command is a string, then it will be executed as:
33+ ['sh', '-c', command]
34 @param stdout, stderr: file handles to write output and error to
35 @param env: environment variables
36 @param rcs: allowed return codes from command
37@@ -137,9 +139,9 @@ class Instance(object):
38 tests.append(self.config['cloud_init_ready_script'])
39
40 formatted_tests = ' && '.join(clean_test(t) for t in tests)
41- test_cmd = ('for ((i=0;i<{time};i++)); do {test} && exit 0; sleep 1; '
42- 'done; exit 1;').format(time=time, test=formatted_tests)
43- cmd = ['/bin/bash', '-c', test_cmd]
44+ cmd = ('i=0; while [ $i -lt {time} ] && i=$(($i+1)); do {test} && '
45+ 'exit 0; sleep 1; done; exit 1').format(time=time,
46+ test=formatted_tests)
47
48 if self.execute(cmd, rcs=(0, 1))[-1] != 0:
49 raise OSError('timeout: after {}s system not started'.format(time))
50diff --git a/tests/cloud_tests/instances/lxd.py b/tests/cloud_tests/instances/lxd.py
51index b9c2cc6..a43918c 100644
52--- a/tests/cloud_tests/instances/lxd.py
53+++ b/tests/cloud_tests/instances/lxd.py
54@@ -31,7 +31,7 @@ class LXDInstance(base.Instance):
55 self._pylxd_container.sync()
56 return self._pylxd_container
57
58- def execute(self, command, stdout=None, stderr=None, env={},
59+ def execute(self, command, stdout=None, stderr=None, env=None,
60 rcs=None, description=None):
61 """Execute command in instance, recording output, error and exit code.
62
63@@ -39,6 +39,8 @@ class LXDInstance(base.Instance):
64 target filesystem being available at /.
65
66 @param command: the command to execute as root inside the image
67+ if command is a string, then it will be executed as:
68+ ['sh', '-c', command]
69 @param stdout: file handler to write output
70 @param stderr: file handler to write error
71 @param env: environment variables
72@@ -46,6 +48,12 @@ class LXDInstance(base.Instance):
73 @param description: purpose of command
74 @return_value: tuple containing stdout data, stderr data, exit code
75 """
76+ if env is None:
77+ env = {}
78+
79+ if isinstance(command, str):
80+ command = ['sh', '-c', command]
81+
82 # ensure instance is running and execute the command
83 self.start()
84 res = self.pylxd_container.execute(command, environment=env)
85diff --git a/tests/cloud_tests/setup_image.py b/tests/cloud_tests/setup_image.py
86index 8053a09..3c0fff6 100644
87--- a/tests/cloud_tests/setup_image.py
88+++ b/tests/cloud_tests/setup_image.py
89@@ -49,8 +49,8 @@ def install_deb(args, image):
90 LOG.debug(msg)
91 remote_path = os.path.join('/tmp', os.path.basename(args.deb))
92 image.push_file(args.deb, remote_path)
93- cmd = 'dpkg -i {} || apt-get install --yes -f'.format(remote_path)
94- image.execute(['/bin/sh', '-c', cmd], description=msg)
95+ cmd = 'dpkg -i {}; apt-get install --yes -f'.format(remote_path)
96+ image.execute(cmd, description=msg)
97
98 # check installed deb version matches package
99 fmt = ['-W', "--showformat='${Version}'"]
100@@ -113,7 +113,7 @@ def upgrade(args, image):
101
102 msg = 'upgrading cloud-init'
103 LOG.debug(msg)
104- image.execute(['/bin/sh', '-c', cmd], description=msg)
105+ image.execute(cmd, description=msg)
106
107
108 def upgrade_full(args, image):
109@@ -134,7 +134,7 @@ def upgrade_full(args, image):
110
111 msg = 'full system upgrade'
112 LOG.debug(msg)
113- image.execute(['/bin/sh', '-c', cmd], description=msg)
114+ image.execute(cmd, description=msg)
115
116
117 def run_script(args, image):
118@@ -165,7 +165,7 @@ def enable_ppa(args, image):
119 msg = 'enable ppa: "{}" in target'.format(ppa)
120 LOG.debug(msg)
121 cmd = 'add-apt-repository --yes {} && apt-get update'.format(ppa)
122- image.execute(['/bin/sh', '-c', cmd], description=msg)
123+ image.execute(cmd, description=msg)
124
125
126 def enable_repo(args, image):
127@@ -188,7 +188,7 @@ def enable_repo(args, image):
128
129 msg = 'enable repo: "{}" in target'.format(args.repo)
130 LOG.debug(msg)
131- image.execute(['/bin/sh', '-c', cmd], description=msg)
132+ image.execute(cmd, description=msg)
133
134
135 def setup_image(args, image):

Subscribers

People subscribed via source and target branches