Merge lp:~raharper/curtin/trunk.fix-trusty-unittest into lp:~curtin-dev/curtin/trunk

Proposed by Ryan Harper
Status: Merged
Merged at revision: 425
Proposed branch: lp:~raharper/curtin/trunk.fix-trusty-unittest
Merge into: lp:~curtin-dev/curtin/trunk
Diff against target: 142 lines (+79/-13)
3 files modified
tests/unittests/helpers.py (+41/-0)
tests/unittests/test_block.py (+16/-10)
tox.ini (+22/-3)
To merge this branch: bzr merge lp:~raharper/curtin/trunk.fix-trusty-unittest
Reviewer Review Type Date Requested Status
Server Team CI bot continuous-integration Approve
curtin developers Pending
Review via email: mp+305990@code.launchpad.net

Commit message

unittest,tox.ini: catch and fix issue with trusty-level mock of open

unittest:
  - Mock out open calls via builtin
  - Handle module name difference between python2.7 and 3.0 via try-except
  - Split wipe_zero and wipe_random into separate tests

tox.ini:
  - add a 'trusty-py3' and 'trusty-py2' tox environment with more
    specific versions as to what is on trusty.

Description of the change

unittest,tox.ini: catch and fix issue with trusty-level mock of open

unittest:
  - Mock out open calls via builtin
  - Handle module name difference between python2.7 and 3.0 via try-except
  - Split wipe_zero and wipe_random into separate tests

tox.ini:
  - add a 'trusty-py3' and 'trusty-py2' tox environment with more
    specific versions as to what is on trusty.

To post a comment you must log in.
Revision history for this message
Server Team CI bot (server-team-bot) wrote :
review: Approve (continuous-integration)
428. By Ryan Harper

unittest,tox.ini: use a mocked_open context manager, add missing tox env

429. By Ryan Harper

don't forget to add the helper

Revision history for this message
Server Team CI bot (server-team-bot) wrote :
review: Approve (continuous-integration)
Revision history for this message
Ryan Harper (raharper) wrote :

Passes vmtest on diglett (though there were Yakkety failures due to cloud-init bug).

Revision history for this message
Ryan Harper (raharper) wrote :

Re-ran after updating cloud-images, all pass now.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added file 'tests/unittests/helpers.py'
2--- tests/unittests/helpers.py 1970-01-01 00:00:00 +0000
3+++ tests/unittests/helpers.py 2016-09-16 18:55:36 +0000
4@@ -0,0 +1,41 @@
5+# Copyright (C) 2016 Canonical Ltd.
6+#
7+# Author: Scott Moser <scott.moser@canonical.com>
8+#
9+# Curtin is free software: you can redistribute it and/or modify it under
10+# the terms of the GNU Affero General Public License as published by the
11+# Free Software Foundation, either version 3 of the License, or (at your
12+# option) any later version.
13+#
14+# Curtin is distributed in the hope that it will be useful, but WITHOUT ANY
15+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16+# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
17+# more details.
18+#
19+# You should have received a copy of the GNU Affero General Public License
20+# along with Curtin. If not, see <http://www.gnu.org/licenses/>.
21+import mock
22+
23+
24+class mocked_open(object):
25+ # older versions of mock can't really mock the builtin 'open' easily.
26+ def __init__(self):
27+ self.mocked = None
28+
29+ def __enter__(self):
30+ if self.mocked:
31+ return self.mocked.start()
32+
33+ py2_p = '__builtin__.open'
34+ py3_p = 'builtins.open'
35+ try:
36+ self.mocked = mock.patch(py2_p, new_callable=mock.mock_open())
37+ return self.mocked.start()
38+ except ImportError:
39+ self.mocked = mock.patch(py3_p, new_callable=mock.mock_open())
40+ return self.mocked.start()
41+
42+ def __exit__(self, etype, value, trace):
43+ if self.mocked:
44+ self.mocked.stop()
45+ self.mocked = None
46
47=== modified file 'tests/unittests/test_block.py'
48--- tests/unittests/test_block.py 2016-08-30 19:21:06 +0000
49+++ tests/unittests/test_block.py 2016-09-16 18:55:36 +0000
50@@ -7,6 +7,7 @@
51
52 from collections import OrderedDict
53
54+from .helpers import mocked_open
55 from curtin import util
56 from curtin import block
57
58@@ -271,16 +272,21 @@
59 block.wipe_volume(self.dev, mode='superblock-recursive')
60 mock_quick_zero.assert_called_with(self.dev, partitions=True)
61
62- @mock.patch('curtin.block.open')
63- @mock.patch('curtin.block.wipe_file')
64- def test_wipe_zero_random(self, mock_wipe_file, mock_open):
65- block.wipe_volume(self.dev, mode='zero')
66- mock_wipe_file.assert_called_with(self.dev)
67- mock_open.return_value = mock.MagicMock()
68- block.wipe_volume(self.dev, mode='random')
69- mock_open.assert_called_with('/dev/urandom', 'rb')
70- mock_wipe_file.assert_called_with(
71- self.dev, reader=mock_open.return_value.__enter__().read)
72+ @mock.patch('curtin.block.wipe_file')
73+ def test_wipe_zero(self, mock_wipe_file):
74+ with mocked_open() as mock_open:
75+ block.wipe_volume(self.dev, mode='zero')
76+ mock_wipe_file.assert_called_with(self.dev)
77+ mock_open.return_value = mock.MagicMock()
78+
79+ @mock.patch('curtin.block.wipe_file')
80+ def test_wipe_random(self, mock_wipe_file):
81+ with mocked_open() as mock_open:
82+ mock_open.return_value = mock.MagicMock()
83+ block.wipe_volume(self.dev, mode='random')
84+ mock_open.assert_called_with('/dev/urandom', 'rb')
85+ mock_wipe_file.assert_called_with(
86+ self.dev, reader=mock_open.return_value.__enter__().read)
87
88 def test_bad_input(self):
89 with self.assertRaises(ValueError):
90
91=== modified file 'tox.ini'
92--- tox.ini 2016-08-18 16:02:27 +0000
93+++ tox.ini 2016-09-16 18:55:36 +0000
94@@ -1,7 +1,7 @@
95 [tox]
96 minversion = 1.6
97 skipsdist = True
98-envlist = py27, py3, py3-flake8, py3-pylint, py27-pylint, trusty-check
99+envlist = py27, py3, py3-flake8, py3-pylint, py27-pylint, trusty-check, trusty-py27, trusty-py3
100
101 [tox:jenkins]
102 downloadcache = ~/cache/pip
103@@ -61,18 +61,37 @@
104 commands =
105 sphinx-build -b html -d doc/_build/doctrees doc/ doc/_build/html
106
107-[testenv:trusty-check]
108+[testenv:trusty]
109 # this environment provides roughly a trusty build environment where
110 # where 'make check' is run during package build. This protects against
111 # package build errors on trusty where pep8 and pyflakes there have subtly
112 # different behavior. Note, we do only run pyflakes3, though.
113-basepython = python3
114 deps = pyflakes==0.8.1
115 pep8==1.4.6
116+ mock==1.0.1
117+ nose==1.3.1
118+ pyyaml==3.10
119+ oauthlib==0.6.1
120+
121+[testenv:trusty-check]
122+deps = {[testenv:trusty]deps}
123+basepython = python3
124 commands =
125 {toxinidir}/tools/run-pyflakes3 {posargs}
126 {toxinidir}/tools/run-pep8 {posargs}
127
128+[testenv:trusty-py27]
129+deps = {[testenv:trusty]deps}
130+basepython = python2.7
131+commands = {envpython} {toxinidir}/tools/noproxy nosetests \
132+ {posargs:tests/unittests}
133+
134+[testenv:trusty-py3]
135+deps = {[testenv:trusty]deps}
136+basepython = python3
137+commands = {envpython} {toxinidir}/tools/noproxy nosetests \
138+ {posargs:tests/unittests}
139+
140 [flake8]
141 builtins = _
142 exclude = .venv,.bzr,.tox,dist,doc,*lib/python*,*egg,build

Subscribers

People subscribed via source and target branches