Merge lp:~tvansteenburgh/juju-deployer/python3-support into lp:juju-deployer
- python3-support
- Merge into trunk
Proposed by
Tim Van Steenburgh
Status: | Merged |
---|---|
Merged at revision: | 174 |
Proposed branch: | lp:~tvansteenburgh/juju-deployer/python3-support |
Merge into: | lp:juju-deployer |
Diff against target: |
3879 lines (+261/-2524) 43 files modified
.bzrignore (+1/-0) HACKING (+3/-2) Makefile (+1/-9) deployer/action/diff.py (+8/-5) deployer/action/export.py (+1/-0) deployer/action/importer.py (+4/-1) deployer/charm.py (+4/-2) deployer/cli.py (+5/-3) deployer/config.py (+10/-8) deployer/deployment.py (+20/-17) deployer/env/__init__.py (+0/-3) deployer/env/base.py (+2/-1) deployer/env/go.py (+5/-4) deployer/env/mem.py (+5/-3) deployer/env/py.py (+1/-3) deployer/env/watchers.py (+2/-1) deployer/errors.py (+4/-2) deployer/feedback.py (+5/-0) deployer/guiserver.py (+1/-0) deployer/relation.py (+3/-4) deployer/service.py (+14/-10) deployer/tests/base.py (+5/-2) deployer/tests/mock.py (+0/-2367) deployer/tests/test_base.py (+4/-2) deployer/tests/test_charm.py (+2/-1) deployer/tests/test_config.py (+20/-12) deployer/tests/test_constraints.py (+8/-5) deployer/tests/test_deployment.py (+19/-11) deployer/tests/test_diff.py (+8/-5) deployer/tests/test_goenv.py (+2/-1) deployer/tests/test_guienv.py (+1/-0) deployer/tests/test_guiserver.py (+1/-0) deployer/tests/test_importer.py (+10/-7) deployer/tests/test_pyenv.py (+4/-2) deployer/tests/test_service.py (+1/-0) deployer/tests/test_utils.py (+13/-10) deployer/tests/test_watchers.py (+16/-7) deployer/utils.py (+11/-5) deployer/vcs.py (+6/-8) doc/conf.py (+1/-0) setup.py (+6/-1) test-requirements.txt (+4/-0) tox.ini (+20/-0) |
To merge this branch: | bzr merge lp:~tvansteenburgh/juju-deployer/python3-support |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
juju-deployers | Pending | ||
Review via email:
|
Commit message
Description of the change
Python 3 support.
All tests passing on python 2.7 and python 3.5 on Xenial (76% coverage). I successfully deployed a bundle using py2.7 and py3.5 on both juju-1.25.5 and juju-2.0-beta6.
These are the configurations I had readily available. If anyone can test others (trusty, py3.4, and especially juju-2.0-beta7), that would be helpful.
To post a comment you must log in.
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === modified file '.bzrignore' | |||
2 | --- .bzrignore 2015-02-20 19:18:58 +0000 | |||
3 | +++ .bzrignore 2016-05-08 03:36:27 +0000 | |||
4 | @@ -12,3 +12,4 @@ | |||
5 | 12 | tmp | 12 | tmp |
6 | 13 | report/ | 13 | report/ |
7 | 14 | .coverage | 14 | .coverage |
8 | 15 | .tox/ | ||
9 | 15 | 16 | ||
10 | === modified file 'HACKING' | |||
11 | --- HACKING 2016-04-04 18:22:59 +0000 | |||
12 | +++ HACKING 2016-05-08 03:36:27 +0000 | |||
13 | @@ -13,9 +13,10 @@ | |||
14 | 13 | Running unit tests | 13 | Running unit tests |
15 | 14 | ------------------ | 14 | ------------------ |
16 | 15 | 15 | ||
18 | 16 | Tests are compatible with nose and can be run using, e.g.: | 16 | Tests can be run using tox, e.g.: |
19 | 17 | 17 | ||
21 | 18 | $ nosetests -s --verbosity=2 deployer/tests | 18 | $ tox # run tests for all supported python versions |
22 | 19 | $ tox -e py27 # run tests on python2.7 | ||
23 | 19 | 20 | ||
24 | 20 | 21 | ||
25 | 21 | Running live environment tests | 22 | Running live environment tests |
26 | 22 | 23 | ||
27 | === modified file 'Makefile' | |||
28 | --- Makefile 2014-08-26 22:34:07 +0000 | |||
29 | +++ Makefile 2016-05-08 03:36:27 +0000 | |||
30 | @@ -1,10 +1,2 @@ | |||
31 | 1 | test: | 1 | test: |
41 | 2 | nosetests -s --verbosity=2 deployer/tests | 2 | tox |
33 | 3 | |||
34 | 4 | freeze: | ||
35 | 5 | pip install -d tools/dist -r requirements.txt | ||
36 | 6 | |||
37 | 7 | cover: | ||
38 | 8 | pip install coverage nose | ||
39 | 9 | nosetests --cover-html-dir=report --cover-html --with-coverage --cover-package=deployer | ||
40 | 10 | gnome-open report/index.html | ||
42 | 11 | 3 | ||
43 | === modified file 'deployer/action/diff.py' | |||
44 | --- deployer/action/diff.py 2015-01-27 15:14:00 +0000 | |||
45 | +++ deployer/action/diff.py 2016-05-08 03:36:27 +0000 | |||
46 | @@ -1,9 +1,12 @@ | |||
47 | 1 | from __future__ import absolute_import | ||
48 | 2 | from __future__ import print_function | ||
49 | 1 | import logging | 3 | import logging |
50 | 2 | import time | 4 | import time |
51 | 3 | 5 | ||
52 | 4 | from .base import BaseAction | 6 | from .base import BaseAction |
53 | 5 | from ..relation import EndpointPair | 7 | from ..relation import EndpointPair |
54 | 6 | from ..utils import parse_constraints, yaml_dump | 8 | from ..utils import parse_constraints, yaml_dump |
55 | 9 | import six | ||
56 | 7 | 10 | ||
57 | 8 | 11 | ||
58 | 9 | class Diff(BaseAction): | 12 | class Diff(BaseAction): |
59 | @@ -22,7 +25,7 @@ | |||
60 | 22 | """ | 25 | """ |
61 | 23 | rels = set() | 26 | rels = set() |
62 | 24 | for svc_name in self.env_status['services']: | 27 | for svc_name in self.env_status['services']: |
64 | 25 | if not svc_name in self.env_status['services']: | 28 | if svc_name not in self.env_status['services']: |
65 | 26 | self.env_state['services'][svc_name] = 'missing' | 29 | self.env_state['services'][svc_name] = 'missing' |
66 | 27 | self.env_state['services'].setdefault(svc_name, {})[ | 30 | self.env_state['services'].setdefault(svc_name, {})[ |
67 | 28 | 'options'] = self.env.get_config(svc_name) | 31 | 'options'] = self.env.get_config(svc_name) |
68 | @@ -115,7 +118,7 @@ | |||
69 | 115 | self.deployment.get_charm_for(cs)) | 118 | self.deployment.get_charm_for(cs)) |
70 | 116 | if not mod: | 119 | if not mod: |
71 | 117 | continue | 120 | continue |
73 | 118 | if not 'modified' in delta: | 121 | if 'modified' not in delta: |
74 | 119 | delta['modified'] = {} | 122 | delta['modified'] = {} |
75 | 120 | delta['modified'][cs] = mod | 123 | delta['modified'][cs] = mod |
76 | 121 | return delta | 124 | return delta |
77 | @@ -125,7 +128,7 @@ | |||
78 | 125 | d_sc = parse_constraints(d_s.get('constraints', '')) | 128 | d_sc = parse_constraints(d_s.get('constraints', '')) |
79 | 126 | # 'tags' is a special case, as it can be multi-valued: convert to list | 129 | # 'tags' is a special case, as it can be multi-valued: convert to list |
80 | 127 | # if cfg one is a string | 130 | # if cfg one is a string |
82 | 128 | if isinstance(d_sc.get('tags'), basestring): | 131 | if isinstance(d_sc.get('tags'), six.string_types): |
83 | 129 | d_sc['tags'] = [d_sc['tags']] | 132 | d_sc['tags'] = [d_sc['tags']] |
84 | 130 | if d_sc != e_s['constraints']: | 133 | if d_sc != e_s['constraints']: |
85 | 131 | mod['env-constraints'] = e_s['constraints'] | 134 | mod['env-constraints'] = e_s['constraints'] |
86 | @@ -133,7 +136,7 @@ | |||
87 | 133 | for k, v in d_s.get('options', {}).items(): | 136 | for k, v in d_s.get('options', {}).items(): |
88 | 134 | # Deploy options not known to the env may originate | 137 | # Deploy options not known to the env may originate |
89 | 135 | # from charm version delta or be an invalid config. | 138 | # from charm version delta or be an invalid config. |
91 | 136 | if not k in e_s['options']: | 139 | if k not in e_s['options']: |
92 | 137 | continue | 140 | continue |
93 | 138 | e_v = e_s['options'].get(k, {}).get('value') | 141 | e_v = e_s['options'].get(k, {}).get('value') |
94 | 139 | if e_v != v: | 142 | if e_v != v: |
95 | @@ -155,4 +158,4 @@ | |||
96 | 155 | def run(self): | 158 | def run(self): |
97 | 156 | diff = self.do_diff() | 159 | diff = self.do_diff() |
98 | 157 | if diff: | 160 | if diff: |
100 | 158 | print yaml_dump(diff) | 161 | print(yaml_dump(diff)) |
101 | 159 | 162 | ||
102 | === modified file 'deployer/action/export.py' | |||
103 | --- deployer/action/export.py 2013-07-22 15:29:31 +0000 | |||
104 | +++ deployer/action/export.py 2016-05-08 03:36:27 +0000 | |||
105 | @@ -1,3 +1,4 @@ | |||
106 | 1 | from __future__ import absolute_import | ||
107 | 1 | import logging | 2 | import logging |
108 | 2 | 3 | ||
109 | 3 | from .base import BaseAction | 4 | from .base import BaseAction |
110 | 4 | 5 | ||
111 | === modified file 'deployer/action/importer.py' | |||
112 | --- deployer/action/importer.py 2016-05-03 16:03:18 +0000 | |||
113 | +++ deployer/action/importer.py 2016-05-08 03:36:27 +0000 | |||
114 | @@ -1,9 +1,12 @@ | |||
115 | 1 | from __future__ import absolute_import | ||
116 | 1 | import logging | 2 | import logging |
117 | 2 | import time | 3 | import time |
118 | 3 | 4 | ||
119 | 4 | from .base import BaseAction | 5 | from .base import BaseAction |
120 | 5 | from ..env import watchers | 6 | from ..env import watchers |
121 | 6 | from ..utils import ErrorExit | 7 | from ..utils import ErrorExit |
122 | 8 | from six.moves import map | ||
123 | 9 | from six.moves import range | ||
124 | 7 | 10 | ||
125 | 8 | 11 | ||
126 | 9 | class Importer(BaseAction): | 12 | class Importer(BaseAction): |
127 | @@ -65,7 +68,7 @@ | |||
128 | 65 | 68 | ||
129 | 66 | def machine_exists(self, id): | 69 | def machine_exists(self, id): |
130 | 67 | """Checks if the given id exists on the current environment.""" | 70 | """Checks if the given id exists on the current environment.""" |
132 | 68 | return str(id) in map(str, self.env.status().get('machines', {})) | 71 | return str(id) in list(map(str, self.env.status().get('machines', {}))) |
133 | 69 | 72 | ||
134 | 70 | def create_machines(self): | 73 | def create_machines(self): |
135 | 71 | """Create machines as specified in the machine spec in the bundle. | 74 | """Create machines as specified in the machine spec in the bundle. |
136 | 72 | 75 | ||
137 | === modified file 'deployer/charm.py' | |||
138 | --- deployer/charm.py 2016-05-03 16:03:18 +0000 | |||
139 | +++ deployer/charm.py 2016-05-08 03:36:27 +0000 | |||
140 | @@ -1,8 +1,10 @@ | |||
141 | 1 | from __future__ import absolute_import | ||
142 | 1 | import logging | 2 | import logging |
143 | 2 | import os | 3 | import os |
144 | 3 | import urllib2 | ||
145 | 4 | import shutil | 4 | import shutil |
146 | 5 | 5 | ||
147 | 6 | from six.moves.urllib.request import urlopen | ||
148 | 7 | |||
149 | 6 | from .vcs import Git, Bzr | 8 | from .vcs import Git, Bzr |
150 | 7 | from .utils import ( | 9 | from .utils import ( |
151 | 8 | _check_call, | 10 | _check_call, |
152 | @@ -210,7 +212,7 @@ | |||
153 | 210 | 212 | ||
154 | 211 | store_url = "%s/charm/%s" % (STORE_URL, qualified_url[3:]) | 213 | store_url = "%s/charm/%s" % (STORE_URL, qualified_url[3:]) |
155 | 212 | with temp_file() as fh: | 214 | with temp_file() as fh: |
157 | 213 | ufh = urllib2.urlopen(store_url) | 215 | ufh = urlopen(store_url) |
158 | 214 | shutil.copyfileobj(ufh, fh) | 216 | shutil.copyfileobj(ufh, fh) |
159 | 215 | fh.flush() | 217 | fh.flush() |
160 | 216 | extract_zip(fh.name, self.path) | 218 | extract_zip(fh.name, self.path) |
161 | 217 | 219 | ||
162 | === modified file 'deployer/cli.py' | |||
163 | --- deployer/cli.py 2016-03-31 02:48:32 +0000 | |||
164 | +++ deployer/cli.py 2016-05-08 03:36:27 +0000 | |||
165 | @@ -7,6 +7,8 @@ | |||
166 | 7 | """ | 7 | """ |
167 | 8 | 8 | ||
168 | 9 | 9 | ||
169 | 10 | from __future__ import absolute_import | ||
170 | 11 | from __future__ import print_function | ||
171 | 10 | import argparse | 12 | import argparse |
172 | 11 | import errno | 13 | import errno |
173 | 12 | import logging | 14 | import logging |
174 | @@ -220,14 +222,14 @@ | |||
175 | 220 | 222 | ||
176 | 221 | # Just list the available deployments | 223 | # Just list the available deployments |
177 | 222 | if options.list_deploys: | 224 | if options.list_deploys: |
179 | 223 | print("\n".join(sorted(config.keys()))) | 225 | print(("\n".join(sorted(config.keys())))) |
180 | 224 | sys.exit(0) | 226 | sys.exit(0) |
181 | 225 | 227 | ||
182 | 226 | # Do something to a deployment | 228 | # Do something to a deployment |
183 | 227 | if not options.deployment: | 229 | if not options.deployment: |
184 | 228 | # If there's only one option then use it. | 230 | # If there's only one option then use it. |
187 | 229 | if len(config.keys()) == 1: | 231 | if len(list(config.keys())) == 1: |
188 | 230 | options.deployment = config.keys()[0] | 232 | options.deployment = list(config.keys())[0] |
189 | 231 | log.info("Using deployment %s", options.deployment) | 233 | log.info("Using deployment %s", options.deployment) |
190 | 232 | else: | 234 | else: |
191 | 233 | log.error( | 235 | log.error( |
192 | 234 | 236 | ||
193 | === modified file 'deployer/config.py' | |||
194 | --- deployer/config.py 2015-08-05 15:39:40 +0000 | |||
195 | +++ deployer/config.py 2016-05-08 03:36:27 +0000 | |||
196 | @@ -1,12 +1,14 @@ | |||
197 | 1 | from __future__ import absolute_import | ||
198 | 1 | from os.path import abspath, isabs, join, dirname | 2 | from os.path import abspath, isabs, join, dirname |
199 | 2 | 3 | ||
200 | 3 | import logging | 4 | import logging |
201 | 4 | import os | 5 | import os |
202 | 5 | import tempfile | 6 | import tempfile |
203 | 6 | import shutil | 7 | import shutil |
204 | 7 | import urllib2 | ||
205 | 8 | import urlparse | ||
206 | 9 | 8 | ||
207 | 9 | import six | ||
208 | 10 | from six.moves.urllib.request import urlopen | ||
209 | 11 | from six.moves.urllib.parse import urlparse | ||
210 | 10 | 12 | ||
211 | 11 | from .deployment import Deployment | 13 | from .deployment import Deployment |
212 | 12 | from .utils import ErrorExit, yaml_load, path_exists, dict_merge | 14 | from .utils import ErrorExit, yaml_load, path_exists, dict_merge |
213 | @@ -23,14 +25,14 @@ | |||
214 | 23 | self.data = {} | 25 | self.data = {} |
215 | 24 | self.yaml = {} | 26 | self.yaml = {} |
216 | 25 | self.include_dirs = [] | 27 | self.include_dirs = [] |
218 | 26 | self.urlopen = urllib2.urlopen | 28 | self.urlopen = urlopen |
219 | 27 | self.load() | 29 | self.load() |
220 | 28 | 30 | ||
221 | 29 | def _yaml_load(self, config_file): | 31 | def _yaml_load(self, config_file): |
222 | 30 | if config_file in self.yaml: | 32 | if config_file in self.yaml: |
223 | 31 | return self.yaml[config_file] | 33 | return self.yaml[config_file] |
224 | 32 | 34 | ||
226 | 33 | if urlparse.urlparse(config_file).scheme: | 35 | if urlparse(config_file).scheme: |
227 | 34 | response = self.urlopen(config_file) | 36 | response = self.urlopen(config_file) |
228 | 35 | if response.getcode() == 200: | 37 | if response.getcode() == 200: |
229 | 36 | temp = tempfile.NamedTemporaryFile(delete=True) | 38 | temp = tempfile.NamedTemporaryFile(delete=True) |
230 | @@ -44,7 +46,7 @@ | |||
231 | 44 | with open(config_file) as fh: | 46 | with open(config_file) as fh: |
232 | 45 | try: | 47 | try: |
233 | 46 | yaml_result = yaml_load(fh.read()) | 48 | yaml_result = yaml_load(fh.read()) |
235 | 47 | except Exception, e: | 49 | except Exception as e: |
236 | 48 | self.log.warning( | 50 | self.log.warning( |
237 | 49 | "Couldn't load config file @ %r, error: %s:%s", | 51 | "Couldn't load config file @ %r, error: %s:%s", |
238 | 50 | config_file, type(e), e) | 52 | config_file, type(e), e) |
239 | @@ -66,7 +68,7 @@ | |||
240 | 66 | def get(self, key): | 68 | def get(self, key): |
241 | 67 | if key not in self.data: | 69 | if key not in self.data: |
242 | 68 | self.log.warning("Deployment %r not found. Available %s", | 70 | self.log.warning("Deployment %r not found. Available %s", |
244 | 69 | key, ", ".join(self.keys())) | 71 | key, ", ".join(list(self.keys()))) |
245 | 70 | raise ErrorExit() | 72 | raise ErrorExit() |
246 | 71 | deploy_data = self.data[key] | 73 | deploy_data = self.data[key] |
247 | 72 | if self.version < 4: | 74 | if self.version < 4: |
248 | @@ -94,7 +96,7 @@ | |||
249 | 94 | 96 | ||
250 | 95 | def _inherits(self, d): | 97 | def _inherits(self, d): |
251 | 96 | parents = d.get('inherits', ()) | 98 | parents = d.get('inherits', ()) |
253 | 97 | if isinstance(parents, basestring): | 99 | if isinstance(parents, six.string_types): |
254 | 98 | parents = [parents] | 100 | parents = [parents] |
255 | 99 | return parents | 101 | return parents |
256 | 100 | 102 | ||
257 | @@ -115,7 +117,7 @@ | |||
258 | 115 | d = self._yaml_load(config_file) | 117 | d = self._yaml_load(config_file) |
259 | 116 | 118 | ||
260 | 117 | incs = d.get('include-configs') or d.get('include-config') | 119 | incs = d.get('include-configs') or d.get('include-config') |
262 | 118 | if isinstance(incs, basestring): | 120 | if isinstance(incs, six.string_types): |
263 | 119 | inc_fs = [incs] | 121 | inc_fs = [incs] |
264 | 120 | else: | 122 | else: |
265 | 121 | inc_fs = incs | 123 | inc_fs = incs |
266 | 122 | 124 | ||
267 | === modified file 'deployer/deployment.py' | |||
268 | --- deployer/deployment.py 2015-08-06 12:04:16 +0000 | |||
269 | +++ deployer/deployment.py 2016-05-08 03:36:27 +0000 | |||
270 | @@ -1,10 +1,14 @@ | |||
271 | 1 | from __future__ import absolute_import | ||
272 | 1 | from base64 import b64encode | 2 | from base64 import b64encode |
273 | 3 | from functools import cmp_to_key | ||
274 | 2 | 4 | ||
275 | 3 | import logging | 5 | import logging |
276 | 4 | import pprint | 6 | import pprint |
277 | 5 | import os | 7 | import os |
278 | 6 | import yaml | 8 | import yaml |
279 | 7 | 9 | ||
280 | 10 | import six | ||
281 | 11 | |||
282 | 8 | from .charm import Charm | 12 | from .charm import Charm |
283 | 9 | from .feedback import Feedback | 13 | from .feedback import Feedback |
284 | 10 | from .service import Service, ServiceUnitPlacementV3, ServiceUnitPlacementV4 | 14 | from .service import Service, ServiceUnitPlacementV3, ServiceUnitPlacementV4 |
285 | @@ -50,7 +54,7 @@ | |||
286 | 50 | # Sort unplaced units first, then sort by name for placed units. | 54 | # Sort unplaced units first, then sort by name for placed units. |
287 | 51 | services.sort(key=lambda svc: (bool(svc.unit_placement), svc.name)) | 55 | services.sort(key=lambda svc: (bool(svc.unit_placement), svc.name)) |
288 | 52 | else: | 56 | else: |
290 | 53 | services.sort(self._machines_placement_sort) | 57 | services.sort(key=cmp_to_key(self._machines_placement_sort)) |
291 | 54 | return services | 58 | return services |
292 | 55 | 59 | ||
293 | 56 | def set_machines(self, machines): | 60 | def set_machines(self, machines): |
294 | @@ -78,7 +82,7 @@ | |||
295 | 78 | 82 | ||
296 | 79 | def get_service_names(self): | 83 | def get_service_names(self): |
297 | 80 | """Return a sequence of service names for this deployment.""" | 84 | """Return a sequence of service names for this deployment.""" |
299 | 81 | return self.data.get('services', {}).keys() | 85 | return list(self.data.get('services', {}).keys()) |
300 | 82 | 86 | ||
301 | 83 | @staticmethod | 87 | @staticmethod |
302 | 84 | def _machines_placement_sort(svc_a, svc_b): | 88 | def _machines_placement_sort(svc_a, svc_b): |
303 | @@ -90,6 +94,9 @@ | |||
304 | 90 | whether or not the service has a unit placement, and then finally | 94 | whether or not the service has a unit placement, and then finally |
305 | 91 | based on the name of the service. | 95 | based on the name of the service. |
306 | 92 | """ | 96 | """ |
307 | 97 | def cmp_(a, b): | ||
308 | 98 | return (a > b) - (a < b) | ||
309 | 99 | |||
310 | 93 | if svc_a.unit_placement: | 100 | if svc_a.unit_placement: |
311 | 94 | if svc_b.unit_placement: | 101 | if svc_b.unit_placement: |
312 | 95 | # Check for colocation. This naively assumes that there is no | 102 | # Check for colocation. This naively assumes that there is no |
313 | @@ -99,14 +106,14 @@ | |||
314 | 99 | if x_in_y(svc_a, svc_b): | 106 | if x_in_y(svc_a, svc_b): |
315 | 100 | return -1 | 107 | return -1 |
316 | 101 | # If no colocation exists, simply compare names. | 108 | # If no colocation exists, simply compare names. |
318 | 102 | return cmp(svc_a.name, svc_b.name) | 109 | return cmp_(svc_a.name, svc_b.name) |
319 | 103 | return 1 | 110 | return 1 |
320 | 104 | if svc_b.unit_placement: | 111 | if svc_b.unit_placement: |
321 | 105 | return -1 | 112 | return -1 |
323 | 106 | return cmp(svc_a.name, svc_b.name) | 113 | return cmp_(svc_a.name, svc_b.name) |
324 | 107 | 114 | ||
325 | 108 | def get_unit_placement(self, svc, status): | 115 | def get_unit_placement(self, svc, status): |
327 | 109 | if isinstance(svc, (str, unicode)): | 116 | if isinstance(svc, (str, six.text_type)): |
328 | 110 | svc = self.get_service(svc) | 117 | svc = self.get_service(svc) |
329 | 111 | if self.version == 3: | 118 | if self.version == 3: |
330 | 112 | return ServiceUnitPlacementV3(svc, self, status) | 119 | return ServiceUnitPlacementV3(svc, self, status) |
331 | @@ -124,7 +131,6 @@ | |||
332 | 124 | def check(a, b): | 131 | def check(a, b): |
333 | 125 | k = tuple(sorted([a, b])) | 132 | k = tuple(sorted([a, b])) |
334 | 126 | if k in seen: | 133 | if k in seen: |
335 | 127 | #self.log.warning(" Skipping duplicate relation %r" % (k,)) | ||
336 | 128 | return | 134 | return |
337 | 129 | seen.add(k) | 135 | seen.add(k) |
338 | 130 | return True | 136 | return True |
339 | @@ -154,8 +160,6 @@ | |||
340 | 154 | for r in rels[k]: | 160 | for r in rels[k]: |
341 | 155 | if check(*r): | 161 | if check(*r): |
342 | 156 | yield r | 162 | yield r |
343 | 157 | #self.log.debug( | ||
344 | 158 | # "Found relations %s\n %s" % (" ".join(map(str, seen)))) | ||
345 | 159 | 163 | ||
346 | 160 | def get_charms(self): | 164 | def get_charms(self): |
347 | 161 | for k, v in self.data.get('services', {}).items(): | 165 | for k, v in self.data.get('services', {}).items(): |
348 | @@ -204,7 +208,7 @@ | |||
349 | 204 | key, value = o.split('=', 1) | 208 | key, value = o.split('=', 1) |
350 | 205 | overrides[key] = value | 209 | overrides[key] = value |
351 | 206 | 210 | ||
353 | 207 | for k, v in overrides.iteritems(): | 211 | for k, v in six.iteritems(overrides): |
354 | 208 | found = False | 212 | found = False |
355 | 209 | for svc_name, svc_data in self.data['services'].items(): | 213 | for svc_name, svc_data in self.data['services'].items(): |
356 | 210 | charm = self.get_charm_for(svc_name) | 214 | charm = self.get_charm_for(svc_name) |
357 | @@ -225,7 +229,7 @@ | |||
358 | 225 | # against defined services | 229 | # against defined services |
359 | 226 | feedback = Feedback() | 230 | feedback = Feedback() |
360 | 227 | for svc_name, svc_data in self.data.get('services', {}).items(): | 231 | for svc_name, svc_data in self.data.get('services', {}).items(): |
362 | 228 | if not 'options' in svc_data: | 232 | if 'options' not in svc_data: |
363 | 229 | continue | 233 | continue |
364 | 230 | charm = self.get_charm_for(svc_name) | 234 | charm = self.get_charm_for(svc_name) |
365 | 231 | config = charm.config | 235 | config = charm.config |
366 | @@ -235,7 +239,7 @@ | |||
367 | 235 | if svc_options is None: | 239 | if svc_options is None: |
368 | 236 | svc_options = {} | 240 | svc_options = {} |
369 | 237 | for k, v in svc_options.items(): | 241 | for k, v in svc_options.items(): |
371 | 238 | if not k in config: | 242 | if k not in config: |
372 | 239 | feedback.error( | 243 | feedback.error( |
373 | 240 | "Invalid config charm %s %s=%s" % (charm.name, k, v)) | 244 | "Invalid config charm %s %s=%s" % (charm.name, k, v)) |
374 | 241 | continue | 245 | continue |
375 | @@ -252,18 +256,17 @@ | |||
376 | 252 | def _resolve_include(self, svc_name, k, v): | 256 | def _resolve_include(self, svc_name, k, v): |
377 | 253 | feedback = Feedback() | 257 | feedback = Feedback() |
378 | 254 | for include_type in ["file", "base64"]: | 258 | for include_type in ["file", "base64"]: |
382 | 255 | if (not isinstance(v, basestring) | 259 | if (not isinstance(v, six.string_types) or |
383 | 256 | or not v.startswith( | 260 | not v.startswith("include-%s://" % include_type)): |
381 | 257 | "include-%s://" % include_type)): | ||
384 | 258 | continue | 261 | continue |
385 | 259 | include, fname = v.split("://", 1) | 262 | include, fname = v.split("://", 1) |
386 | 260 | ip = resolve_include(fname, self.include_dirs) | 263 | ip = resolve_include(fname, self.include_dirs) |
387 | 261 | if ip is None: | 264 | if ip is None: |
388 | 262 | feedback.error( | 265 | feedback.error( |
389 | 263 | "Invalid config %s.%s include not found %s" % ( | 266 | "Invalid config %s.%s include not found %s" % ( |
391 | 264 | svc_name, k, v)) | 267 | svc_name, k, v)) |
392 | 265 | continue | 268 | continue |
394 | 266 | with open(ip) as fh: | 269 | with open(ip, 'rb') as fh: |
395 | 267 | v = fh.read() | 270 | v = fh.read() |
396 | 268 | if include_type == "base64": | 271 | if include_type == "base64": |
397 | 269 | v = b64encode(v) | 272 | v = b64encode(v) |
398 | @@ -277,7 +280,7 @@ | |||
399 | 277 | feedback = Feedback() | 280 | feedback = Feedback() |
400 | 278 | for e_a, e_b in self.get_relations(): | 281 | for e_a, e_b in self.get_relations(): |
401 | 279 | for ep in [Endpoint(e_a), Endpoint(e_b)]: | 282 | for ep in [Endpoint(e_a), Endpoint(e_b)]: |
403 | 280 | if not ep.service in services: | 283 | if ep.service not in services: |
404 | 281 | feedback.error( | 284 | feedback.error( |
405 | 282 | ("Invalid relation in config," | 285 | ("Invalid relation in config," |
406 | 283 | " service %s not found, rel %s") % ( | 286 | " service %s not found, rel %s") % ( |
407 | 284 | 287 | ||
408 | === modified file 'deployer/env/__init__.py' | |||
409 | --- deployer/env/__init__.py 2014-02-20 01:14:53 +0000 | |||
410 | +++ deployer/env/__init__.py 2016-05-08 03:36:27 +0000 | |||
411 | @@ -1,4 +1,3 @@ | |||
412 | 1 | # | ||
413 | 2 | from .go import GoEnvironment | 1 | from .go import GoEnvironment |
414 | 3 | from .py import PyEnvironment | 2 | from .py import PyEnvironment |
415 | 4 | from ..utils import _check_call | 3 | from ..utils import _check_call |
416 | @@ -10,5 +9,3 @@ | |||
417 | 10 | if result is None: | 9 | if result is None: |
418 | 11 | return PyEnvironment(name, options) | 10 | return PyEnvironment(name, options) |
419 | 12 | return GoEnvironment(name, options) | 11 | return GoEnvironment(name, options) |
420 | 13 | |||
421 | 14 | |||
422 | 15 | 12 | ||
423 | === modified file 'deployer/env/base.py' | |||
424 | --- deployer/env/base.py 2016-05-05 17:39:13 +0000 | |||
425 | +++ deployer/env/base.py 2016-05-08 03:36:27 +0000 | |||
426 | @@ -1,3 +1,4 @@ | |||
427 | 1 | from __future__ import absolute_import | ||
428 | 1 | import logging | 2 | import logging |
429 | 2 | 3 | ||
430 | 3 | from ..utils import ( | 4 | from ..utils import ( |
431 | @@ -66,7 +67,7 @@ | |||
432 | 66 | params = self._named_env(["juju", "deploy"]) | 67 | params = self._named_env(["juju", "deploy"]) |
433 | 67 | with temp_file() as fh: | 68 | with temp_file() as fh: |
434 | 68 | if config: | 69 | if config: |
436 | 69 | fh.write(yaml_dump({name: config})) | 70 | fh.write(yaml_dump({name: config}).encode()) |
437 | 70 | fh.flush() | 71 | fh.flush() |
438 | 71 | params.extend(["--config", fh.name]) | 72 | params.extend(["--config", fh.name]) |
439 | 72 | if constraints: | 73 | if constraints: |
440 | 73 | 74 | ||
441 | === modified file 'deployer/env/go.py' | |||
442 | --- deployer/env/go.py 2016-03-10 14:14:54 +0000 | |||
443 | +++ deployer/env/go.py 2016-05-08 03:36:27 +0000 | |||
444 | @@ -1,3 +1,5 @@ | |||
445 | 1 | from __future__ import absolute_import | ||
446 | 2 | from functools import cmp_to_key | ||
447 | 1 | import time | 3 | import time |
448 | 2 | 4 | ||
449 | 3 | from .base import BaseEnvironment | 5 | from .base import BaseEnvironment |
450 | @@ -56,7 +58,6 @@ | |||
451 | 56 | if self.client: | 58 | if self.client: |
452 | 57 | self.client.close() | 59 | self.client.close() |
453 | 58 | 60 | ||
454 | 59 | |||
455 | 60 | def connect(self): | 61 | def connect(self): |
456 | 61 | self.log.debug("Connecting to environment...") | 62 | self.log.debug("Connecting to environment...") |
457 | 62 | if self.juju_version == 1: | 63 | if self.juju_version == 1: |
458 | @@ -130,7 +131,7 @@ | |||
459 | 130 | return | 131 | return |
460 | 131 | 132 | ||
461 | 132 | # containers before machines, container hosts post wait. | 133 | # containers before machines, container hosts post wait. |
463 | 133 | machines = status['machines'].keys() | 134 | machines = list(status['machines'].keys()) |
464 | 134 | 135 | ||
465 | 135 | container_hosts = set() | 136 | container_hosts = set() |
466 | 136 | containers = set() | 137 | containers = set() |
467 | @@ -145,9 +146,9 @@ | |||
468 | 145 | return -1 | 146 | return -1 |
469 | 146 | if m == y: | 147 | if m == y: |
470 | 147 | return 1 | 148 | return 1 |
472 | 148 | return cmp(x, y) | 149 | return (x > y) - (x < y) |
473 | 149 | 150 | ||
475 | 150 | machines.sort(machine_sort) | 151 | machines.sort(key=cmp_to_key(machine_sort)) |
476 | 151 | 152 | ||
477 | 152 | for mid in machines: | 153 | for mid in machines: |
478 | 153 | self._terminate_machine(mid, container_hosts, force=force) | 154 | self._terminate_machine(mid, container_hosts, force=force) |
479 | 154 | 155 | ||
480 | === modified file 'deployer/env/mem.py' | |||
481 | --- deployer/env/mem.py 2014-04-21 22:49:05 +0000 | |||
482 | +++ deployer/env/mem.py 2016-05-08 03:36:27 +0000 | |||
483 | @@ -1,6 +1,8 @@ | |||
484 | 1 | from __future__ import absolute_import | ||
485 | 1 | from deployer.utils import parse_constraints | 2 | from deployer.utils import parse_constraints |
486 | 2 | from jujuclient import (UnitErrors, | 3 | from jujuclient import (UnitErrors, |
487 | 3 | EnvError) | 4 | EnvError) |
488 | 5 | from six.moves import range | ||
489 | 4 | 6 | ||
490 | 5 | 7 | ||
491 | 6 | class MemoryEnvironment(object): | 8 | class MemoryEnvironment(object): |
492 | @@ -24,7 +26,7 @@ | |||
493 | 24 | """Add units | 26 | """Add units |
494 | 25 | """ | 27 | """ |
495 | 26 | next_num = self._services_data[svc_name]['next_unit_num'] | 28 | next_num = self._services_data[svc_name]['next_unit_num'] |
497 | 27 | for idx in xrange(next_num, next_num + num): | 29 | for idx in range(next_num, next_num + num): |
498 | 28 | self._services[svc_name]['units'].append( | 30 | self._services[svc_name]['units'].append( |
499 | 29 | '{}/{}'.format(svc_name, idx)) | 31 | '{}/{}'.format(svc_name, idx)) |
500 | 30 | self._services_data[svc_name]['next_unit_num'] = \ | 32 | self._services_data[svc_name]['next_unit_num'] = \ |
501 | @@ -44,7 +46,7 @@ | |||
502 | 44 | def _get_service(self, svc_name): | 46 | def _get_service(self, svc_name): |
503 | 45 | """ Get service by name (as returned by status()) | 47 | """ Get service by name (as returned by status()) |
504 | 46 | """ | 48 | """ |
506 | 47 | if not svc_name in self._services: | 49 | if svc_name not in self._services: |
507 | 48 | raise EnvError("Invalid service name") | 50 | raise EnvError("Invalid service name") |
508 | 49 | return self._services[svc_name] | 51 | return self._services[svc_name] |
509 | 50 | 52 | ||
510 | @@ -55,7 +57,7 @@ | |||
511 | 55 | def destroy_service(self, svc_name): | 57 | def destroy_service(self, svc_name): |
512 | 56 | """ Destroy a service | 58 | """ Destroy a service |
513 | 57 | """ | 59 | """ |
515 | 58 | if not svc_name in self._services: | 60 | if svc_name not in self._services: |
516 | 59 | raise EnvError("Invalid service name") | 61 | raise EnvError("Invalid service name") |
517 | 60 | del self._services[svc_name] | 62 | del self._services[svc_name] |
518 | 61 | 63 | ||
519 | 62 | 64 | ||
520 | === modified file 'deployer/env/py.py' | |||
521 | --- deployer/env/py.py 2014-02-18 12:16:46 +0000 | |||
522 | +++ deployer/env/py.py 2016-05-08 03:36:27 +0000 | |||
523 | @@ -1,8 +1,6 @@ | |||
524 | 1 | from __future__ import absolute_import | ||
525 | 1 | import time | 2 | import time |
526 | 2 | 3 | ||
527 | 3 | from deployer.errors import UnitErrors | ||
528 | 4 | from deployer.utils import ErrorExit | ||
529 | 5 | |||
530 | 6 | from .base import BaseEnvironment | 4 | from .base import BaseEnvironment |
531 | 7 | 5 | ||
532 | 8 | 6 | ||
533 | 9 | 7 | ||
534 | === modified file 'deployer/env/watchers.py' | |||
535 | --- deployer/env/watchers.py 2016-03-31 20:08:08 +0000 | |||
536 | +++ deployer/env/watchers.py 2016-05-08 03:36:27 +0000 | |||
537 | @@ -1,8 +1,9 @@ | |||
538 | 1 | """A collection of juju-core environment watchers.""" | 1 | """A collection of juju-core environment watchers.""" |
539 | 2 | 2 | ||
540 | 3 | from __future__ import absolute_import | ||
541 | 3 | from jujuclient import WatchWrapper | 4 | from jujuclient import WatchWrapper |
542 | 4 | 5 | ||
544 | 5 | from ..utils import ErrorExit, get_juju_major_version | 6 | from ..utils import ErrorExit |
545 | 6 | 7 | ||
546 | 7 | # _status_map provides a translation of Juju 2 status codes to the closest | 8 | # _status_map provides a translation of Juju 2 status codes to the closest |
547 | 8 | # Juju 1 equivalent. Only defines codes that need translation. | 9 | # Juju 1 equivalent. Only defines codes that need translation. |
548 | 9 | 10 | ||
549 | === modified file 'deployer/errors.py' | |||
550 | --- deployer/errors.py 2013-07-22 15:29:31 +0000 | |||
551 | +++ deployer/errors.py 2016-05-08 03:36:27 +0000 | |||
552 | @@ -1,2 +1,4 @@ | |||
555 | 1 | # TODO make deployer specific exceptions, also move errorexit from utils to here. | 1 | # TODO make deployer specific exceptions, |
556 | 2 | from jujuclient import UnitErrors, EnvError | 2 | # also move errorexit from utils to here. |
557 | 3 | from __future__ import absolute_import | ||
558 | 4 | from jujuclient import UnitErrors, EnvError # noqa | ||
559 | 3 | 5 | ||
560 | === modified file 'deployer/feedback.py' | |||
561 | --- deployer/feedback.py 2013-11-20 05:08:15 +0000 | |||
562 | +++ deployer/feedback.py 2016-05-08 03:36:27 +0000 | |||
563 | @@ -21,6 +21,11 @@ | |||
564 | 21 | return iter(self.messages) | 21 | return iter(self.messages) |
565 | 22 | 22 | ||
566 | 23 | def __nonzero__(self): | 23 | def __nonzero__(self): |
567 | 24 | # py2 | ||
568 | 25 | return bool(self.messages) | ||
569 | 26 | |||
570 | 27 | def __bool__(self): | ||
571 | 28 | # py3 | ||
572 | 24 | return bool(self.messages) | 29 | return bool(self.messages) |
573 | 25 | 30 | ||
574 | 26 | def get_errors(self): | 31 | def get_errors(self): |
575 | 27 | 32 | ||
576 | === modified file 'deployer/guiserver.py' | |||
577 | --- deployer/guiserver.py 2015-08-10 13:12:20 +0000 | |||
578 | +++ deployer/guiserver.py 2016-05-08 03:36:27 +0000 | |||
579 | @@ -9,6 +9,7 @@ | |||
580 | 9 | <https://code.launchpad.net/~juju-gui/charms/precise/juju-gui/trunk>. | 9 | <https://code.launchpad.net/~juju-gui/charms/precise/juju-gui/trunk>. |
581 | 10 | """ | 10 | """ |
582 | 11 | 11 | ||
583 | 12 | from __future__ import absolute_import | ||
584 | 12 | import os | 13 | import os |
585 | 13 | 14 | ||
586 | 14 | from deployer.action.importer import Importer | 15 | from deployer.action.importer import Importer |
587 | 15 | 16 | ||
588 | === modified file 'deployer/relation.py' | |||
589 | --- deployer/relation.py 2013-07-22 15:29:31 +0000 | |||
590 | +++ deployer/relation.py 2016-05-08 03:36:27 +0000 | |||
591 | @@ -1,3 +1,4 @@ | |||
592 | 1 | from __future__ import absolute_import | ||
593 | 1 | import yaml | 2 | import yaml |
594 | 2 | 3 | ||
595 | 3 | 4 | ||
596 | @@ -23,13 +24,11 @@ | |||
597 | 23 | def __eq__(self, ep_pair): | 24 | def __eq__(self, ep_pair): |
598 | 24 | if not isinstance(ep_pair, EndpointPair): | 25 | if not isinstance(ep_pair, EndpointPair): |
599 | 25 | return False | 26 | return False |
602 | 26 | return (ep_pair.ep_x.service in self | 27 | return (ep_pair.ep_x.service in self and |
601 | 27 | and | ||
603 | 28 | ep_pair.ep_y.service in self) | 28 | ep_pair.ep_y.service in self) |
604 | 29 | 29 | ||
605 | 30 | def __contains__(self, svc_name): | 30 | def __contains__(self, svc_name): |
608 | 31 | return (svc_name == self.ep_x.service | 31 | return (svc_name == self.ep_x.service or |
607 | 32 | or | ||
609 | 33 | svc_name == self.ep_y.service) | 32 | svc_name == self.ep_y.service) |
610 | 34 | 33 | ||
611 | 35 | def __hash__(self): | 34 | def __hash__(self): |
612 | 36 | 35 | ||
613 | === modified file 'deployer/service.py' | |||
614 | --- deployer/service.py 2015-12-11 16:09:53 +0000 | |||
615 | +++ deployer/service.py 2016-05-08 03:36:27 +0000 | |||
616 | @@ -1,6 +1,8 @@ | |||
617 | 1 | from __future__ import absolute_import | ||
618 | 1 | import itertools | 2 | import itertools |
619 | 2 | 3 | ||
621 | 3 | from feedback import Feedback | 4 | from .feedback import Feedback |
622 | 5 | from six.moves import map | ||
623 | 4 | 6 | ||
624 | 5 | 7 | ||
625 | 6 | class Service(object): | 8 | class Service(object): |
626 | @@ -43,7 +45,7 @@ | |||
627 | 43 | value = self.svc_data.get('force-machine') | 45 | value = self.svc_data.get('force-machine') |
628 | 44 | if value is not None and not isinstance(value, list): | 46 | if value is not None and not isinstance(value, list): |
629 | 45 | value = [value] | 47 | value = [value] |
631 | 46 | return value and map(str, value) or [] | 48 | return value and list(map(str, value)) or [] |
632 | 47 | 49 | ||
633 | 48 | @property | 50 | @property |
634 | 49 | def expose(self): | 51 | def expose(self): |
635 | @@ -93,7 +95,7 @@ | |||
636 | 93 | "Cannot solve, falling back to default placement", | 95 | "Cannot solve, falling back to default placement", |
637 | 94 | svc.name, placement, u_idx) | 96 | svc.name, placement, u_idx) |
638 | 95 | return None | 97 | return None |
640 | 96 | unit_names = svc_units.keys() | 98 | unit_names = list(svc_units.keys()) |
641 | 97 | unit_names.sort() | 99 | unit_names.sort() |
642 | 98 | machine = svc_units[unit_names[int(u_idx)]].get('machine') | 100 | machine = svc_units[unit_names[int(u_idx)]].get('machine') |
643 | 99 | if not machine: | 101 | if not machine: |
644 | @@ -125,7 +127,7 @@ | |||
645 | 125 | 127 | ||
646 | 126 | if not isinstance(unit_placement, list): | 128 | if not isinstance(unit_placement, list): |
647 | 127 | unit_placement = [unit_placement] | 129 | unit_placement = [unit_placement] |
649 | 128 | unit_placement = map(str, unit_placement) | 130 | unit_placement = list(map(str, unit_placement)) |
650 | 129 | 131 | ||
651 | 130 | services = dict([(s.name, s) for s in self.deployment.get_services()]) | 132 | services = dict([(s.name, s) for s in self.deployment.get_services()]) |
652 | 131 | machines = self.deployment.get_machines() | 133 | machines = self.deployment.get_machines() |
653 | @@ -134,15 +136,15 @@ | |||
654 | 134 | container, p, u_idx = self._parse_placement(p) | 136 | container, p, u_idx = self._parse_placement(p) |
655 | 135 | if container and container not in ('lxc', 'kvm'): | 137 | if container and container not in ('lxc', 'kvm'): |
656 | 136 | feedback.error( | 138 | feedback.error( |
659 | 137 | "Invalid container type:%s service: %s placement: %s" \ | 139 | "Invalid container type:%s service: %s placement: %s" |
660 | 138 | % (container, self.service.name, unit_placement[idx])) | 140 | % (container, self.service.name, unit_placement[idx])) |
661 | 139 | if u_idx: | 141 | if u_idx: |
662 | 140 | if p in ('maas', 'zone'): | 142 | if p in ('maas', 'zone'): |
663 | 141 | continue | 143 | continue |
664 | 142 | if not u_idx.isdigit(): | 144 | if not u_idx.isdigit(): |
665 | 143 | feedback.error( | 145 | feedback.error( |
666 | 144 | "Invalid service:%s placement: %s" % ( | 146 | "Invalid service:%s placement: %s" % ( |
668 | 145 | self.service.name, unit_placement[idx])) | 147 | self.service.name, unit_placement[idx])) |
669 | 146 | if p.isdigit(): | 148 | if p.isdigit(): |
670 | 147 | if p == '0' or p in machines or self.arbitrary_machines: | 149 | if p == '0' or p in machines or self.arbitrary_machines: |
671 | 148 | continue | 150 | continue |
672 | @@ -237,7 +239,8 @@ | |||
673 | 237 | 239 | ||
674 | 238 | self.service.svc_data['to'] = ( | 240 | self.service.svc_data['to'] = ( |
675 | 239 | unit_mapping + | 241 | unit_mapping + |
677 | 240 | list(itertools.repeat(unit_mapping[-1], unit_count - len(unit_mapping))) | 242 | list(itertools.repeat( |
678 | 243 | unit_mapping[-1], unit_count - len(unit_mapping))) | ||
679 | 241 | ) | 244 | ) |
680 | 242 | unit_mapping = self.service.unit_placement | 245 | unit_mapping = self.service.unit_placement |
681 | 243 | 246 | ||
682 | @@ -288,7 +291,7 @@ | |||
683 | 288 | 291 | ||
684 | 289 | if not isinstance(unit_placement, (list, tuple)): | 292 | if not isinstance(unit_placement, (list, tuple)): |
685 | 290 | unit_placement = [unit_placement] | 293 | unit_placement = [unit_placement] |
687 | 291 | unit_placement = map(str, unit_placement) | 294 | unit_placement = list(map(str, unit_placement)) |
688 | 292 | 295 | ||
689 | 293 | services = dict([(s.name, s) for s in self.deployment.get_services()]) | 296 | services = dict([(s.name, s) for s in self.deployment.get_services()]) |
690 | 294 | machines = self.deployment.get_machines() | 297 | machines = self.deployment.get_machines() |
691 | @@ -349,7 +352,8 @@ | |||
692 | 349 | # Generate a name for this machine to be used in the | 352 | # Generate a name for this machine to be used in the |
693 | 350 | # machines_map used later; as a quick path forward, simply use | 353 | # machines_map used later; as a quick path forward, simply use |
694 | 351 | # the unit's name. | 354 | # the unit's name. |
696 | 352 | new_machines.append('{}/{}'.format(self.service.name, unit.next())) | 355 | new_machines.append('{}/{}'.format( |
697 | 356 | self.service.name, next(unit))) | ||
698 | 353 | return new_machines | 357 | return new_machines |
699 | 354 | 358 | ||
700 | 355 | def get(self, unit_number): | 359 | def get(self, unit_number): |
701 | 356 | 360 | ||
702 | === modified file 'deployer/tests/base.py' | |||
703 | --- deployer/tests/base.py 2015-08-06 13:02:14 +0000 | |||
704 | +++ deployer/tests/base.py 2016-05-08 03:36:27 +0000 | |||
705 | @@ -1,9 +1,9 @@ | |||
706 | 1 | from __future__ import absolute_import | ||
707 | 1 | import inspect | 2 | import inspect |
708 | 2 | import logging | 3 | import logging |
709 | 3 | import os | 4 | import os |
710 | 4 | import unittest | 5 | import unittest |
711 | 5 | import shutil | 6 | import shutil |
712 | 6 | import StringIO | ||
713 | 7 | import tempfile | 7 | import tempfile |
714 | 8 | 8 | ||
715 | 9 | import mock | 9 | import mock |
716 | @@ -11,6 +11,9 @@ | |||
717 | 11 | import deployer | 11 | import deployer |
718 | 12 | from deployer.config import ConfigStack | 12 | from deployer.config import ConfigStack |
719 | 13 | 13 | ||
720 | 14 | from six import StringIO | ||
721 | 15 | from six.moves import range | ||
722 | 16 | |||
723 | 14 | 17 | ||
724 | 15 | # Skip during launchpad recipe package builds (DEB_BUILD_ARCH) or if explicitly | 18 | # Skip during launchpad recipe package builds (DEB_BUILD_ARCH) or if explicitly |
725 | 16 | # requested with 'TEST_OFFLINE=1' | 19 | # requested with 'TEST_OFFLINE=1' |
726 | @@ -48,7 +51,7 @@ | |||
727 | 48 | def capture_logging(self, name="", level=logging.INFO, | 51 | def capture_logging(self, name="", level=logging.INFO, |
728 | 49 | log_file=None, formatter=None): | 52 | log_file=None, formatter=None): |
729 | 50 | if log_file is None: | 53 | if log_file is None: |
731 | 51 | log_file = StringIO.StringIO() | 54 | log_file = StringIO() |
732 | 52 | log_handler = logging.StreamHandler(log_file) | 55 | log_handler = logging.StreamHandler(log_file) |
733 | 53 | if formatter: | 56 | if formatter: |
734 | 54 | log_handler.setFormatter(formatter) | 57 | log_handler.setFormatter(formatter) |
735 | 55 | 58 | ||
736 | === removed file 'deployer/tests/mock.py' | |||
737 | --- deployer/tests/mock.py 2013-11-22 20:43:40 +0000 | |||
738 | +++ deployer/tests/mock.py 1970-01-01 00:00:00 +0000 | |||
739 | @@ -1,2367 +0,0 @@ | |||
740 | 1 | # mock.py | ||
741 | 2 | # Test tools for mocking and patching. | ||
742 | 3 | # Copyright (C) 2007-2012 Michael Foord & the mock team | ||
743 | 4 | # E-mail: fuzzyman AT voidspace DOT org DOT uk | ||
744 | 5 | |||
745 | 6 | # mock 1.0 | ||
746 | 7 | # http://www.voidspace.org.uk/python/mock/ | ||
747 | 8 | |||
748 | 9 | # Released subject to the BSD License | ||
749 | 10 | # Please see http://www.voidspace.org.uk/python/license.shtml | ||
750 | 11 | |||
751 | 12 | # Scripts maintained at http://www.voidspace.org.uk/python/index.shtml | ||
752 | 13 | # Comments, suggestions and bug reports welcome. | ||
753 | 14 | |||
754 | 15 | |||
755 | 16 | __all__ = ( | ||
756 | 17 | 'Mock', | ||
757 | 18 | 'MagicMock', | ||
758 | 19 | 'patch', | ||
759 | 20 | 'sentinel', | ||
760 | 21 | 'DEFAULT', | ||
761 | 22 | 'ANY', | ||
762 | 23 | 'call', | ||
763 | 24 | 'create_autospec', | ||
764 | 25 | 'FILTER_DIR', | ||
765 | 26 | 'NonCallableMock', | ||
766 | 27 | 'NonCallableMagicMock', | ||
767 | 28 | 'mock_open', | ||
768 | 29 | 'PropertyMock', | ||
769 | 30 | ) | ||
770 | 31 | |||
771 | 32 | |||
772 | 33 | __version__ = '1.0.1' | ||
773 | 34 | |||
774 | 35 | |||
775 | 36 | import pprint | ||
776 | 37 | import sys | ||
777 | 38 | |||
778 | 39 | try: | ||
779 | 40 | import inspect | ||
780 | 41 | except ImportError: | ||
781 | 42 | # for alternative platforms that | ||
782 | 43 | # may not have inspect | ||
783 | 44 | inspect = None | ||
784 | 45 | |||
785 | 46 | try: | ||
786 | 47 | from functools import wraps as original_wraps | ||
787 | 48 | except ImportError: | ||
788 | 49 | # Python 2.4 compatibility | ||
789 | 50 | def wraps(original): | ||
790 | 51 | def inner(f): | ||
791 | 52 | f.__name__ = original.__name__ | ||
792 | 53 | f.__doc__ = original.__doc__ | ||
793 | 54 | f.__module__ = original.__module__ | ||
794 | 55 | f.__wrapped__ = original | ||
795 | 56 | return f | ||
796 | 57 | return inner | ||
797 | 58 | else: | ||
798 | 59 | if sys.version_info[:2] >= (3, 3): | ||
799 | 60 | wraps = original_wraps | ||
800 | 61 | else: | ||
801 | 62 | def wraps(func): | ||
802 | 63 | def inner(f): | ||
803 | 64 | f = original_wraps(func)(f) | ||
804 | 65 | f.__wrapped__ = func | ||
805 | 66 | return f | ||
806 | 67 | return inner | ||
807 | 68 | |||
808 | 69 | try: | ||
809 | 70 | unicode | ||
810 | 71 | except NameError: | ||
811 | 72 | # Python 3 | ||
812 | 73 | basestring = unicode = str | ||
813 | 74 | |||
814 | 75 | try: | ||
815 | 76 | long | ||
816 | 77 | except NameError: | ||
817 | 78 | # Python 3 | ||
818 | 79 | long = int | ||
819 | 80 | |||
820 | 81 | try: | ||
821 | 82 | BaseException | ||
822 | 83 | except NameError: | ||
823 | 84 | # Python 2.4 compatibility | ||
824 | 85 | BaseException = Exception | ||
825 | 86 | |||
826 | 87 | try: | ||
827 | 88 | next | ||
828 | 89 | except NameError: | ||
829 | 90 | def next(obj): | ||
830 | 91 | return obj.next() | ||
831 | 92 | |||
832 | 93 | |||
833 | 94 | BaseExceptions = (BaseException,) | ||
834 | 95 | if 'java' in sys.platform: | ||
835 | 96 | # jython | ||
836 | 97 | import java | ||
837 | 98 | BaseExceptions = (BaseException, java.lang.Throwable) | ||
838 | 99 | |||
839 | 100 | try: | ||
840 | 101 | _isidentifier = str.isidentifier | ||
841 | 102 | except AttributeError: | ||
842 | 103 | # Python 2.X | ||
843 | 104 | import keyword | ||
844 | 105 | import re | ||
845 | 106 | regex = re.compile(r'^[a-z_][a-z0-9_]*$', re.I) | ||
846 | 107 | def _isidentifier(string): | ||
847 | 108 | if string in keyword.kwlist: | ||
848 | 109 | return False | ||
849 | 110 | return regex.match(string) | ||
850 | 111 | |||
851 | 112 | |||
852 | 113 | inPy3k = sys.version_info[0] == 3 | ||
853 | 114 | |||
854 | 115 | # Needed to work around Python 3 bug where use of "super" interferes with | ||
855 | 116 | # defining __class__ as a descriptor | ||
856 | 117 | _super = super | ||
857 | 118 | |||
858 | 119 | self = 'im_self' | ||
859 | 120 | builtin = '__builtin__' | ||
860 | 121 | if inPy3k: | ||
861 | 122 | self = '__self__' | ||
862 | 123 | builtin = 'builtins' | ||
863 | 124 | |||
864 | 125 | FILTER_DIR = True | ||
865 | 126 | |||
866 | 127 | |||
867 | 128 | def _is_instance_mock(obj): | ||
868 | 129 | # can't use isinstance on Mock objects because they override __class__ | ||
869 | 130 | # The base class for all mocks is NonCallableMock | ||
870 | 131 | return issubclass(type(obj), NonCallableMock) | ||
871 | 132 | |||
872 | 133 | |||
873 | 134 | def _is_exception(obj): | ||
874 | 135 | return ( | ||
875 | 136 | isinstance(obj, BaseExceptions) or | ||
876 | 137 | isinstance(obj, ClassTypes) and issubclass(obj, BaseExceptions) | ||
877 | 138 | ) | ||
878 | 139 | |||
879 | 140 | |||
880 | 141 | class _slotted(object): | ||
881 | 142 | __slots__ = ['a'] | ||
882 | 143 | |||
883 | 144 | |||
884 | 145 | DescriptorTypes = ( | ||
885 | 146 | type(_slotted.a), | ||
886 | 147 | property, | ||
887 | 148 | ) | ||
888 | 149 | |||
889 | 150 | |||
890 | 151 | def _getsignature(func, skipfirst, instance=False): | ||
891 | 152 | if inspect is None: | ||
892 | 153 | raise ImportError('inspect module not available') | ||
893 | 154 | |||
894 | 155 | if isinstance(func, ClassTypes) and not instance: | ||
895 | 156 | try: | ||
896 | 157 | func = func.__init__ | ||
897 | 158 | except AttributeError: | ||
898 | 159 | return | ||
899 | 160 | skipfirst = True | ||
900 | 161 | elif not isinstance(func, FunctionTypes): | ||
901 | 162 | # for classes where instance is True we end up here too | ||
902 | 163 | try: | ||
903 | 164 | func = func.__call__ | ||
904 | 165 | except AttributeError: | ||
905 | 166 | return | ||
906 | 167 | |||
907 | 168 | if inPy3k: | ||
908 | 169 | try: | ||
909 | 170 | argspec = inspect.getfullargspec(func) | ||
910 | 171 | except TypeError: | ||
911 | 172 | # C function / method, possibly inherited object().__init__ | ||
912 | 173 | return | ||
913 | 174 | regargs, varargs, varkw, defaults, kwonly, kwonlydef, ann = argspec | ||
914 | 175 | else: | ||
915 | 176 | try: | ||
916 | 177 | regargs, varargs, varkwargs, defaults = inspect.getargspec(func) | ||
917 | 178 | except TypeError: | ||
918 | 179 | # C function / method, possibly inherited object().__init__ | ||
919 | 180 | return | ||
920 | 181 | |||
921 | 182 | # instance methods and classmethods need to lose the self argument | ||
922 | 183 | if getattr(func, self, None) is not None: | ||
923 | 184 | regargs = regargs[1:] | ||
924 | 185 | if skipfirst: | ||
925 | 186 | # this condition and the above one are never both True - why? | ||
926 | 187 | regargs = regargs[1:] | ||
927 | 188 | |||
928 | 189 | if inPy3k: | ||
929 | 190 | signature = inspect.formatargspec( | ||
930 | 191 | regargs, varargs, varkw, defaults, | ||
931 | 192 | kwonly, kwonlydef, ann, formatvalue=lambda value: "") | ||
932 | 193 | else: | ||
933 | 194 | signature = inspect.formatargspec( | ||
934 | 195 | regargs, varargs, varkwargs, defaults, | ||
935 | 196 | formatvalue=lambda value: "") | ||
936 | 197 | return signature[1:-1], func | ||
937 | 198 | |||
938 | 199 | |||
939 | 200 | def _check_signature(func, mock, skipfirst, instance=False): | ||
940 | 201 | if not _callable(func): | ||
941 | 202 | return | ||
942 | 203 | |||
943 | 204 | result = _getsignature(func, skipfirst, instance) | ||
944 | 205 | if result is None: | ||
945 | 206 | return | ||
946 | 207 | signature, func = result | ||
947 | 208 | |||
948 | 209 | # can't use self because "self" is common as an argument name | ||
949 | 210 | # unfortunately even not in the first place | ||
950 | 211 | src = "lambda _mock_self, %s: None" % signature | ||
951 | 212 | checksig = eval(src, {}) | ||
952 | 213 | _copy_func_details(func, checksig) | ||
953 | 214 | type(mock)._mock_check_sig = checksig | ||
954 | 215 | |||
955 | 216 | |||
956 | 217 | def _copy_func_details(func, funcopy): | ||
957 | 218 | funcopy.__name__ = func.__name__ | ||
958 | 219 | funcopy.__doc__ = func.__doc__ | ||
959 | 220 | #funcopy.__dict__.update(func.__dict__) | ||
960 | 221 | funcopy.__module__ = func.__module__ | ||
961 | 222 | if not inPy3k: | ||
962 | 223 | funcopy.func_defaults = func.func_defaults | ||
963 | 224 | return | ||
964 | 225 | funcopy.__defaults__ = func.__defaults__ | ||
965 | 226 | funcopy.__kwdefaults__ = func.__kwdefaults__ | ||
966 | 227 | |||
967 | 228 | |||
968 | 229 | def _callable(obj): | ||
969 | 230 | if isinstance(obj, ClassTypes): | ||
970 | 231 | return True | ||
971 | 232 | if getattr(obj, '__call__', None) is not None: | ||
972 | 233 | return True | ||
973 | 234 | return False | ||
974 | 235 | |||
975 | 236 | |||
976 | 237 | def _is_list(obj): | ||
977 | 238 | # checks for list or tuples | ||
978 | 239 | # XXXX badly named! | ||
979 | 240 | return type(obj) in (list, tuple) | ||
980 | 241 | |||
981 | 242 | |||
982 | 243 | def _instance_callable(obj): | ||
983 | 244 | """Given an object, return True if the object is callable. | ||
984 | 245 | For classes, return True if instances would be callable.""" | ||
985 | 246 | if not isinstance(obj, ClassTypes): | ||
986 | 247 | # already an instance | ||
987 | 248 | return getattr(obj, '__call__', None) is not None | ||
988 | 249 | |||
989 | 250 | klass = obj | ||
990 | 251 | # uses __bases__ instead of __mro__ so that we work with old style classes | ||
991 | 252 | if klass.__dict__.get('__call__') is not None: | ||
992 | 253 | return True | ||
993 | 254 | |||
994 | 255 | for base in klass.__bases__: | ||
995 | 256 | if _instance_callable(base): | ||
996 | 257 | return True | ||
997 | 258 | return False | ||
998 | 259 | |||
999 | 260 | |||
1000 | 261 | def _set_signature(mock, original, instance=False): | ||
1001 | 262 | # creates a function with signature (*args, **kwargs) that delegates to a | ||
1002 | 263 | # mock. It still does signature checking by calling a lambda with the same | ||
1003 | 264 | # signature as the original. | ||
1004 | 265 | if not _callable(original): | ||
1005 | 266 | return | ||
1006 | 267 | |||
1007 | 268 | skipfirst = isinstance(original, ClassTypes) | ||
1008 | 269 | result = _getsignature(original, skipfirst, instance) | ||
1009 | 270 | if result is None: | ||
1010 | 271 | # was a C function (e.g. object().__init__ ) that can't be mocked | ||
1011 | 272 | return | ||
1012 | 273 | |||
1013 | 274 | signature, func = result | ||
1014 | 275 | |||
1015 | 276 | src = "lambda %s: None" % signature | ||
1016 | 277 | checksig = eval(src, {}) | ||
1017 | 278 | _copy_func_details(func, checksig) | ||
1018 | 279 | |||
1019 | 280 | name = original.__name__ | ||
1020 | 281 | if not _isidentifier(name): | ||
1021 | 282 | name = 'funcopy' | ||
1022 | 283 | context = {'_checksig_': checksig, 'mock': mock} | ||
1023 | 284 | src = """def %s(*args, **kwargs): | ||
1024 | 285 | _checksig_(*args, **kwargs) | ||
1025 | 286 | return mock(*args, **kwargs)""" % name | ||
1026 | 287 | exec (src, context) | ||
1027 | 288 | funcopy = context[name] | ||
1028 | 289 | _setup_func(funcopy, mock) | ||
1029 | 290 | return funcopy | ||
1030 | 291 | |||
1031 | 292 | |||
1032 | 293 | def _setup_func(funcopy, mock): | ||
1033 | 294 | funcopy.mock = mock | ||
1034 | 295 | |||
1035 | 296 | # can't use isinstance with mocks | ||
1036 | 297 | if not _is_instance_mock(mock): | ||
1037 | 298 | return | ||
1038 | 299 | |||
1039 | 300 | def assert_called_with(*args, **kwargs): | ||
1040 | 301 | return mock.assert_called_with(*args, **kwargs) | ||
1041 | 302 | def assert_called_once_with(*args, **kwargs): | ||
1042 | 303 | return mock.assert_called_once_with(*args, **kwargs) | ||
1043 | 304 | def assert_has_calls(*args, **kwargs): | ||
1044 | 305 | return mock.assert_has_calls(*args, **kwargs) | ||
1045 | 306 | def assert_any_call(*args, **kwargs): | ||
1046 | 307 | return mock.assert_any_call(*args, **kwargs) | ||
1047 | 308 | def reset_mock(): | ||
1048 | 309 | funcopy.method_calls = _CallList() | ||
1049 | 310 | funcopy.mock_calls = _CallList() | ||
1050 | 311 | mock.reset_mock() | ||
1051 | 312 | ret = funcopy.return_value | ||
1052 | 313 | if _is_instance_mock(ret) and not ret is mock: | ||
1053 | 314 | ret.reset_mock() | ||
1054 | 315 | |||
1055 | 316 | funcopy.called = False | ||
1056 | 317 | funcopy.call_count = 0 | ||
1057 | 318 | funcopy.call_args = None | ||
1058 | 319 | funcopy.call_args_list = _CallList() | ||
1059 | 320 | funcopy.method_calls = _CallList() | ||
1060 | 321 | funcopy.mock_calls = _CallList() | ||
1061 | 322 | |||
1062 | 323 | funcopy.return_value = mock.return_value | ||
1063 | 324 | funcopy.side_effect = mock.side_effect | ||
1064 | 325 | funcopy._mock_children = mock._mock_children | ||
1065 | 326 | |||
1066 | 327 | funcopy.assert_called_with = assert_called_with | ||
1067 | 328 | funcopy.assert_called_once_with = assert_called_once_with | ||
1068 | 329 | funcopy.assert_has_calls = assert_has_calls | ||
1069 | 330 | funcopy.assert_any_call = assert_any_call | ||
1070 | 331 | funcopy.reset_mock = reset_mock | ||
1071 | 332 | |||
1072 | 333 | mock._mock_delegate = funcopy | ||
1073 | 334 | |||
1074 | 335 | |||
1075 | 336 | def _is_magic(name): | ||
1076 | 337 | return '__%s__' % name[2:-2] == name | ||
1077 | 338 | |||
1078 | 339 | |||
1079 | 340 | class _SentinelObject(object): | ||
1080 | 341 | "A unique, named, sentinel object." | ||
1081 | 342 | def __init__(self, name): | ||
1082 | 343 | self.name = name | ||
1083 | 344 | |||
1084 | 345 | def __repr__(self): | ||
1085 | 346 | return 'sentinel.%s' % self.name | ||
1086 | 347 | |||
1087 | 348 | |||
1088 | 349 | class _Sentinel(object): | ||
1089 | 350 | """Access attributes to return a named object, usable as a sentinel.""" | ||
1090 | 351 | def __init__(self): | ||
1091 | 352 | self._sentinels = {} | ||
1092 | 353 | |||
1093 | 354 | def __getattr__(self, name): | ||
1094 | 355 | if name == '__bases__': | ||
1095 | 356 | # Without this help(mock) raises an exception | ||
1096 | 357 | raise AttributeError | ||
1097 | 358 | return self._sentinels.setdefault(name, _SentinelObject(name)) | ||
1098 | 359 | |||
1099 | 360 | |||
1100 | 361 | sentinel = _Sentinel() | ||
1101 | 362 | |||
1102 | 363 | DEFAULT = sentinel.DEFAULT | ||
1103 | 364 | _missing = sentinel.MISSING | ||
1104 | 365 | _deleted = sentinel.DELETED | ||
1105 | 366 | |||
1106 | 367 | |||
1107 | 368 | class OldStyleClass: | ||
1108 | 369 | pass | ||
1109 | 370 | ClassType = type(OldStyleClass) | ||
1110 | 371 | |||
1111 | 372 | |||
1112 | 373 | def _copy(value): | ||
1113 | 374 | if type(value) in (dict, list, tuple, set): | ||
1114 | 375 | return type(value)(value) | ||
1115 | 376 | return value | ||
1116 | 377 | |||
1117 | 378 | |||
1118 | 379 | ClassTypes = (type,) | ||
1119 | 380 | if not inPy3k: | ||
1120 | 381 | ClassTypes = (type, ClassType) | ||
1121 | 382 | |||
1122 | 383 | _allowed_names = set( | ||
1123 | 384 | [ | ||
1124 | 385 | 'return_value', '_mock_return_value', 'side_effect', | ||
1125 | 386 | '_mock_side_effect', '_mock_parent', '_mock_new_parent', | ||
1126 | 387 | '_mock_name', '_mock_new_name' | ||
1127 | 388 | ] | ||
1128 | 389 | ) | ||
1129 | 390 | |||
1130 | 391 | |||
1131 | 392 | def _delegating_property(name): | ||
1132 | 393 | _allowed_names.add(name) | ||
1133 | 394 | _the_name = '_mock_' + name | ||
1134 | 395 | def _get(self, name=name, _the_name=_the_name): | ||
1135 | 396 | sig = self._mock_delegate | ||
1136 | 397 | if sig is None: | ||
1137 | 398 | return getattr(self, _the_name) | ||
1138 | 399 | return getattr(sig, name) | ||
1139 | 400 | def _set(self, value, name=name, _the_name=_the_name): | ||
1140 | 401 | sig = self._mock_delegate | ||
1141 | 402 | if sig is None: | ||
1142 | 403 | self.__dict__[_the_name] = value | ||
1143 | 404 | else: | ||
1144 | 405 | setattr(sig, name, value) | ||
1145 | 406 | |||
1146 | 407 | return property(_get, _set) | ||
1147 | 408 | |||
1148 | 409 | |||
1149 | 410 | |||
1150 | 411 | class _CallList(list): | ||
1151 | 412 | |||
1152 | 413 | def __contains__(self, value): | ||
1153 | 414 | if not isinstance(value, list): | ||
1154 | 415 | return list.__contains__(self, value) | ||
1155 | 416 | len_value = len(value) | ||
1156 | 417 | len_self = len(self) | ||
1157 | 418 | if len_value > len_self: | ||
1158 | 419 | return False | ||
1159 | 420 | |||
1160 | 421 | for i in range(0, len_self - len_value + 1): | ||
1161 | 422 | sub_list = self[i:i+len_value] | ||
1162 | 423 | if sub_list == value: | ||
1163 | 424 | return True | ||
1164 | 425 | return False | ||
1165 | 426 | |||
1166 | 427 | def __repr__(self): | ||
1167 | 428 | return pprint.pformat(list(self)) | ||
1168 | 429 | |||
1169 | 430 | |||
1170 | 431 | def _check_and_set_parent(parent, value, name, new_name): | ||
1171 | 432 | if not _is_instance_mock(value): | ||
1172 | 433 | return False | ||
1173 | 434 | if ((value._mock_name or value._mock_new_name) or | ||
1174 | 435 | (value._mock_parent is not None) or | ||
1175 | 436 | (value._mock_new_parent is not None)): | ||
1176 | 437 | return False | ||
1177 | 438 | |||
1178 | 439 | _parent = parent | ||
1179 | 440 | while _parent is not None: | ||
1180 | 441 | # setting a mock (value) as a child or return value of itself | ||
1181 | 442 | # should not modify the mock | ||
1182 | 443 | if _parent is value: | ||
1183 | 444 | return False | ||
1184 | 445 | _parent = _parent._mock_new_parent | ||
1185 | 446 | |||
1186 | 447 | if new_name: | ||
1187 | 448 | value._mock_new_parent = parent | ||
1188 | 449 | value._mock_new_name = new_name | ||
1189 | 450 | if name: | ||
1190 | 451 | value._mock_parent = parent | ||
1191 | 452 | value._mock_name = name | ||
1192 | 453 | return True | ||
1193 | 454 | |||
1194 | 455 | |||
1195 | 456 | |||
1196 | 457 | class Base(object): | ||
1197 | 458 | _mock_return_value = DEFAULT | ||
1198 | 459 | _mock_side_effect = None | ||
1199 | 460 | def __init__(self, *args, **kwargs): | ||
1200 | 461 | pass | ||
1201 | 462 | |||
1202 | 463 | |||
1203 | 464 | |||
1204 | 465 | class NonCallableMock(Base): | ||
1205 | 466 | """A non-callable version of `Mock`""" | ||
1206 | 467 | |||
1207 | 468 | def __new__(cls, *args, **kw): | ||
1208 | 469 | # every instance has its own class | ||
1209 | 470 | # so we can create magic methods on the | ||
1210 | 471 | # class without stomping on other mocks | ||
1211 | 472 | new = type(cls.__name__, (cls,), {'__doc__': cls.__doc__}) | ||
1212 | 473 | instance = object.__new__(new) | ||
1213 | 474 | return instance | ||
1214 | 475 | |||
1215 | 476 | |||
1216 | 477 | def __init__( | ||
1217 | 478 | self, spec=None, wraps=None, name=None, spec_set=None, | ||
1218 | 479 | parent=None, _spec_state=None, _new_name='', _new_parent=None, | ||
1219 | 480 | **kwargs | ||
1220 | 481 | ): | ||
1221 | 482 | if _new_parent is None: | ||
1222 | 483 | _new_parent = parent | ||
1223 | 484 | |||
1224 | 485 | __dict__ = self.__dict__ | ||
1225 | 486 | __dict__['_mock_parent'] = parent | ||
1226 | 487 | __dict__['_mock_name'] = name | ||
1227 | 488 | __dict__['_mock_new_name'] = _new_name | ||
1228 | 489 | __dict__['_mock_new_parent'] = _new_parent | ||
1229 | 490 | |||
1230 | 491 | if spec_set is not None: | ||
1231 | 492 | spec = spec_set | ||
1232 | 493 | spec_set = True | ||
1233 | 494 | |||
1234 | 495 | self._mock_add_spec(spec, spec_set) | ||
1235 | 496 | |||
1236 | 497 | __dict__['_mock_children'] = {} | ||
1237 | 498 | __dict__['_mock_wraps'] = wraps | ||
1238 | 499 | __dict__['_mock_delegate'] = None | ||
1239 | 500 | |||
1240 | 501 | __dict__['_mock_called'] = False | ||
1241 | 502 | __dict__['_mock_call_args'] = None | ||
1242 | 503 | __dict__['_mock_call_count'] = 0 | ||
1243 | 504 | __dict__['_mock_call_args_list'] = _CallList() | ||
1244 | 505 | __dict__['_mock_mock_calls'] = _CallList() | ||
1245 | 506 | |||
1246 | 507 | __dict__['method_calls'] = _CallList() | ||
1247 | 508 | |||
1248 | 509 | if kwargs: | ||
1249 | 510 | self.configure_mock(**kwargs) | ||
1250 | 511 | |||
1251 | 512 | _super(NonCallableMock, self).__init__( | ||
1252 | 513 | spec, wraps, name, spec_set, parent, | ||
1253 | 514 | _spec_state | ||
1254 | 515 | ) | ||
1255 | 516 | |||
1256 | 517 | |||
1257 | 518 | def attach_mock(self, mock, attribute): | ||
1258 | 519 | """ | ||
1259 | 520 | Attach a mock as an attribute of this one, replacing its name and | ||
1260 | 521 | parent. Calls to the attached mock will be recorded in the | ||
1261 | 522 | `method_calls` and `mock_calls` attributes of this one.""" | ||
1262 | 523 | mock._mock_parent = None | ||
1263 | 524 | mock._mock_new_parent = None | ||
1264 | 525 | mock._mock_name = '' | ||
1265 | 526 | mock._mock_new_name = None | ||
1266 | 527 | |||
1267 | 528 | setattr(self, attribute, mock) | ||
1268 | 529 | |||
1269 | 530 | |||
1270 | 531 | def mock_add_spec(self, spec, spec_set=False): | ||
1271 | 532 | """Add a spec to a mock. `spec` can either be an object or a | ||
1272 | 533 | list of strings. Only attributes on the `spec` can be fetched as | ||
1273 | 534 | attributes from the mock. | ||
1274 | 535 | |||
1275 | 536 | If `spec_set` is True then only attributes on the spec can be set.""" | ||
1276 | 537 | self._mock_add_spec(spec, spec_set) | ||
1277 | 538 | |||
1278 | 539 | |||
1279 | 540 | def _mock_add_spec(self, spec, spec_set): | ||
1280 | 541 | _spec_class = None | ||
1281 | 542 | |||
1282 | 543 | if spec is not None and not _is_list(spec): | ||
1283 | 544 | if isinstance(spec, ClassTypes): | ||
1284 | 545 | _spec_class = spec | ||
1285 | 546 | else: | ||
1286 | 547 | _spec_class = _get_class(spec) | ||
1287 | 548 | |||
1288 | 549 | spec = dir(spec) | ||
1289 | 550 | |||
1290 | 551 | __dict__ = self.__dict__ | ||
1291 | 552 | __dict__['_spec_class'] = _spec_class | ||
1292 | 553 | __dict__['_spec_set'] = spec_set | ||
1293 | 554 | __dict__['_mock_methods'] = spec | ||
1294 | 555 | |||
1295 | 556 | |||
1296 | 557 | def __get_return_value(self): | ||
1297 | 558 | ret = self._mock_return_value | ||
1298 | 559 | if self._mock_delegate is not None: | ||
1299 | 560 | ret = self._mock_delegate.return_value | ||
1300 | 561 | |||
1301 | 562 | if ret is DEFAULT: | ||
1302 | 563 | ret = self._get_child_mock( | ||
1303 | 564 | _new_parent=self, _new_name='()' | ||
1304 | 565 | ) | ||
1305 | 566 | self.return_value = ret | ||
1306 | 567 | return ret | ||
1307 | 568 | |||
1308 | 569 | |||
1309 | 570 | def __set_return_value(self, value): | ||
1310 | 571 | if self._mock_delegate is not None: | ||
1311 | 572 | self._mock_delegate.return_value = value | ||
1312 | 573 | else: | ||
1313 | 574 | self._mock_return_value = value | ||
1314 | 575 | _check_and_set_parent(self, value, None, '()') | ||
1315 | 576 | |||
1316 | 577 | __return_value_doc = "The value to be returned when the mock is called." | ||
1317 | 578 | return_value = property(__get_return_value, __set_return_value, | ||
1318 | 579 | __return_value_doc) | ||
1319 | 580 | |||
1320 | 581 | |||
1321 | 582 | @property | ||
1322 | 583 | def __class__(self): | ||
1323 | 584 | if self._spec_class is None: | ||
1324 | 585 | return type(self) | ||
1325 | 586 | return self._spec_class | ||
1326 | 587 | |||
1327 | 588 | called = _delegating_property('called') | ||
1328 | 589 | call_count = _delegating_property('call_count') | ||
1329 | 590 | call_args = _delegating_property('call_args') | ||
1330 | 591 | call_args_list = _delegating_property('call_args_list') | ||
1331 | 592 | mock_calls = _delegating_property('mock_calls') | ||
1332 | 593 | |||
1333 | 594 | |||
1334 | 595 | def __get_side_effect(self): | ||
1335 | 596 | sig = self._mock_delegate | ||
1336 | 597 | if sig is None: | ||
1337 | 598 | return self._mock_side_effect | ||
1338 | 599 | return sig.side_effect | ||
1339 | 600 | |||
1340 | 601 | def __set_side_effect(self, value): | ||
1341 | 602 | value = _try_iter(value) | ||
1342 | 603 | sig = self._mock_delegate | ||
1343 | 604 | if sig is None: | ||
1344 | 605 | self._mock_side_effect = value | ||
1345 | 606 | else: | ||
1346 | 607 | sig.side_effect = value | ||
1347 | 608 | |||
1348 | 609 | side_effect = property(__get_side_effect, __set_side_effect) | ||
1349 | 610 | |||
1350 | 611 | |||
1351 | 612 | def reset_mock(self): | ||
1352 | 613 | "Restore the mock object to its initial state." | ||
1353 | 614 | self.called = False | ||
1354 | 615 | self.call_args = None | ||
1355 | 616 | self.call_count = 0 | ||
1356 | 617 | self.mock_calls = _CallList() | ||
1357 | 618 | self.call_args_list = _CallList() | ||
1358 | 619 | self.method_calls = _CallList() | ||
1359 | 620 | |||
1360 | 621 | for child in self._mock_children.values(): | ||
1361 | 622 | if isinstance(child, _SpecState): | ||
1362 | 623 | continue | ||
1363 | 624 | child.reset_mock() | ||
1364 | 625 | |||
1365 | 626 | ret = self._mock_return_value | ||
1366 | 627 | if _is_instance_mock(ret) and ret is not self: | ||
1367 | 628 | ret.reset_mock() | ||
1368 | 629 | |||
1369 | 630 | |||
1370 | 631 | def configure_mock(self, **kwargs): | ||
1371 | 632 | """Set attributes on the mock through keyword arguments. | ||
1372 | 633 | |||
1373 | 634 | Attributes plus return values and side effects can be set on child | ||
1374 | 635 | mocks using standard dot notation and unpacking a dictionary in the | ||
1375 | 636 | method call: | ||
1376 | 637 | |||
1377 | 638 | >>> attrs = {'method.return_value': 3, 'other.side_effect': KeyError} | ||
1378 | 639 | >>> mock.configure_mock(**attrs)""" | ||
1379 | 640 | for arg, val in sorted(kwargs.items(), | ||
1380 | 641 | # we sort on the number of dots so that | ||
1381 | 642 | # attributes are set before we set attributes on | ||
1382 | 643 | # attributes | ||
1383 | 644 | key=lambda entry: entry[0].count('.')): | ||
1384 | 645 | args = arg.split('.') | ||
1385 | 646 | final = args.pop() | ||
1386 | 647 | obj = self | ||
1387 | 648 | for entry in args: | ||
1388 | 649 | obj = getattr(obj, entry) | ||
1389 | 650 | setattr(obj, final, val) | ||
1390 | 651 | |||
1391 | 652 | |||
1392 | 653 | def __getattr__(self, name): | ||
1393 | 654 | if name == '_mock_methods': | ||
1394 | 655 | raise AttributeError(name) | ||
1395 | 656 | elif self._mock_methods is not None: | ||
1396 | 657 | if name not in self._mock_methods or name in _all_magics: | ||
1397 | 658 | raise AttributeError("Mock object has no attribute %r" % name) | ||
1398 | 659 | elif _is_magic(name): | ||
1399 | 660 | raise AttributeError(name) | ||
1400 | 661 | |||
1401 | 662 | result = self._mock_children.get(name) | ||
1402 | 663 | if result is _deleted: | ||
1403 | 664 | raise AttributeError(name) | ||
1404 | 665 | elif result is None: | ||
1405 | 666 | wraps = None | ||
1406 | 667 | if self._mock_wraps is not None: | ||
1407 | 668 | # XXXX should we get the attribute without triggering code | ||
1408 | 669 | # execution? | ||
1409 | 670 | wraps = getattr(self._mock_wraps, name) | ||
1410 | 671 | |||
1411 | 672 | result = self._get_child_mock( | ||
1412 | 673 | parent=self, name=name, wraps=wraps, _new_name=name, | ||
1413 | 674 | _new_parent=self | ||
1414 | 675 | ) | ||
1415 | 676 | self._mock_children[name] = result | ||
1416 | 677 | |||
1417 | 678 | elif isinstance(result, _SpecState): | ||
1418 | 679 | result = create_autospec( | ||
1419 | 680 | result.spec, result.spec_set, result.instance, | ||
1420 | 681 | result.parent, result.name | ||
1421 | 682 | ) | ||
1422 | 683 | self._mock_children[name] = result | ||
1423 | 684 | |||
1424 | 685 | return result | ||
1425 | 686 | |||
1426 | 687 | |||
1427 | 688 | def __repr__(self): | ||
1428 | 689 | _name_list = [self._mock_new_name] | ||
1429 | 690 | _parent = self._mock_new_parent | ||
1430 | 691 | last = self | ||
1431 | 692 | |||
1432 | 693 | dot = '.' | ||
1433 | 694 | if _name_list == ['()']: | ||
1434 | 695 | dot = '' | ||
1435 | 696 | seen = set() | ||
1436 | 697 | while _parent is not None: | ||
1437 | 698 | last = _parent | ||
1438 | 699 | |||
1439 | 700 | _name_list.append(_parent._mock_new_name + dot) | ||
1440 | 701 | dot = '.' | ||
1441 | 702 | if _parent._mock_new_name == '()': | ||
1442 | 703 | dot = '' | ||
1443 | 704 | |||
1444 | 705 | _parent = _parent._mock_new_parent | ||
1445 | 706 | |||
1446 | 707 | # use ids here so as not to call __hash__ on the mocks | ||
1447 | 708 | if id(_parent) in seen: | ||
1448 | 709 | break | ||
1449 | 710 | seen.add(id(_parent)) | ||
1450 | 711 | |||
1451 | 712 | _name_list = list(reversed(_name_list)) | ||
1452 | 713 | _first = last._mock_name or 'mock' | ||
1453 | 714 | if len(_name_list) > 1: | ||
1454 | 715 | if _name_list[1] not in ('()', '().'): | ||
1455 | 716 | _first += '.' | ||
1456 | 717 | _name_list[0] = _first | ||
1457 | 718 | name = ''.join(_name_list) | ||
1458 | 719 | |||
1459 | 720 | name_string = '' | ||
1460 | 721 | if name not in ('mock', 'mock.'): | ||
1461 | 722 | name_string = ' name=%r' % name | ||
1462 | 723 | |||
1463 | 724 | spec_string = '' | ||
1464 | 725 | if self._spec_class is not None: | ||
1465 | 726 | spec_string = ' spec=%r' | ||
1466 | 727 | if self._spec_set: | ||
1467 | 728 | spec_string = ' spec_set=%r' | ||
1468 | 729 | spec_string = spec_string % self._spec_class.__name__ | ||
1469 | 730 | return "<%s%s%s id='%s'>" % ( | ||
1470 | 731 | type(self).__name__, | ||
1471 | 732 | name_string, | ||
1472 | 733 | spec_string, | ||
1473 | 734 | id(self) | ||
1474 | 735 | ) | ||
1475 | 736 | |||
1476 | 737 | |||
1477 | 738 | def __dir__(self): | ||
1478 | 739 | """Filter the output of `dir(mock)` to only useful members. | ||
1479 | 740 | XXXX | ||
1480 | 741 | """ | ||
1481 | 742 | extras = self._mock_methods or [] | ||
1482 | 743 | from_type = dir(type(self)) | ||
1483 | 744 | from_dict = list(self.__dict__) | ||
1484 | 745 | |||
1485 | 746 | if FILTER_DIR: | ||
1486 | 747 | from_type = [e for e in from_type if not e.startswith('_')] | ||
1487 | 748 | from_dict = [e for e in from_dict if not e.startswith('_') or | ||
1488 | 749 | _is_magic(e)] | ||
1489 | 750 | return sorted(set(extras + from_type + from_dict + | ||
1490 | 751 | list(self._mock_children))) | ||
1491 | 752 | |||
1492 | 753 | |||
1493 | 754 | def __setattr__(self, name, value): | ||
1494 | 755 | if name in _allowed_names: | ||
1495 | 756 | # property setters go through here | ||
1496 | 757 | return object.__setattr__(self, name, value) | ||
1497 | 758 | elif (self._spec_set and self._mock_methods is not None and | ||
1498 | 759 | name not in self._mock_methods and | ||
1499 | 760 | name not in self.__dict__): | ||
1500 | 761 | raise AttributeError("Mock object has no attribute '%s'" % name) | ||
1501 | 762 | elif name in _unsupported_magics: | ||
1502 | 763 | msg = 'Attempting to set unsupported magic method %r.' % name | ||
1503 | 764 | raise AttributeError(msg) | ||
1504 | 765 | elif name in _all_magics: | ||
1505 | 766 | if self._mock_methods is not None and name not in self._mock_methods: | ||
1506 | 767 | raise AttributeError("Mock object has no attribute '%s'" % name) | ||
1507 | 768 | |||
1508 | 769 | if not _is_instance_mock(value): | ||
1509 | 770 | setattr(type(self), name, _get_method(name, value)) | ||
1510 | 771 | original = value | ||
1511 | 772 | value = lambda *args, **kw: original(self, *args, **kw) | ||
1512 | 773 | else: | ||
1513 | 774 | # only set _new_name and not name so that mock_calls is tracked | ||
1514 | 775 | # but not method calls | ||
1515 | 776 | _check_and_set_parent(self, value, None, name) | ||
1516 | 777 | setattr(type(self), name, value) | ||
1517 | 778 | self._mock_children[name] = value | ||
1518 | 779 | elif name == '__class__': | ||
1519 | 780 | self._spec_class = value | ||
1520 | 781 | return | ||
1521 | 782 | else: | ||
1522 | 783 | if _check_and_set_parent(self, value, name, name): | ||
1523 | 784 | self._mock_children[name] = value | ||
1524 | 785 | return object.__setattr__(self, name, value) | ||
1525 | 786 | |||
1526 | 787 | |||
1527 | 788 | def __delattr__(self, name): | ||
1528 | 789 | if name in _all_magics and name in type(self).__dict__: | ||
1529 | 790 | delattr(type(self), name) | ||
1530 | 791 | if name not in self.__dict__: | ||
1531 | 792 | # for magic methods that are still MagicProxy objects and | ||
1532 | 793 | # not set on the instance itself | ||
1533 | 794 | return | ||
1534 | 795 | |||
1535 | 796 | if name in self.__dict__: | ||
1536 | 797 | object.__delattr__(self, name) | ||
1537 | 798 | |||
1538 | 799 | obj = self._mock_children.get(name, _missing) | ||
1539 | 800 | if obj is _deleted: | ||
1540 | 801 | raise AttributeError(name) | ||
1541 | 802 | if obj is not _missing: | ||
1542 | 803 | del self._mock_children[name] | ||
1543 | 804 | self._mock_children[name] = _deleted | ||
1544 | 805 | |||
1545 | 806 | |||
1546 | 807 | |||
1547 | 808 | def _format_mock_call_signature(self, args, kwargs): | ||
1548 | 809 | name = self._mock_name or 'mock' | ||
1549 | 810 | return _format_call_signature(name, args, kwargs) | ||
1550 | 811 | |||
1551 | 812 | |||
1552 | 813 | def _format_mock_failure_message(self, args, kwargs): | ||
1553 | 814 | message = 'Expected call: %s\nActual call: %s' | ||
1554 | 815 | expected_string = self._format_mock_call_signature(args, kwargs) | ||
1555 | 816 | call_args = self.call_args | ||
1556 | 817 | if len(call_args) == 3: | ||
1557 | 818 | call_args = call_args[1:] | ||
1558 | 819 | actual_string = self._format_mock_call_signature(*call_args) | ||
1559 | 820 | return message % (expected_string, actual_string) | ||
1560 | 821 | |||
1561 | 822 | |||
1562 | 823 | def assert_called_with(_mock_self, *args, **kwargs): | ||
1563 | 824 | """assert that the mock was called with the specified arguments. | ||
1564 | 825 | |||
1565 | 826 | Raises an AssertionError if the args and keyword args passed in are | ||
1566 | 827 | different to the last call to the mock.""" | ||
1567 | 828 | self = _mock_self | ||
1568 | 829 | if self.call_args is None: | ||
1569 | 830 | expected = self._format_mock_call_signature(args, kwargs) | ||
1570 | 831 | raise AssertionError('Expected call: %s\nNot called' % (expected,)) | ||
1571 | 832 | |||
1572 | 833 | if self.call_args != (args, kwargs): | ||
1573 | 834 | msg = self._format_mock_failure_message(args, kwargs) | ||
1574 | 835 | raise AssertionError(msg) | ||
1575 | 836 | |||
1576 | 837 | |||
1577 | 838 | def assert_called_once_with(_mock_self, *args, **kwargs): | ||
1578 | 839 | """assert that the mock was called exactly once and with the specified | ||
1579 | 840 | arguments.""" | ||
1580 | 841 | self = _mock_self | ||
1581 | 842 | if not self.call_count == 1: | ||
1582 | 843 | msg = ("Expected to be called once. Called %s times." % | ||
1583 | 844 | self.call_count) | ||
1584 | 845 | raise AssertionError(msg) | ||
1585 | 846 | return self.assert_called_with(*args, **kwargs) | ||
1586 | 847 | |||
1587 | 848 | |||
1588 | 849 | def assert_has_calls(self, calls, any_order=False): | ||
1589 | 850 | """assert the mock has been called with the specified calls. | ||
1590 | 851 | The `mock_calls` list is checked for the calls. | ||
1591 | 852 | |||
1592 | 853 | If `any_order` is False (the default) then the calls must be | ||
1593 | 854 | sequential. There can be extra calls before or after the | ||
1594 | 855 | specified calls. | ||
1595 | 856 | |||
1596 | 857 | If `any_order` is True then the calls can be in any order, but | ||
1597 | 858 | they must all appear in `mock_calls`.""" | ||
1598 | 859 | if not any_order: | ||
1599 | 860 | if calls not in self.mock_calls: | ||
1600 | 861 | raise AssertionError( | ||
1601 | 862 | 'Calls not found.\nExpected: %r\n' | ||
1602 | 863 | 'Actual: %r' % (calls, self.mock_calls) | ||
1603 | 864 | ) | ||
1604 | 865 | return | ||
1605 | 866 | |||
1606 | 867 | all_calls = list(self.mock_calls) | ||
1607 | 868 | |||
1608 | 869 | not_found = [] | ||
1609 | 870 | for kall in calls: | ||
1610 | 871 | try: | ||
1611 | 872 | all_calls.remove(kall) | ||
1612 | 873 | except ValueError: | ||
1613 | 874 | not_found.append(kall) | ||
1614 | 875 | if not_found: | ||
1615 | 876 | raise AssertionError( | ||
1616 | 877 | '%r not all found in call list' % (tuple(not_found),) | ||
1617 | 878 | ) | ||
1618 | 879 | |||
1619 | 880 | |||
1620 | 881 | def assert_any_call(self, *args, **kwargs): | ||
1621 | 882 | """assert the mock has been called with the specified arguments. | ||
1622 | 883 | |||
1623 | 884 | The assert passes if the mock has *ever* been called, unlike | ||
1624 | 885 | `assert_called_with` and `assert_called_once_with` that only pass if | ||
1625 | 886 | the call is the most recent one.""" | ||
1626 | 887 | kall = call(*args, **kwargs) | ||
1627 | 888 | if kall not in self.call_args_list: | ||
1628 | 889 | expected_string = self._format_mock_call_signature(args, kwargs) | ||
1629 | 890 | raise AssertionError( | ||
1630 | 891 | '%s call not found' % expected_string | ||
1631 | 892 | ) | ||
1632 | 893 | |||
1633 | 894 | |||
1634 | 895 | def _get_child_mock(self, **kw): | ||
1635 | 896 | """Create the child mocks for attributes and return value. | ||
1636 | 897 | By default child mocks will be the same type as the parent. | ||
1637 | 898 | Subclasses of Mock may want to override this to customize the way | ||
1638 | 899 | child mocks are made. | ||
1639 | 900 | |||
1640 | 901 | For non-callable mocks the callable variant will be used (rather than | ||
1641 | 902 | any custom subclass).""" | ||
1642 | 903 | _type = type(self) | ||
1643 | 904 | if not issubclass(_type, CallableMixin): | ||
1644 | 905 | if issubclass(_type, NonCallableMagicMock): | ||
1645 | 906 | klass = MagicMock | ||
1646 | 907 | elif issubclass(_type, NonCallableMock) : | ||
1647 | 908 | klass = Mock | ||
1648 | 909 | else: | ||
1649 | 910 | klass = _type.__mro__[1] | ||
1650 | 911 | return klass(**kw) | ||
1651 | 912 | |||
1652 | 913 | |||
1653 | 914 | |||
1654 | 915 | def _try_iter(obj): | ||
1655 | 916 | if obj is None: | ||
1656 | 917 | return obj | ||
1657 | 918 | if _is_exception(obj): | ||
1658 | 919 | return obj | ||
1659 | 920 | if _callable(obj): | ||
1660 | 921 | return obj | ||
1661 | 922 | try: | ||
1662 | 923 | return iter(obj) | ||
1663 | 924 | except TypeError: | ||
1664 | 925 | # XXXX backwards compatibility | ||
1665 | 926 | # but this will blow up on first call - so maybe we should fail early? | ||
1666 | 927 | return obj | ||
1667 | 928 | |||
1668 | 929 | |||
1669 | 930 | |||
1670 | 931 | class CallableMixin(Base): | ||
1671 | 932 | |||
1672 | 933 | def __init__(self, spec=None, side_effect=None, return_value=DEFAULT, | ||
1673 | 934 | wraps=None, name=None, spec_set=None, parent=None, | ||
1674 | 935 | _spec_state=None, _new_name='', _new_parent=None, **kwargs): | ||
1675 | 936 | self.__dict__['_mock_return_value'] = return_value | ||
1676 | 937 | |||
1677 | 938 | _super(CallableMixin, self).__init__( | ||
1678 | 939 | spec, wraps, name, spec_set, parent, | ||
1679 | 940 | _spec_state, _new_name, _new_parent, **kwargs | ||
1680 | 941 | ) | ||
1681 | 942 | |||
1682 | 943 | self.side_effect = side_effect | ||
1683 | 944 | |||
1684 | 945 | |||
1685 | 946 | def _mock_check_sig(self, *args, **kwargs): | ||
1686 | 947 | # stub method that can be replaced with one with a specific signature | ||
1687 | 948 | pass | ||
1688 | 949 | |||
1689 | 950 | |||
1690 | 951 | def __call__(_mock_self, *args, **kwargs): | ||
1691 | 952 | # can't use self in-case a function / method we are mocking uses self | ||
1692 | 953 | # in the signature | ||
1693 | 954 | _mock_self._mock_check_sig(*args, **kwargs) | ||
1694 | 955 | return _mock_self._mock_call(*args, **kwargs) | ||
1695 | 956 | |||
1696 | 957 | |||
1697 | 958 | def _mock_call(_mock_self, *args, **kwargs): | ||
1698 | 959 | self = _mock_self | ||
1699 | 960 | self.called = True | ||
1700 | 961 | self.call_count += 1 | ||
1701 | 962 | self.call_args = _Call((args, kwargs), two=True) | ||
1702 | 963 | self.call_args_list.append(_Call((args, kwargs), two=True)) | ||
1703 | 964 | |||
1704 | 965 | _new_name = self._mock_new_name | ||
1705 | 966 | _new_parent = self._mock_new_parent | ||
1706 | 967 | self.mock_calls.append(_Call(('', args, kwargs))) | ||
1707 | 968 | |||
1708 | 969 | seen = set() | ||
1709 | 970 | skip_next_dot = _new_name == '()' | ||
1710 | 971 | do_method_calls = self._mock_parent is not None | ||
1711 | 972 | name = self._mock_name | ||
1712 | 973 | while _new_parent is not None: | ||
1713 | 974 | this_mock_call = _Call((_new_name, args, kwargs)) | ||
1714 | 975 | if _new_parent._mock_new_name: | ||
1715 | 976 | dot = '.' | ||
1716 | 977 | if skip_next_dot: | ||
1717 | 978 | dot = '' | ||
1718 | 979 | |||
1719 | 980 | skip_next_dot = False | ||
1720 | 981 | if _new_parent._mock_new_name == '()': | ||
1721 | 982 | skip_next_dot = True | ||
1722 | 983 | |||
1723 | 984 | _new_name = _new_parent._mock_new_name + dot + _new_name | ||
1724 | 985 | |||
1725 | 986 | if do_method_calls: | ||
1726 | 987 | if _new_name == name: | ||
1727 | 988 | this_method_call = this_mock_call | ||
1728 | 989 | else: | ||
1729 | 990 | this_method_call = _Call((name, args, kwargs)) | ||
1730 | 991 | _new_parent.method_calls.append(this_method_call) | ||
1731 | 992 | |||
1732 | 993 | do_method_calls = _new_parent._mock_parent is not None | ||
1733 | 994 | if do_method_calls: | ||
1734 | 995 | name = _new_parent._mock_name + '.' + name | ||
1735 | 996 | |||
1736 | 997 | _new_parent.mock_calls.append(this_mock_call) | ||
1737 | 998 | _new_parent = _new_parent._mock_new_parent | ||
1738 | 999 | |||
1739 | 1000 | # use ids here so as not to call __hash__ on the mocks | ||
1740 | 1001 | _new_parent_id = id(_new_parent) | ||
1741 | 1002 | if _new_parent_id in seen: | ||
1742 | 1003 | break | ||
1743 | 1004 | seen.add(_new_parent_id) | ||
1744 | 1005 | |||
1745 | 1006 | ret_val = DEFAULT | ||
1746 | 1007 | effect = self.side_effect | ||
1747 | 1008 | if effect is not None: | ||
1748 | 1009 | if _is_exception(effect): | ||
1749 | 1010 | raise effect | ||
1750 | 1011 | |||
1751 | 1012 | if not _callable(effect): | ||
1752 | 1013 | result = next(effect) | ||
1753 | 1014 | if _is_exception(result): | ||
1754 | 1015 | raise result | ||
1755 | 1016 | return result | ||
1756 | 1017 | |||
1757 | 1018 | ret_val = effect(*args, **kwargs) | ||
1758 | 1019 | if ret_val is DEFAULT: | ||
1759 | 1020 | ret_val = self.return_value | ||
1760 | 1021 | |||
1761 | 1022 | if (self._mock_wraps is not None and | ||
1762 | 1023 | self._mock_return_value is DEFAULT): | ||
1763 | 1024 | return self._mock_wraps(*args, **kwargs) | ||
1764 | 1025 | if ret_val is DEFAULT: | ||
1765 | 1026 | ret_val = self.return_value | ||
1766 | 1027 | return ret_val | ||
1767 | 1028 | |||
1768 | 1029 | |||
1769 | 1030 | |||
1770 | 1031 | class Mock(CallableMixin, NonCallableMock): | ||
1771 | 1032 | """ | ||
1772 | 1033 | Create a new `Mock` object. `Mock` takes several optional arguments | ||
1773 | 1034 | that specify the behaviour of the Mock object: | ||
1774 | 1035 | |||
1775 | 1036 | * `spec`: This can be either a list of strings or an existing object (a | ||
1776 | 1037 | class or instance) that acts as the specification for the mock object. If | ||
1777 | 1038 | you pass in an object then a list of strings is formed by calling dir on | ||
1778 | 1039 | the object (excluding unsupported magic attributes and methods). Accessing | ||
1779 | 1040 | any attribute not in this list will raise an `AttributeError`. | ||
1780 | 1041 | |||
1781 | 1042 | If `spec` is an object (rather than a list of strings) then | ||
1782 | 1043 | `mock.__class__` returns the class of the spec object. This allows mocks | ||
1783 | 1044 | to pass `isinstance` tests. | ||
1784 | 1045 | |||
1785 | 1046 | * `spec_set`: A stricter variant of `spec`. If used, attempting to *set* | ||
1786 | 1047 | or get an attribute on the mock that isn't on the object passed as | ||
1787 | 1048 | `spec_set` will raise an `AttributeError`. | ||
1788 | 1049 | |||
1789 | 1050 | * `side_effect`: A function to be called whenever the Mock is called. See | ||
1790 | 1051 | the `side_effect` attribute. Useful for raising exceptions or | ||
1791 | 1052 | dynamically changing return values. The function is called with the same | ||
1792 | 1053 | arguments as the mock, and unless it returns `DEFAULT`, the return | ||
1793 | 1054 | value of this function is used as the return value. | ||
1794 | 1055 | |||
1795 | 1056 | Alternatively `side_effect` can be an exception class or instance. In | ||
1796 | 1057 | this case the exception will be raised when the mock is called. | ||
1797 | 1058 | |||
1798 | 1059 | If `side_effect` is an iterable then each call to the mock will return | ||
1799 | 1060 | the next value from the iterable. If any of the members of the iterable | ||
1800 | 1061 | are exceptions they will be raised instead of returned. | ||
1801 | 1062 | |||
1802 | 1063 | * `return_value`: The value returned when the mock is called. By default | ||
1803 | 1064 | this is a new Mock (created on first access). See the | ||
1804 | 1065 | `return_value` attribute. | ||
1805 | 1066 | |||
1806 | 1067 | * `wraps`: Item for the mock object to wrap. If `wraps` is not None then | ||
1807 | 1068 | calling the Mock will pass the call through to the wrapped object | ||
1808 | 1069 | (returning the real result). Attribute access on the mock will return a | ||
1809 | 1070 | Mock object that wraps the corresponding attribute of the wrapped object | ||
1810 | 1071 | (so attempting to access an attribute that doesn't exist will raise an | ||
1811 | 1072 | `AttributeError`). | ||
1812 | 1073 | |||
1813 | 1074 | If the mock has an explicit `return_value` set then calls are not passed | ||
1814 | 1075 | to the wrapped object and the `return_value` is returned instead. | ||
1815 | 1076 | |||
1816 | 1077 | * `name`: If the mock has a name then it will be used in the repr of the | ||
1817 | 1078 | mock. This can be useful for debugging. The name is propagated to child | ||
1818 | 1079 | mocks. | ||
1819 | 1080 | |||
1820 | 1081 | Mocks can also be called with arbitrary keyword arguments. These will be | ||
1821 | 1082 | used to set attributes on the mock after it is created. | ||
1822 | 1083 | """ | ||
1823 | 1084 | |||
1824 | 1085 | |||
1825 | 1086 | |||
1826 | 1087 | def _dot_lookup(thing, comp, import_path): | ||
1827 | 1088 | try: | ||
1828 | 1089 | return getattr(thing, comp) | ||
1829 | 1090 | except AttributeError: | ||
1830 | 1091 | __import__(import_path) | ||
1831 | 1092 | return getattr(thing, comp) | ||
1832 | 1093 | |||
1833 | 1094 | |||
1834 | 1095 | def _importer(target): | ||
1835 | 1096 | components = target.split('.') | ||
1836 | 1097 | import_path = components.pop(0) | ||
1837 | 1098 | thing = __import__(import_path) | ||
1838 | 1099 | |||
1839 | 1100 | for comp in components: | ||
1840 | 1101 | import_path += ".%s" % comp | ||
1841 | 1102 | thing = _dot_lookup(thing, comp, import_path) | ||
1842 | 1103 | return thing | ||
1843 | 1104 | |||
1844 | 1105 | |||
1845 | 1106 | def _is_started(patcher): | ||
1846 | 1107 | # XXXX horrible | ||
1847 | 1108 | return hasattr(patcher, 'is_local') | ||
1848 | 1109 | |||
1849 | 1110 | |||
1850 | 1111 | class _patch(object): | ||
1851 | 1112 | |||
1852 | 1113 | attribute_name = None | ||
1853 | 1114 | _active_patches = set() | ||
1854 | 1115 | |||
1855 | 1116 | def __init__( | ||
1856 | 1117 | self, getter, attribute, new, spec, create, | ||
1857 | 1118 | spec_set, autospec, new_callable, kwargs | ||
1858 | 1119 | ): | ||
1859 | 1120 | if new_callable is not None: | ||
1860 | 1121 | if new is not DEFAULT: | ||
1861 | 1122 | raise ValueError( | ||
1862 | 1123 | "Cannot use 'new' and 'new_callable' together" | ||
1863 | 1124 | ) | ||
1864 | 1125 | if autospec is not None: | ||
1865 | 1126 | raise ValueError( | ||
1866 | 1127 | "Cannot use 'autospec' and 'new_callable' together" | ||
1867 | 1128 | ) | ||
1868 | 1129 | |||
1869 | 1130 | self.getter = getter | ||
1870 | 1131 | self.attribute = attribute | ||
1871 | 1132 | self.new = new | ||
1872 | 1133 | self.new_callable = new_callable | ||
1873 | 1134 | self.spec = spec | ||
1874 | 1135 | self.create = create | ||
1875 | 1136 | self.has_local = False | ||
1876 | 1137 | self.spec_set = spec_set | ||
1877 | 1138 | self.autospec = autospec | ||
1878 | 1139 | self.kwargs = kwargs | ||
1879 | 1140 | self.additional_patchers = [] | ||
1880 | 1141 | |||
1881 | 1142 | |||
1882 | 1143 | def copy(self): | ||
1883 | 1144 | patcher = _patch( | ||
1884 | 1145 | self.getter, self.attribute, self.new, self.spec, | ||
1885 | 1146 | self.create, self.spec_set, | ||
1886 | 1147 | self.autospec, self.new_callable, self.kwargs | ||
1887 | 1148 | ) | ||
1888 | 1149 | patcher.attribute_name = self.attribute_name | ||
1889 | 1150 | patcher.additional_patchers = [ | ||
1890 | 1151 | p.copy() for p in self.additional_patchers | ||
1891 | 1152 | ] | ||
1892 | 1153 | return patcher | ||
1893 | 1154 | |||
1894 | 1155 | |||
1895 | 1156 | def __call__(self, func): | ||
1896 | 1157 | if isinstance(func, ClassTypes): | ||
1897 | 1158 | return self.decorate_class(func) | ||
1898 | 1159 | return self.decorate_callable(func) | ||
1899 | 1160 | |||
1900 | 1161 | |||
1901 | 1162 | def decorate_class(self, klass): | ||
1902 | 1163 | for attr in dir(klass): | ||
1903 | 1164 | if not attr.startswith(patch.TEST_PREFIX): | ||
1904 | 1165 | continue | ||
1905 | 1166 | |||
1906 | 1167 | attr_value = getattr(klass, attr) | ||
1907 | 1168 | if not hasattr(attr_value, "__call__"): | ||
1908 | 1169 | continue | ||
1909 | 1170 | |||
1910 | 1171 | patcher = self.copy() | ||
1911 | 1172 | setattr(klass, attr, patcher(attr_value)) | ||
1912 | 1173 | return klass | ||
1913 | 1174 | |||
1914 | 1175 | |||
1915 | 1176 | def decorate_callable(self, func): | ||
1916 | 1177 | if hasattr(func, 'patchings'): | ||
1917 | 1178 | func.patchings.append(self) | ||
1918 | 1179 | return func | ||
1919 | 1180 | |||
1920 | 1181 | @wraps(func) | ||
1921 | 1182 | def patched(*args, **keywargs): | ||
1922 | 1183 | # don't use a with here (backwards compatability with Python 2.4) | ||
1923 | 1184 | extra_args = [] | ||
1924 | 1185 | entered_patchers = [] | ||
1925 | 1186 | |||
1926 | 1187 | # can't use try...except...finally because of Python 2.4 | ||
1927 | 1188 | # compatibility | ||
1928 | 1189 | exc_info = tuple() | ||
1929 | 1190 | try: | ||
1930 | 1191 | try: | ||
1931 | 1192 | for patching in patched.patchings: | ||
1932 | 1193 | arg = patching.__enter__() | ||
1933 | 1194 | entered_patchers.append(patching) | ||
1934 | 1195 | if patching.attribute_name is not None: | ||
1935 | 1196 | keywargs.update(arg) | ||
1936 | 1197 | elif patching.new is DEFAULT: | ||
1937 | 1198 | extra_args.append(arg) | ||
1938 | 1199 | |||
1939 | 1200 | args += tuple(extra_args) | ||
1940 | 1201 | return func(*args, **keywargs) | ||
1941 | 1202 | except: | ||
1942 | 1203 | if (patching not in entered_patchers and | ||
1943 | 1204 | _is_started(patching)): | ||
1944 | 1205 | # the patcher may have been started, but an exception | ||
1945 | 1206 | # raised whilst entering one of its additional_patchers | ||
1946 | 1207 | entered_patchers.append(patching) | ||
1947 | 1208 | # Pass the exception to __exit__ | ||
1948 | 1209 | exc_info = sys.exc_info() | ||
1949 | 1210 | # re-raise the exception | ||
1950 | 1211 | raise | ||
1951 | 1212 | finally: | ||
1952 | 1213 | for patching in reversed(entered_patchers): | ||
1953 | 1214 | patching.__exit__(*exc_info) | ||
1954 | 1215 | |||
1955 | 1216 | patched.patchings = [self] | ||
1956 | 1217 | if hasattr(func, 'func_code'): | ||
1957 | 1218 | # not in Python 3 | ||
1958 | 1219 | patched.compat_co_firstlineno = getattr( | ||
1959 | 1220 | func, "compat_co_firstlineno", | ||
1960 | 1221 | func.func_code.co_firstlineno | ||
1961 | 1222 | ) | ||
1962 | 1223 | return patched | ||
1963 | 1224 | |||
1964 | 1225 | |||
1965 | 1226 | def get_original(self): | ||
1966 | 1227 | target = self.getter() | ||
1967 | 1228 | name = self.attribute | ||
1968 | 1229 | |||
1969 | 1230 | original = DEFAULT | ||
1970 | 1231 | local = False | ||
1971 | 1232 | |||
1972 | 1233 | try: | ||
1973 | 1234 | original = target.__dict__[name] | ||
1974 | 1235 | except (AttributeError, KeyError): | ||
1975 | 1236 | original = getattr(target, name, DEFAULT) | ||
1976 | 1237 | else: | ||
1977 | 1238 | local = True | ||
1978 | 1239 | |||
1979 | 1240 | if not self.create and original is DEFAULT: | ||
1980 | 1241 | raise AttributeError( | ||
1981 | 1242 | "%s does not have the attribute %r" % (target, name) | ||
1982 | 1243 | ) | ||
1983 | 1244 | return original, local | ||
1984 | 1245 | |||
1985 | 1246 | |||
1986 | 1247 | def __enter__(self): | ||
1987 | 1248 | """Perform the patch.""" | ||
1988 | 1249 | new, spec, spec_set = self.new, self.spec, self.spec_set | ||
1989 | 1250 | autospec, kwargs = self.autospec, self.kwargs | ||
1990 | 1251 | new_callable = self.new_callable | ||
1991 | 1252 | self.target = self.getter() | ||
1992 | 1253 | |||
1993 | 1254 | # normalise False to None | ||
1994 | 1255 | if spec is False: | ||
1995 | 1256 | spec = None | ||
1996 | 1257 | if spec_set is False: | ||
1997 | 1258 | spec_set = None | ||
1998 | 1259 | if autospec is False: | ||
1999 | 1260 | autospec = None | ||
2000 | 1261 | |||
2001 | 1262 | if spec is not None and autospec is not None: | ||
2002 | 1263 | raise TypeError("Can't specify spec and autospec") | ||
2003 | 1264 | if ((spec is not None or autospec is not None) and | ||
2004 | 1265 | spec_set not in (True, None)): | ||
2005 | 1266 | raise TypeError("Can't provide explicit spec_set *and* spec or autospec") | ||
2006 | 1267 | |||
2007 | 1268 | original, local = self.get_original() | ||
2008 | 1269 | |||
2009 | 1270 | if new is DEFAULT and autospec is None: | ||
2010 | 1271 | inherit = False | ||
2011 | 1272 | if spec is True: | ||
2012 | 1273 | # set spec to the object we are replacing | ||
2013 | 1274 | spec = original | ||
2014 | 1275 | if spec_set is True: | ||
2015 | 1276 | spec_set = original | ||
2016 | 1277 | spec = None | ||
2017 | 1278 | elif spec is not None: | ||
2018 | 1279 | if spec_set is True: | ||
2019 | 1280 | spec_set = spec | ||
2020 | 1281 | spec = None | ||
2021 | 1282 | elif spec_set is True: | ||
2022 | 1283 | spec_set = original | ||
2023 | 1284 | |||
2024 | 1285 | if spec is not None or spec_set is not None: | ||
2025 | 1286 | if original is DEFAULT: | ||
2026 | 1287 | raise TypeError("Can't use 'spec' with create=True") | ||
2027 | 1288 | if isinstance(original, ClassTypes): | ||
2028 | 1289 | # If we're patching out a class and there is a spec | ||
2029 | 1290 | inherit = True | ||
2030 | 1291 | |||
2031 | 1292 | Klass = MagicMock | ||
2032 | 1293 | _kwargs = {} | ||
2033 | 1294 | if new_callable is not None: | ||
2034 | 1295 | Klass = new_callable | ||
2035 | 1296 | elif spec is not None or spec_set is not None: | ||
2036 | 1297 | this_spec = spec | ||
2037 | 1298 | if spec_set is not None: | ||
2038 | 1299 | this_spec = spec_set | ||
2039 | 1300 | if _is_list(this_spec): | ||
2040 | 1301 | not_callable = '__call__' not in this_spec | ||
2041 | 1302 | else: | ||
2042 | 1303 | not_callable = not _callable(this_spec) | ||
2043 | 1304 | if not_callable: | ||
2044 | 1305 | Klass = NonCallableMagicMock | ||
2045 | 1306 | |||
2046 | 1307 | if spec is not None: | ||
2047 | 1308 | _kwargs['spec'] = spec | ||
2048 | 1309 | if spec_set is not None: | ||
2049 | 1310 | _kwargs['spec_set'] = spec_set | ||
2050 | 1311 | |||
2051 | 1312 | # add a name to mocks | ||
2052 | 1313 | if (isinstance(Klass, type) and | ||
2053 | 1314 | issubclass(Klass, NonCallableMock) and self.attribute): | ||
2054 | 1315 | _kwargs['name'] = self.attribute | ||
2055 | 1316 | |||
2056 | 1317 | _kwargs.update(kwargs) | ||
2057 | 1318 | new = Klass(**_kwargs) | ||
2058 | 1319 | |||
2059 | 1320 | if inherit and _is_instance_mock(new): | ||
2060 | 1321 | # we can only tell if the instance should be callable if the | ||
2061 | 1322 | # spec is not a list | ||
2062 | 1323 | this_spec = spec | ||
2063 | 1324 | if spec_set is not None: | ||
2064 | 1325 | this_spec = spec_set | ||
2065 | 1326 | if (not _is_list(this_spec) and not | ||
2066 | 1327 | _instance_callable(this_spec)): | ||
2067 | 1328 | Klass = NonCallableMagicMock | ||
2068 | 1329 | |||
2069 | 1330 | _kwargs.pop('name') | ||
2070 | 1331 | new.return_value = Klass(_new_parent=new, _new_name='()', | ||
2071 | 1332 | **_kwargs) | ||
2072 | 1333 | elif autospec is not None: | ||
2073 | 1334 | # spec is ignored, new *must* be default, spec_set is treated | ||
2074 | 1335 | # as a boolean. Should we check spec is not None and that spec_set | ||
2075 | 1336 | # is a bool? | ||
2076 | 1337 | if new is not DEFAULT: | ||
2077 | 1338 | raise TypeError( | ||
2078 | 1339 | "autospec creates the mock for you. Can't specify " | ||
2079 | 1340 | "autospec and new." | ||
2080 | 1341 | ) | ||
2081 | 1342 | if original is DEFAULT: | ||
2082 | 1343 | raise TypeError("Can't use 'autospec' with create=True") | ||
2083 | 1344 | spec_set = bool(spec_set) | ||
2084 | 1345 | if autospec is True: | ||
2085 | 1346 | autospec = original | ||
2086 | 1347 | |||
2087 | 1348 | new = create_autospec(autospec, spec_set=spec_set, | ||
2088 | 1349 | _name=self.attribute, **kwargs) | ||
2089 | 1350 | elif kwargs: | ||
2090 | 1351 | # can't set keyword args when we aren't creating the mock | ||
2091 | 1352 | # XXXX If new is a Mock we could call new.configure_mock(**kwargs) | ||
2092 | 1353 | raise TypeError("Can't pass kwargs to a mock we aren't creating") | ||
2093 | 1354 | |||
2094 | 1355 | new_attr = new | ||
2095 | 1356 | |||
2096 | 1357 | self.temp_original = original | ||
2097 | 1358 | self.is_local = local | ||
2098 | 1359 | setattr(self.target, self.attribute, new_attr) | ||
2099 | 1360 | if self.attribute_name is not None: | ||
2100 | 1361 | extra_args = {} | ||
2101 | 1362 | if self.new is DEFAULT: | ||
2102 | 1363 | extra_args[self.attribute_name] = new | ||
2103 | 1364 | for patching in self.additional_patchers: | ||
2104 | 1365 | arg = patching.__enter__() | ||
2105 | 1366 | if patching.new is DEFAULT: | ||
2106 | 1367 | extra_args.update(arg) | ||
2107 | 1368 | return extra_args | ||
2108 | 1369 | |||
2109 | 1370 | return new | ||
2110 | 1371 | |||
2111 | 1372 | |||
2112 | 1373 | def __exit__(self, *exc_info): | ||
2113 | 1374 | """Undo the patch.""" | ||
2114 | 1375 | if not _is_started(self): | ||
2115 | 1376 | raise RuntimeError('stop called on unstarted patcher') | ||
2116 | 1377 | |||
2117 | 1378 | if self.is_local and self.temp_original is not DEFAULT: | ||
2118 | 1379 | setattr(self.target, self.attribute, self.temp_original) | ||
2119 | 1380 | else: | ||
2120 | 1381 | delattr(self.target, self.attribute) | ||
2121 | 1382 | if not self.create and not hasattr(self.target, self.attribute): | ||
2122 | 1383 | # needed for proxy objects like django settings | ||
2123 | 1384 | setattr(self.target, self.attribute, self.temp_original) | ||
2124 | 1385 | |||
2125 | 1386 | del self.temp_original | ||
2126 | 1387 | del self.is_local | ||
2127 | 1388 | del self.target | ||
2128 | 1389 | for patcher in reversed(self.additional_patchers): | ||
2129 | 1390 | if _is_started(patcher): | ||
2130 | 1391 | patcher.__exit__(*exc_info) | ||
2131 | 1392 | |||
2132 | 1393 | |||
2133 | 1394 | def start(self): | ||
2134 | 1395 | """Activate a patch, returning any created mock.""" | ||
2135 | 1396 | result = self.__enter__() | ||
2136 | 1397 | self._active_patches.add(self) | ||
2137 | 1398 | return result | ||
2138 | 1399 | |||
2139 | 1400 | |||
2140 | 1401 | def stop(self): | ||
2141 | 1402 | """Stop an active patch.""" | ||
2142 | 1403 | self._active_patches.discard(self) | ||
2143 | 1404 | return self.__exit__() | ||
2144 | 1405 | |||
2145 | 1406 | |||
2146 | 1407 | |||
2147 | 1408 | def _get_target(target): | ||
2148 | 1409 | try: | ||
2149 | 1410 | target, attribute = target.rsplit('.', 1) | ||
2150 | 1411 | except (TypeError, ValueError): | ||
2151 | 1412 | raise TypeError("Need a valid target to patch. You supplied: %r" % | ||
2152 | 1413 | (target,)) | ||
2153 | 1414 | getter = lambda: _importer(target) | ||
2154 | 1415 | return getter, attribute | ||
2155 | 1416 | |||
2156 | 1417 | |||
2157 | 1418 | def _patch_object( | ||
2158 | 1419 | target, attribute, new=DEFAULT, spec=None, | ||
2159 | 1420 | create=False, spec_set=None, autospec=None, | ||
2160 | 1421 | new_callable=None, **kwargs | ||
2161 | 1422 | ): | ||
2162 | 1423 | """ | ||
2163 | 1424 | patch.object(target, attribute, new=DEFAULT, spec=None, create=False, | ||
2164 | 1425 | spec_set=None, autospec=None, new_callable=None, **kwargs) | ||
2165 | 1426 | |||
2166 | 1427 | patch the named member (`attribute`) on an object (`target`) with a mock | ||
2167 | 1428 | object. | ||
2168 | 1429 | |||
2169 | 1430 | `patch.object` can be used as a decorator, class decorator or a context | ||
2170 | 1431 | manager. Arguments `new`, `spec`, `create`, `spec_set`, | ||
2171 | 1432 | `autospec` and `new_callable` have the same meaning as for `patch`. Like | ||
2172 | 1433 | `patch`, `patch.object` takes arbitrary keyword arguments for configuring | ||
2173 | 1434 | the mock object it creates. | ||
2174 | 1435 | |||
2175 | 1436 | When used as a class decorator `patch.object` honours `patch.TEST_PREFIX` | ||
2176 | 1437 | for choosing which methods to wrap. | ||
2177 | 1438 | """ | ||
2178 | 1439 | getter = lambda: target | ||
2179 | 1440 | return _patch( | ||
2180 | 1441 | getter, attribute, new, spec, create, | ||
2181 | 1442 | spec_set, autospec, new_callable, kwargs | ||
2182 | 1443 | ) | ||
2183 | 1444 | |||
2184 | 1445 | |||
2185 | 1446 | def _patch_multiple(target, spec=None, create=False, spec_set=None, | ||
2186 | 1447 | autospec=None, new_callable=None, **kwargs): | ||
2187 | 1448 | """Perform multiple patches in a single call. It takes the object to be | ||
2188 | 1449 | patched (either as an object or a string to fetch the object by importing) | ||
2189 | 1450 | and keyword arguments for the patches:: | ||
2190 | 1451 | |||
2191 | 1452 | with patch.multiple(settings, FIRST_PATCH='one', SECOND_PATCH='two'): | ||
2192 | 1453 | ... | ||
2193 | 1454 | |||
2194 | 1455 | Use `DEFAULT` as the value if you want `patch.multiple` to create | ||
2195 | 1456 | mocks for you. In this case the created mocks are passed into a decorated | ||
2196 | 1457 | function by keyword, and a dictionary is returned when `patch.multiple` is | ||
2197 | 1458 | used as a context manager. | ||
2198 | 1459 | |||
2199 | 1460 | `patch.multiple` can be used as a decorator, class decorator or a context | ||
2200 | 1461 | manager. The arguments `spec`, `spec_set`, `create`, | ||
2201 | 1462 | `autospec` and `new_callable` have the same meaning as for `patch`. These | ||
2202 | 1463 | arguments will be applied to *all* patches done by `patch.multiple`. | ||
2203 | 1464 | |||
2204 | 1465 | When used as a class decorator `patch.multiple` honours `patch.TEST_PREFIX` | ||
2205 | 1466 | for choosing which methods to wrap. | ||
2206 | 1467 | """ | ||
2207 | 1468 | if type(target) in (unicode, str): | ||
2208 | 1469 | getter = lambda: _importer(target) | ||
2209 | 1470 | else: | ||
2210 | 1471 | getter = lambda: target | ||
2211 | 1472 | |||
2212 | 1473 | if not kwargs: | ||
2213 | 1474 | raise ValueError( | ||
2214 | 1475 | 'Must supply at least one keyword argument with patch.multiple' | ||
2215 | 1476 | ) | ||
2216 | 1477 | # need to wrap in a list for python 3, where items is a view | ||
2217 | 1478 | items = list(kwargs.items()) | ||
2218 | 1479 | attribute, new = items[0] | ||
2219 | 1480 | patcher = _patch( | ||
2220 | 1481 | getter, attribute, new, spec, create, spec_set, | ||
2221 | 1482 | autospec, new_callable, {} | ||
2222 | 1483 | ) | ||
2223 | 1484 | patcher.attribute_name = attribute | ||
2224 | 1485 | for attribute, new in items[1:]: | ||
2225 | 1486 | this_patcher = _patch( | ||
2226 | 1487 | getter, attribute, new, spec, create, spec_set, | ||
2227 | 1488 | autospec, new_callable, {} | ||
2228 | 1489 | ) | ||
2229 | 1490 | this_patcher.attribute_name = attribute | ||
2230 | 1491 | patcher.additional_patchers.append(this_patcher) | ||
2231 | 1492 | return patcher | ||
2232 | 1493 | |||
2233 | 1494 | |||
2234 | 1495 | def patch( | ||
2235 | 1496 | target, new=DEFAULT, spec=None, create=False, | ||
2236 | 1497 | spec_set=None, autospec=None, new_callable=None, **kwargs | ||
2237 | 1498 | ): | ||
2238 | 1499 | """ | ||
2239 | 1500 | `patch` acts as a function decorator, class decorator or a context | ||
2240 | 1501 | manager. Inside the body of the function or with statement, the `target` | ||
2241 | 1502 | is patched with a `new` object. When the function/with statement exits | ||
2242 | 1503 | the patch is undone. | ||
2243 | 1504 | |||
2244 | 1505 | If `new` is omitted, then the target is replaced with a | ||
2245 | 1506 | `MagicMock`. If `patch` is used as a decorator and `new` is | ||
2246 | 1507 | omitted, the created mock is passed in as an extra argument to the | ||
2247 | 1508 | decorated function. If `patch` is used as a context manager the created | ||
2248 | 1509 | mock is returned by the context manager. | ||
2249 | 1510 | |||
2250 | 1511 | `target` should be a string in the form `'package.module.ClassName'`. The | ||
2251 | 1512 | `target` is imported and the specified object replaced with the `new` | ||
2252 | 1513 | object, so the `target` must be importable from the environment you are | ||
2253 | 1514 | calling `patch` from. The target is imported when the decorated function | ||
2254 | 1515 | is executed, not at decoration time. | ||
2255 | 1516 | |||
2256 | 1517 | The `spec` and `spec_set` keyword arguments are passed to the `MagicMock` | ||
2257 | 1518 | if patch is creating one for you. | ||
2258 | 1519 | |||
2259 | 1520 | In addition you can pass `spec=True` or `spec_set=True`, which causes | ||
2260 | 1521 | patch to pass in the object being mocked as the spec/spec_set object. | ||
2261 | 1522 | |||
2262 | 1523 | `new_callable` allows you to specify a different class, or callable object, | ||
2263 | 1524 | that will be called to create the `new` object. By default `MagicMock` is | ||
2264 | 1525 | used. | ||
2265 | 1526 | |||
2266 | 1527 | A more powerful form of `spec` is `autospec`. If you set `autospec=True` | ||
2267 | 1528 | then the mock with be created with a spec from the object being replaced. | ||
2268 | 1529 | All attributes of the mock will also have the spec of the corresponding | ||
2269 | 1530 | attribute of the object being replaced. Methods and functions being | ||
2270 | 1531 | mocked will have their arguments checked and will raise a `TypeError` if | ||
2271 | 1532 | they are called with the wrong signature. For mocks replacing a class, | ||
2272 | 1533 | their return value (the 'instance') will have the same spec as the class. | ||
2273 | 1534 | |||
2274 | 1535 | Instead of `autospec=True` you can pass `autospec=some_object` to use an | ||
2275 | 1536 | arbitrary object as the spec instead of the one being replaced. | ||
2276 | 1537 | |||
2277 | 1538 | By default `patch` will fail to replace attributes that don't exist. If | ||
2278 | 1539 | you pass in `create=True`, and the attribute doesn't exist, patch will | ||
2279 | 1540 | create the attribute for you when the patched function is called, and | ||
2280 | 1541 | delete it again afterwards. This is useful for writing tests against | ||
2281 | 1542 | attributes that your production code creates at runtime. It is off by by | ||
2282 | 1543 | default because it can be dangerous. With it switched on you can write | ||
2283 | 1544 | passing tests against APIs that don't actually exist! | ||
2284 | 1545 | |||
2285 | 1546 | Patch can be used as a `TestCase` class decorator. It works by | ||
2286 | 1547 | decorating each test method in the class. This reduces the boilerplate | ||
2287 | 1548 | code when your test methods share a common patchings set. `patch` finds | ||
2288 | 1549 | tests by looking for method names that start with `patch.TEST_PREFIX`. | ||
2289 | 1550 | By default this is `test`, which matches the way `unittest` finds tests. | ||
2290 | 1551 | You can specify an alternative prefix by setting `patch.TEST_PREFIX`. | ||
2291 | 1552 | |||
2292 | 1553 | Patch can be used as a context manager, with the with statement. Here the | ||
2293 | 1554 | patching applies to the indented block after the with statement. If you | ||
2294 | 1555 | use "as" then the patched object will be bound to the name after the | ||
2295 | 1556 | "as"; very useful if `patch` is creating a mock object for you. | ||
2296 | 1557 | |||
2297 | 1558 | `patch` takes arbitrary keyword arguments. These will be passed to | ||
2298 | 1559 | the `Mock` (or `new_callable`) on construction. | ||
2299 | 1560 | |||
2300 | 1561 | `patch.dict(...)`, `patch.multiple(...)` and `patch.object(...)` are | ||
2301 | 1562 | available for alternate use-cases. | ||
2302 | 1563 | """ | ||
2303 | 1564 | getter, attribute = _get_target(target) | ||
2304 | 1565 | return _patch( | ||
2305 | 1566 | getter, attribute, new, spec, create, | ||
2306 | 1567 | spec_set, autospec, new_callable, kwargs | ||
2307 | 1568 | ) | ||
2308 | 1569 | |||
2309 | 1570 | |||
2310 | 1571 | class _patch_dict(object): | ||
2311 | 1572 | """ | ||
2312 | 1573 | Patch a dictionary, or dictionary like object, and restore the dictionary | ||
2313 | 1574 | to its original state after the test. | ||
2314 | 1575 | |||
2315 | 1576 | `in_dict` can be a dictionary or a mapping like container. If it is a | ||
2316 | 1577 | mapping then it must at least support getting, setting and deleting items | ||
2317 | 1578 | plus iterating over keys. | ||
2318 | 1579 | |||
2319 | 1580 | `in_dict` can also be a string specifying the name of the dictionary, which | ||
2320 | 1581 | will then be fetched by importing it. | ||
2321 | 1582 | |||
2322 | 1583 | `values` can be a dictionary of values to set in the dictionary. `values` | ||
2323 | 1584 | can also be an iterable of `(key, value)` pairs. | ||
2324 | 1585 | |||
2325 | 1586 | If `clear` is True then the dictionary will be cleared before the new | ||
2326 | 1587 | values are set. | ||
2327 | 1588 | |||
2328 | 1589 | `patch.dict` can also be called with arbitrary keyword arguments to set | ||
2329 | 1590 | values in the dictionary:: | ||
2330 | 1591 | |||
2331 | 1592 | with patch.dict('sys.modules', mymodule=Mock(), other_module=Mock()): | ||
2332 | 1593 | ... | ||
2333 | 1594 | |||
2334 | 1595 | `patch.dict` can be used as a context manager, decorator or class | ||
2335 | 1596 | decorator. When used as a class decorator `patch.dict` honours | ||
2336 | 1597 | `patch.TEST_PREFIX` for choosing which methods to wrap. | ||
2337 | 1598 | """ | ||
2338 | 1599 | |||
2339 | 1600 | def __init__(self, in_dict, values=(), clear=False, **kwargs): | ||
2340 | 1601 | if isinstance(in_dict, basestring): | ||
2341 | 1602 | in_dict = _importer(in_dict) | ||
2342 | 1603 | self.in_dict = in_dict | ||
2343 | 1604 | # support any argument supported by dict(...) constructor | ||
2344 | 1605 | self.values = dict(values) | ||
2345 | 1606 | self.values.update(kwargs) | ||
2346 | 1607 | self.clear = clear | ||
2347 | 1608 | self._original = None | ||
2348 | 1609 | |||
2349 | 1610 | |||
2350 | 1611 | def __call__(self, f): | ||
2351 | 1612 | if isinstance(f, ClassTypes): | ||
2352 | 1613 | return self.decorate_class(f) | ||
2353 | 1614 | @wraps(f) | ||
2354 | 1615 | def _inner(*args, **kw): | ||
2355 | 1616 | self._patch_dict() | ||
2356 | 1617 | try: | ||
2357 | 1618 | return f(*args, **kw) | ||
2358 | 1619 | finally: | ||
2359 | 1620 | self._unpatch_dict() | ||
2360 | 1621 | |||
2361 | 1622 | return _inner | ||
2362 | 1623 | |||
2363 | 1624 | |||
2364 | 1625 | def decorate_class(self, klass): | ||
2365 | 1626 | for attr in dir(klass): | ||
2366 | 1627 | attr_value = getattr(klass, attr) | ||
2367 | 1628 | if (attr.startswith(patch.TEST_PREFIX) and | ||
2368 | 1629 | hasattr(attr_value, "__call__")): | ||
2369 | 1630 | decorator = _patch_dict(self.in_dict, self.values, self.clear) | ||
2370 | 1631 | decorated = decorator(attr_value) | ||
2371 | 1632 | setattr(klass, attr, decorated) | ||
2372 | 1633 | return klass | ||
2373 | 1634 | |||
2374 | 1635 | |||
2375 | 1636 | def __enter__(self): | ||
2376 | 1637 | """Patch the dict.""" | ||
2377 | 1638 | self._patch_dict() | ||
2378 | 1639 | |||
2379 | 1640 | |||
2380 | 1641 | def _patch_dict(self): | ||
2381 | 1642 | values = self.values | ||
2382 | 1643 | in_dict = self.in_dict | ||
2383 | 1644 | clear = self.clear | ||
2384 | 1645 | |||
2385 | 1646 | try: | ||
2386 | 1647 | original = in_dict.copy() | ||
2387 | 1648 | except AttributeError: | ||
2388 | 1649 | # dict like object with no copy method | ||
2389 | 1650 | # must support iteration over keys | ||
2390 | 1651 | original = {} | ||
2391 | 1652 | for key in in_dict: | ||
2392 | 1653 | original[key] = in_dict[key] | ||
2393 | 1654 | self._original = original | ||
2394 | 1655 | |||
2395 | 1656 | if clear: | ||
2396 | 1657 | _clear_dict(in_dict) | ||
2397 | 1658 | |||
2398 | 1659 | try: | ||
2399 | 1660 | in_dict.update(values) | ||
2400 | 1661 | except AttributeError: | ||
2401 | 1662 | # dict like object with no update method | ||
2402 | 1663 | for key in values: | ||
2403 | 1664 | in_dict[key] = values[key] | ||
2404 | 1665 | |||
2405 | 1666 | |||
2406 | 1667 | def _unpatch_dict(self): | ||
2407 | 1668 | in_dict = self.in_dict | ||
2408 | 1669 | original = self._original | ||
2409 | 1670 | |||
2410 | 1671 | _clear_dict(in_dict) | ||
2411 | 1672 | |||
2412 | 1673 | try: | ||
2413 | 1674 | in_dict.update(original) | ||
2414 | 1675 | except AttributeError: | ||
2415 | 1676 | for key in original: | ||
2416 | 1677 | in_dict[key] = original[key] | ||
2417 | 1678 | |||
2418 | 1679 | |||
2419 | 1680 | def __exit__(self, *args): | ||
2420 | 1681 | """Unpatch the dict.""" | ||
2421 | 1682 | self._unpatch_dict() | ||
2422 | 1683 | return False | ||
2423 | 1684 | |||
2424 | 1685 | start = __enter__ | ||
2425 | 1686 | stop = __exit__ | ||
2426 | 1687 | |||
2427 | 1688 | |||
2428 | 1689 | def _clear_dict(in_dict): | ||
2429 | 1690 | try: | ||
2430 | 1691 | in_dict.clear() | ||
2431 | 1692 | except AttributeError: | ||
2432 | 1693 | keys = list(in_dict) | ||
2433 | 1694 | for key in keys: | ||
2434 | 1695 | del in_dict[key] | ||
2435 | 1696 | |||
2436 | 1697 | |||
2437 | 1698 | def _patch_stopall(): | ||
2438 | 1699 | """Stop all active patches.""" | ||
2439 | 1700 | for patch in list(_patch._active_patches): | ||
2440 | 1701 | patch.stop() | ||
2441 | 1702 | |||
2442 | 1703 | |||
2443 | 1704 | patch.object = _patch_object | ||
2444 | 1705 | patch.dict = _patch_dict | ||
2445 | 1706 | patch.multiple = _patch_multiple | ||
2446 | 1707 | patch.stopall = _patch_stopall | ||
2447 | 1708 | patch.TEST_PREFIX = 'test' | ||
2448 | 1709 | |||
2449 | 1710 | magic_methods = ( | ||
2450 | 1711 | "lt le gt ge eq ne " | ||
2451 | 1712 | "getitem setitem delitem " | ||
2452 | 1713 | "len contains iter " | ||
2453 | 1714 | "hash str sizeof " | ||
2454 | 1715 | "enter exit " | ||
2455 | 1716 | "divmod neg pos abs invert " | ||
2456 | 1717 | "complex int float index " | ||
2457 | 1718 | "trunc floor ceil " | ||
2458 | 1719 | ) | ||
2459 | 1720 | |||
2460 | 1721 | numerics = "add sub mul div floordiv mod lshift rshift and xor or pow " | ||
2461 | 1722 | inplace = ' '.join('i%s' % n for n in numerics.split()) | ||
2462 | 1723 | right = ' '.join('r%s' % n for n in numerics.split()) | ||
2463 | 1724 | extra = '' | ||
2464 | 1725 | if inPy3k: | ||
2465 | 1726 | extra = 'bool next ' | ||
2466 | 1727 | else: | ||
2467 | 1728 | extra = 'unicode long nonzero oct hex truediv rtruediv ' | ||
2468 | 1729 | |||
2469 | 1730 | # not including __prepare__, __instancecheck__, __subclasscheck__ | ||
2470 | 1731 | # (as they are metaclass methods) | ||
2471 | 1732 | # __del__ is not supported at all as it causes problems if it exists | ||
2472 | 1733 | |||
2473 | 1734 | _non_defaults = set('__%s__' % method for method in [ | ||
2474 | 1735 | 'cmp', 'getslice', 'setslice', 'coerce', 'subclasses', | ||
2475 | 1736 | 'format', 'get', 'set', 'delete', 'reversed', | ||
2476 | 1737 | 'missing', 'reduce', 'reduce_ex', 'getinitargs', | ||
2477 | 1738 | 'getnewargs', 'getstate', 'setstate', 'getformat', | ||
2478 | 1739 | 'setformat', 'repr', 'dir' | ||
2479 | 1740 | ]) | ||
2480 | 1741 | |||
2481 | 1742 | |||
2482 | 1743 | def _get_method(name, func): | ||
2483 | 1744 | "Turns a callable object (like a mock) into a real function" | ||
2484 | 1745 | def method(self, *args, **kw): | ||
2485 | 1746 | return func(self, *args, **kw) | ||
2486 | 1747 | method.__name__ = name | ||
2487 | 1748 | return method | ||
2488 | 1749 | |||
2489 | 1750 | |||
2490 | 1751 | _magics = set( | ||
2491 | 1752 | '__%s__' % method for method in | ||
2492 | 1753 | ' '.join([magic_methods, numerics, inplace, right, extra]).split() | ||
2493 | 1754 | ) | ||
2494 | 1755 | |||
2495 | 1756 | _all_magics = _magics | _non_defaults | ||
2496 | 1757 | |||
2497 | 1758 | _unsupported_magics = set([ | ||
2498 | 1759 | '__getattr__', '__setattr__', | ||
2499 | 1760 | '__init__', '__new__', '__prepare__' | ||
2500 | 1761 | '__instancecheck__', '__subclasscheck__', | ||
2501 | 1762 | '__del__' | ||
2502 | 1763 | ]) | ||
2503 | 1764 | |||
2504 | 1765 | _calculate_return_value = { | ||
2505 | 1766 | '__hash__': lambda self: object.__hash__(self), | ||
2506 | 1767 | '__str__': lambda self: object.__str__(self), | ||
2507 | 1768 | '__sizeof__': lambda self: object.__sizeof__(self), | ||
2508 | 1769 | '__unicode__': lambda self: unicode(object.__str__(self)), | ||
2509 | 1770 | } | ||
2510 | 1771 | |||
2511 | 1772 | _return_values = { | ||
2512 | 1773 | '__lt__': NotImplemented, | ||
2513 | 1774 | '__gt__': NotImplemented, | ||
2514 | 1775 | '__le__': NotImplemented, | ||
2515 | 1776 | '__ge__': NotImplemented, | ||
2516 | 1777 | '__int__': 1, | ||
2517 | 1778 | '__contains__': False, | ||
2518 | 1779 | '__len__': 0, | ||
2519 | 1780 | '__exit__': False, | ||
2520 | 1781 | '__complex__': 1j, | ||
2521 | 1782 | '__float__': 1.0, | ||
2522 | 1783 | '__bool__': True, | ||
2523 | 1784 | '__nonzero__': True, | ||
2524 | 1785 | '__oct__': '1', | ||
2525 | 1786 | '__hex__': '0x1', | ||
2526 | 1787 | '__long__': long(1), | ||
2527 | 1788 | '__index__': 1, | ||
2528 | 1789 | } | ||
2529 | 1790 | |||
2530 | 1791 | |||
2531 | 1792 | def _get_eq(self): | ||
2532 | 1793 | def __eq__(other): | ||
2533 | 1794 | ret_val = self.__eq__._mock_return_value | ||
2534 | 1795 | if ret_val is not DEFAULT: | ||
2535 | 1796 | return ret_val | ||
2536 | 1797 | return self is other | ||
2537 | 1798 | return __eq__ | ||
2538 | 1799 | |||
2539 | 1800 | def _get_ne(self): | ||
2540 | 1801 | def __ne__(other): | ||
2541 | 1802 | if self.__ne__._mock_return_value is not DEFAULT: | ||
2542 | 1803 | return DEFAULT | ||
2543 | 1804 | return self is not other | ||
2544 | 1805 | return __ne__ | ||
2545 | 1806 | |||
2546 | 1807 | def _get_iter(self): | ||
2547 | 1808 | def __iter__(): | ||
2548 | 1809 | ret_val = self.__iter__._mock_return_value | ||
2549 | 1810 | if ret_val is DEFAULT: | ||
2550 | 1811 | return iter([]) | ||
2551 | 1812 | # if ret_val was already an iterator, then calling iter on it should | ||
2552 | 1813 | # return the iterator unchanged | ||
2553 | 1814 | return iter(ret_val) | ||
2554 | 1815 | return __iter__ | ||
2555 | 1816 | |||
2556 | 1817 | _side_effect_methods = { | ||
2557 | 1818 | '__eq__': _get_eq, | ||
2558 | 1819 | '__ne__': _get_ne, | ||
2559 | 1820 | '__iter__': _get_iter, | ||
2560 | 1821 | } | ||
2561 | 1822 | |||
2562 | 1823 | |||
2563 | 1824 | |||
2564 | 1825 | def _set_return_value(mock, method, name): | ||
2565 | 1826 | fixed = _return_values.get(name, DEFAULT) | ||
2566 | 1827 | if fixed is not DEFAULT: | ||
2567 | 1828 | method.return_value = fixed | ||
2568 | 1829 | return | ||
2569 | 1830 | |||
2570 | 1831 | return_calulator = _calculate_return_value.get(name) | ||
2571 | 1832 | if return_calulator is not None: | ||
2572 | 1833 | try: | ||
2573 | 1834 | return_value = return_calulator(mock) | ||
2574 | 1835 | except AttributeError: | ||
2575 | 1836 | # XXXX why do we return AttributeError here? | ||
2576 | 1837 | # set it as a side_effect instead? | ||
2577 | 1838 | return_value = AttributeError(name) | ||
2578 | 1839 | method.return_value = return_value | ||
2579 | 1840 | return | ||
2580 | 1841 | |||
2581 | 1842 | side_effector = _side_effect_methods.get(name) | ||
2582 | 1843 | if side_effector is not None: | ||
2583 | 1844 | method.side_effect = side_effector(mock) | ||
2584 | 1845 | |||
2585 | 1846 | |||
2586 | 1847 | |||
2587 | 1848 | class MagicMixin(object): | ||
2588 | 1849 | def __init__(self, *args, **kw): | ||
2589 | 1850 | _super(MagicMixin, self).__init__(*args, **kw) | ||
2590 | 1851 | self._mock_set_magics() | ||
2591 | 1852 | |||
2592 | 1853 | |||
2593 | 1854 | def _mock_set_magics(self): | ||
2594 | 1855 | these_magics = _magics | ||
2595 | 1856 | |||
2596 | 1857 | if self._mock_methods is not None: | ||
2597 | 1858 | these_magics = _magics.intersection(self._mock_methods) | ||
2598 | 1859 | |||
2599 | 1860 | remove_magics = set() | ||
2600 | 1861 | remove_magics = _magics - these_magics | ||
2601 | 1862 | |||
2602 | 1863 | for entry in remove_magics: | ||
2603 | 1864 | if entry in type(self).__dict__: | ||
2604 | 1865 | # remove unneeded magic methods | ||
2605 | 1866 | delattr(self, entry) | ||
2606 | 1867 | |||
2607 | 1868 | # don't overwrite existing attributes if called a second time | ||
2608 | 1869 | these_magics = these_magics - set(type(self).__dict__) | ||
2609 | 1870 | |||
2610 | 1871 | _type = type(self) | ||
2611 | 1872 | for entry in these_magics: | ||
2612 | 1873 | setattr(_type, entry, MagicProxy(entry, self)) | ||
2613 | 1874 | |||
2614 | 1875 | |||
2615 | 1876 | |||
2616 | 1877 | class NonCallableMagicMock(MagicMixin, NonCallableMock): | ||
2617 | 1878 | """A version of `MagicMock` that isn't callable.""" | ||
2618 | 1879 | def mock_add_spec(self, spec, spec_set=False): | ||
2619 | 1880 | """Add a spec to a mock. `spec` can either be an object or a | ||
2620 | 1881 | list of strings. Only attributes on the `spec` can be fetched as | ||
2621 | 1882 | attributes from the mock. | ||
2622 | 1883 | |||
2623 | 1884 | If `spec_set` is True then only attributes on the spec can be set.""" | ||
2624 | 1885 | self._mock_add_spec(spec, spec_set) | ||
2625 | 1886 | self._mock_set_magics() | ||
2626 | 1887 | |||
2627 | 1888 | |||
2628 | 1889 | |||
2629 | 1890 | class MagicMock(MagicMixin, Mock): | ||
2630 | 1891 | """ | ||
2631 | 1892 | MagicMock is a subclass of Mock with default implementations | ||
2632 | 1893 | of most of the magic methods. You can use MagicMock without having to | ||
2633 | 1894 | configure the magic methods yourself. | ||
2634 | 1895 | |||
2635 | 1896 | If you use the `spec` or `spec_set` arguments then *only* magic | ||
2636 | 1897 | methods that exist in the spec will be created. | ||
2637 | 1898 | |||
2638 | 1899 | Attributes and the return value of a `MagicMock` will also be `MagicMocks`. | ||
2639 | 1900 | """ | ||
2640 | 1901 | def mock_add_spec(self, spec, spec_set=False): | ||
2641 | 1902 | """Add a spec to a mock. `spec` can either be an object or a | ||
2642 | 1903 | list of strings. Only attributes on the `spec` can be fetched as | ||
2643 | 1904 | attributes from the mock. | ||
2644 | 1905 | |||
2645 | 1906 | If `spec_set` is True then only attributes on the spec can be set.""" | ||
2646 | 1907 | self._mock_add_spec(spec, spec_set) | ||
2647 | 1908 | self._mock_set_magics() | ||
2648 | 1909 | |||
2649 | 1910 | |||
2650 | 1911 | |||
2651 | 1912 | class MagicProxy(object): | ||
2652 | 1913 | def __init__(self, name, parent): | ||
2653 | 1914 | self.name = name | ||
2654 | 1915 | self.parent = parent | ||
2655 | 1916 | |||
2656 | 1917 | def __call__(self, *args, **kwargs): | ||
2657 | 1918 | m = self.create_mock() | ||
2658 | 1919 | return m(*args, **kwargs) | ||
2659 | 1920 | |||
2660 | 1921 | def create_mock(self): | ||
2661 | 1922 | entry = self.name | ||
2662 | 1923 | parent = self.parent | ||
2663 | 1924 | m = parent._get_child_mock(name=entry, _new_name=entry, | ||
2664 | 1925 | _new_parent=parent) | ||
2665 | 1926 | setattr(parent, entry, m) | ||
2666 | 1927 | _set_return_value(parent, m, entry) | ||
2667 | 1928 | return m | ||
2668 | 1929 | |||
2669 | 1930 | def __get__(self, obj, _type=None): | ||
2670 | 1931 | return self.create_mock() | ||
2671 | 1932 | |||
2672 | 1933 | |||
2673 | 1934 | |||
2674 | 1935 | class _ANY(object): | ||
2675 | 1936 | "A helper object that compares equal to everything." | ||
2676 | 1937 | |||
2677 | 1938 | def __eq__(self, other): | ||
2678 | 1939 | return True | ||
2679 | 1940 | |||
2680 | 1941 | def __ne__(self, other): | ||
2681 | 1942 | return False | ||
2682 | 1943 | |||
2683 | 1944 | def __repr__(self): | ||
2684 | 1945 | return '<ANY>' | ||
2685 | 1946 | |||
2686 | 1947 | ANY = _ANY() | ||
2687 | 1948 | |||
2688 | 1949 | |||
2689 | 1950 | |||
2690 | 1951 | def _format_call_signature(name, args, kwargs): | ||
2691 | 1952 | message = '%s(%%s)' % name | ||
2692 | 1953 | formatted_args = '' | ||
2693 | 1954 | args_string = ', '.join([repr(arg) for arg in args]) | ||
2694 | 1955 | kwargs_string = ', '.join([ | ||
2695 | 1956 | '%s=%r' % (key, value) for key, value in kwargs.items() | ||
2696 | 1957 | ]) | ||
2697 | 1958 | if args_string: | ||
2698 | 1959 | formatted_args = args_string | ||
2699 | 1960 | if kwargs_string: | ||
2700 | 1961 | if formatted_args: | ||
2701 | 1962 | formatted_args += ', ' | ||
2702 | 1963 | formatted_args += kwargs_string | ||
2703 | 1964 | |||
2704 | 1965 | return message % formatted_args | ||
2705 | 1966 | |||
2706 | 1967 | |||
2707 | 1968 | |||
2708 | 1969 | class _Call(tuple): | ||
2709 | 1970 | """ | ||
2710 | 1971 | A tuple for holding the results of a call to a mock, either in the form | ||
2711 | 1972 | `(args, kwargs)` or `(name, args, kwargs)`. | ||
2712 | 1973 | |||
2713 | 1974 | If args or kwargs are empty then a call tuple will compare equal to | ||
2714 | 1975 | a tuple without those values. This makes comparisons less verbose:: | ||
2715 | 1976 | |||
2716 | 1977 | _Call(('name', (), {})) == ('name',) | ||
2717 | 1978 | _Call(('name', (1,), {})) == ('name', (1,)) | ||
2718 | 1979 | _Call(((), {'a': 'b'})) == ({'a': 'b'},) | ||
2719 | 1980 | |||
2720 | 1981 | The `_Call` object provides a useful shortcut for comparing with call:: | ||
2721 | 1982 | |||
2722 | 1983 | _Call(((1, 2), {'a': 3})) == call(1, 2, a=3) | ||
2723 | 1984 | _Call(('foo', (1, 2), {'a': 3})) == call.foo(1, 2, a=3) | ||
2724 | 1985 | |||
2725 | 1986 | If the _Call has no name then it will match any name. | ||
2726 | 1987 | """ | ||
2727 | 1988 | def __new__(cls, value=(), name=None, parent=None, two=False, | ||
2728 | 1989 | from_kall=True): | ||
2729 | 1990 | name = '' | ||
2730 | 1991 | args = () | ||
2731 | 1992 | kwargs = {} | ||
2732 | 1993 | _len = len(value) | ||
2733 | 1994 | if _len == 3: | ||
2734 | 1995 | name, args, kwargs = value | ||
2735 | 1996 | elif _len == 2: | ||
2736 | 1997 | first, second = value | ||
2737 | 1998 | if isinstance(first, basestring): | ||
2738 | 1999 | name = first | ||
2739 | 2000 | if isinstance(second, tuple): | ||
2740 | 2001 | args = second | ||
2741 | 2002 | else: | ||
2742 | 2003 | kwargs = second | ||
2743 | 2004 | else: | ||
2744 | 2005 | args, kwargs = first, second | ||
2745 | 2006 | elif _len == 1: | ||
2746 | 2007 | value, = value | ||
2747 | 2008 | if isinstance(value, basestring): | ||
2748 | 2009 | name = value | ||
2749 | 2010 | elif isinstance(value, tuple): | ||
2750 | 2011 | args = value | ||
2751 | 2012 | else: | ||
2752 | 2013 | kwargs = value | ||
2753 | 2014 | |||
2754 | 2015 | if two: | ||
2755 | 2016 | return tuple.__new__(cls, (args, kwargs)) | ||
2756 | 2017 | |||
2757 | 2018 | return tuple.__new__(cls, (name, args, kwargs)) | ||
2758 | 2019 | |||
2759 | 2020 | |||
2760 | 2021 | def __init__(self, value=(), name=None, parent=None, two=False, | ||
2761 | 2022 | from_kall=True): | ||
2762 | 2023 | self.name = name | ||
2763 | 2024 | self.parent = parent | ||
2764 | 2025 | self.from_kall = from_kall | ||
2765 | 2026 | |||
2766 | 2027 | |||
2767 | 2028 | def __eq__(self, other): | ||
2768 | 2029 | if other is ANY: | ||
2769 | 2030 | return True | ||
2770 | 2031 | try: | ||
2771 | 2032 | len_other = len(other) | ||
2772 | 2033 | except TypeError: | ||
2773 | 2034 | return False | ||
2774 | 2035 | |||
2775 | 2036 | self_name = '' | ||
2776 | 2037 | if len(self) == 2: | ||
2777 | 2038 | self_args, self_kwargs = self | ||
2778 | 2039 | else: | ||
2779 | 2040 | self_name, self_args, self_kwargs = self | ||
2780 | 2041 | |||
2781 | 2042 | other_name = '' | ||
2782 | 2043 | if len_other == 0: | ||
2783 | 2044 | other_args, other_kwargs = (), {} | ||
2784 | 2045 | elif len_other == 3: | ||
2785 | 2046 | other_name, other_args, other_kwargs = other | ||
2786 | 2047 | elif len_other == 1: | ||
2787 | 2048 | value, = other | ||
2788 | 2049 | if isinstance(value, tuple): | ||
2789 | 2050 | other_args = value | ||
2790 | 2051 | other_kwargs = {} | ||
2791 | 2052 | elif isinstance(value, basestring): | ||
2792 | 2053 | other_name = value | ||
2793 | 2054 | other_args, other_kwargs = (), {} | ||
2794 | 2055 | else: | ||
2795 | 2056 | other_args = () | ||
2796 | 2057 | other_kwargs = value | ||
2797 | 2058 | else: | ||
2798 | 2059 | # len 2 | ||
2799 | 2060 | # could be (name, args) or (name, kwargs) or (args, kwargs) | ||
2800 | 2061 | first, second = other | ||
2801 | 2062 | if isinstance(first, basestring): | ||
2802 | 2063 | other_name = first | ||
2803 | 2064 | if isinstance(second, tuple): | ||
2804 | 2065 | other_args, other_kwargs = second, {} | ||
2805 | 2066 | else: | ||
2806 | 2067 | other_args, other_kwargs = (), second | ||
2807 | 2068 | else: | ||
2808 | 2069 | other_args, other_kwargs = first, second | ||
2809 | 2070 | |||
2810 | 2071 | if self_name and other_name != self_name: | ||
2811 | 2072 | return False | ||
2812 | 2073 | |||
2813 | 2074 | # this order is important for ANY to work! | ||
2814 | 2075 | return (other_args, other_kwargs) == (self_args, self_kwargs) | ||
2815 | 2076 | |||
2816 | 2077 | |||
2817 | 2078 | def __ne__(self, other): | ||
2818 | 2079 | return not self.__eq__(other) | ||
2819 | 2080 | |||
2820 | 2081 | |||
2821 | 2082 | def __call__(self, *args, **kwargs): | ||
2822 | 2083 | if self.name is None: | ||
2823 | 2084 | return _Call(('', args, kwargs), name='()') | ||
2824 | 2085 | |||
2825 | 2086 | name = self.name + '()' | ||
2826 | 2087 | return _Call((self.name, args, kwargs), name=name, parent=self) | ||
2827 | 2088 | |||
2828 | 2089 | |||
2829 | 2090 | def __getattr__(self, attr): | ||
2830 | 2091 | if self.name is None: | ||
2831 | 2092 | return _Call(name=attr, from_kall=False) | ||
2832 | 2093 | name = '%s.%s' % (self.name, attr) | ||
2833 | 2094 | return _Call(name=name, parent=self, from_kall=False) | ||
2834 | 2095 | |||
2835 | 2096 | |||
2836 | 2097 | def __repr__(self): | ||
2837 | 2098 | if not self.from_kall: | ||
2838 | 2099 | name = self.name or 'call' | ||
2839 | 2100 | if name.startswith('()'): | ||
2840 | 2101 | name = 'call%s' % name | ||
2841 | 2102 | return name | ||
2842 | 2103 | |||
2843 | 2104 | if len(self) == 2: | ||
2844 | 2105 | name = 'call' | ||
2845 | 2106 | args, kwargs = self | ||
2846 | 2107 | else: | ||
2847 | 2108 | name, args, kwargs = self | ||
2848 | 2109 | if not name: | ||
2849 | 2110 | name = 'call' | ||
2850 | 2111 | elif not name.startswith('()'): | ||
2851 | 2112 | name = 'call.%s' % name | ||
2852 | 2113 | else: | ||
2853 | 2114 | name = 'call%s' % name | ||
2854 | 2115 | return _format_call_signature(name, args, kwargs) | ||
2855 | 2116 | |||
2856 | 2117 | |||
2857 | 2118 | def call_list(self): | ||
2858 | 2119 | """For a call object that represents multiple calls, `call_list` | ||
2859 | 2120 | returns a list of all the intermediate calls as well as the | ||
2860 | 2121 | final call.""" | ||
2861 | 2122 | vals = [] | ||
2862 | 2123 | thing = self | ||
2863 | 2124 | while thing is not None: | ||
2864 | 2125 | if thing.from_kall: | ||
2865 | 2126 | vals.append(thing) | ||
2866 | 2127 | thing = thing.parent | ||
2867 | 2128 | return _CallList(reversed(vals)) | ||
2868 | 2129 | |||
2869 | 2130 | |||
2870 | 2131 | call = _Call(from_kall=False) | ||
2871 | 2132 | |||
2872 | 2133 | |||
2873 | 2134 | |||
2874 | 2135 | def create_autospec(spec, spec_set=False, instance=False, _parent=None, | ||
2875 | 2136 | _name=None, **kwargs): | ||
2876 | 2137 | """Create a mock object using another object as a spec. Attributes on the | ||
2877 | 2138 | mock will use the corresponding attribute on the `spec` object as their | ||
2878 | 2139 | spec. | ||
2879 | 2140 | |||
2880 | 2141 | Functions or methods being mocked will have their arguments checked | ||
2881 | 2142 | to check that they are called with the correct signature. | ||
2882 | 2143 | |||
2883 | 2144 | If `spec_set` is True then attempting to set attributes that don't exist | ||
2884 | 2145 | on the spec object will raise an `AttributeError`. | ||
2885 | 2146 | |||
2886 | 2147 | If a class is used as a spec then the return value of the mock (the | ||
2887 | 2148 | instance of the class) will have the same spec. You can use a class as the | ||
2888 | 2149 | spec for an instance object by passing `instance=True`. The returned mock | ||
2889 | 2150 | will only be callable if instances of the mock are callable. | ||
2890 | 2151 | |||
2891 | 2152 | `create_autospec` also takes arbitrary keyword arguments that are passed to | ||
2892 | 2153 | the constructor of the created mock.""" | ||
2893 | 2154 | if _is_list(spec): | ||
2894 | 2155 | # can't pass a list instance to the mock constructor as it will be | ||
2895 | 2156 | # interpreted as a list of strings | ||
2896 | 2157 | spec = type(spec) | ||
2897 | 2158 | |||
2898 | 2159 | is_type = isinstance(spec, ClassTypes) | ||
2899 | 2160 | |||
2900 | 2161 | _kwargs = {'spec': spec} | ||
2901 | 2162 | if spec_set: | ||
2902 | 2163 | _kwargs = {'spec_set': spec} | ||
2903 | 2164 | elif spec is None: | ||
2904 | 2165 | # None we mock with a normal mock without a spec | ||
2905 | 2166 | _kwargs = {} | ||
2906 | 2167 | |||
2907 | 2168 | _kwargs.update(kwargs) | ||
2908 | 2169 | |||
2909 | 2170 | Klass = MagicMock | ||
2910 | 2171 | if type(spec) in DescriptorTypes: | ||
2911 | 2172 | # descriptors don't have a spec | ||
2912 | 2173 | # because we don't know what type they return | ||
2913 | 2174 | _kwargs = {} | ||
2914 | 2175 | elif not _callable(spec): | ||
2915 | 2176 | Klass = NonCallableMagicMock | ||
2916 | 2177 | elif is_type and instance and not _instance_callable(spec): | ||
2917 | 2178 | Klass = NonCallableMagicMock | ||
2918 | 2179 | |||
2919 | 2180 | _new_name = _name | ||
2920 | 2181 | if _parent is None: | ||
2921 | 2182 | # for a top level object no _new_name should be set | ||
2922 | 2183 | _new_name = '' | ||
2923 | 2184 | |||
2924 | 2185 | mock = Klass(parent=_parent, _new_parent=_parent, _new_name=_new_name, | ||
2925 | 2186 | name=_name, **_kwargs) | ||
2926 | 2187 | |||
2927 | 2188 | if isinstance(spec, FunctionTypes): | ||
2928 | 2189 | # should only happen at the top level because we don't | ||
2929 | 2190 | # recurse for functions | ||
2930 | 2191 | mock = _set_signature(mock, spec) | ||
2931 | 2192 | else: | ||
2932 | 2193 | _check_signature(spec, mock, is_type, instance) | ||
2933 | 2194 | |||
2934 | 2195 | if _parent is not None and not instance: | ||
2935 | 2196 | _parent._mock_children[_name] = mock | ||
2936 | 2197 | |||
2937 | 2198 | if is_type and not instance and 'return_value' not in kwargs: | ||
2938 | 2199 | mock.return_value = create_autospec(spec, spec_set, instance=True, | ||
2939 | 2200 | _name='()', _parent=mock) | ||
2940 | 2201 | |||
2941 | 2202 | for entry in dir(spec): | ||
2942 | 2203 | if _is_magic(entry): | ||
2943 | 2204 | # MagicMock already does the useful magic methods for us | ||
2944 | 2205 | continue | ||
2945 | 2206 | |||
2946 | 2207 | if isinstance(spec, FunctionTypes) and entry in FunctionAttributes: | ||
2947 | 2208 | # allow a mock to actually be a function | ||
2948 | 2209 | continue | ||
2949 | 2210 | |||
2950 | 2211 | # XXXX do we need a better way of getting attributes without | ||
2951 | 2212 | # triggering code execution (?) Probably not - we need the actual | ||
2952 | 2213 | # object to mock it so we would rather trigger a property than mock | ||
2953 | 2214 | # the property descriptor. Likewise we want to mock out dynamically | ||
2954 | 2215 | # provided attributes. | ||
2955 | 2216 | # XXXX what about attributes that raise exceptions other than | ||
2956 | 2217 | # AttributeError on being fetched? | ||
2957 | 2218 | # we could be resilient against it, or catch and propagate the | ||
2958 | 2219 | # exception when the attribute is fetched from the mock | ||
2959 | 2220 | try: | ||
2960 | 2221 | original = getattr(spec, entry) | ||
2961 | 2222 | except AttributeError: | ||
2962 | 2223 | continue | ||
2963 | 2224 | |||
2964 | 2225 | kwargs = {'spec': original} | ||
2965 | 2226 | if spec_set: | ||
2966 | 2227 | kwargs = {'spec_set': original} | ||
2967 | 2228 | |||
2968 | 2229 | if not isinstance(original, FunctionTypes): | ||
2969 | 2230 | new = _SpecState(original, spec_set, mock, entry, instance) | ||
2970 | 2231 | mock._mock_children[entry] = new | ||
2971 | 2232 | else: | ||
2972 | 2233 | parent = mock | ||
2973 | 2234 | if isinstance(spec, FunctionTypes): | ||
2974 | 2235 | parent = mock.mock | ||
2975 | 2236 | |||
2976 | 2237 | new = MagicMock(parent=parent, name=entry, _new_name=entry, | ||
2977 | 2238 | _new_parent=parent, **kwargs) | ||
2978 | 2239 | mock._mock_children[entry] = new | ||
2979 | 2240 | skipfirst = _must_skip(spec, entry, is_type) | ||
2980 | 2241 | _check_signature(original, new, skipfirst=skipfirst) | ||
2981 | 2242 | |||
2982 | 2243 | # so functions created with _set_signature become instance attributes, | ||
2983 | 2244 | # *plus* their underlying mock exists in _mock_children of the parent | ||
2984 | 2245 | # mock. Adding to _mock_children may be unnecessary where we are also | ||
2985 | 2246 | # setting as an instance attribute? | ||
2986 | 2247 | if isinstance(new, FunctionTypes): | ||
2987 | 2248 | setattr(mock, entry, new) | ||
2988 | 2249 | |||
2989 | 2250 | return mock | ||
2990 | 2251 | |||
2991 | 2252 | |||
2992 | 2253 | def _must_skip(spec, entry, is_type): | ||
2993 | 2254 | if not isinstance(spec, ClassTypes): | ||
2994 | 2255 | if entry in getattr(spec, '__dict__', {}): | ||
2995 | 2256 | # instance attribute - shouldn't skip | ||
2996 | 2257 | return False | ||
2997 | 2258 | spec = spec.__class__ | ||
2998 | 2259 | if not hasattr(spec, '__mro__'): | ||
2999 | 2260 | # old style class: can't have descriptors anyway | ||
3000 | 2261 | return is_type | ||
3001 | 2262 | |||
3002 | 2263 | for klass in spec.__mro__: | ||
3003 | 2264 | result = klass.__dict__.get(entry, DEFAULT) | ||
3004 | 2265 | if result is DEFAULT: | ||
3005 | 2266 | continue | ||
3006 | 2267 | if isinstance(result, (staticmethod, classmethod)): | ||
3007 | 2268 | return False | ||
3008 | 2269 | return is_type | ||
3009 | 2270 | |||
3010 | 2271 | # shouldn't get here unless function is a dynamically provided attribute | ||
3011 | 2272 | # XXXX untested behaviour | ||
3012 | 2273 | return is_type | ||
3013 | 2274 | |||
3014 | 2275 | |||
3015 | 2276 | def _get_class(obj): | ||
3016 | 2277 | try: | ||
3017 | 2278 | return obj.__class__ | ||
3018 | 2279 | except AttributeError: | ||
3019 | 2280 | # in Python 2, _sre.SRE_Pattern objects have no __class__ | ||
3020 | 2281 | return type(obj) | ||
3021 | 2282 | |||
3022 | 2283 | |||
3023 | 2284 | class _SpecState(object): | ||
3024 | 2285 | |||
3025 | 2286 | def __init__(self, spec, spec_set=False, parent=None, | ||
3026 | 2287 | name=None, ids=None, instance=False): | ||
3027 | 2288 | self.spec = spec | ||
3028 | 2289 | self.ids = ids | ||
3029 | 2290 | self.spec_set = spec_set | ||
3030 | 2291 | self.parent = parent | ||
3031 | 2292 | self.instance = instance | ||
3032 | 2293 | self.name = name | ||
3033 | 2294 | |||
3034 | 2295 | |||
3035 | 2296 | FunctionTypes = ( | ||
3036 | 2297 | # python function | ||
3037 | 2298 | type(create_autospec), | ||
3038 | 2299 | # instance method | ||
3039 | 2300 | type(ANY.__eq__), | ||
3040 | 2301 | # unbound method | ||
3041 | 2302 | type(_ANY.__eq__), | ||
3042 | 2303 | ) | ||
3043 | 2304 | |||
3044 | 2305 | FunctionAttributes = set([ | ||
3045 | 2306 | 'func_closure', | ||
3046 | 2307 | 'func_code', | ||
3047 | 2308 | 'func_defaults', | ||
3048 | 2309 | 'func_dict', | ||
3049 | 2310 | 'func_doc', | ||
3050 | 2311 | 'func_globals', | ||
3051 | 2312 | 'func_name', | ||
3052 | 2313 | ]) | ||
3053 | 2314 | |||
3054 | 2315 | |||
3055 | 2316 | file_spec = None | ||
3056 | 2317 | |||
3057 | 2318 | |||
3058 | 2319 | def mock_open(mock=None, read_data=''): | ||
3059 | 2320 | """ | ||
3060 | 2321 | A helper function to create a mock to replace the use of `open`. It works | ||
3061 | 2322 | for `open` called directly or used as a context manager. | ||
3062 | 2323 | |||
3063 | 2324 | The `mock` argument is the mock object to configure. If `None` (the | ||
3064 | 2325 | default) then a `MagicMock` will be created for you, with the API limited | ||
3065 | 2326 | to methods or attributes available on standard file handles. | ||
3066 | 2327 | |||
3067 | 2328 | `read_data` is a string for the `read` method of the file handle to return. | ||
3068 | 2329 | This is an empty string by default. | ||
3069 | 2330 | """ | ||
3070 | 2331 | global file_spec | ||
3071 | 2332 | if file_spec is None: | ||
3072 | 2333 | # set on first use | ||
3073 | 2334 | if inPy3k: | ||
3074 | 2335 | import _io | ||
3075 | 2336 | file_spec = list(set(dir(_io.TextIOWrapper)).union(set(dir(_io.BytesIO)))) | ||
3076 | 2337 | else: | ||
3077 | 2338 | file_spec = file | ||
3078 | 2339 | |||
3079 | 2340 | if mock is None: | ||
3080 | 2341 | mock = MagicMock(name='open', spec=open) | ||
3081 | 2342 | |||
3082 | 2343 | handle = MagicMock(spec=file_spec) | ||
3083 | 2344 | handle.write.return_value = None | ||
3084 | 2345 | handle.__enter__.return_value = handle | ||
3085 | 2346 | handle.read.return_value = read_data | ||
3086 | 2347 | |||
3087 | 2348 | mock.return_value = handle | ||
3088 | 2349 | return mock | ||
3089 | 2350 | |||
3090 | 2351 | |||
3091 | 2352 | class PropertyMock(Mock): | ||
3092 | 2353 | """ | ||
3093 | 2354 | A mock intended to be used as a property, or other descriptor, on a class. | ||
3094 | 2355 | `PropertyMock` provides `__get__` and `__set__` methods so you can specify | ||
3095 | 2356 | a return value when it is fetched. | ||
3096 | 2357 | |||
3097 | 2358 | Fetching a `PropertyMock` instance from an object calls the mock, with | ||
3098 | 2359 | no args. Setting it calls the mock with the value being set. | ||
3099 | 2360 | """ | ||
3100 | 2361 | def _get_child_mock(self, **kwargs): | ||
3101 | 2362 | return MagicMock(**kwargs) | ||
3102 | 2363 | |||
3103 | 2364 | def __get__(self, obj, obj_type): | ||
3104 | 2365 | return self() | ||
3105 | 2366 | def __set__(self, obj, val): | ||
3106 | 2367 | self(val) | ||
3107 | 2368 | 0 | ||
3108 | === modified file 'deployer/tests/test_base.py' | |||
3109 | --- deployer/tests/test_base.py 2013-07-22 15:29:31 +0000 | |||
3110 | +++ deployer/tests/test_base.py 2016-05-08 03:36:27 +0000 | |||
3111 | @@ -1,17 +1,19 @@ | |||
3112 | 1 | from __future__ import absolute_import | ||
3113 | 1 | import os | 2 | import os |
3114 | 2 | import shutil | 3 | import shutil |
3115 | 3 | import unittest | 4 | import unittest |
3116 | 4 | import StringIO | ||
3117 | 5 | import logging | 5 | import logging |
3118 | 6 | import tempfile | 6 | import tempfile |
3119 | 7 | 7 | ||
3120 | 8 | from six import StringIO | ||
3121 | 9 | |||
3122 | 8 | 10 | ||
3123 | 9 | class Base(unittest.TestCase): | 11 | class Base(unittest.TestCase): |
3124 | 10 | 12 | ||
3125 | 11 | def capture_logging(self, name="", level=logging.INFO, | 13 | def capture_logging(self, name="", level=logging.INFO, |
3126 | 12 | log_file=None, formatter=None): | 14 | log_file=None, formatter=None): |
3127 | 13 | if log_file is None: | 15 | if log_file is None: |
3129 | 14 | log_file = StringIO.StringIO() | 16 | log_file = StringIO() |
3130 | 15 | log_handler = logging.StreamHandler(log_file) | 17 | log_handler = logging.StreamHandler(log_file) |
3131 | 16 | if formatter: | 18 | if formatter: |
3132 | 17 | log_handler.setFormatter(formatter) | 19 | log_handler.setFormatter(formatter) |
3133 | 18 | 20 | ||
3134 | === modified file 'deployer/tests/test_charm.py' | |||
3135 | --- deployer/tests/test_charm.py 2016-03-10 17:59:21 +0000 | |||
3136 | +++ deployer/tests/test_charm.py 2016-05-08 03:36:27 +0000 | |||
3137 | @@ -1,3 +1,4 @@ | |||
3138 | 1 | from __future__ import absolute_import | ||
3139 | 1 | import os | 2 | import os |
3140 | 2 | import logging | 3 | import logging |
3141 | 3 | import subprocess | 4 | import subprocess |
3142 | @@ -313,5 +314,5 @@ | |||
3143 | 313 | Charm.from_service( | 314 | Charm.from_service( |
3144 | 314 | "scratch", self.repo_path, "precise", params) | 315 | "scratch", self.repo_path, "precise", params) |
3145 | 315 | self.fail("should have failed, vcs ambigious") | 316 | self.fail("should have failed, vcs ambigious") |
3147 | 316 | except ValueError, e: | 317 | except ValueError as e: |
3148 | 317 | self.assertIn("Could not determine vcs backend", str(e)) | 318 | self.assertIn("Could not determine vcs backend", str(e)) |
3149 | 318 | 319 | ||
3150 | === modified file 'deployer/tests/test_config.py' | |||
3151 | --- deployer/tests/test_config.py 2015-03-04 19:27:31 +0000 | |||
3152 | +++ deployer/tests/test_config.py 2016-05-08 03:36:27 +0000 | |||
3153 | @@ -1,9 +1,12 @@ | |||
3154 | 1 | from __future__ import absolute_import | ||
3155 | 1 | import logging | 2 | import logging |
3156 | 2 | import mock | 3 | import mock |
3157 | 3 | import os | 4 | import os |
3158 | 4 | import tempfile | 5 | import tempfile |
3159 | 5 | import yaml | 6 | import yaml |
3160 | 6 | 7 | ||
3161 | 8 | import six | ||
3162 | 9 | |||
3163 | 7 | from deployer.deployment import Deployment | 10 | from deployer.deployment import Deployment |
3164 | 8 | from deployer.config import ConfigStack | 11 | from deployer.config import ConfigStack |
3165 | 9 | from deployer.utils import ErrorExit | 12 | from deployer.utils import ErrorExit |
3166 | @@ -21,7 +24,7 @@ | |||
3167 | 21 | config = ConfigStack(['configs/ostack-testing-sample.cfg']) | 24 | config = ConfigStack(['configs/ostack-testing-sample.cfg']) |
3168 | 22 | config.load() | 25 | config.load() |
3169 | 23 | self.assertEqual( | 26 | self.assertEqual( |
3171 | 24 | config.keys(), | 27 | list(config.keys()), |
3172 | 25 | [u'openstack-precise-ec2', | 28 | [u'openstack-precise-ec2', |
3173 | 26 | u'openstack-precise-ec2-trunk', | 29 | u'openstack-precise-ec2-trunk', |
3174 | 27 | u'openstack-ubuntu-testing']) | 30 | u'openstack-ubuntu-testing']) |
3175 | @@ -36,7 +39,7 @@ | |||
3176 | 36 | os.path.join(self.test_data_dir, "stack-inherits.cfg")]) | 39 | os.path.join(self.test_data_dir, "stack-inherits.cfg")]) |
3177 | 37 | config.load() | 40 | config.load() |
3178 | 38 | self.assertEqual( | 41 | self.assertEqual( |
3180 | 39 | config.keys(), | 42 | list(config.keys()), |
3181 | 40 | [u'my-files-frontend-dev', u'wordpress']) | 43 | [u'my-files-frontend-dev', u'wordpress']) |
3182 | 41 | deployment = config.get("wordpress") | 44 | deployment = config.get("wordpress") |
3183 | 42 | self.assertTrue(deployment) | 45 | self.assertTrue(deployment) |
3184 | @@ -47,11 +50,11 @@ | |||
3185 | 47 | os.path.join(self.test_data_dir, 'v4', 'simple.yaml')]) | 50 | os.path.join(self.test_data_dir, 'v4', 'simple.yaml')]) |
3186 | 48 | config.load() | 51 | config.load() |
3187 | 49 | self.assertEqual( | 52 | self.assertEqual( |
3189 | 50 | config.keys(), | 53 | list(config.keys()), |
3190 | 51 | [os.path.join(self.test_data_dir, 'v4', 'simple.yaml')]) | 54 | [os.path.join(self.test_data_dir, 'v4', 'simple.yaml')]) |
3191 | 52 | with mock.patch('deployer.config.ConfigStack._resolve_inherited') \ | 55 | with mock.patch('deployer.config.ConfigStack._resolve_inherited') \ |
3192 | 53 | as mock_resolve: | 56 | as mock_resolve: |
3194 | 54 | deployment = config.get(config.keys()[0]) | 57 | deployment = config.get(list(config.keys())[0]) |
3195 | 55 | self.assertTrue(deployment) | 58 | self.assertTrue(deployment) |
3196 | 56 | self.assertFalse(mock_resolve.called) | 59 | self.assertFalse(mock_resolve.called) |
3197 | 57 | self.assertEqual(config.version, 4) | 60 | self.assertEqual(config.version, 4) |
3198 | @@ -62,7 +65,7 @@ | |||
3199 | 62 | config.load() | 65 | config.load() |
3200 | 63 | # ensure picked up stacks from both files | 66 | # ensure picked up stacks from both files |
3201 | 64 | self.assertEqual( | 67 | self.assertEqual( |
3203 | 65 | config.keys(), | 68 | list(config.keys()), |
3204 | 66 | [u'my-files-frontend-dev', u'wordpress']) | 69 | [u'my-files-frontend-dev', u'wordpress']) |
3205 | 67 | 70 | ||
3206 | 68 | # ensure inheritance was adhered to during cross-file load | 71 | # ensure inheritance was adhered to during cross-file load |
3207 | @@ -104,7 +107,7 @@ | |||
3208 | 104 | for key in ['include-config', 'include-configs']: | 107 | for key in ['include-config', 'include-configs']: |
3209 | 105 | test_conf[key] = includes | 108 | test_conf[key] = includes |
3210 | 106 | with tempfile.NamedTemporaryFile() as tmp_cfg: | 109 | with tempfile.NamedTemporaryFile() as tmp_cfg: |
3212 | 107 | tmp_cfg.write(yaml.dump(test_conf)) | 110 | tmp_cfg.write(yaml.dump(test_conf).encode()) |
3213 | 108 | tmp_cfg.flush() | 111 | tmp_cfg.flush() |
3214 | 109 | config = ConfigStack([tmp_cfg.name]) | 112 | config = ConfigStack([tmp_cfg.name]) |
3215 | 110 | self._test_multiple_inheritance(config) | 113 | self._test_multiple_inheritance(config) |
3216 | @@ -141,7 +144,9 @@ | |||
3217 | 141 | ex_rels = [('quantum-gateway', 'nova-cloud-controller'), | 144 | ex_rels = [('quantum-gateway', 'nova-cloud-controller'), |
3218 | 142 | ('quantum-gateway', 'mysql'), | 145 | ('quantum-gateway', 'mysql'), |
3219 | 143 | ('nova-cloud-controller', 'mysql')] | 146 | ('nova-cloud-controller', 'mysql')] |
3221 | 144 | self.assertEquals(ex_rels, list(deployment.get_relations())) | 147 | self.assertEquals( |
3222 | 148 | sorted(ex_rels), | ||
3223 | 149 | sorted(list(deployment.get_relations()))) | ||
3224 | 145 | 150 | ||
3225 | 146 | def test_config_series_override(self): | 151 | def test_config_series_override(self): |
3226 | 147 | config = ConfigStack(['configs/wiki.yaml'], 'trusty') | 152 | config = ConfigStack(['configs/wiki.yaml'], 'trusty') |
3227 | @@ -166,18 +171,19 @@ | |||
3228 | 166 | config = ConfigStack([]) | 171 | config = ConfigStack([]) |
3229 | 167 | config.config_files = [CONFIG_URL] | 172 | config.config_files = [CONFIG_URL] |
3230 | 168 | 173 | ||
3232 | 169 | class FauxResponse(file): | 174 | class FauxResponse(six.BytesIO): |
3233 | 170 | def getcode(self): | 175 | def getcode(self): |
3234 | 171 | return 200 | 176 | return 200 |
3235 | 172 | 177 | ||
3236 | 173 | def faux_urlopen(url): | 178 | def faux_urlopen(url): |
3237 | 174 | self.assertEqual(url, CONFIG_URL) | 179 | self.assertEqual(url, CONFIG_URL) |
3239 | 175 | return FauxResponse('configs/ostack-testing-sample.cfg') | 180 | with open('configs/ostack-testing-sample.cfg', 'rb') as f: |
3240 | 181 | return FauxResponse(f.read()) | ||
3241 | 176 | 182 | ||
3242 | 177 | config.urlopen = faux_urlopen | 183 | config.urlopen = faux_urlopen |
3243 | 178 | config.load() | 184 | config.load() |
3244 | 179 | self.assertEqual( | 185 | self.assertEqual( |
3246 | 180 | config.keys(), | 186 | list(config.keys()), |
3247 | 181 | [u'openstack-precise-ec2', | 187 | [u'openstack-precise-ec2', |
3248 | 182 | u'openstack-precise-ec2-trunk', | 188 | u'openstack-precise-ec2-trunk', |
3249 | 183 | u'openstack-ubuntu-testing']) | 189 | u'openstack-ubuntu-testing']) |
3250 | @@ -193,12 +199,14 @@ | |||
3251 | 193 | config = ConfigStack([]) | 199 | config = ConfigStack([]) |
3252 | 194 | config.config_files = [CONFIG_URL] | 200 | config.config_files = [CONFIG_URL] |
3253 | 195 | 201 | ||
3255 | 196 | class FauxResponse(file): | 202 | class FauxResponse(six.BytesIO): |
3256 | 197 | def getcode(self): | 203 | def getcode(self): |
3257 | 198 | return 400 | 204 | return 400 |
3258 | 199 | 205 | ||
3259 | 200 | def faux_urlopen(url): | 206 | def faux_urlopen(url): |
3260 | 201 | self.assertEqual(url, CONFIG_URL) | 207 | self.assertEqual(url, CONFIG_URL) |
3262 | 202 | return FauxResponse('configs/ostack-testing-sample.cfg') | 208 | with open('configs/ostack-testing-sample.cfg', 'rb') as f: |
3263 | 209 | return FauxResponse(f.read()) | ||
3264 | 210 | |||
3265 | 203 | config.urlopen = faux_urlopen | 211 | config.urlopen = faux_urlopen |
3266 | 204 | self.assertRaises(ErrorExit, config.load) | 212 | self.assertRaises(ErrorExit, config.load) |
3267 | 205 | 213 | ||
3268 | === modified file 'deployer/tests/test_constraints.py' | |||
3269 | --- deployer/tests/test_constraints.py 2015-09-03 14:25:56 +0000 | |||
3270 | +++ deployer/tests/test_constraints.py 2016-05-08 03:36:27 +0000 | |||
3271 | @@ -1,3 +1,4 @@ | |||
3272 | 1 | from __future__ import absolute_import | ||
3273 | 1 | from deployer.service import Service | 2 | from deployer.service import Service |
3274 | 2 | from .base import Base | 3 | from .base import Base |
3275 | 3 | from ..utils import parse_constraints | 4 | from ..utils import parse_constraints |
3276 | @@ -94,9 +95,10 @@ | |||
3277 | 94 | 'mem': '1E', | 95 | 'mem': '1E', |
3278 | 95 | } | 96 | } |
3279 | 96 | with self.assertRaises(ValueError) as exc: | 97 | with self.assertRaises(ValueError) as exc: |
3283 | 97 | result = parse_constraints(value) | 98 | parse_constraints(value) |
3284 | 98 | self.assertEqual('Constraint mem has invalid value 1E', | 99 | self.assertEqual( |
3285 | 99 | exc.exception.message) | 100 | 'Constraint mem has invalid value 1E', |
3286 | 101 | exc.exception.args[0]) | ||
3287 | 100 | 102 | ||
3288 | 101 | def test_other_numeric_constraints_have_no_units(self): | 103 | def test_other_numeric_constraints_have_no_units(self): |
3289 | 102 | # If any other numeric constraint gets a units specifier an error is | 104 | # If any other numeric constraint gets a units specifier an error is |
3290 | @@ -108,8 +110,9 @@ | |||
3291 | 108 | } | 110 | } |
3292 | 109 | with self.assertRaises(ValueError) as exc: | 111 | with self.assertRaises(ValueError) as exc: |
3293 | 110 | parse_constraints(value) | 112 | parse_constraints(value) |
3296 | 111 | self.assertEqual('Constraint {} has invalid value 1T'.format(k), | 113 | self.assertEqual( |
3297 | 112 | exc.exception.message) | 114 | 'Constraint {} has invalid value 1T'.format(k), |
3298 | 115 | exc.exception.args[0]) | ||
3299 | 113 | 116 | ||
3300 | 114 | def test_non_numerics_are_not_converted(self): | 117 | def test_non_numerics_are_not_converted(self): |
3301 | 115 | # Constraints that expect strings are not affected by the parsing. | 118 | # Constraints that expect strings are not affected by the parsing. |
3302 | 116 | 119 | ||
3303 | === modified file 'deployer/tests/test_deployment.py' | |||
3304 | --- deployer/tests/test_deployment.py 2015-08-06 12:04:16 +0000 | |||
3305 | +++ deployer/tests/test_deployment.py 2016-05-08 03:36:27 +0000 | |||
3306 | @@ -1,7 +1,9 @@ | |||
3307 | 1 | from __future__ import absolute_import | ||
3308 | 1 | import base64 | 2 | import base64 |
3309 | 2 | import StringIO | ||
3310 | 3 | import os | 3 | import os |
3311 | 4 | 4 | ||
3312 | 5 | from six import StringIO | ||
3313 | 6 | |||
3314 | 5 | from deployer.deployment import Deployment | 7 | from deployer.deployment import Deployment |
3315 | 6 | from deployer.utils import setup_logging, ErrorExit | 8 | from deployer.utils import setup_logging, ErrorExit |
3316 | 7 | 9 | ||
3317 | @@ -22,7 +24,7 @@ | |||
3318 | 22 | 24 | ||
3319 | 23 | def setUp(self): | 25 | def setUp(self): |
3320 | 24 | self.output = setup_logging( | 26 | self.output = setup_logging( |
3322 | 25 | debug=True, verbose=True, stream=StringIO.StringIO()) | 27 | debug=True, verbose=True, stream=StringIO()) |
3323 | 26 | 28 | ||
3324 | 27 | def get_named_deployment_and_fetch_v3(self, file_name, stack_name): | 29 | def get_named_deployment_and_fetch_v3(self, file_name, stack_name): |
3325 | 28 | deployment = self.get_named_deployment_v3(file_name, stack_name) | 30 | deployment = self.get_named_deployment_v3(file_name, stack_name) |
3326 | @@ -46,7 +48,8 @@ | |||
3327 | 46 | 48 | ||
3328 | 47 | @skip_if_offline | 49 | @skip_if_offline |
3329 | 48 | def test_deployer(self): | 50 | def test_deployer(self): |
3331 | 49 | d = self.get_named_deployment_and_fetch_v3('blog.yaml', 'wordpress-prod') | 51 | d = self.get_named_deployment_and_fetch_v3( |
3332 | 52 | 'blog.yaml', 'wordpress-prod') | ||
3333 | 50 | services = d.get_services() | 53 | services = d.get_services() |
3334 | 51 | self.assertTrue([s for s in services if s.name == "newrelic"]) | 54 | self.assertTrue([s for s in services if s.name == "newrelic"]) |
3335 | 52 | 55 | ||
3336 | @@ -63,7 +66,7 @@ | |||
3337 | 63 | self.assertEqual(d.get_service('newrelic').config, {'key': 'abc'}) | 66 | self.assertEqual(d.get_service('newrelic').config, {'key': 'abc'}) |
3338 | 64 | self.assertEqual( | 67 | self.assertEqual( |
3339 | 65 | base64.b64decode(d.get_service('blog').config['wp-content']), | 68 | base64.b64decode(d.get_service('blog').config['wp-content']), |
3341 | 66 | "HelloWorld") | 69 | b"HelloWorld") |
3342 | 67 | 70 | ||
3343 | 68 | # TODO verify include-file | 71 | # TODO verify include-file |
3344 | 69 | 72 | ||
3345 | @@ -82,7 +85,8 @@ | |||
3346 | 82 | 85 | ||
3347 | 83 | @skip_if_offline | 86 | @skip_if_offline |
3348 | 84 | def test_validate_placement_sorting(self): | 87 | def test_validate_placement_sorting(self): |
3350 | 85 | d = self.get_named_deployment_and_fetch_v3("stack-placement.yaml", "stack") | 88 | d = self.get_named_deployment_and_fetch_v3( |
3351 | 89 | "stack-placement.yaml", "stack") | ||
3352 | 86 | services = d.get_services() | 90 | services = d.get_services() |
3353 | 87 | self.assertEqual(services[0].name, 'nova-compute') | 91 | self.assertEqual(services[0].name, 'nova-compute') |
3354 | 88 | try: | 92 | try: |
3355 | @@ -164,7 +168,8 @@ | |||
3356 | 164 | 168 | ||
3357 | 165 | @skip_if_offline | 169 | @skip_if_offline |
3358 | 166 | def test_validate_invalid_placement_nested(self): | 170 | def test_validate_invalid_placement_nested(self): |
3360 | 167 | d = self.get_named_deployment_and_fetch_v3("stack-placement-invalid.yaml", "stack") | 171 | d = self.get_named_deployment_and_fetch_v3( |
3361 | 172 | "stack-placement-invalid.yaml", "stack") | ||
3362 | 168 | services = d.get_services() | 173 | services = d.get_services() |
3363 | 169 | self.assertEqual(services[0].name, 'nova-compute') | 174 | self.assertEqual(services[0].name, 'nova-compute') |
3364 | 170 | try: | 175 | try: |
3365 | @@ -438,7 +443,8 @@ | |||
3366 | 438 | d.set_machines(machines) | 443 | d.set_machines(machines) |
3367 | 439 | 444 | ||
3368 | 440 | placement = d.get_unit_placement('mysql', status) | 445 | placement = d.get_unit_placement('mysql', status) |
3370 | 441 | self.assertEqual(placement.get_new_machines_for_containers(), | 446 | self.assertEqual( |
3371 | 447 | placement.get_new_machines_for_containers(), | ||
3372 | 442 | ['mysql/0']) | 448 | ['mysql/0']) |
3373 | 443 | self.assertEqual(placement.get(0), 'lxc:2') | 449 | self.assertEqual(placement.get(0), 'lxc:2') |
3374 | 444 | 450 | ||
3375 | @@ -450,8 +456,8 @@ | |||
3376 | 450 | "nginx": {"consumes": ["wordpress"]}}} | 456 | "nginx": {"consumes": ["wordpress"]}}} |
3377 | 451 | d = Deployment("foo", data, include_dirs=()) | 457 | d = Deployment("foo", data, include_dirs=()) |
3378 | 452 | self.assertEqual( | 458 | self.assertEqual( |
3381 | 453 | [('nginx', 'wordpress'), ('wordpress', 'mysql')], | 459 | sorted([('nginx', 'wordpress'), ('wordpress', 'mysql')]), |
3382 | 454 | list(d.get_relations())) | 460 | sorted(list(d.get_relations()))) |
3383 | 455 | 461 | ||
3384 | 456 | def test_multiple_relations_weighted(self): | 462 | def test_multiple_relations_weighted(self): |
3385 | 457 | data = { | 463 | data = { |
3386 | @@ -477,10 +483,12 @@ | |||
3387 | 477 | 483 | ||
3388 | 478 | def test_getting_service_names(self): | 484 | def test_getting_service_names(self): |
3389 | 479 | # It is possible to retrieve the service names. | 485 | # It is possible to retrieve the service names. |
3391 | 480 | deployment = self.get_named_deployment_v3("stack-placement.yaml", "stack") | 486 | deployment = self.get_named_deployment_v3( |
3392 | 487 | "stack-placement.yaml", "stack") | ||
3393 | 481 | service_names = deployment.get_service_names() | 488 | service_names = deployment.get_service_names() |
3394 | 482 | expected_service_names = [ | 489 | expected_service_names = [ |
3396 | 483 | 'ceph', 'mysql', 'nova-compute', 'quantum', 'semper', 'verity', 'lxc-service'] | 490 | 'ceph', 'mysql', 'nova-compute', 'quantum', |
3397 | 491 | 'semper', 'verity', 'lxc-service'] | ||
3398 | 484 | self.assertEqual(set(expected_service_names), set(service_names)) | 492 | self.assertEqual(set(expected_service_names), set(service_names)) |
3399 | 485 | 493 | ||
3400 | 486 | def test_resolve_config_handles_empty_options(self): | 494 | def test_resolve_config_handles_empty_options(self): |
3401 | 487 | 495 | ||
3402 | === modified file 'deployer/tests/test_diff.py' | |||
3403 | --- deployer/tests/test_diff.py 2015-03-17 17:34:57 +0000 | |||
3404 | +++ deployer/tests/test_diff.py 2016-05-08 03:36:27 +0000 | |||
3405 | @@ -1,11 +1,13 @@ | |||
3406 | 1 | """ Unittest for juju-deployer diff action (--diff) """ | 1 | """ Unittest for juju-deployer diff action (--diff) """ |
3407 | 2 | # pylint: disable=C0103 | 2 | # pylint: disable=C0103 |
3409 | 3 | import StringIO | 3 | from __future__ import absolute_import |
3410 | 4 | import os | 4 | import os |
3411 | 5 | import shutil | 5 | import shutil |
3412 | 6 | import tempfile | 6 | import tempfile |
3413 | 7 | import unittest | 7 | import unittest |
3414 | 8 | 8 | ||
3415 | 9 | from six import StringIO | ||
3416 | 10 | |||
3417 | 9 | from deployer.config import ConfigStack | 11 | from deployer.config import ConfigStack |
3418 | 10 | from deployer.env.mem import MemoryEnvironment | 12 | from deployer.env.mem import MemoryEnvironment |
3419 | 11 | from deployer.utils import setup_logging | 13 | from deployer.utils import setup_logging |
3420 | @@ -19,7 +21,7 @@ | |||
3421 | 19 | 21 | ||
3422 | 20 | def setUp(self): | 22 | def setUp(self): |
3423 | 21 | self.output = setup_logging( | 23 | self.output = setup_logging( |
3425 | 22 | debug=True, verbose=True, stream=StringIO.StringIO()) | 24 | debug=True, verbose=True, stream=StringIO()) |
3426 | 23 | 25 | ||
3427 | 24 | # Because fetch_charms is expensive, do it once for all tests | 26 | # Because fetch_charms is expensive, do it once for all tests |
3428 | 25 | @classmethod | 27 | @classmethod |
3429 | @@ -113,6 +115,7 @@ | |||
3430 | 113 | env = MemoryEnvironment(dpl.name, dpl) | 115 | env = MemoryEnvironment(dpl.name, dpl) |
3431 | 114 | env.destroy_service('haproxy') | 116 | env.destroy_service('haproxy') |
3432 | 115 | diff = Diff(env, dpl, {}).do_diff() | 117 | diff = Diff(env, dpl, {}).do_diff() |
3436 | 116 | self.assertTrue(str(diff['relations']['missing'][0]).find('haproxy') | 118 | self.assertTrue( |
3437 | 117 | != -1) | 119 | str(diff['relations']['missing'][0]).find('haproxy') != -1) |
3438 | 118 | self.assertTrue(diff['services']['missing'].keys() == ['haproxy']) | 120 | self.assertTrue( |
3439 | 121 | list(diff['services']['missing'].keys()) == ['haproxy']) | ||
3440 | 119 | 122 | ||
3441 | === modified file 'deployer/tests/test_goenv.py' | |||
3442 | --- deployer/tests/test_goenv.py 2016-04-01 02:22:26 +0000 | |||
3443 | +++ deployer/tests/test_goenv.py 2016-05-08 03:36:27 +0000 | |||
3444 | @@ -1,3 +1,4 @@ | |||
3445 | 1 | from __future__ import absolute_import | ||
3446 | 1 | import logging | 2 | import logging |
3447 | 2 | import os | 3 | import os |
3448 | 3 | import time | 4 | import time |
3449 | @@ -44,7 +45,7 @@ | |||
3450 | 44 | self.assertFalse(status.get('services')) | 45 | self.assertFalse(status.get('services')) |
3451 | 45 | # Destroy everything.. consistent baseline | 46 | # Destroy everything.. consistent baseline |
3452 | 46 | self.env.reset( | 47 | self.env.reset( |
3454 | 47 | terminate_machines=len(status['machines'].keys()) > 1, | 48 | terminate_machines=len(list(status['machines'].keys())) > 1, |
3455 | 48 | terminate_delay=240) | 49 | terminate_delay=240) |
3456 | 49 | 50 | ||
3457 | 50 | def tearDown(self): | 51 | def tearDown(self): |
3458 | 51 | 52 | ||
3459 | === modified file 'deployer/tests/test_guienv.py' | |||
3460 | --- deployer/tests/test_guienv.py 2014-12-17 13:06:49 +0000 | |||
3461 | +++ deployer/tests/test_guienv.py 2016-05-08 03:36:27 +0000 | |||
3462 | @@ -1,5 +1,6 @@ | |||
3463 | 1 | """Tests for the GUIEnvironment.""" | 1 | """Tests for the GUIEnvironment.""" |
3464 | 2 | 2 | ||
3465 | 3 | from __future__ import absolute_import | ||
3466 | 3 | import unittest | 4 | import unittest |
3467 | 4 | import mock | 5 | import mock |
3468 | 5 | 6 | ||
3469 | 6 | 7 | ||
3470 | === modified file 'deployer/tests/test_guiserver.py' | |||
3471 | --- deployer/tests/test_guiserver.py 2016-05-03 16:03:18 +0000 | |||
3472 | +++ deployer/tests/test_guiserver.py 2016-05-08 03:36:27 +0000 | |||
3473 | @@ -1,5 +1,6 @@ | |||
3474 | 1 | """Tests for the GUI server bundles deployment support.""" | 1 | """Tests for the GUI server bundles deployment support.""" |
3475 | 2 | 2 | ||
3476 | 3 | from __future__ import absolute_import | ||
3477 | 3 | from contextlib import contextmanager | 4 | from contextlib import contextmanager |
3478 | 4 | import os | 5 | import os |
3479 | 5 | import shutil | 6 | import shutil |
3480 | 6 | 7 | ||
3481 | === modified file 'deployer/tests/test_importer.py' | |||
3482 | --- deployer/tests/test_importer.py 2016-05-03 16:03:18 +0000 | |||
3483 | +++ deployer/tests/test_importer.py 2016-05-08 03:36:27 +0000 | |||
3484 | @@ -1,3 +1,4 @@ | |||
3485 | 1 | from __future__ import absolute_import | ||
3486 | 1 | import os | 2 | import os |
3487 | 2 | 3 | ||
3488 | 3 | import mock | 4 | import mock |
3489 | @@ -5,7 +6,7 @@ | |||
3490 | 5 | from deployer.config import ConfigStack | 6 | from deployer.config import ConfigStack |
3491 | 6 | from deployer.action.importer import Importer | 7 | from deployer.action.importer import Importer |
3492 | 7 | 8 | ||
3494 | 8 | from base import ( | 9 | from .base import ( |
3495 | 9 | Base, | 10 | Base, |
3496 | 10 | patch_env_status, | 11 | patch_env_status, |
3497 | 11 | skip_if_offline, | 12 | skip_if_offline, |
3498 | @@ -50,6 +51,7 @@ | |||
3499 | 50 | @skip_if_offline | 51 | @skip_if_offline |
3500 | 51 | @mock.patch('deployer.action.importer.time') | 52 | @mock.patch('deployer.action.importer.time') |
3501 | 52 | def test_importer(self, mock_time): | 53 | def test_importer(self, mock_time): |
3502 | 54 | mock_time.time.return_value = 0 | ||
3503 | 53 | # Trying to track down where this comes from http://pad.lv/1243827 | 55 | # Trying to track down where this comes from http://pad.lv/1243827 |
3504 | 54 | stack = ConfigStack(self.options.configs) | 56 | stack = ConfigStack(self.options.configs) |
3505 | 55 | deploy = stack.get('wiki') | 57 | deploy = stack.get('wiki') |
3506 | @@ -69,6 +71,7 @@ | |||
3507 | 69 | @skip_if_offline | 71 | @skip_if_offline |
3508 | 70 | @mock.patch('deployer.action.importer.time') | 72 | @mock.patch('deployer.action.importer.time') |
3509 | 71 | def test_importer_no_relations(self, mock_time): | 73 | def test_importer_no_relations(self, mock_time): |
3510 | 74 | mock_time.time.return_value = 0 | ||
3511 | 72 | self.options.no_relations = True | 75 | self.options.no_relations = True |
3512 | 73 | stack = ConfigStack(self.options.configs) | 76 | stack = ConfigStack(self.options.configs) |
3513 | 74 | deploy = stack.get('wiki') | 77 | deploy = stack.get('wiki') |
3514 | @@ -81,6 +84,7 @@ | |||
3515 | 81 | @skip_if_offline | 84 | @skip_if_offline |
3516 | 82 | @mock.patch('deployer.action.importer.time') | 85 | @mock.patch('deployer.action.importer.time') |
3517 | 83 | def test_importer_add_machine_series(self, mock_time): | 86 | def test_importer_add_machine_series(self, mock_time): |
3518 | 87 | mock_time.time.return_value = 0 | ||
3519 | 84 | self.options.configs = [ | 88 | self.options.configs = [ |
3520 | 85 | os.path.join(self.test_data_dir, 'v4', 'series.yaml')] | 89 | os.path.join(self.test_data_dir, 'v4', 'series.yaml')] |
3521 | 86 | stack = ConfigStack(self.options.configs) | 90 | stack = ConfigStack(self.options.configs) |
3522 | @@ -91,16 +95,15 @@ | |||
3523 | 91 | importer.run() | 95 | importer.run() |
3524 | 92 | 96 | ||
3525 | 93 | self.assertEqual(env.add_machine.call_count, 2) | 97 | self.assertEqual(env.add_machine.call_count, 2) |
3532 | 94 | self.assertEqual( | 98 | env.add_machine.assert_has_calls([ |
3533 | 95 | env.add_machine.call_args_list[0][1], | 99 | mock.call(series='precise', constraints='mem=512M'), |
3534 | 96 | {'series': 'precise', 'constraints': 'mem=512M'}) | 100 | mock.call(series='trusty', constraints='mem=512M'), |
3535 | 97 | self.assertEqual( | 101 | ], any_order=True) |
3530 | 98 | env.add_machine.call_args_list[1][1], | ||
3531 | 99 | {'series': 'trusty', 'constraints': 'mem=512M'}) | ||
3536 | 100 | 102 | ||
3537 | 101 | @skip_if_offline | 103 | @skip_if_offline |
3538 | 102 | @mock.patch('deployer.action.importer.time') | 104 | @mock.patch('deployer.action.importer.time') |
3539 | 103 | def test_importer_existing_machine(self, mock_time): | 105 | def test_importer_existing_machine(self, mock_time): |
3540 | 106 | mock_time.time.return_value = 0 | ||
3541 | 104 | self.options.configs = [ | 107 | self.options.configs = [ |
3542 | 105 | os.path.join(self.test_data_dir, 'v4', | 108 | os.path.join(self.test_data_dir, 'v4', |
3543 | 106 | 'container-existing-machine.yaml')] | 109 | 'container-existing-machine.yaml')] |
3544 | 107 | 110 | ||
3545 | === modified file 'deployer/tests/test_pyenv.py' | |||
3546 | --- deployer/tests/test_pyenv.py 2014-02-22 23:11:02 +0000 | |||
3547 | +++ deployer/tests/test_pyenv.py 2016-05-08 03:36:27 +0000 | |||
3548 | @@ -1,4 +1,6 @@ | |||
3550 | 1 | import StringIO | 1 | from __future__ import absolute_import |
3551 | 2 | |||
3552 | 3 | from six import StringIO | ||
3553 | 2 | 4 | ||
3554 | 3 | from .base import Base | 5 | from .base import Base |
3555 | 4 | from deployer.env import watchers | 6 | from deployer.env import watchers |
3556 | @@ -21,7 +23,7 @@ | |||
3557 | 21 | 23 | ||
3558 | 22 | def setUp(self): | 24 | def setUp(self): |
3559 | 23 | self.output = setup_logging( | 25 | self.output = setup_logging( |
3561 | 24 | debug=True, verbose=True, stream=StringIO.StringIO()) | 26 | debug=True, verbose=True, stream=StringIO()) |
3562 | 25 | 27 | ||
3563 | 26 | def test_wait_for_units_error_no_exit(self): | 28 | def test_wait_for_units_error_no_exit(self): |
3564 | 27 | env = FakePyEnvironment( | 29 | env = FakePyEnvironment( |
3565 | 28 | 30 | ||
3566 | === modified file 'deployer/tests/test_service.py' | |||
3567 | --- deployer/tests/test_service.py 2013-07-22 15:29:31 +0000 | |||
3568 | +++ deployer/tests/test_service.py 2016-05-08 03:36:27 +0000 | |||
3569 | @@ -1,3 +1,4 @@ | |||
3570 | 1 | from __future__ import absolute_import | ||
3571 | 1 | from deployer.service import Service | 2 | from deployer.service import Service |
3572 | 2 | from .base import Base | 3 | from .base import Base |
3573 | 3 | 4 | ||
3574 | 4 | 5 | ||
3575 | === modified file 'deployer/tests/test_utils.py' | |||
3576 | --- deployer/tests/test_utils.py 2016-01-11 13:35:47 +0000 | |||
3577 | +++ deployer/tests/test_utils.py 2016-05-08 03:36:27 +0000 | |||
3578 | @@ -1,5 +1,5 @@ | |||
3579 | 1 | from __future__ import absolute_import | ||
3580 | 1 | import os | 2 | import os |
3581 | 2 | from StringIO import StringIO | ||
3582 | 3 | from subprocess import CalledProcessError | 3 | from subprocess import CalledProcessError |
3583 | 4 | 4 | ||
3584 | 5 | from mock import ( | 5 | from mock import ( |
3585 | @@ -7,6 +7,8 @@ | |||
3586 | 7 | patch, | 7 | patch, |
3587 | 8 | ) | 8 | ) |
3588 | 9 | 9 | ||
3589 | 10 | from six import BytesIO | ||
3590 | 11 | |||
3591 | 10 | from .base import Base | 12 | from .base import Base |
3592 | 11 | from deployer.utils import ( | 13 | from deployer.utils import ( |
3593 | 12 | _check_call, | 14 | _check_call, |
3594 | @@ -73,7 +75,7 @@ | |||
3595 | 73 | self.assertRaises( | 75 | self.assertRaises( |
3596 | 74 | OSError, _check_call, params=[cmd], log=MagicMock()) | 76 | OSError, _check_call, params=[cmd], log=MagicMock()) |
3597 | 75 | output = _check_call(params=[cmd], log=MagicMock(), shell=True) | 77 | output = _check_call(params=[cmd], log=MagicMock(), shell=True) |
3599 | 76 | self.assertEqual(output, "foo\n") | 78 | self.assertEqual(output, b"foo\n") |
3600 | 77 | 79 | ||
3601 | 78 | 80 | ||
3602 | 79 | class TestMkdir(Base): | 81 | class TestMkdir(Base): |
3603 | @@ -117,7 +119,7 @@ | |||
3604 | 117 | # Errors are correctly re-raised. | 119 | # Errors are correctly re-raised. |
3605 | 118 | path = os.path.join(self.playground, 'foo') | 120 | path = os.path.join(self.playground, 'foo') |
3606 | 119 | os.chmod(self.playground, 0000) | 121 | os.chmod(self.playground, 0000) |
3608 | 120 | self.addCleanup(os.chmod, self.playground, 0700) | 122 | self.addCleanup(os.chmod, self.playground, 0o700) |
3609 | 121 | with self.assertRaises(OSError): | 123 | with self.assertRaises(OSError): |
3610 | 122 | mkdir(os.path.join(path)) | 124 | mkdir(os.path.join(path)) |
3611 | 123 | self.assertFalse(os.path.exists(path)) | 125 | self.assertFalse(os.path.exists(path)) |
3612 | @@ -143,14 +145,14 @@ | |||
3613 | 143 | self.assertFalse(_is_qualified_charm_url(url)) | 145 | self.assertFalse(_is_qualified_charm_url(url)) |
3614 | 144 | 146 | ||
3615 | 145 | def test_get_qualified_url(self): | 147 | def test_get_qualified_url(self): |
3617 | 146 | fake_json = """ | 148 | fake_json = b""" |
3618 | 147 | {"cs:precise/mysql": | 149 | {"cs:precise/mysql": |
3619 | 148 | {"revision":333} | 150 | {"revision":333} |
3620 | 149 | } | 151 | } |
3621 | 150 | """ | 152 | """ |
3622 | 151 | 153 | ||
3623 | 152 | def mocked_urlopen(url): | 154 | def mocked_urlopen(url): |
3625 | 153 | return StringIO(fake_json) | 155 | return BytesIO(fake_json) |
3626 | 154 | 156 | ||
3627 | 155 | path = 'deployer.utils.urlopen' | 157 | path = 'deployer.utils.urlopen' |
3628 | 156 | with patch(path, mocked_urlopen): | 158 | with patch(path, mocked_urlopen): |
3629 | @@ -164,10 +166,11 @@ | |||
3630 | 164 | with patch('deployer.utils.urlopen', mocked_urlopen): | 166 | with patch('deployer.utils.urlopen', mocked_urlopen): |
3631 | 165 | with self.assertRaises(DeploymentError) as exc: | 167 | with self.assertRaises(DeploymentError) as exc: |
3632 | 166 | get_qualified_charm_url('cs:precise/mysql') | 168 | get_qualified_charm_url('cs:precise/mysql') |
3637 | 167 | expected = ('HTTP Error 404: ' | 169 | expected = ( |
3638 | 168 | 'Bad Earl (https://api.jujucharms.com/charmstore/charm-info' | 170 | 'HTTP Error 404: ' |
3639 | 169 | '?charms=cs:precise/mysql)') | 171 | 'Bad Earl (https://api.jujucharms.com/charmstore/charm-info' |
3640 | 170 | self.assertEqual([expected], exc.exception.message) | 172 | '?charms=cs:precise/mysql)') |
3641 | 173 | self.assertEqual([expected], exc.exception.args[0]) | ||
3642 | 171 | 174 | ||
3643 | 172 | def test_get_qualified_url_raise_exception_on_URLError(self): | 175 | def test_get_qualified_url_raise_exception_on_URLError(self): |
3644 | 173 | def mocked_urlopen(url): | 176 | def mocked_urlopen(url): |
3645 | @@ -179,4 +182,4 @@ | |||
3646 | 179 | expected = ('<urlopen error Hinky URL> ' | 182 | expected = ('<urlopen error Hinky URL> ' |
3647 | 180 | '(https://api.jujucharms.com/charmstore/charm-info' | 183 | '(https://api.jujucharms.com/charmstore/charm-info' |
3648 | 181 | '?charms=cs:precise/mysql)') | 184 | '?charms=cs:precise/mysql)') |
3650 | 182 | self.assertEqual([expected], exc.exception.message) | 185 | self.assertEqual([expected], exc.exception.args[0]) |
3651 | 183 | 186 | ||
3652 | === modified file 'deployer/tests/test_watchers.py' | |||
3653 | --- deployer/tests/test_watchers.py 2014-03-06 21:17:14 +0000 | |||
3654 | +++ deployer/tests/test_watchers.py 2016-05-08 03:36:27 +0000 | |||
3655 | @@ -1,5 +1,7 @@ | |||
3656 | 1 | """Tests juju-core environment watchers.""" | 1 | """Tests juju-core environment watchers.""" |
3657 | 2 | 2 | ||
3658 | 3 | from __future__ import absolute_import | ||
3659 | 4 | import operator | ||
3660 | 3 | import unittest | 5 | import unittest |
3661 | 4 | 6 | ||
3662 | 5 | import mock | 7 | import mock |
3663 | @@ -80,14 +82,21 @@ | |||
3664 | 80 | # The errors handler has been called once for each changeset containing | 82 | # The errors handler has been called once for each changeset containing |
3665 | 81 | # errors. | 83 | # errors. |
3666 | 82 | self.assertEqual(2, on_errors.call_count) | 84 | self.assertEqual(2, on_errors.call_count) |
3669 | 83 | on_errors.assert_has_calls([ | 85 | # Because of the implementation, we can't guarantee the order of the |
3670 | 84 | mock.call([ | 86 | # list in the first call to on_errors here: |
3671 | 87 | sort_key = operator.itemgetter('Name') | ||
3672 | 88 | self.assertEqual( | ||
3673 | 89 | sorted([ | ||
3674 | 85 | {'Status': 'error', 'Name': 'django/42', 'Service': 'django'}, | 90 | {'Status': 'error', 'Name': 'django/42', 'Service': 'django'}, |
3675 | 86 | {'Status': 'error', 'Name': 'haproxy/1', 'Service': 'haproxy'} | 91 | {'Status': 'error', 'Name': 'haproxy/1', 'Service': 'haproxy'} |
3680 | 87 | ]), | 92 | ], key=sort_key), |
3681 | 88 | mock.call([ | 93 | # [0][0][0] = first call, positional args, first positional arg |
3682 | 89 | {'Status': 'error', 'Name': 'django/0', 'Service': 'django'}]), | 94 | sorted(on_errors.call_args_list[0][0][0], key=sort_key) |
3683 | 90 | ]) | 95 | ) |
3684 | 96 | self.assertEqual( | ||
3685 | 97 | [{'Status': 'error', 'Name': 'django/0', 'Service': 'django'}], | ||
3686 | 98 | on_errors.call_args_list[1][0][0] | ||
3687 | 99 | ) | ||
3688 | 91 | 100 | ||
3689 | 92 | def test_specific_services(self): | 101 | def test_specific_services(self): |
3690 | 93 | # It is possible to only watch units belonging to specific services. | 102 | # It is possible to only watch units belonging to specific services. |
3691 | @@ -164,4 +173,4 @@ | |||
3692 | 164 | callback = watchers.raise_on_errors(ValueError) | 173 | callback = watchers.raise_on_errors(ValueError) |
3693 | 165 | with self.assertRaises(ValueError) as cm: | 174 | with self.assertRaises(ValueError) as cm: |
3694 | 166 | callback('bad wolf') | 175 | callback('bad wolf') |
3696 | 167 | self.assertEqual('bad wolf', bytes(cm.exception)) | 176 | self.assertEqual('bad wolf', cm.exception.args[0]) |
3697 | 168 | 177 | ||
3698 | === modified file 'deployer/utils.py' | |||
3699 | --- deployer/utils.py 2016-05-03 17:28:13 +0000 | |||
3700 | +++ deployer/utils.py 2016-05-08 03:36:27 +0000 | |||
3701 | @@ -1,3 +1,4 @@ | |||
3702 | 1 | from __future__ import absolute_import | ||
3703 | 1 | from copy import deepcopy | 2 | from copy import deepcopy |
3704 | 2 | from contextlib import contextmanager | 3 | from contextlib import contextmanager |
3705 | 3 | 4 | ||
3706 | @@ -20,12 +21,15 @@ | |||
3707 | 20 | import subprocess | 21 | import subprocess |
3708 | 21 | import time | 22 | import time |
3709 | 22 | import tempfile | 23 | import tempfile |
3711 | 23 | from urllib2 import ( | 24 | from six.moves.urllib.error import ( |
3712 | 24 | HTTPError, | 25 | HTTPError, |
3713 | 25 | URLError, | 26 | URLError, |
3714 | 27 | ) | ||
3715 | 28 | from six.moves.urllib.request import ( | ||
3716 | 26 | urlopen, | 29 | urlopen, |
3717 | 27 | ) | 30 | ) |
3718 | 28 | import zipfile | 31 | import zipfile |
3719 | 32 | import six | ||
3720 | 29 | 33 | ||
3721 | 30 | try: | 34 | try: |
3722 | 31 | from yaml import CSafeLoader, CSafeDumper | 35 | from yaml import CSafeLoader, CSafeDumper |
3723 | @@ -71,7 +75,7 @@ | |||
3724 | 71 | node = yaml.ScalarNode(tag=u'tag:yaml.org,2002:str', value=uni) | 75 | node = yaml.ScalarNode(tag=u'tag:yaml.org,2002:str', value=uni) |
3725 | 72 | return node | 76 | return node |
3726 | 73 | 77 | ||
3728 | 74 | yaml.add_representer(unicode, _unicode_representer) | 78 | yaml.add_representer(six.text_type, _unicode_representer) |
3729 | 75 | 79 | ||
3730 | 76 | 80 | ||
3731 | 77 | DEFAULT_LOGGING = """ | 81 | DEFAULT_LOGGING = """ |
3732 | @@ -250,12 +254,14 @@ | |||
3733 | 250 | 254 | ||
3734 | 251 | 255 | ||
3735 | 252 | _juju_major_version = None | 256 | _juju_major_version = None |
3736 | 257 | |||
3737 | 258 | |||
3738 | 253 | def get_juju_major_version(): | 259 | def get_juju_major_version(): |
3739 | 254 | global _juju_major_version | 260 | global _juju_major_version |
3740 | 255 | if _juju_major_version is None: | 261 | if _juju_major_version is None: |
3741 | 256 | log = logging.getLogger("deployer.utils") | 262 | log = logging.getLogger("deployer.utils") |
3742 | 257 | _juju_major_version = int(_check_call( | 263 | _juju_major_version = int(_check_call( |
3744 | 258 | ["juju", "--version"], log).split('.')[0]) | 264 | ["juju", "--version"], log).split(b'.')[0]) |
3745 | 259 | return _juju_major_version | 265 | return _juju_major_version |
3746 | 260 | 266 | ||
3747 | 261 | 267 | ||
3748 | @@ -370,7 +376,7 @@ | |||
3749 | 370 | except (HTTPError, URLError) as e: | 376 | except (HTTPError, URLError) as e: |
3750 | 371 | errmsg = '{} ({})'.format(e, info_url) | 377 | errmsg = '{} ({})'.format(e, info_url) |
3751 | 372 | raise DeploymentError([errmsg]) | 378 | raise DeploymentError([errmsg]) |
3753 | 373 | content = json.loads(fh.read()) | 379 | content = json.loads(fh.read().decode()) |
3754 | 374 | rev = content[url]['revision'] | 380 | rev = content[url]['revision'] |
3755 | 375 | return "%s-%d" % (url, rev) | 381 | return "%s-%d" % (url, rev) |
3756 | 376 | 382 | ||
3757 | @@ -399,7 +405,7 @@ | |||
3758 | 399 | else: | 405 | else: |
3759 | 400 | # Return the juju2 controller:model combo | 406 | # Return the juju2 controller:model combo |
3760 | 401 | log = logging.getLogger("deployer.utils") | 407 | log = logging.getLogger("deployer.utils") |
3762 | 402 | return _check_call(["juju", "switch"], log).strip() | 408 | return _check_call(["juju", "switch"], log).strip().decode() |
3763 | 403 | 409 | ||
3764 | 404 | 410 | ||
3765 | 405 | def x_in_y(x, y): | 411 | def x_in_y(x, y): |
3766 | 406 | 412 | ||
3767 | === modified file 'deployer/vcs.py' | |||
3768 | --- deployer/vcs.py 2016-03-03 14:18:30 +0000 | |||
3769 | +++ deployer/vcs.py 2016-05-08 03:36:27 +0000 | |||
3770 | @@ -1,9 +1,8 @@ | |||
3771 | 1 | from __future__ import absolute_import | ||
3772 | 1 | import subprocess | 2 | import subprocess |
3773 | 2 | import os | 3 | import os |
3774 | 3 | import re | 4 | import re |
3775 | 4 | 5 | ||
3776 | 5 | from bzrlib.workingtree import WorkingTree | ||
3777 | 6 | |||
3778 | 7 | from .utils import ErrorExit | 6 | from .utils import ErrorExit |
3779 | 8 | 7 | ||
3780 | 9 | 8 | ||
3781 | @@ -34,10 +33,10 @@ | |||
3782 | 34 | stderr = subprocess.STDOUT | 33 | stderr = subprocess.STDOUT |
3783 | 35 | output = subprocess.check_output( | 34 | output = subprocess.check_output( |
3784 | 36 | args, cwd=cwd or self.path, stderr=stderr) | 35 | args, cwd=cwd or self.path, stderr=stderr) |
3786 | 37 | except subprocess.CalledProcessError, e: | 36 | except subprocess.CalledProcessError as e: |
3787 | 38 | self.log.error(error_msg % self.get_err_msg_ctx(e)) | 37 | self.log.error(error_msg % self.get_err_msg_ctx(e)) |
3788 | 39 | raise ErrorExit() | 38 | raise ErrorExit() |
3790 | 40 | return output.strip() | 39 | return output.strip().decode() |
3791 | 41 | 40 | ||
3792 | 42 | def get_err_msg_ctx(self, e): | 41 | def get_err_msg_ctx(self, e): |
3793 | 43 | return { | 42 | return { |
3794 | @@ -99,10 +98,9 @@ | |||
3795 | 99 | self._call(params, self.err_branch, cwd) | 98 | self._call(params, self.err_branch, cwd) |
3796 | 100 | 99 | ||
3797 | 101 | def is_modified(self): | 100 | def is_modified(self): |
3802 | 102 | # To replace with bzr cli, we need to be able to detect | 101 | return subprocess.call( |
3803 | 103 | # changes to a wc @ a rev or @ trunk. | 102 | ["bzr", "diff"], |
3804 | 104 | tree = WorkingTree.open(self.path) | 103 | cwd=self.path, stdout=subprocess.PIPE) != 0 |
3801 | 105 | return tree.has_changes() | ||
3805 | 106 | 104 | ||
3806 | 107 | 105 | ||
3807 | 108 | class Git(Vcs): | 106 | class Git(Vcs): |
3808 | 109 | 107 | ||
3809 | === modified file 'doc/conf.py' | |||
3810 | --- doc/conf.py 2013-05-16 03:05:55 +0000 | |||
3811 | +++ doc/conf.py 2016-05-08 03:36:27 +0000 | |||
3812 | @@ -11,6 +11,7 @@ | |||
3813 | 11 | # All configuration values have a default; values that are commented out | 11 | # All configuration values have a default; values that are commented out |
3814 | 12 | # serve to show the default. | 12 | # serve to show the default. |
3815 | 13 | 13 | ||
3816 | 14 | from __future__ import absolute_import | ||
3817 | 14 | import sys, os | 15 | import sys, os |
3818 | 15 | 16 | ||
3819 | 16 | # If extensions (or modules to document with autodoc) are in another directory, | 17 | # If extensions (or modules to document with autodoc) are in another directory, |
3820 | 17 | 18 | ||
3821 | === modified file 'setup.py' | |||
3822 | --- setup.py 2016-05-05 17:42:42 +0000 | |||
3823 | +++ setup.py 2016-05-08 03:36:27 +0000 | |||
3824 | @@ -1,3 +1,4 @@ | |||
3825 | 1 | from __future__ import absolute_import | ||
3826 | 1 | from setuptools import setup, find_packages | 2 | from setuptools import setup, find_packages |
3827 | 2 | 3 | ||
3828 | 3 | 4 | ||
3829 | @@ -9,11 +10,15 @@ | |||
3830 | 9 | author="Kapil Thangavelu", | 10 | author="Kapil Thangavelu", |
3831 | 10 | author_email="kapil.foss@gmail.com", | 11 | author_email="kapil.foss@gmail.com", |
3832 | 11 | url="http://launchpad.net/juju-deployer", | 12 | url="http://launchpad.net/juju-deployer", |
3834 | 12 | install_requires=["jujuclient>=0.18", "PyYAML>=3.10", "bzr"], | 13 | install_requires=["jujuclient>=0.18", "PyYAML>=3.10", "six"], |
3835 | 13 | packages=find_packages(), | 14 | packages=find_packages(), |
3836 | 14 | classifiers=[ | 15 | classifiers=[ |
3837 | 15 | "Development Status :: 4 - Beta", | 16 | "Development Status :: 4 - Beta", |
3838 | 16 | "Programming Language :: Python", | 17 | "Programming Language :: Python", |
3839 | 18 | "Programming Language :: Python :: 2", | ||
3840 | 19 | "Programming Language :: Python :: 2.7", | ||
3841 | 20 | "Programming Language :: Python :: 3", | ||
3842 | 21 | "Programming Language :: Python :: 3.5", | ||
3843 | 17 | "Topic :: Internet", | 22 | "Topic :: Internet", |
3844 | 18 | "Topic :: Software Development :: Libraries :: Python Modules", | 23 | "Topic :: Software Development :: Libraries :: Python Modules", |
3845 | 19 | "Intended Audience :: System Administrators", | 24 | "Intended Audience :: System Administrators", |
3846 | 20 | 25 | ||
3847 | === added file 'test-requirements.txt' | |||
3848 | --- test-requirements.txt 1970-01-01 00:00:00 +0000 | |||
3849 | +++ test-requirements.txt 2016-05-08 03:36:27 +0000 | |||
3850 | @@ -0,0 +1,4 @@ | |||
3851 | 1 | coverage | ||
3852 | 2 | flake8 | ||
3853 | 3 | mock | ||
3854 | 4 | nose | ||
3855 | 0 | 5 | ||
3856 | === added file 'tox.ini' | |||
3857 | --- tox.ini 1970-01-01 00:00:00 +0000 | |||
3858 | +++ tox.ini 2016-05-08 03:36:27 +0000 | |||
3859 | @@ -0,0 +1,20 @@ | |||
3860 | 1 | [tox] | ||
3861 | 2 | minversion = 1.8 | ||
3862 | 3 | envlist = py27,py35,pep8 | ||
3863 | 4 | |||
3864 | 5 | [testenv] | ||
3865 | 6 | usedevelop=True | ||
3866 | 7 | |||
3867 | 8 | # need SSH_AUTH_SOCK for bzr calls to work | ||
3868 | 9 | passenv = SSH_AUTH_SOCK | ||
3869 | 10 | |||
3870 | 11 | deps = -r{toxinidir}/test-requirements.txt | ||
3871 | 12 | setenv = | ||
3872 | 13 | JUJU_TEST_ENV = {env:JUJU_TEST_ENV:"test"} | ||
3873 | 14 | JUJU_DATA = {homedir}/.local/share/juju | ||
3874 | 15 | HOME = {env:HOME} | ||
3875 | 16 | commands= | ||
3876 | 17 | nosetests --with-coverage --cover-package=deployer deployer/tests | ||
3877 | 18 | |||
3878 | 19 | [testenv:pep8] | ||
3879 | 20 | commands = flake8 deployer |