Merge ~smoser/cloud-init:bug/fix-centos6-unittests into cloud-init:master

Proposed by Scott Moser on 2017-04-26
Status: Merged
Merge reported by: Scott Moser
Merged at revision: not available
Proposed branch: ~smoser/cloud-init:bug/fix-centos6-unittests
Merge into: cloud-init:master
Diff against target: 135 lines (+51/-27)
4 files modified
cloudinit/config/cc_apt_configure.py (+12/-7)
tests/unittests/test_handler/test_handler_apt_configure_sources_list_v3.py (+36/-17)
tests/unittests/test_handler/test_handler_yum_add_repo.py (+2/-2)
tox.ini (+1/-1)
Reviewer Review Type Date Requested Status
Ryan Harper 2017-04-26 Needs Fixing on 2017-04-26
Server Team CI bot continuous-integration Needs Fixing on 2017-04-26
Review via email: mp+323265@code.launchpad.net

Commit Message

unittests: fix unittests run on centos.

Some unit tests were broken when running on centos.
This fixes the unit test, with a small re-work of apt_configure.

Also in 'tox -e centos6' only run nose on tests/unittests.

To post a comment you must log in.
Ryan Harper (raharper) wrote :

We should add a testpath with distro=Ubuntu but system_is_snappy = True.

review: Needs Fixing
Scott Moser (smoser) wrote :

well, marked this as merged, which it kind of was as it went into
https://code.launchpad.net/~powersj/cloud-init/+git/cloud-init/+merge/323351

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/cloudinit/config/cc_apt_configure.py b/cloudinit/config/cc_apt_configure.py
2index 7e75177..78c4e06 100644
3--- a/cloudinit/config/cc_apt_configure.py
4+++ b/cloudinit/config/cc_apt_configure.py
5@@ -282,16 +282,21 @@ def handle(name, ocfg, cloud, log, _):
6 apply_apt(cfg, cloud, target)
7
8
9+def _should_configure_on_empty_apt():
10+ # if no config was provided, should apt configuration be done?
11+ if util.system_is_snappy():
12+ return False, "system is snappy."
13+ if not (util.which('apt-get') or util.which('apt')):
14+ return False, "no apt commands."
15+ return True, "Apt is available."
16+
17+
18 def apply_apt(cfg, cloud, target):
19 # cfg is the 'apt' top level dictionary already in 'v3' format.
20 if not cfg:
21- # no config was provided. If apt configuration does not seem
22- # necessary on this system, then return.
23- if util.system_is_snappy():
24- LOG.debug("Nothing to do: No apt config and running on snappy")
25- return
26- if not (util.which('apt-get') or util.which('apt')):
27- LOG.debug("Nothing to do: No apt config and no apt commands")
28+ should_config, msg = _should_configure_on_empty_apt()
29+ if not should_config:
30+ LOG.debug("Nothing to do: No apt config and %s", msg)
31 return
32
33 LOG.debug("handling apt config: %s", cfg)
34diff --git a/tests/unittests/test_handler/test_handler_apt_configure_sources_list_v3.py b/tests/unittests/test_handler/test_handler_apt_configure_sources_list_v3.py
35index 24e4523..a5bd02a 100644
36--- a/tests/unittests/test_handler/test_handler_apt_configure_sources_list_v3.py
37+++ b/tests/unittests/test_handler/test_handler_apt_configure_sources_list_v3.py
38@@ -124,26 +124,45 @@ class TestAptSourceConfigSourceList(t_help.FilesystemMockingTestCase):
39 def _apt_source_list(self, cfg, expected, distro):
40 "_apt_source_list - Test rendering from template (generic)"
41
42+ cfg_on_empty = distro in ('debian', 'ubuntu')
43+ pre="cloudinit.config.cc_apt_configure."
44 # entry at top level now, wrap in 'apt' key
45 cfg = {'apt': cfg}
46 mycloud = self._get_cloud(distro)
47- with mock.patch.object(util, 'write_file') as mockwf:
48- with mock.patch.object(util, 'load_file',
49- return_value=MOCKED_APT_SRC_LIST) as mocklf:
50- with mock.patch.object(os.path, 'isfile',
51- return_value=True) as mockisfile:
52- with mock.patch.object(util, 'rename'):
53- cc_apt_configure.handle("test", cfg, mycloud,
54- LOG, None)
55-
56- # check if it would have loaded the distro template
57- mockisfile.assert_any_call(
58- ('/etc/cloud/templates/sources.list.%s.tmpl' % distro))
59- mocklf.assert_any_call(
60- ('/etc/cloud/templates/sources.list.%s.tmpl' % distro))
61- # check expected content in result
62- mockwf.assert_called_once_with('/etc/apt/sources.list', expected,
63- mode=0o644)
64+ unmocks = []
65+ unmocks.append(mock.patch.object(util, 'write_file'))
66+ mockwf = unmocks[-1].start()
67+ unmocks.append(mock.patch.object(util, 'load_file',
68+ return_value=MOCKED_APT_SRC_LIST))
69+ mocklf = unmocks[-1].start()
70+ unmocks.append(mock.patch.object(os.path, 'isfile', return_value=True))
71+ mockisfile = unmocks[-1].start()
72+ unmocks.append(mock.patch.object(util, 'rename'))
73+ mockrename = unmocks[-1].start()
74+ unmocks.append(mock.patch(pre + '_should_configure_on_empty_apt',
75+ return_value=(cfg_on_empty, "this is for test")))
76+ mockshouldcfg = unmocks[-1].start()
77+
78+ try:
79+ cc_apt_configure.handle("test", cfg, mycloud, LOG, None)
80+ finally:
81+ for u in unmocks:
82+ u.stop()
83+
84+ if not cfg['apt']:
85+ self.assertEqual(1, mockshouldcfg.call_count)
86+
87+ if cfg['apt'] or cfg_on_empty:
88+ # check if it would have loaded the distro template
89+ mockisfile.assert_any_call(
90+ ('/etc/cloud/templates/sources.list.%s.tmpl' % distro))
91+ mocklf.assert_any_call(
92+ ('/etc/cloud/templates/sources.list.%s.tmpl' % distro))
93+ # check expected content in result
94+ mockwf.assert_called_once_with('/etc/apt/sources.list', expected,
95+ mode=0o644)
96+ else:
97+ self.assertEqual(0, mockwf.call_count)
98
99 def test_apt_v3_source_list_debian(self):
100 """test_apt_v3_source_list_debian - without custom sources or parms"""
101diff --git a/tests/unittests/test_handler/test_handler_yum_add_repo.py b/tests/unittests/test_handler/test_handler_yum_add_repo.py
102index 4815bdb..c4396df 100644
103--- a/tests/unittests/test_handler/test_handler_yum_add_repo.py
104+++ b/tests/unittests/test_handler/test_handler_yum_add_repo.py
105@@ -72,7 +72,7 @@ class TestConfig(helpers.FilesystemMockingTestCase):
106 }
107 for section in expected:
108 self.assertTrue(parser.has_section(section),
109- "Contains section {}".format(section))
110+ "Contains section {0}".format(section))
111 for k, v in expected[section].items():
112 self.assertEqual(parser.get(section, k), v)
113
114@@ -109,7 +109,7 @@ class TestConfig(helpers.FilesystemMockingTestCase):
115 }
116 for section in expected:
117 self.assertTrue(parser.has_section(section),
118- "Contains section {}".format(section))
119+ "Contains section {0}".format(section))
120 for k, v in expected[section].items():
121 self.assertEqual(parser.get(section, k), v)
122
123diff --git a/tox.ini b/tox.ini
124index bf9046a..826f554 100644
125--- a/tox.ini
126+++ b/tox.ini
127@@ -70,7 +70,7 @@ deps =
128
129 [testenv:centos6]
130 basepython = python2.6
131-commands = nosetests {posargs:tests}
132+commands = nosetests {posargs:tests/unittests}
133 deps =
134 # requirements
135 argparse==1.2.1

Subscribers

People subscribed via source and target branches

to all changes: