Merge lp:~mabac/linaro-image-tools/bug-888694-fix-sudo-tests into lp:linaro-image-tools/11.11

Proposed by Mattias Backman
Status: Merged
Merged at revision: 459
Proposed branch: lp:~mabac/linaro-image-tools/bug-888694-fix-sudo-tests
Merge into: lp:linaro-image-tools/11.11
Diff against target: 51 lines (+19/-0)
1 file modified
linaro_image_tools/media_create/tests/test_media_create.py (+19/-0)
To merge this branch: bzr merge lp:~mabac/linaro-image-tools/bug-888694-fix-sudo-tests
Reviewer Review Type Date Requested Status
Guilherme Salgado (community) Approve
Review via email: mp+82155@code.launchpad.net

Description of the change

Hi,

This branch fixes the problem with the update_interfaces_* tests executing commands using sudo.

The fix is mocking write_data_to_protected_file() to do the same thing just no using sudo. That works since in the tests we're only creating the interfaces file in /tmp and not in a directory owned by root. I intentionally use the mock even for the test where no change will be made to make sure that messing up the update_interfaces function won't use sudo.

Thanks,

Mattias

To post a comment you must log in.
Revision history for this message
Guilherme Salgado (salgado) wrote :

Thanks for fixing those tests, Mattias.

 review approve

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'linaro_image_tools/media_create/tests/test_media_create.py'
2--- linaro_image_tools/media_create/tests/test_media_create.py 2011-10-19 13:59:30 +0000
3+++ linaro_image_tools/media_create/tests/test_media_create.py 2011-11-14 14:51:03 +0000
4@@ -2390,7 +2390,17 @@
5 self.assertFalse(
6 has_space_left_for_swap('/', swap_size_in_megs))
7
8+ def mock_write_data_to_protected_file(self, path, data):
9+ # Duplicate of write_data_to_protected_file() but does not sudo.
10+ _, tmpfile = tempfile.mkstemp()
11+ with open(tmpfile, 'w') as fd:
12+ fd.write(data)
13+ cmd_runner.run(['mv', '-f', tmpfile, path], as_root=False).wait()
14+
15 def test_update_interfaces_no_interfaces_no_update(self):
16+ self.useFixture(MockSomethingFixture(
17+ rootfs, 'write_data_to_protected_file',
18+ self.mock_write_data_to_protected_file))
19 tempdir = self.useFixture(CreateTempDirFixture()).get_temp_dir()
20 os.makedirs(os.path.join(tempdir, 'etc', 'network'))
21 if_path = os.path.join(tempdir, 'etc', 'network', 'interfaces')
22@@ -2401,6 +2411,9 @@
23 self.assertFalse(os.path.exists(if_path))
24
25 def test_update_interfaces_creates_entry(self):
26+ self.useFixture(MockSomethingFixture(
27+ rootfs, 'write_data_to_protected_file',
28+ self.mock_write_data_to_protected_file))
29 tempdir = self.useFixture(CreateTempDirFixture()).get_temp_dir()
30 os.makedirs(os.path.join(tempdir, 'etc', 'network'))
31 if_path = os.path.join(tempdir, 'etc', 'network', 'interfaces')
32@@ -2413,6 +2426,9 @@
33 self.assertEqual(expected, open(if_path).read())
34
35 def test_update_interfaces_creates_entries(self):
36+ self.useFixture(MockSomethingFixture(
37+ rootfs, 'write_data_to_protected_file',
38+ self.mock_write_data_to_protected_file))
39 tempdir = self.useFixture(CreateTempDirFixture()).get_temp_dir()
40 os.makedirs(os.path.join(tempdir, 'etc', 'network'))
41 if_path = os.path.join(tempdir, 'etc', 'network', 'interfaces')
42@@ -2429,6 +2445,9 @@
43 self.assertIn(expected % {'if': 'wlan0'}, open(if_path).read())
44
45 def test_update_interfaces_leaves_original(self):
46+ self.useFixture(MockSomethingFixture(
47+ rootfs, 'write_data_to_protected_file',
48+ self.mock_write_data_to_protected_file))
49 tempdir = self.useFixture(CreateTempDirFixture()).get_temp_dir()
50 os.makedirs(os.path.join(tempdir, 'etc', 'network'))
51 if_path = os.path.join(tempdir, 'etc', 'network', 'interfaces')

Subscribers

People subscribed via source and target branches