Merge lp:~harlowja/cloud-init/sudoers-fix into lp:~cloud-init-dev/cloud-init/trunk

Proposed by Joshua Harlow
Status: Merged
Merged at revision: 740
Proposed branch: lp:~harlowja/cloud-init/sudoers-fix
Merge into: lp:~cloud-init-dev/cloud-init/trunk
Diff against target: 92 lines (+58/-3)
2 files modified
cloudinit/distros/__init__.py (+5/-3)
tests/unittests/test_distros/test_generic.py (+53/-0)
To merge this branch: bzr merge lp:~harlowja/cloud-init/sudoers-fix
Reviewer Review Type Date Requested Status
Scott Moser Approve
Review via email: mp+134397@code.launchpad.net

Description of the change

Strings are iterable...

To post a comment you must log in.
lp:~harlowja/cloud-init/sudoers-fix updated
741. By Joshua Harlow

Add a test to make sure this doesn't happen again.

742. By Joshua Harlow

Add the string sudoers rule test case as well.

743. By Joshua Harlow

Cleanup the tests slightly.

744. By Joshua Harlow

Raise a type error when a sudoers rule is not an accepted type.

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

looks good. go ahead and push.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'cloudinit/distros/__init__.py'
2--- cloudinit/distros/__init__.py 2012-11-13 21:47:30 +0000
3+++ cloudinit/distros/__init__.py 2012-11-15 20:34:21 +0000
4@@ -24,7 +24,6 @@
5 from StringIO import StringIO
6
7 import abc
8-import collections
9 import itertools
10 import os
11 import re
12@@ -421,11 +420,14 @@
13 '',
14 "# User rules for %s" % user,
15 ]
16- if isinstance(rules, collections.Iterable):
17+ if isinstance(rules, (list, tuple)):
18 for rule in rules:
19 lines.append("%s %s" % (user, rule))
20- else:
21+ elif isinstance(rules, (basestring, str)):
22 lines.append("%s %s" % (user, rules))
23+ else:
24+ msg = "Can not create sudoers rule addition with type %r"
25+ raise TypeError(msg % (util.obj_name(rules)))
26 content = "\n".join(lines)
27
28 self.ensure_sudo_dir(os.path.dirname(sudo_file))
29
30=== modified file 'tests/unittests/test_distros/test_generic.py'
31--- tests/unittests/test_distros/test_generic.py 2012-11-10 18:15:16 +0000
32+++ tests/unittests/test_distros/test_generic.py 2012-11-15 20:34:21 +0000
33@@ -55,6 +55,59 @@
34 # Make a temp directoy for tests to use.
35 self.tmp = self.makeDir()
36
37+ def _write_load_sudoers(self, user, rules):
38+ cls = distros.fetch("ubuntu")
39+ d = cls("ubuntu", {}, None)
40+ os.makedirs(os.path.join(self.tmp, "etc"))
41+ os.makedirs(os.path.join(self.tmp, "etc", 'sudoers.d'))
42+ self.patchOS(self.tmp)
43+ self.patchUtils(self.tmp)
44+ d.write_sudo_rules("harlowja", rules)
45+ contents = util.load_file(d.ci_sudoers_fn)
46+ self.restore()
47+ return contents
48+
49+ def _count_in(self, lines_look_for, text_content):
50+ found_amount = 0
51+ for e in lines_look_for:
52+ for line in text_content.splitlines():
53+ line = line.strip()
54+ if line == e:
55+ found_amount += 1
56+ return found_amount
57+
58+ def test_sudoers_ensure_rules(self):
59+ rules = 'ALL=(ALL:ALL) ALL'
60+ contents = self._write_load_sudoers('harlowja', rules)
61+ expected = ['harlowja ALL=(ALL:ALL) ALL']
62+ self.assertEquals(len(expected), self._count_in(expected, contents))
63+ not_expected = [
64+ 'harlowja A',
65+ 'harlowja L',
66+ 'harlowja L',
67+ ]
68+ self.assertEquals(0, self._count_in(not_expected, contents))
69+
70+ def test_sudoers_ensure_rules_list(self):
71+ rules = [
72+ 'ALL=(ALL:ALL) ALL',
73+ 'B-ALL=(ALL:ALL) ALL',
74+ 'C-ALL=(ALL:ALL) ALL',
75+ ]
76+ contents = self._write_load_sudoers('harlowja', rules)
77+ expected = [
78+ 'harlowja ALL=(ALL:ALL) ALL',
79+ 'harlowja B-ALL=(ALL:ALL) ALL',
80+ 'harlowja C-ALL=(ALL:ALL) ALL',
81+ ]
82+ self.assertEquals(len(expected), self._count_in(expected, contents))
83+ not_expected = [
84+ 'harlowja A',
85+ 'harlowja L',
86+ 'harlowja L',
87+ ]
88+ self.assertEquals(0, self._count_in(not_expected, contents))
89+
90 def test_sudoers_ensure_new(self):
91 cls = distros.fetch("ubuntu")
92 d = cls("ubuntu", {}, None)