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
=== modified file 'cloudinit/distros/__init__.py'
--- cloudinit/distros/__init__.py 2012-11-13 21:47:30 +0000
+++ cloudinit/distros/__init__.py 2012-11-15 20:34:21 +0000
@@ -24,7 +24,6 @@
24from StringIO import StringIO24from StringIO import StringIO
2525
26import abc26import abc
27import collections
28import itertools27import itertools
29import os28import os
30import re29import re
@@ -421,11 +420,14 @@
421 '',420 '',
422 "# User rules for %s" % user,421 "# User rules for %s" % user,
423 ]422 ]
424 if isinstance(rules, collections.Iterable):423 if isinstance(rules, (list, tuple)):
425 for rule in rules:424 for rule in rules:
426 lines.append("%s %s" % (user, rule))425 lines.append("%s %s" % (user, rule))
427 else:426 elif isinstance(rules, (basestring, str)):
428 lines.append("%s %s" % (user, rules))427 lines.append("%s %s" % (user, rules))
428 else:
429 msg = "Can not create sudoers rule addition with type %r"
430 raise TypeError(msg % (util.obj_name(rules)))
429 content = "\n".join(lines)431 content = "\n".join(lines)
430432
431 self.ensure_sudo_dir(os.path.dirname(sudo_file))433 self.ensure_sudo_dir(os.path.dirname(sudo_file))
432434
=== modified file 'tests/unittests/test_distros/test_generic.py'
--- tests/unittests/test_distros/test_generic.py 2012-11-10 18:15:16 +0000
+++ tests/unittests/test_distros/test_generic.py 2012-11-15 20:34:21 +0000
@@ -55,6 +55,59 @@
55 # Make a temp directoy for tests to use.55 # Make a temp directoy for tests to use.
56 self.tmp = self.makeDir()56 self.tmp = self.makeDir()
5757
58 def _write_load_sudoers(self, user, rules):
59 cls = distros.fetch("ubuntu")
60 d = cls("ubuntu", {}, None)
61 os.makedirs(os.path.join(self.tmp, "etc"))
62 os.makedirs(os.path.join(self.tmp, "etc", 'sudoers.d'))
63 self.patchOS(self.tmp)
64 self.patchUtils(self.tmp)
65 d.write_sudo_rules("harlowja", rules)
66 contents = util.load_file(d.ci_sudoers_fn)
67 self.restore()
68 return contents
69
70 def _count_in(self, lines_look_for, text_content):
71 found_amount = 0
72 for e in lines_look_for:
73 for line in text_content.splitlines():
74 line = line.strip()
75 if line == e:
76 found_amount += 1
77 return found_amount
78
79 def test_sudoers_ensure_rules(self):
80 rules = 'ALL=(ALL:ALL) ALL'
81 contents = self._write_load_sudoers('harlowja', rules)
82 expected = ['harlowja ALL=(ALL:ALL) ALL']
83 self.assertEquals(len(expected), self._count_in(expected, contents))
84 not_expected = [
85 'harlowja A',
86 'harlowja L',
87 'harlowja L',
88 ]
89 self.assertEquals(0, self._count_in(not_expected, contents))
90
91 def test_sudoers_ensure_rules_list(self):
92 rules = [
93 'ALL=(ALL:ALL) ALL',
94 'B-ALL=(ALL:ALL) ALL',
95 'C-ALL=(ALL:ALL) ALL',
96 ]
97 contents = self._write_load_sudoers('harlowja', rules)
98 expected = [
99 'harlowja ALL=(ALL:ALL) ALL',
100 'harlowja B-ALL=(ALL:ALL) ALL',
101 'harlowja C-ALL=(ALL:ALL) ALL',
102 ]
103 self.assertEquals(len(expected), self._count_in(expected, contents))
104 not_expected = [
105 'harlowja A',
106 'harlowja L',
107 'harlowja L',
108 ]
109 self.assertEquals(0, self._count_in(not_expected, contents))
110
58 def test_sudoers_ensure_new(self):111 def test_sudoers_ensure_new(self):
59 cls = distros.fetch("ubuntu")112 cls = distros.fetch("ubuntu")
60 d = cls("ubuntu", {}, None)113 d = cls("ubuntu", {}, None)