Merge lp:~ericsnowcurrently/fake-juju/python-lib-classes into lp:~landscape/fake-juju/trunk-old

Proposed by Eric Snow
Status: Superseded
Proposed branch: lp:~ericsnowcurrently/fake-juju/python-lib-classes
Merge into: lp:~landscape/fake-juju/trunk-old
Diff against target: 963 lines (+915/-0)
9 files modified
python/LICENSE (+191/-0)
python/Makefile (+9/-0)
python/README.md (+1/-0)
python/fakejuju/__init__.py (+57/-0)
python/fakejuju/failures.py (+65/-0)
python/fakejuju/fakejuju.py (+145/-0)
python/fakejuju/tests/test_failures.py (+98/-0)
python/fakejuju/tests/test_fakejuju.py (+280/-0)
python/setup.py (+69/-0)
To merge this branch: bzr merge lp:~ericsnowcurrently/fake-juju/python-lib-classes
Reviewer Review Type Date Requested Status
Landscape Pending
Landscape Pending
Review via email: mp+307894@code.launchpad.net

This proposal has been superseded by a proposal from 2016-10-06.

Description of the change

Add the FakeJuju and Failures classes.

Testing instructions:

Run the unit tests.

To post a comment you must log in.
49. By Eric Snow

Merge from parent.

50. By Eric Snow

Failures() doesn' do any coercion.

51. By Eric Snow

FakeJuju() doesn't do any coercion.

52. By Eric Snow

Do not use namedtuple for FakeJuju.

53. By Eric Snow

Fix a docstring.

Unmerged revisions

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added directory 'python'
2=== added file 'python/LICENSE'
3--- python/LICENSE 1970-01-01 00:00:00 +0000
4+++ python/LICENSE 2016-10-06 22:49:20 +0000
5@@ -0,0 +1,191 @@
6+All files in this repository are licensed as follows. If you contribute
7+to this repository, it is assumed that you license your contribution
8+under the same license unless you state otherwise.
9+
10+All files Copyright (C) 2012-2016 Canonical Ltd. unless otherwise specified in the file.
11+
12+This software is licensed under the LGPLv3, included below.
13+
14+As a special exception to the GNU Lesser General Public License version 3
15+("LGPL3"), the copyright holders of this Library give you permission to
16+convey to a third party a Combined Work that links statically or dynamically
17+to this Library without providing any Minimal Corresponding Source or
18+Minimal Application Code as set out in 4d or providing the installation
19+information set out in section 4e, provided that you comply with the other
20+provisions of LGPL3 and provided that you meet, for the Application the
21+terms and conditions of the license(s) which apply to the Application.
22+
23+Except as stated in this special exception, the provisions of LGPL3 will
24+continue to comply in full to this Library. If you modify this Library, you
25+may apply this exception to your version of this Library, but you are not
26+obliged to do so. If you do not wish to do so, delete this exception
27+statement from your version. This exception does not (and cannot) modify any
28+license terms which apply to the Application, with which you must still
29+comply.
30+
31+
32+ GNU LESSER GENERAL PUBLIC LICENSE
33+ Version 3, 29 June 2007
34+
35+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
36+ Everyone is permitted to copy and distribute verbatim copies
37+ of this license document, but changing it is not allowed.
38+
39+
40+ This version of the GNU Lesser General Public License incorporates
41+the terms and conditions of version 3 of the GNU General Public
42+License, supplemented by the additional permissions listed below.
43+
44+ 0. Additional Definitions.
45+
46+ As used herein, "this License" refers to version 3 of the GNU Lesser
47+General Public License, and the "GNU GPL" refers to version 3 of the GNU
48+General Public License.
49+
50+ "The Library" refers to a covered work governed by this License,
51+other than an Application or a Combined Work as defined below.
52+
53+ An "Application" is any work that makes use of an interface provided
54+by the Library, but which is not otherwise based on the Library.
55+Defining a subclass of a class defined by the Library is deemed a mode
56+of using an interface provided by the Library.
57+
58+ A "Combined Work" is a work produced by combining or linking an
59+Application with the Library. The particular version of the Library
60+with which the Combined Work was made is also called the "Linked
61+Version".
62+
63+ The "Minimal Corresponding Source" for a Combined Work means the
64+Corresponding Source for the Combined Work, excluding any source code
65+for portions of the Combined Work that, considered in isolation, are
66+based on the Application, and not on the Linked Version.
67+
68+ The "Corresponding Application Code" for a Combined Work means the
69+object code and/or source code for the Application, including any data
70+and utility programs needed for reproducing the Combined Work from the
71+Application, but excluding the System Libraries of the Combined Work.
72+
73+ 1. Exception to Section 3 of the GNU GPL.
74+
75+ You may convey a covered work under sections 3 and 4 of this License
76+without being bound by section 3 of the GNU GPL.
77+
78+ 2. Conveying Modified Versions.
79+
80+ If you modify a copy of the Library, and, in your modifications, a
81+facility refers to a function or data to be supplied by an Application
82+that uses the facility (other than as an argument passed when the
83+facility is invoked), then you may convey a copy of the modified
84+version:
85+
86+ a) under this License, provided that you make a good faith effort to
87+ ensure that, in the event an Application does not supply the
88+ function or data, the facility still operates, and performs
89+ whatever part of its purpose remains meaningful, or
90+
91+ b) under the GNU GPL, with none of the additional permissions of
92+ this License applicable to that copy.
93+
94+ 3. Object Code Incorporating Material from Library Header Files.
95+
96+ The object code form of an Application may incorporate material from
97+a header file that is part of the Library. You may convey such object
98+code under terms of your choice, provided that, if the incorporated
99+material is not limited to numerical parameters, data structure
100+layouts and accessors, or small macros, inline functions and templates
101+(ten or fewer lines in length), you do both of the following:
102+
103+ a) Give prominent notice with each copy of the object code that the
104+ Library is used in it and that the Library and its use are
105+ covered by this License.
106+
107+ b) Accompany the object code with a copy of the GNU GPL and this license
108+ document.
109+
110+ 4. Combined Works.
111+
112+ You may convey a Combined Work under terms of your choice that,
113+taken together, effectively do not restrict modification of the
114+portions of the Library contained in the Combined Work and reverse
115+engineering for debugging such modifications, if you also do each of
116+the following:
117+
118+ a) Give prominent notice with each copy of the Combined Work that
119+ the Library is used in it and that the Library and its use are
120+ covered by this License.
121+
122+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
123+ document.
124+
125+ c) For a Combined Work that displays copyright notices during
126+ execution, include the copyright notice for the Library among
127+ these notices, as well as a reference directing the user to the
128+ copies of the GNU GPL and this license document.
129+
130+ d) Do one of the following:
131+
132+ 0) Convey the Minimal Corresponding Source under the terms of this
133+ License, and the Corresponding Application Code in a form
134+ suitable for, and under terms that permit, the user to
135+ recombine or relink the Application with a modified version of
136+ the Linked Version to produce a modified Combined Work, in the
137+ manner specified by section 6 of the GNU GPL for conveying
138+ Corresponding Source.
139+
140+ 1) Use a suitable shared library mechanism for linking with the
141+ Library. A suitable mechanism is one that (a) uses at run time
142+ a copy of the Library already present on the user's computer
143+ system, and (b) will operate properly with a modified version
144+ of the Library that is interface-compatible with the Linked
145+ Version.
146+
147+ e) Provide Installation Information, but only if you would otherwise
148+ be required to provide such information under section 6 of the
149+ GNU GPL, and only to the extent that such information is
150+ necessary to install and execute a modified version of the
151+ Combined Work produced by recombining or relinking the
152+ Application with a modified version of the Linked Version. (If
153+ you use option 4d0, the Installation Information must accompany
154+ the Minimal Corresponding Source and Corresponding Application
155+ Code. If you use option 4d1, you must provide the Installation
156+ Information in the manner specified by section 6 of the GNU GPL
157+ for conveying Corresponding Source.)
158+
159+ 5. Combined Libraries.
160+
161+ You may place library facilities that are a work based on the
162+Library side by side in a single library together with other library
163+facilities that are not Applications and are not covered by this
164+License, and convey such a combined library under terms of your
165+choice, if you do both of the following:
166+
167+ a) Accompany the combined library with a copy of the same work based
168+ on the Library, uncombined with any other library facilities,
169+ conveyed under the terms of this License.
170+
171+ b) Give prominent notice with the combined library that part of it
172+ is a work based on the Library, and explaining where to find the
173+ accompanying uncombined form of the same work.
174+
175+ 6. Revised Versions of the GNU Lesser General Public License.
176+
177+ The Free Software Foundation may publish revised and/or new versions
178+of the GNU Lesser General Public License from time to time. Such new
179+versions will be similar in spirit to the present version, but may
180+differ in detail to address new problems or concerns.
181+
182+ Each version is given a distinguishing version number. If the
183+Library as you received it specifies that a certain numbered version
184+of the GNU Lesser General Public License "or any later version"
185+applies to it, you have the option of following the terms and
186+conditions either of that published version or of any later version
187+published by the Free Software Foundation. If the Library as you
188+received it does not specify a version number of the GNU Lesser
189+General Public License, you may choose any version of the GNU Lesser
190+General Public License ever published by the Free Software Foundation.
191+
192+ If the Library as you received it specifies that a proxy can decide
193+whether future versions of the GNU Lesser General Public License shall
194+apply, that proxy's public statement of acceptance of any version is
195+permanent authorization for you to choose that version for the
196+Library.
197
198=== added file 'python/Makefile'
199--- python/Makefile 1970-01-01 00:00:00 +0000
200+++ python/Makefile 2016-10-06 22:49:20 +0000
201@@ -0,0 +1,9 @@
202+PYTHON = python
203+
204+.PHONY: test
205+test:
206+ $(PYTHON) -m unittest discover -t $(shell pwd) -s $(shell pwd)/fakejuju
207+
208+.PHONY: install-dev
209+install-dev:
210+ ln -s $(shell pwd)/fakejuju /usr/local/lib/python2.7/dist-packages/fakejuju
211
212=== added file 'python/README.md'
213--- python/README.md 1970-01-01 00:00:00 +0000
214+++ python/README.md 2016-10-06 22:49:20 +0000
215@@ -0,0 +1,1 @@
216+# fakejuju
217
218=== added directory 'python/fakejuju'
219=== added file 'python/fakejuju/__init__.py'
220--- python/fakejuju/__init__.py 1970-01-01 00:00:00 +0000
221+++ python/fakejuju/__init__.py 2016-10-06 22:49:20 +0000
222@@ -0,0 +1,57 @@
223+# Copyright 2016 Canonical Limited. All rights reserved.
224+
225+"""Support for interaction with fake-juju.
226+
227+"fake-juju" is a combination of the juju and jujud commands that is
228+suitable for use in integration tests. It exposes a limited subset
229+of the standard juju subcommands (see FakeJuju in this module for
230+specifics). When called without any arguments it runs jujud (using
231+the dummy provider) with extra logging and testing hooks available to
232+control failures. See https://launchpad.net/fake-juju for the project.
233+
234+The binary is named with the Juju version for which it was built.
235+For example, for version 1.25.6 the file is named "fake-juju-1.25.6".
236+
237+fake-juju uses the normal Juju local config directory. This defaults
238+to ~/.local/shared/juju and may be set using the JUJU_DATA environment
239+variable (in 2.x, for 1.x it is JUJU_HOME).
240+
241+In addition to all the normal Juju environment variables (e.g.
242+JUJU_DATA), fake-juju uses the following:
243+
244+ FAKE_JUJU_FAILURES - the path to the failures file
245+ The Failures class below sets this to $JUJU_DATA/juju-failures.
246+ FAKE_JUJU_LOGS_DIR - the path to the logs directory
247+ This defaults to $JUJU_DATA.
248+
249+fake-juju also creates several extra files:
250+
251+ $FAKE_JUJU_LOGS_DIR/fake-juju.log - where fake-juju logs are written
252+ $JUJU_DATA/fakejuju - fake-juju's data cache
253+ $JUJU_DATA/fifo - a FIFO file that triggers jujud shutdown
254+ $JUJU_DATA/cert.ca - the API's CA certificate
255+
256+Normal Juju logging for is written to $JUJU_DATA/fake-juju.log.
257+
258+Failures may be injected into a running fake-juju (or set before
259+running). They may be injected by adding them to the file identified
260+by $FAKE_JUJU_FAILURES. The format is a single failure definition per
261+line. The syntax of the failure definition depends on the failure.
262+The currently supported failures (with their definition syntax) are
263+listed here:
264+
265+ * when adding a unit with a specific ID
266+ format: "unit-<ID>" (e.g. unit-mysql/0)
267+
268+"""
269+
270+from .fakejuju import get_bootstrap_spec, get_filename, set_envvars, FakeJuju
271+
272+
273+__all__ = [
274+ "__version__",
275+ "get_bootstrap_spec", "get_filename", "set_envvars",
276+ "FakeJuju",
277+ ]
278+
279+__version__ = "0.9.0b1"
280
281=== added file 'python/fakejuju/failures.py'
282--- python/fakejuju/failures.py 1970-01-01 00:00:00 +0000
283+++ python/fakejuju/failures.py 2016-10-06 22:49:20 +0000
284@@ -0,0 +1,65 @@
285+# Copyright 2016 Canonical Limited. All rights reserved.
286+
287+import errno
288+import os
289+import os.path
290+
291+
292+class Failures(object):
293+ """The collection of injected failures to use with a fake-juju.
294+
295+ The failures are tracked here as well as injected into any
296+ fake-juju using the initial config dir (aka "juju home").
297+
298+ Note that fake-juju provides only limited capability for
299+ failure injection.
300+ """
301+
302+ def __init__(self, cfgdir, entities=None):
303+ """
304+ @param cfgdir: The "juju home" directory into which the
305+ failures will be registered for injection.
306+ @param entities: The entity names to start with, if any.
307+ """
308+ filename = os.path.join(cfgdir, "juju-failures")
309+ entities = set(unicode(tag) for tag in entities or ())
310+
311+ self._filename = unicode(filename)
312+ self._entities = entities
313+
314+ @property
315+ def filename(self):
316+ """The path to the failures file the fake-juju reads."""
317+ return self._filename
318+
319+ @property
320+ def entities(self):
321+ """The IDs of the failing entities."""
322+ return set(self._entities)
323+
324+ def _flush(self):
325+ """Write the failures to disk."""
326+ data = "\n".join(self._entities) + "\n"
327+ try:
328+ file = open(self._filename, "w")
329+ except IOError:
330+ dirname = os.path.dirname(self._filename)
331+ if not os.path.exists(dirname):
332+ os.makedirs(dirname)
333+ file = open(self._filename, "w")
334+ with file:
335+ file.write(data)
336+
337+ def fail_entity(self, tag):
338+ """Inject a global failure for the identified Juju entity."""
339+ self._entities.add(tag)
340+ self._flush()
341+
342+ def clear(self):
343+ """Remove all injected failures."""
344+ try:
345+ os.remove(self._filename)
346+ except OSError as e:
347+ if e.errno != errno.ENOENT:
348+ raise
349+ self._entities.clear()
350
351=== added file 'python/fakejuju/fakejuju.py'
352--- python/fakejuju/fakejuju.py 1970-01-01 00:00:00 +0000
353+++ python/fakejuju/fakejuju.py 2016-10-06 22:49:20 +0000
354@@ -0,0 +1,145 @@
355+# Copyright 2016 Canonical Limited. All rights reserved.
356+
357+from collections import namedtuple
358+import os.path
359+
360+import txjuju.cli
361+
362+from .failures import Failures
363+
364+
365+def get_bootstrap_spec(name, admin_secret=None):
366+ """Return the BootstrapSpec instance for the given controller.
367+
368+ @param name: The controller name to set up.
369+ @param admin_secret: The admin user password to use.
370+ """
371+ type = "dummy"
372+ default_series = None # Use the default.
373+ return txjuju.cli.BootstrapSpec(name, type, default_series, admin_secret)
374+
375+
376+def get_filename(version, bindir=None):
377+ """Return the full path to the fake-juju binary for the given version.
378+
379+ @param version: The Juju version to use.
380+ @param bindir: The directory containing the fake-juju binary.
381+ This defaults to /usr/bin.
382+ """
383+ if not version:
384+ raise ValueError("version not provided")
385+ filename = "fake-juju-{}".format(version)
386+ if bindir is None:
387+ # XXX Search $PATH.
388+ bindir = "/usr/bin"
389+ return os.path.join(bindir, filename)
390+
391+
392+def set_envvars(envvars, failures_filename=None, logsdir=None):
393+ """Return the environment variables with which to run fake-juju.
394+
395+ @param envvars: The env dict to update.
396+ @param failures_filename: The path to the failures file that
397+ fake-juju will use.
398+ @params logsdir: The path to the directory where fake-juju will
399+ write its log files.
400+ """
401+ envvars["FAKE_JUJU_FAILURES"] = failures_filename or ""
402+ envvars["FAKE_JUJU_LOGS_DIR"] = logsdir or ""
403+
404+
405+class FakeJuju(
406+ namedtuple("FakeJuju", "filename version cfgdir logsdir failures")):
407+ """The fundamental details for fake-juju."""
408+
409+ @classmethod
410+ def from_version(cls, version, cfgdir,
411+ logsdir=None, failuresdir=None, bindir=None):
412+ """Return a new instance given the provided information.
413+
414+ @param version: The Juju version to fake.
415+ @param cfgdir: The "juju home" directory to use.
416+ @param logsdir: The directory where logs will be written.
417+ This defaults to cfgdir.
418+ @params failuresdir: The directory where failure injection
419+ is managed.
420+ @param bindir: The directory containing the fake-juju binary.
421+ This defaults to /usr/bin.
422+ """
423+ if logsdir is None:
424+ logsdir = cfgdir
425+ if failuresdir is None:
426+ failuresdir = cfgdir
427+ filename = get_filename(version, bindir=bindir)
428+ failures = Failures(failuresdir)
429+ return cls(filename, version, cfgdir, logsdir, failures)
430+
431+ def __new__(cls, filename, version, cfgdir, logsdir=None, failures=None):
432+ """
433+ @param filename: The path to the fake-juju binary.
434+ @param version: The Juju version to fake.
435+ @param cfgdir: The "juju home" directory to use.
436+ @param logsdir: The directory where logs will be written.
437+ This defaults to cfgdir.
438+ @param failures: The set of fake-juju failures to use.
439+ """
440+ filename = unicode(filename) if filename else None
441+ version = unicode(version) if version else None
442+ cfgdir = unicode(cfgdir) if cfgdir else None
443+ logsdir = unicode(logsdir) if logsdir is not None else cfgdir
444+ if failures is None and cfgdir:
445+ failures = Failures(cfgdir)
446+ return super(FakeJuju, cls).__new__(
447+ cls, filename, version, cfgdir, logsdir, failures)
448+
449+ def __init__(self, *args, **kwargs):
450+ if not self.filename:
451+ raise ValueError("missing filename")
452+ if not self.version:
453+ raise ValueError("missing version")
454+ if not self.cfgdir:
455+ raise ValueError("missing cfgdir")
456+ if not self.logsdir:
457+ raise ValueError("missing logsdir")
458+ if self.failures is None:
459+ raise ValueError("missing failures")
460+
461+ @property
462+ def logfile(self):
463+ """The path to fake-juju's log file."""
464+ return os.path.join(self.logsdir, "fake-juju.log")
465+
466+ @property
467+ def infofile(self):
468+ """The path to fake-juju's data cache."""
469+ return os.path.join(self.cfgdir, "fakejuju")
470+
471+ @property
472+ def fifo(self):
473+ """The path to the fifo file that triggers shutdown."""
474+ return os.path.join(self.cfgdir, "fifo")
475+
476+ @property
477+ def cacertfile(self):
478+ """The path to the API server's certificate."""
479+ return os.path.join(self.cfgdir, "cert.ca")
480+
481+ def cli(self, envvars=None):
482+ """
483+
484+ Currently only the following juju subcommands are supported:
485+
486+ * bootstrap
487+ Not that this only supports the dummy provider and the local
488+ system is only minimally impacted.
489+ * api-info
490+ Note that passwords are always omited, even if requested.
491+ * api-endpoints
492+ * destroy-environment
493+ """
494+ if envvars is None:
495+ envvars = os.environ
496+ envvars = dict(envvars)
497+ set_envvars(envvars, self.failures._filename, self.logsdir)
498+ return txjuju.cli.CLI.from_version(
499+ self.filename, self.version, self.cfgdir, envvars)
500
501=== added directory 'python/fakejuju/tests'
502=== added file 'python/fakejuju/tests/__init__.py'
503=== added file 'python/fakejuju/tests/test_failures.py'
504--- python/fakejuju/tests/test_failures.py 1970-01-01 00:00:00 +0000
505+++ python/fakejuju/tests/test_failures.py 2016-10-06 22:49:20 +0000
506@@ -0,0 +1,98 @@
507+# Copyright 2016 Canonical Limited. All rights reserved.
508+
509+import os
510+import os.path
511+import shutil
512+import tempfile
513+import unittest
514+
515+from fakejuju.failures import Failures
516+
517+
518+class FailuresTests(unittest.TestCase):
519+
520+ def setUp(self):
521+ super(FailuresTests, self).setUp()
522+ self.dirname = tempfile.mkdtemp(prefix="fakejuju-test-")
523+
524+ def tearDown(self):
525+ shutil.rmtree(self.dirname)
526+ super(FailuresTests, self).tearDown()
527+
528+ def test_full(self):
529+ """Failures() works correctly when given all args."""
530+ entities = [u"x", u"y", u"z"]
531+ failures = Failures(u"/some/dir", entities)
532+
533+ self.assertEqual(failures.filename, u"/some/dir/juju-failures")
534+ self.assertEqual(failures.entities, set(entities))
535+
536+ def test_minimal(self):
537+ """Failures() works correctly when given minimal args."""
538+ failures = Failures(u"/some/dir")
539+
540+ self.assertEqual(failures.filename, u"/some/dir/juju-failures")
541+ self.assertEqual(failures.entities, set())
542+
543+ def test_conversion(self):
544+ """Failures() converts str to unicode."""
545+ entities = ["x", "y", "z"]
546+ failures = Failures("/some/dir", entities)
547+
548+ self.assertIsInstance(failures.filename, unicode)
549+ for id in failures.entities:
550+ self.assertIsInstance(id, unicode)
551+
552+ def test_file_not_created_initially(self):
553+ """Failures() doesn't create a missing cfgdir until necessary."""
554+ failures = Failures(self.dirname)
555+
556+ self.assertFalse(os.path.exists(failures.filename))
557+
558+ def test_cfgdir_created(self):
559+ """Failures() creates a missing cfgdir as soon as it's needed."""
560+ dirname = os.path.join(self.dirname, "subdir")
561+ self.assertFalse(os.path.exists(dirname))
562+ failures = Failures(dirname)
563+ failures.fail_entity("unit-xyz")
564+
565+ self.assertTrue(os.path.exists(dirname))
566+
567+ def test_fail_entity_one(self):
568+ """Failures,fail_entity() writes an initial entry to disk."""
569+ failures = Failures(self.dirname)
570+ failures.fail_entity("unit-abc")
571+ with open(failures.filename) as file:
572+ data = file.read()
573+
574+ self.assertEqual(data, "unit-abc\n")
575+
576+ def test_fail_entity_multiple(self):
577+ """Failures.fail_entity() correctly writes multiple entries to disk."""
578+ failures = Failures(self.dirname)
579+ failures.fail_entity("unit-abc")
580+ failures.fail_entity("unit-xyz")
581+
582+ with open(failures.filename) as file:
583+ data = file.read()
584+ entities = set(tag for tag in data.splitlines() if tag)
585+ self.assertEqual(entities, failures.entities)
586+ self.assertTrue(data.endswith("\n"))
587+
588+ def test_clear_exists(self):
589+ """Failures.clear() deletes the failures file if it exists."""
590+ failures = Failures(self.dirname)
591+ failures.fail_entity("unit-abc")
592+ self.assertTrue(os.path.exists(failures.filename))
593+ failures.clear()
594+
595+ self.assertFalse(os.path.exists(failures.filename))
596+ self.assertEqual(failures.entities, set())
597+
598+ def test_clear_not_exists(self):
599+ """Failures.clear() does nothing if the failures file is missing."""
600+ failures = Failures(self.dirname)
601+ self.assertFalse(os.path.exists(failures.filename))
602+ failures.clear()
603+
604+ self.assertFalse(os.path.exists(failures.filename))
605
606=== added file 'python/fakejuju/tests/test_fakejuju.py'
607--- python/fakejuju/tests/test_fakejuju.py 1970-01-01 00:00:00 +0000
608+++ python/fakejuju/tests/test_fakejuju.py 2016-10-06 22:49:20 +0000
609@@ -0,0 +1,280 @@
610+# Copyright 2016 Canonical Limited. All rights reserved.
611+
612+import os
613+import unittest
614+
615+from txjuju import _juju1, _juju2
616+from txjuju._utils import Executable
617+import txjuju.cli
618+
619+from fakejuju.failures import Failures
620+from fakejuju.fakejuju import (
621+ get_bootstrap_spec, get_filename, set_envvars, FakeJuju)
622+
623+
624+class HelperTests(unittest.TestCase):
625+
626+ def test_get_bootstrap_spec_full(self):
627+ """get_bootstrap_spec() works correctly when given all args."""
628+ spec = get_bootstrap_spec("my-env", "pw")
629+
630+ self.assertEqual(
631+ spec,
632+ txjuju.cli.BootstrapSpec("my-env", "dummy", admin_secret="pw"))
633+
634+ def test_get_bootstrap_spec_minimal(self):
635+ """get_bootstrap_spec() works correctly when given minimal args."""
636+ spec = get_bootstrap_spec("my-env")
637+
638+ self.assertEqual(spec, txjuju.cli.BootstrapSpec("my-env", "dummy"))
639+
640+ def test_get_filename_full(self):
641+ """get_filename() works correctly when given all args."""
642+ filename = get_filename("1.25.6", "/spam")
643+
644+ self.assertEqual(filename, "/spam/fake-juju-1.25.6")
645+
646+ def test_get_filename_minimal(self):
647+ """get_filename() works correctly when given minimal args."""
648+ filename = get_filename("1.25.6")
649+
650+ self.assertEqual(filename, "/usr/bin/fake-juju-1.25.6")
651+
652+ def test_get_filename_empty_bindir(self):
653+ """get_filename() works correctly when given an empty string
654+ for bindir."""
655+ filename = get_filename("1.25.6", "")
656+
657+ self.assertEqual(filename, "fake-juju-1.25.6")
658+
659+ def test_get_filename_missing_version(self):
660+ """get_filename() fails if version is None or empty."""
661+ with self.assertRaises(ValueError):
662+ get_filename(None)
663+ with self.assertRaises(ValueError):
664+ get_filename("")
665+
666+ def test_set_envvars_full(self):
667+ """set_envvars() works correctly when given all args."""
668+ envvars = {}
669+ set_envvars(envvars, "/spam/failures", "/eggs/logsdir")
670+
671+ self.assertEqual(envvars, {
672+ "FAKE_JUJU_FAILURES": "/spam/failures",
673+ "FAKE_JUJU_LOGS_DIR": "/eggs/logsdir",
674+ })
675+
676+ def test_set_envvars_minimal(self):
677+ """set_envvars() works correctly when given minimal args."""
678+ envvars = {}
679+ set_envvars(envvars)
680+
681+ self.assertEqual(envvars, {
682+ "FAKE_JUJU_FAILURES": "",
683+ "FAKE_JUJU_LOGS_DIR": "",
684+ })
685+
686+ def test_set_envvars_start_empty(self):
687+ """set_envvars() sets all values on an empty dict."""
688+ envvars = {}
689+ set_envvars(envvars, "x", "y")
690+
691+ self.assertEqual(envvars, {
692+ "FAKE_JUJU_FAILURES": "x",
693+ "FAKE_JUJU_LOGS_DIR": "y",
694+ })
695+
696+ def test_set_envvars_no_collisions(self):
697+ """set_envvars() sets all values when none are set yet."""
698+ envvars = {"SPAM": "eggs"}
699+ set_envvars(envvars, "x", "y")
700+
701+ self.assertEqual(envvars, {
702+ "SPAM": "eggs",
703+ "FAKE_JUJU_FAILURES": "x",
704+ "FAKE_JUJU_LOGS_DIR": "y",
705+ })
706+
707+ def test_set_envvars_empty_to_nonempty(self):
708+ """set_envvars() updates empty values."""
709+ envvars = {
710+ "FAKE_JUJU_FAILURES": "",
711+ "FAKE_JUJU_LOGS_DIR": "",
712+ }
713+ set_envvars(envvars, "x", "y")
714+
715+ self.assertEqual(envvars, {
716+ "FAKE_JUJU_FAILURES": "x",
717+ "FAKE_JUJU_LOGS_DIR": "y",
718+ })
719+
720+ def test_set_envvars_nonempty_to_nonempty(self):
721+ """set_envvars() overwrites existing values."""
722+ envvars = {
723+ "FAKE_JUJU_FAILURES": "spam",
724+ "FAKE_JUJU_LOGS_DIR": "ham",
725+ }
726+ set_envvars(envvars, "x", "y")
727+
728+ self.assertEqual(envvars, {
729+ "FAKE_JUJU_FAILURES": "x",
730+ "FAKE_JUJU_LOGS_DIR": "y",
731+ })
732+
733+ def test_set_envvars_nonempty_to_empty(self):
734+ """set_envvars() with no args "unsets" existing values."""
735+ envvars = {
736+ "FAKE_JUJU_FAILURES": "x",
737+ "FAKE_JUJU_LOGS_DIR": "y",
738+ }
739+ set_envvars(envvars)
740+
741+ self.assertEqual(envvars, {
742+ "FAKE_JUJU_FAILURES": "",
743+ "FAKE_JUJU_LOGS_DIR": "",
744+ })
745+
746+
747+class FakeJujuTests(unittest.TestCase):
748+
749+ def test_from_version_full(self):
750+ """FakeJuju.from_version() works correctly when given all args."""
751+ juju = FakeJuju.from_version(
752+ "1.25.6", "/a/juju/home", "/logs/dir", "/failures/dir", "/bin/dir")
753+
754+ self.assertEqual(juju.filename, "/bin/dir/fake-juju-1.25.6")
755+ self.assertEqual(juju.version, "1.25.6")
756+ self.assertEqual(juju.cfgdir, "/a/juju/home")
757+ self.assertEqual(juju.logsdir, "/logs/dir")
758+ self.assertEqual(juju.failures.filename, "/failures/dir/juju-failures")
759+
760+ def test_from_version_minimal(self):
761+ """FakeJuju.from_version() works correctly when given minimal args."""
762+ juju = FakeJuju.from_version("1.25.6", "/my/juju/home")
763+
764+ self.assertEqual(juju.filename, "/usr/bin/fake-juju-1.25.6")
765+ self.assertEqual(juju.version, "1.25.6")
766+ self.assertEqual(juju.cfgdir, "/my/juju/home")
767+ self.assertEqual(juju.logsdir, "/my/juju/home")
768+ self.assertEqual(juju.failures.filename, "/my/juju/home/juju-failures")
769+
770+ def test_full(self):
771+ """FakeJuju() works correctly when given all args."""
772+ cfgdir = "/my/juju/home"
773+ failures = Failures(cfgdir)
774+ juju = FakeJuju("/fake-juju", "1.25.6", cfgdir, "/some/logs", failures)
775+
776+ self.assertEqual(juju.filename, "/fake-juju")
777+ self.assertEqual(juju.version, "1.25.6")
778+ self.assertEqual(juju.cfgdir, cfgdir)
779+ self.assertEqual(juju.logsdir, "/some/logs")
780+ self.assertIs(juju.failures, failures)
781+
782+ def test_minimal(self):
783+ """FakeJuju() works correctly when given minimal args."""
784+ juju = FakeJuju("/fake-juju", "1.25.6", "/my/juju/home")
785+
786+ self.assertEqual(juju.filename, "/fake-juju")
787+ self.assertEqual(juju.version, "1.25.6")
788+ self.assertEqual(juju.cfgdir, "/my/juju/home")
789+ self.assertEqual(juju.logsdir, "/my/juju/home")
790+ self.assertEqual(juju.failures.filename, "/my/juju/home/juju-failures")
791+
792+ def test_conversions(self):
793+ """FakeJuju() converts str to unicode."""
794+ juju = FakeJuju("/fake-juju", "1.25.6", "/x", "/y", Failures("/..."))
795+
796+ self.assertIsInstance(juju.filename, unicode)
797+ self.assertIsInstance(juju.version, unicode)
798+ self.assertIsInstance(juju.cfgdir, unicode)
799+ self.assertIsInstance(juju.logsdir, unicode)
800+
801+ def test_missing_filename(self):
802+ """FakeJuju() fails if filename is None or empty."""
803+ with self.assertRaises(ValueError):
804+ FakeJuju(None, "1.25.6", "/my/juju/home")
805+ with self.assertRaises(ValueError):
806+ FakeJuju("", "1.25.6", "/my/juju/home")
807+
808+ def test_missing_version(self):
809+ """FakeJuju() fails if version is None or empty."""
810+ with self.assertRaises(ValueError):
811+ FakeJuju("/fake-juju", None, "/my/juju/home")
812+ with self.assertRaises(ValueError):
813+ FakeJuju("/fake-juju", "", "/my/juju/home")
814+
815+ def test_missing_cfgdir(self):
816+ """FakeJuju() fails if cfgdir is None or empty."""
817+ with self.assertRaises(ValueError):
818+ FakeJuju("/fake-juju", "1.25.6", None)
819+ with self.assertRaises(ValueError):
820+ FakeJuju("/fake-juju", "1.25.6", "")
821+
822+ def test_logfile(self):
823+ """FakeJuju.logfile returns the path to the fake-juju log file."""
824+ juju = FakeJuju("/fake-juju", "1.25.6", "/x", "/some/logs")
825+
826+ self.assertEqual(juju.logfile, "/some/logs/fake-juju.log")
827+
828+ def test_infofile(self):
829+ """FakeJuju.logfile returns the path to the fake-juju info file."""
830+ juju = FakeJuju("/fake-juju", "1.25.6", "/x")
831+
832+ self.assertEqual(juju.infofile, "/x/fakejuju")
833+
834+ def test_fifo(self):
835+ """FakeJuju.logfile returns the path to the fake-juju fifo."""
836+ juju = FakeJuju("/fake-juju", "1.25.6", "/x")
837+
838+ self.assertEqual(juju.fifo, "/x/fifo")
839+
840+ def test_cacertfile(self):
841+ """FakeJuju.cacertfile returns the path to the Juju API cert."""
842+ juju = FakeJuju("/fake-juju", "1.25.6", "/x")
843+
844+ self.assertEqual(juju.cacertfile, "/x/cert.ca")
845+
846+ def test_cli_full(self):
847+ """FakeJuju.cli() works correctly when given all args."""
848+ juju = FakeJuju("/fake-juju", "1.25.6", "/x")
849+ cli = juju.cli({"SPAM": "eggs"})
850+
851+ self.assertEqual(
852+ cli._exe,
853+ Executable("/fake-juju", {
854+ "SPAM": "eggs",
855+ "FAKE_JUJU_FAILURES": "/x/juju-failures",
856+ "FAKE_JUJU_LOGS_DIR": "/x",
857+ "JUJU_HOME": "/x",
858+ }),
859+ )
860+
861+ def test_cli_minimal(self):
862+ """FakeJuju.cli() works correctly when given minimal args."""
863+ juju = FakeJuju("/fake-juju", "1.25.6", "/x")
864+ cli = juju.cli()
865+
866+ self.assertEqual(
867+ cli._exe,
868+ Executable("/fake-juju", dict(os.environ, **{
869+ "FAKE_JUJU_FAILURES": "/x/juju-failures",
870+ "FAKE_JUJU_LOGS_DIR": "/x",
871+ "JUJU_HOME": "/x",
872+ })),
873+ )
874+
875+ def test_cli_juju1(self):
876+ """FakeJuju.cli() works correctly for Juju 1.x."""
877+ juju = FakeJuju.from_version("1.25.6", "/x")
878+ cli = juju.cli()
879+
880+ self.assertEqual(cli._exe.envvars["JUJU_HOME"], "/x")
881+ self.assertIsInstance(cli._juju, _juju1.CLIHooks)
882+
883+ def test_cli_juju2(self):
884+ """FakeJuju.cli() works correctly for Juju 2.x."""
885+ juju = FakeJuju.from_version("2.0.0", "/x")
886+ cli = juju.cli()
887+
888+ self.assertEqual(cli._exe.envvars["JUJU_DATA"], "/x")
889+ self.assertIsInstance(cli._juju, _juju2.CLIHooks)
890
891=== added file 'python/setup.py'
892--- python/setup.py 1970-01-01 00:00:00 +0000
893+++ python/setup.py 2016-10-06 22:49:20 +0000
894@@ -0,0 +1,69 @@
895+import os
896+from importlib import import_module
897+try:
898+ from setuptools import setup
899+except ImportError:
900+ from distutils.core import setup
901+
902+
903+basedir = os.path.abspath(os.path.dirname(__file__) or '.')
904+
905+# required data
906+
907+package_name = 'fakejuju'
908+NAME = package_name
909+SUMMARY = 'A limited adaptation of Juju\'s client, with testing hooks.'
910+AUTHOR = 'Canonical Landscape team'
911+EMAIL = 'juju@lists.ubuntu.com'
912+PROJECT_URL = 'https://launchpad.net/fake-juju'
913+LICENSE = 'LGPLv3'
914+
915+with open(os.path.join(basedir, 'README.md')) as readme_file:
916+ DESCRIPTION = readme_file.read()
917+
918+# dymanically generated data
919+
920+VERSION = import_module(package_name).__version__
921+
922+# set up packages
923+
924+exclude_dirs = [
925+ 'tests',
926+ ]
927+
928+PACKAGES = []
929+for path, dirs, files in os.walk(package_name):
930+ if "__init__.py" not in files:
931+ continue
932+ path = path.split(os.sep)
933+ if path[-1] in exclude_dirs:
934+ continue
935+ PACKAGES.append(".".join(path))
936+
937+# dependencies
938+
939+DEPS = ['yaml',
940+ # for testing
941+ 'txjuju',
942+ 'fixtures',
943+ 'testtools',
944+ ]
945+
946+
947+if __name__ == "__main__":
948+ setup(name=NAME,
949+ version=VERSION,
950+ author=AUTHOR,
951+ author_email=EMAIL,
952+ url=PROJECT_URL,
953+ license=LICENSE,
954+ description=SUMMARY,
955+ long_description=DESCRIPTION,
956+ packages=PACKAGES,
957+
958+ # for distutils
959+ requires=DEPS,
960+
961+ # for setuptools
962+ install_requires=DEPS,
963+ )

Subscribers

People subscribed via source and target branches

to all changes: