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
=== added file 'tests/unittests/helpers.py'
--- tests/unittests/helpers.py 1970-01-01 00:00:00 +0000
+++ tests/unittests/helpers.py 2016-09-16 18:55:36 +0000
@@ -0,0 +1,41 @@
1# Copyright (C) 2016 Canonical Ltd.
2#
3# Author: Scott Moser <scott.moser@canonical.com>
4#
5# Curtin is free software: you can redistribute it and/or modify it under
6# the terms of the GNU Affero General Public License as published by the
7# Free Software Foundation, either version 3 of the License, or (at your
8# option) any later version.
9#
10# Curtin is distributed in the hope that it will be useful, but WITHOUT ANY
11# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
13# more details.
14#
15# You should have received a copy of the GNU Affero General Public License
16# along with Curtin. If not, see <http://www.gnu.org/licenses/>.
17import mock
18
19
20class mocked_open(object):
21 # older versions of mock can't really mock the builtin 'open' easily.
22 def __init__(self):
23 self.mocked = None
24
25 def __enter__(self):
26 if self.mocked:
27 return self.mocked.start()
28
29 py2_p = '__builtin__.open'
30 py3_p = 'builtins.open'
31 try:
32 self.mocked = mock.patch(py2_p, new_callable=mock.mock_open())
33 return self.mocked.start()
34 except ImportError:
35 self.mocked = mock.patch(py3_p, new_callable=mock.mock_open())
36 return self.mocked.start()
37
38 def __exit__(self, etype, value, trace):
39 if self.mocked:
40 self.mocked.stop()
41 self.mocked = None
042
=== modified file 'tests/unittests/test_block.py'
--- tests/unittests/test_block.py 2016-08-30 19:21:06 +0000
+++ tests/unittests/test_block.py 2016-09-16 18:55:36 +0000
@@ -7,6 +7,7 @@
77
8from collections import OrderedDict8from collections import OrderedDict
99
10from .helpers import mocked_open
10from curtin import util11from curtin import util
11from curtin import block12from curtin import block
1213
@@ -271,16 +272,21 @@
271 block.wipe_volume(self.dev, mode='superblock-recursive')272 block.wipe_volume(self.dev, mode='superblock-recursive')
272 mock_quick_zero.assert_called_with(self.dev, partitions=True)273 mock_quick_zero.assert_called_with(self.dev, partitions=True)
273274
274 @mock.patch('curtin.block.open')275 @mock.patch('curtin.block.wipe_file')
275 @mock.patch('curtin.block.wipe_file')276 def test_wipe_zero(self, mock_wipe_file):
276 def test_wipe_zero_random(self, mock_wipe_file, mock_open):277 with mocked_open() as mock_open:
277 block.wipe_volume(self.dev, mode='zero')278 block.wipe_volume(self.dev, mode='zero')
278 mock_wipe_file.assert_called_with(self.dev)279 mock_wipe_file.assert_called_with(self.dev)
279 mock_open.return_value = mock.MagicMock()280 mock_open.return_value = mock.MagicMock()
280 block.wipe_volume(self.dev, mode='random')281
281 mock_open.assert_called_with('/dev/urandom', 'rb')282 @mock.patch('curtin.block.wipe_file')
282 mock_wipe_file.assert_called_with(283 def test_wipe_random(self, mock_wipe_file):
283 self.dev, reader=mock_open.return_value.__enter__().read)284 with mocked_open() as mock_open:
285 mock_open.return_value = mock.MagicMock()
286 block.wipe_volume(self.dev, mode='random')
287 mock_open.assert_called_with('/dev/urandom', 'rb')
288 mock_wipe_file.assert_called_with(
289 self.dev, reader=mock_open.return_value.__enter__().read)
284290
285 def test_bad_input(self):291 def test_bad_input(self):
286 with self.assertRaises(ValueError):292 with self.assertRaises(ValueError):
287293
=== modified file 'tox.ini'
--- tox.ini 2016-08-18 16:02:27 +0000
+++ tox.ini 2016-09-16 18:55:36 +0000
@@ -1,7 +1,7 @@
1[tox]1[tox]
2minversion = 1.62minversion = 1.6
3skipsdist = True3skipsdist = True
4envlist = py27, py3, py3-flake8, py3-pylint, py27-pylint, trusty-check4envlist = py27, py3, py3-flake8, py3-pylint, py27-pylint, trusty-check, trusty-py27, trusty-py3
55
6[tox:jenkins]6[tox:jenkins]
7downloadcache = ~/cache/pip7downloadcache = ~/cache/pip
@@ -61,18 +61,37 @@
61commands =61commands =
62 sphinx-build -b html -d doc/_build/doctrees doc/ doc/_build/html62 sphinx-build -b html -d doc/_build/doctrees doc/ doc/_build/html
6363
64[testenv:trusty-check]64[testenv:trusty]
65# this environment provides roughly a trusty build environment where65# this environment provides roughly a trusty build environment where
66# where 'make check' is run during package build. This protects against66# where 'make check' is run during package build. This protects against
67# package build errors on trusty where pep8 and pyflakes there have subtly67# package build errors on trusty where pep8 and pyflakes there have subtly
68# different behavior. Note, we do only run pyflakes3, though.68# different behavior. Note, we do only run pyflakes3, though.
69basepython = python3
70deps = pyflakes==0.8.169deps = pyflakes==0.8.1
71 pep8==1.4.670 pep8==1.4.6
71 mock==1.0.1
72 nose==1.3.1
73 pyyaml==3.10
74 oauthlib==0.6.1
75
76[testenv:trusty-check]
77deps = {[testenv:trusty]deps}
78basepython = python3
72commands =79commands =
73 {toxinidir}/tools/run-pyflakes3 {posargs}80 {toxinidir}/tools/run-pyflakes3 {posargs}
74 {toxinidir}/tools/run-pep8 {posargs}81 {toxinidir}/tools/run-pep8 {posargs}
7582
83[testenv:trusty-py27]
84deps = {[testenv:trusty]deps}
85basepython = python2.7
86commands = {envpython} {toxinidir}/tools/noproxy nosetests \
87 {posargs:tests/unittests}
88
89[testenv:trusty-py3]
90deps = {[testenv:trusty]deps}
91basepython = python3
92commands = {envpython} {toxinidir}/tools/noproxy nosetests \
93 {posargs:tests/unittests}
94
76[flake8]95[flake8]
77builtins = _96builtins = _
78exclude = .venv,.bzr,.tox,dist,doc,*lib/python*,*egg,build97exclude = .venv,.bzr,.tox,dist,doc,*lib/python*,*egg,build

Subscribers

People subscribed via source and target branches