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
=== added directory 'python'
=== added file 'python/LICENSE'
--- python/LICENSE 1970-01-01 00:00:00 +0000
+++ python/LICENSE 2016-10-06 22:49:20 +0000
@@ -0,0 +1,191 @@
1All files in this repository are licensed as follows. If you contribute
2to this repository, it is assumed that you license your contribution
3under the same license unless you state otherwise.
4
5All files Copyright (C) 2012-2016 Canonical Ltd. unless otherwise specified in the file.
6
7This software is licensed under the LGPLv3, included below.
8
9As a special exception to the GNU Lesser General Public License version 3
10("LGPL3"), the copyright holders of this Library give you permission to
11convey to a third party a Combined Work that links statically or dynamically
12to this Library without providing any Minimal Corresponding Source or
13Minimal Application Code as set out in 4d or providing the installation
14information set out in section 4e, provided that you comply with the other
15provisions of LGPL3 and provided that you meet, for the Application the
16terms and conditions of the license(s) which apply to the Application.
17
18Except as stated in this special exception, the provisions of LGPL3 will
19continue to comply in full to this Library. If you modify this Library, you
20may apply this exception to your version of this Library, but you are not
21obliged to do so. If you do not wish to do so, delete this exception
22statement from your version. This exception does not (and cannot) modify any
23license terms which apply to the Application, with which you must still
24comply.
25
26
27 GNU LESSER GENERAL PUBLIC LICENSE
28 Version 3, 29 June 2007
29
30 Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
31 Everyone is permitted to copy and distribute verbatim copies
32 of this license document, but changing it is not allowed.
33
34
35 This version of the GNU Lesser General Public License incorporates
36the terms and conditions of version 3 of the GNU General Public
37License, supplemented by the additional permissions listed below.
38
39 0. Additional Definitions.
40
41 As used herein, "this License" refers to version 3 of the GNU Lesser
42General Public License, and the "GNU GPL" refers to version 3 of the GNU
43General Public License.
44
45 "The Library" refers to a covered work governed by this License,
46other than an Application or a Combined Work as defined below.
47
48 An "Application" is any work that makes use of an interface provided
49by the Library, but which is not otherwise based on the Library.
50Defining a subclass of a class defined by the Library is deemed a mode
51of using an interface provided by the Library.
52
53 A "Combined Work" is a work produced by combining or linking an
54Application with the Library. The particular version of the Library
55with which the Combined Work was made is also called the "Linked
56Version".
57
58 The "Minimal Corresponding Source" for a Combined Work means the
59Corresponding Source for the Combined Work, excluding any source code
60for portions of the Combined Work that, considered in isolation, are
61based on the Application, and not on the Linked Version.
62
63 The "Corresponding Application Code" for a Combined Work means the
64object code and/or source code for the Application, including any data
65and utility programs needed for reproducing the Combined Work from the
66Application, but excluding the System Libraries of the Combined Work.
67
68 1. Exception to Section 3 of the GNU GPL.
69
70 You may convey a covered work under sections 3 and 4 of this License
71without being bound by section 3 of the GNU GPL.
72
73 2. Conveying Modified Versions.
74
75 If you modify a copy of the Library, and, in your modifications, a
76facility refers to a function or data to be supplied by an Application
77that uses the facility (other than as an argument passed when the
78facility is invoked), then you may convey a copy of the modified
79version:
80
81 a) under this License, provided that you make a good faith effort to
82 ensure that, in the event an Application does not supply the
83 function or data, the facility still operates, and performs
84 whatever part of its purpose remains meaningful, or
85
86 b) under the GNU GPL, with none of the additional permissions of
87 this License applicable to that copy.
88
89 3. Object Code Incorporating Material from Library Header Files.
90
91 The object code form of an Application may incorporate material from
92a header file that is part of the Library. You may convey such object
93code under terms of your choice, provided that, if the incorporated
94material is not limited to numerical parameters, data structure
95layouts and accessors, or small macros, inline functions and templates
96(ten or fewer lines in length), you do both of the following:
97
98 a) Give prominent notice with each copy of the object code that the
99 Library is used in it and that the Library and its use are
100 covered by this License.
101
102 b) Accompany the object code with a copy of the GNU GPL and this license
103 document.
104
105 4. Combined Works.
106
107 You may convey a Combined Work under terms of your choice that,
108taken together, effectively do not restrict modification of the
109portions of the Library contained in the Combined Work and reverse
110engineering for debugging such modifications, if you also do each of
111the following:
112
113 a) Give prominent notice with each copy of the Combined Work that
114 the Library is used in it and that the Library and its use are
115 covered by this License.
116
117 b) Accompany the Combined Work with a copy of the GNU GPL and this license
118 document.
119
120 c) For a Combined Work that displays copyright notices during
121 execution, include the copyright notice for the Library among
122 these notices, as well as a reference directing the user to the
123 copies of the GNU GPL and this license document.
124
125 d) Do one of the following:
126
127 0) Convey the Minimal Corresponding Source under the terms of this
128 License, and the Corresponding Application Code in a form
129 suitable for, and under terms that permit, the user to
130 recombine or relink the Application with a modified version of
131 the Linked Version to produce a modified Combined Work, in the
132 manner specified by section 6 of the GNU GPL for conveying
133 Corresponding Source.
134
135 1) Use a suitable shared library mechanism for linking with the
136 Library. A suitable mechanism is one that (a) uses at run time
137 a copy of the Library already present on the user's computer
138 system, and (b) will operate properly with a modified version
139 of the Library that is interface-compatible with the Linked
140 Version.
141
142 e) Provide Installation Information, but only if you would otherwise
143 be required to provide such information under section 6 of the
144 GNU GPL, and only to the extent that such information is
145 necessary to install and execute a modified version of the
146 Combined Work produced by recombining or relinking the
147 Application with a modified version of the Linked Version. (If
148 you use option 4d0, the Installation Information must accompany
149 the Minimal Corresponding Source and Corresponding Application
150 Code. If you use option 4d1, you must provide the Installation
151 Information in the manner specified by section 6 of the GNU GPL
152 for conveying Corresponding Source.)
153
154 5. Combined Libraries.
155
156 You may place library facilities that are a work based on the
157Library side by side in a single library together with other library
158facilities that are not Applications and are not covered by this
159License, and convey such a combined library under terms of your
160choice, if you do both of the following:
161
162 a) Accompany the combined library with a copy of the same work based
163 on the Library, uncombined with any other library facilities,
164 conveyed under the terms of this License.
165
166 b) Give prominent notice with the combined library that part of it
167 is a work based on the Library, and explaining where to find the
168 accompanying uncombined form of the same work.
169
170 6. Revised Versions of the GNU Lesser General Public License.
171
172 The Free Software Foundation may publish revised and/or new versions
173of the GNU Lesser General Public License from time to time. Such new
174versions will be similar in spirit to the present version, but may
175differ in detail to address new problems or concerns.
176
177 Each version is given a distinguishing version number. If the
178Library as you received it specifies that a certain numbered version
179of the GNU Lesser General Public License "or any later version"
180applies to it, you have the option of following the terms and
181conditions either of that published version or of any later version
182published by the Free Software Foundation. If the Library as you
183received it does not specify a version number of the GNU Lesser
184General Public License, you may choose any version of the GNU Lesser
185General Public License ever published by the Free Software Foundation.
186
187 If the Library as you received it specifies that a proxy can decide
188whether future versions of the GNU Lesser General Public License shall
189apply, that proxy's public statement of acceptance of any version is
190permanent authorization for you to choose that version for the
191Library.
0192
=== added file 'python/Makefile'
--- python/Makefile 1970-01-01 00:00:00 +0000
+++ python/Makefile 2016-10-06 22:49:20 +0000
@@ -0,0 +1,9 @@
1PYTHON = python
2
3.PHONY: test
4test:
5 $(PYTHON) -m unittest discover -t $(shell pwd) -s $(shell pwd)/fakejuju
6
7.PHONY: install-dev
8install-dev:
9 ln -s $(shell pwd)/fakejuju /usr/local/lib/python2.7/dist-packages/fakejuju
010
=== added file 'python/README.md'
--- python/README.md 1970-01-01 00:00:00 +0000
+++ python/README.md 2016-10-06 22:49:20 +0000
@@ -0,0 +1,1 @@
1# fakejuju
02
=== added directory 'python/fakejuju'
=== added file 'python/fakejuju/__init__.py'
--- python/fakejuju/__init__.py 1970-01-01 00:00:00 +0000
+++ python/fakejuju/__init__.py 2016-10-06 22:49:20 +0000
@@ -0,0 +1,57 @@
1# Copyright 2016 Canonical Limited. All rights reserved.
2
3"""Support for interaction with fake-juju.
4
5"fake-juju" is a combination of the juju and jujud commands that is
6suitable for use in integration tests. It exposes a limited subset
7of the standard juju subcommands (see FakeJuju in this module for
8specifics). When called without any arguments it runs jujud (using
9the dummy provider) with extra logging and testing hooks available to
10control failures. See https://launchpad.net/fake-juju for the project.
11
12The binary is named with the Juju version for which it was built.
13For example, for version 1.25.6 the file is named "fake-juju-1.25.6".
14
15fake-juju uses the normal Juju local config directory. This defaults
16to ~/.local/shared/juju and may be set using the JUJU_DATA environment
17variable (in 2.x, for 1.x it is JUJU_HOME).
18
19In addition to all the normal Juju environment variables (e.g.
20JUJU_DATA), fake-juju uses the following:
21
22 FAKE_JUJU_FAILURES - the path to the failures file
23 The Failures class below sets this to $JUJU_DATA/juju-failures.
24 FAKE_JUJU_LOGS_DIR - the path to the logs directory
25 This defaults to $JUJU_DATA.
26
27fake-juju also creates several extra files:
28
29 $FAKE_JUJU_LOGS_DIR/fake-juju.log - where fake-juju logs are written
30 $JUJU_DATA/fakejuju - fake-juju's data cache
31 $JUJU_DATA/fifo - a FIFO file that triggers jujud shutdown
32 $JUJU_DATA/cert.ca - the API's CA certificate
33
34Normal Juju logging for is written to $JUJU_DATA/fake-juju.log.
35
36Failures may be injected into a running fake-juju (or set before
37running). They may be injected by adding them to the file identified
38by $FAKE_JUJU_FAILURES. The format is a single failure definition per
39line. The syntax of the failure definition depends on the failure.
40The currently supported failures (with their definition syntax) are
41listed here:
42
43 * when adding a unit with a specific ID
44 format: "unit-<ID>" (e.g. unit-mysql/0)
45
46"""
47
48from .fakejuju import get_bootstrap_spec, get_filename, set_envvars, FakeJuju
49
50
51__all__ = [
52 "__version__",
53 "get_bootstrap_spec", "get_filename", "set_envvars",
54 "FakeJuju",
55 ]
56
57__version__ = "0.9.0b1"
058
=== added file 'python/fakejuju/failures.py'
--- python/fakejuju/failures.py 1970-01-01 00:00:00 +0000
+++ python/fakejuju/failures.py 2016-10-06 22:49:20 +0000
@@ -0,0 +1,65 @@
1# Copyright 2016 Canonical Limited. All rights reserved.
2
3import errno
4import os
5import os.path
6
7
8class Failures(object):
9 """The collection of injected failures to use with a fake-juju.
10
11 The failures are tracked here as well as injected into any
12 fake-juju using the initial config dir (aka "juju home").
13
14 Note that fake-juju provides only limited capability for
15 failure injection.
16 """
17
18 def __init__(self, cfgdir, entities=None):
19 """
20 @param cfgdir: The "juju home" directory into which the
21 failures will be registered for injection.
22 @param entities: The entity names to start with, if any.
23 """
24 filename = os.path.join(cfgdir, "juju-failures")
25 entities = set(unicode(tag) for tag in entities or ())
26
27 self._filename = unicode(filename)
28 self._entities = entities
29
30 @property
31 def filename(self):
32 """The path to the failures file the fake-juju reads."""
33 return self._filename
34
35 @property
36 def entities(self):
37 """The IDs of the failing entities."""
38 return set(self._entities)
39
40 def _flush(self):
41 """Write the failures to disk."""
42 data = "\n".join(self._entities) + "\n"
43 try:
44 file = open(self._filename, "w")
45 except IOError:
46 dirname = os.path.dirname(self._filename)
47 if not os.path.exists(dirname):
48 os.makedirs(dirname)
49 file = open(self._filename, "w")
50 with file:
51 file.write(data)
52
53 def fail_entity(self, tag):
54 """Inject a global failure for the identified Juju entity."""
55 self._entities.add(tag)
56 self._flush()
57
58 def clear(self):
59 """Remove all injected failures."""
60 try:
61 os.remove(self._filename)
62 except OSError as e:
63 if e.errno != errno.ENOENT:
64 raise
65 self._entities.clear()
066
=== added file 'python/fakejuju/fakejuju.py'
--- python/fakejuju/fakejuju.py 1970-01-01 00:00:00 +0000
+++ python/fakejuju/fakejuju.py 2016-10-06 22:49:20 +0000
@@ -0,0 +1,145 @@
1# Copyright 2016 Canonical Limited. All rights reserved.
2
3from collections import namedtuple
4import os.path
5
6import txjuju.cli
7
8from .failures import Failures
9
10
11def get_bootstrap_spec(name, admin_secret=None):
12 """Return the BootstrapSpec instance for the given controller.
13
14 @param name: The controller name to set up.
15 @param admin_secret: The admin user password to use.
16 """
17 type = "dummy"
18 default_series = None # Use the default.
19 return txjuju.cli.BootstrapSpec(name, type, default_series, admin_secret)
20
21
22def get_filename(version, bindir=None):
23 """Return the full path to the fake-juju binary for the given version.
24
25 @param version: The Juju version to use.
26 @param bindir: The directory containing the fake-juju binary.
27 This defaults to /usr/bin.
28 """
29 if not version:
30 raise ValueError("version not provided")
31 filename = "fake-juju-{}".format(version)
32 if bindir is None:
33 # XXX Search $PATH.
34 bindir = "/usr/bin"
35 return os.path.join(bindir, filename)
36
37
38def set_envvars(envvars, failures_filename=None, logsdir=None):
39 """Return the environment variables with which to run fake-juju.
40
41 @param envvars: The env dict to update.
42 @param failures_filename: The path to the failures file that
43 fake-juju will use.
44 @params logsdir: The path to the directory where fake-juju will
45 write its log files.
46 """
47 envvars["FAKE_JUJU_FAILURES"] = failures_filename or ""
48 envvars["FAKE_JUJU_LOGS_DIR"] = logsdir or ""
49
50
51class FakeJuju(
52 namedtuple("FakeJuju", "filename version cfgdir logsdir failures")):
53 """The fundamental details for fake-juju."""
54
55 @classmethod
56 def from_version(cls, version, cfgdir,
57 logsdir=None, failuresdir=None, bindir=None):
58 """Return a new instance given the provided information.
59
60 @param version: The Juju version to fake.
61 @param cfgdir: The "juju home" directory to use.
62 @param logsdir: The directory where logs will be written.
63 This defaults to cfgdir.
64 @params failuresdir: The directory where failure injection
65 is managed.
66 @param bindir: The directory containing the fake-juju binary.
67 This defaults to /usr/bin.
68 """
69 if logsdir is None:
70 logsdir = cfgdir
71 if failuresdir is None:
72 failuresdir = cfgdir
73 filename = get_filename(version, bindir=bindir)
74 failures = Failures(failuresdir)
75 return cls(filename, version, cfgdir, logsdir, failures)
76
77 def __new__(cls, filename, version, cfgdir, logsdir=None, failures=None):
78 """
79 @param filename: The path to the fake-juju binary.
80 @param version: The Juju version to fake.
81 @param cfgdir: The "juju home" directory to use.
82 @param logsdir: The directory where logs will be written.
83 This defaults to cfgdir.
84 @param failures: The set of fake-juju failures to use.
85 """
86 filename = unicode(filename) if filename else None
87 version = unicode(version) if version else None
88 cfgdir = unicode(cfgdir) if cfgdir else None
89 logsdir = unicode(logsdir) if logsdir is not None else cfgdir
90 if failures is None and cfgdir:
91 failures = Failures(cfgdir)
92 return super(FakeJuju, cls).__new__(
93 cls, filename, version, cfgdir, logsdir, failures)
94
95 def __init__(self, *args, **kwargs):
96 if not self.filename:
97 raise ValueError("missing filename")
98 if not self.version:
99 raise ValueError("missing version")
100 if not self.cfgdir:
101 raise ValueError("missing cfgdir")
102 if not self.logsdir:
103 raise ValueError("missing logsdir")
104 if self.failures is None:
105 raise ValueError("missing failures")
106
107 @property
108 def logfile(self):
109 """The path to fake-juju's log file."""
110 return os.path.join(self.logsdir, "fake-juju.log")
111
112 @property
113 def infofile(self):
114 """The path to fake-juju's data cache."""
115 return os.path.join(self.cfgdir, "fakejuju")
116
117 @property
118 def fifo(self):
119 """The path to the fifo file that triggers shutdown."""
120 return os.path.join(self.cfgdir, "fifo")
121
122 @property
123 def cacertfile(self):
124 """The path to the API server's certificate."""
125 return os.path.join(self.cfgdir, "cert.ca")
126
127 def cli(self, envvars=None):
128 """
129
130 Currently only the following juju subcommands are supported:
131
132 * bootstrap
133 Not that this only supports the dummy provider and the local
134 system is only minimally impacted.
135 * api-info
136 Note that passwords are always omited, even if requested.
137 * api-endpoints
138 * destroy-environment
139 """
140 if envvars is None:
141 envvars = os.environ
142 envvars = dict(envvars)
143 set_envvars(envvars, self.failures._filename, self.logsdir)
144 return txjuju.cli.CLI.from_version(
145 self.filename, self.version, self.cfgdir, envvars)
0146
=== added directory 'python/fakejuju/tests'
=== added file 'python/fakejuju/tests/__init__.py'
=== added file 'python/fakejuju/tests/test_failures.py'
--- python/fakejuju/tests/test_failures.py 1970-01-01 00:00:00 +0000
+++ python/fakejuju/tests/test_failures.py 2016-10-06 22:49:20 +0000
@@ -0,0 +1,98 @@
1# Copyright 2016 Canonical Limited. All rights reserved.
2
3import os
4import os.path
5import shutil
6import tempfile
7import unittest
8
9from fakejuju.failures import Failures
10
11
12class FailuresTests(unittest.TestCase):
13
14 def setUp(self):
15 super(FailuresTests, self).setUp()
16 self.dirname = tempfile.mkdtemp(prefix="fakejuju-test-")
17
18 def tearDown(self):
19 shutil.rmtree(self.dirname)
20 super(FailuresTests, self).tearDown()
21
22 def test_full(self):
23 """Failures() works correctly when given all args."""
24 entities = [u"x", u"y", u"z"]
25 failures = Failures(u"/some/dir", entities)
26
27 self.assertEqual(failures.filename, u"/some/dir/juju-failures")
28 self.assertEqual(failures.entities, set(entities))
29
30 def test_minimal(self):
31 """Failures() works correctly when given minimal args."""
32 failures = Failures(u"/some/dir")
33
34 self.assertEqual(failures.filename, u"/some/dir/juju-failures")
35 self.assertEqual(failures.entities, set())
36
37 def test_conversion(self):
38 """Failures() converts str to unicode."""
39 entities = ["x", "y", "z"]
40 failures = Failures("/some/dir", entities)
41
42 self.assertIsInstance(failures.filename, unicode)
43 for id in failures.entities:
44 self.assertIsInstance(id, unicode)
45
46 def test_file_not_created_initially(self):
47 """Failures() doesn't create a missing cfgdir until necessary."""
48 failures = Failures(self.dirname)
49
50 self.assertFalse(os.path.exists(failures.filename))
51
52 def test_cfgdir_created(self):
53 """Failures() creates a missing cfgdir as soon as it's needed."""
54 dirname = os.path.join(self.dirname, "subdir")
55 self.assertFalse(os.path.exists(dirname))
56 failures = Failures(dirname)
57 failures.fail_entity("unit-xyz")
58
59 self.assertTrue(os.path.exists(dirname))
60
61 def test_fail_entity_one(self):
62 """Failures,fail_entity() writes an initial entry to disk."""
63 failures = Failures(self.dirname)
64 failures.fail_entity("unit-abc")
65 with open(failures.filename) as file:
66 data = file.read()
67
68 self.assertEqual(data, "unit-abc\n")
69
70 def test_fail_entity_multiple(self):
71 """Failures.fail_entity() correctly writes multiple entries to disk."""
72 failures = Failures(self.dirname)
73 failures.fail_entity("unit-abc")
74 failures.fail_entity("unit-xyz")
75
76 with open(failures.filename) as file:
77 data = file.read()
78 entities = set(tag for tag in data.splitlines() if tag)
79 self.assertEqual(entities, failures.entities)
80 self.assertTrue(data.endswith("\n"))
81
82 def test_clear_exists(self):
83 """Failures.clear() deletes the failures file if it exists."""
84 failures = Failures(self.dirname)
85 failures.fail_entity("unit-abc")
86 self.assertTrue(os.path.exists(failures.filename))
87 failures.clear()
88
89 self.assertFalse(os.path.exists(failures.filename))
90 self.assertEqual(failures.entities, set())
91
92 def test_clear_not_exists(self):
93 """Failures.clear() does nothing if the failures file is missing."""
94 failures = Failures(self.dirname)
95 self.assertFalse(os.path.exists(failures.filename))
96 failures.clear()
97
98 self.assertFalse(os.path.exists(failures.filename))
099
=== added file 'python/fakejuju/tests/test_fakejuju.py'
--- python/fakejuju/tests/test_fakejuju.py 1970-01-01 00:00:00 +0000
+++ python/fakejuju/tests/test_fakejuju.py 2016-10-06 22:49:20 +0000
@@ -0,0 +1,280 @@
1# Copyright 2016 Canonical Limited. All rights reserved.
2
3import os
4import unittest
5
6from txjuju import _juju1, _juju2
7from txjuju._utils import Executable
8import txjuju.cli
9
10from fakejuju.failures import Failures
11from fakejuju.fakejuju import (
12 get_bootstrap_spec, get_filename, set_envvars, FakeJuju)
13
14
15class HelperTests(unittest.TestCase):
16
17 def test_get_bootstrap_spec_full(self):
18 """get_bootstrap_spec() works correctly when given all args."""
19 spec = get_bootstrap_spec("my-env", "pw")
20
21 self.assertEqual(
22 spec,
23 txjuju.cli.BootstrapSpec("my-env", "dummy", admin_secret="pw"))
24
25 def test_get_bootstrap_spec_minimal(self):
26 """get_bootstrap_spec() works correctly when given minimal args."""
27 spec = get_bootstrap_spec("my-env")
28
29 self.assertEqual(spec, txjuju.cli.BootstrapSpec("my-env", "dummy"))
30
31 def test_get_filename_full(self):
32 """get_filename() works correctly when given all args."""
33 filename = get_filename("1.25.6", "/spam")
34
35 self.assertEqual(filename, "/spam/fake-juju-1.25.6")
36
37 def test_get_filename_minimal(self):
38 """get_filename() works correctly when given minimal args."""
39 filename = get_filename("1.25.6")
40
41 self.assertEqual(filename, "/usr/bin/fake-juju-1.25.6")
42
43 def test_get_filename_empty_bindir(self):
44 """get_filename() works correctly when given an empty string
45 for bindir."""
46 filename = get_filename("1.25.6", "")
47
48 self.assertEqual(filename, "fake-juju-1.25.6")
49
50 def test_get_filename_missing_version(self):
51 """get_filename() fails if version is None or empty."""
52 with self.assertRaises(ValueError):
53 get_filename(None)
54 with self.assertRaises(ValueError):
55 get_filename("")
56
57 def test_set_envvars_full(self):
58 """set_envvars() works correctly when given all args."""
59 envvars = {}
60 set_envvars(envvars, "/spam/failures", "/eggs/logsdir")
61
62 self.assertEqual(envvars, {
63 "FAKE_JUJU_FAILURES": "/spam/failures",
64 "FAKE_JUJU_LOGS_DIR": "/eggs/logsdir",
65 })
66
67 def test_set_envvars_minimal(self):
68 """set_envvars() works correctly when given minimal args."""
69 envvars = {}
70 set_envvars(envvars)
71
72 self.assertEqual(envvars, {
73 "FAKE_JUJU_FAILURES": "",
74 "FAKE_JUJU_LOGS_DIR": "",
75 })
76
77 def test_set_envvars_start_empty(self):
78 """set_envvars() sets all values on an empty dict."""
79 envvars = {}
80 set_envvars(envvars, "x", "y")
81
82 self.assertEqual(envvars, {
83 "FAKE_JUJU_FAILURES": "x",
84 "FAKE_JUJU_LOGS_DIR": "y",
85 })
86
87 def test_set_envvars_no_collisions(self):
88 """set_envvars() sets all values when none are set yet."""
89 envvars = {"SPAM": "eggs"}
90 set_envvars(envvars, "x", "y")
91
92 self.assertEqual(envvars, {
93 "SPAM": "eggs",
94 "FAKE_JUJU_FAILURES": "x",
95 "FAKE_JUJU_LOGS_DIR": "y",
96 })
97
98 def test_set_envvars_empty_to_nonempty(self):
99 """set_envvars() updates empty values."""
100 envvars = {
101 "FAKE_JUJU_FAILURES": "",
102 "FAKE_JUJU_LOGS_DIR": "",
103 }
104 set_envvars(envvars, "x", "y")
105
106 self.assertEqual(envvars, {
107 "FAKE_JUJU_FAILURES": "x",
108 "FAKE_JUJU_LOGS_DIR": "y",
109 })
110
111 def test_set_envvars_nonempty_to_nonempty(self):
112 """set_envvars() overwrites existing values."""
113 envvars = {
114 "FAKE_JUJU_FAILURES": "spam",
115 "FAKE_JUJU_LOGS_DIR": "ham",
116 }
117 set_envvars(envvars, "x", "y")
118
119 self.assertEqual(envvars, {
120 "FAKE_JUJU_FAILURES": "x",
121 "FAKE_JUJU_LOGS_DIR": "y",
122 })
123
124 def test_set_envvars_nonempty_to_empty(self):
125 """set_envvars() with no args "unsets" existing values."""
126 envvars = {
127 "FAKE_JUJU_FAILURES": "x",
128 "FAKE_JUJU_LOGS_DIR": "y",
129 }
130 set_envvars(envvars)
131
132 self.assertEqual(envvars, {
133 "FAKE_JUJU_FAILURES": "",
134 "FAKE_JUJU_LOGS_DIR": "",
135 })
136
137
138class FakeJujuTests(unittest.TestCase):
139
140 def test_from_version_full(self):
141 """FakeJuju.from_version() works correctly when given all args."""
142 juju = FakeJuju.from_version(
143 "1.25.6", "/a/juju/home", "/logs/dir", "/failures/dir", "/bin/dir")
144
145 self.assertEqual(juju.filename, "/bin/dir/fake-juju-1.25.6")
146 self.assertEqual(juju.version, "1.25.6")
147 self.assertEqual(juju.cfgdir, "/a/juju/home")
148 self.assertEqual(juju.logsdir, "/logs/dir")
149 self.assertEqual(juju.failures.filename, "/failures/dir/juju-failures")
150
151 def test_from_version_minimal(self):
152 """FakeJuju.from_version() works correctly when given minimal args."""
153 juju = FakeJuju.from_version("1.25.6", "/my/juju/home")
154
155 self.assertEqual(juju.filename, "/usr/bin/fake-juju-1.25.6")
156 self.assertEqual(juju.version, "1.25.6")
157 self.assertEqual(juju.cfgdir, "/my/juju/home")
158 self.assertEqual(juju.logsdir, "/my/juju/home")
159 self.assertEqual(juju.failures.filename, "/my/juju/home/juju-failures")
160
161 def test_full(self):
162 """FakeJuju() works correctly when given all args."""
163 cfgdir = "/my/juju/home"
164 failures = Failures(cfgdir)
165 juju = FakeJuju("/fake-juju", "1.25.6", cfgdir, "/some/logs", failures)
166
167 self.assertEqual(juju.filename, "/fake-juju")
168 self.assertEqual(juju.version, "1.25.6")
169 self.assertEqual(juju.cfgdir, cfgdir)
170 self.assertEqual(juju.logsdir, "/some/logs")
171 self.assertIs(juju.failures, failures)
172
173 def test_minimal(self):
174 """FakeJuju() works correctly when given minimal args."""
175 juju = FakeJuju("/fake-juju", "1.25.6", "/my/juju/home")
176
177 self.assertEqual(juju.filename, "/fake-juju")
178 self.assertEqual(juju.version, "1.25.6")
179 self.assertEqual(juju.cfgdir, "/my/juju/home")
180 self.assertEqual(juju.logsdir, "/my/juju/home")
181 self.assertEqual(juju.failures.filename, "/my/juju/home/juju-failures")
182
183 def test_conversions(self):
184 """FakeJuju() converts str to unicode."""
185 juju = FakeJuju("/fake-juju", "1.25.6", "/x", "/y", Failures("/..."))
186
187 self.assertIsInstance(juju.filename, unicode)
188 self.assertIsInstance(juju.version, unicode)
189 self.assertIsInstance(juju.cfgdir, unicode)
190 self.assertIsInstance(juju.logsdir, unicode)
191
192 def test_missing_filename(self):
193 """FakeJuju() fails if filename is None or empty."""
194 with self.assertRaises(ValueError):
195 FakeJuju(None, "1.25.6", "/my/juju/home")
196 with self.assertRaises(ValueError):
197 FakeJuju("", "1.25.6", "/my/juju/home")
198
199 def test_missing_version(self):
200 """FakeJuju() fails if version is None or empty."""
201 with self.assertRaises(ValueError):
202 FakeJuju("/fake-juju", None, "/my/juju/home")
203 with self.assertRaises(ValueError):
204 FakeJuju("/fake-juju", "", "/my/juju/home")
205
206 def test_missing_cfgdir(self):
207 """FakeJuju() fails if cfgdir is None or empty."""
208 with self.assertRaises(ValueError):
209 FakeJuju("/fake-juju", "1.25.6", None)
210 with self.assertRaises(ValueError):
211 FakeJuju("/fake-juju", "1.25.6", "")
212
213 def test_logfile(self):
214 """FakeJuju.logfile returns the path to the fake-juju log file."""
215 juju = FakeJuju("/fake-juju", "1.25.6", "/x", "/some/logs")
216
217 self.assertEqual(juju.logfile, "/some/logs/fake-juju.log")
218
219 def test_infofile(self):
220 """FakeJuju.logfile returns the path to the fake-juju info file."""
221 juju = FakeJuju("/fake-juju", "1.25.6", "/x")
222
223 self.assertEqual(juju.infofile, "/x/fakejuju")
224
225 def test_fifo(self):
226 """FakeJuju.logfile returns the path to the fake-juju fifo."""
227 juju = FakeJuju("/fake-juju", "1.25.6", "/x")
228
229 self.assertEqual(juju.fifo, "/x/fifo")
230
231 def test_cacertfile(self):
232 """FakeJuju.cacertfile returns the path to the Juju API cert."""
233 juju = FakeJuju("/fake-juju", "1.25.6", "/x")
234
235 self.assertEqual(juju.cacertfile, "/x/cert.ca")
236
237 def test_cli_full(self):
238 """FakeJuju.cli() works correctly when given all args."""
239 juju = FakeJuju("/fake-juju", "1.25.6", "/x")
240 cli = juju.cli({"SPAM": "eggs"})
241
242 self.assertEqual(
243 cli._exe,
244 Executable("/fake-juju", {
245 "SPAM": "eggs",
246 "FAKE_JUJU_FAILURES": "/x/juju-failures",
247 "FAKE_JUJU_LOGS_DIR": "/x",
248 "JUJU_HOME": "/x",
249 }),
250 )
251
252 def test_cli_minimal(self):
253 """FakeJuju.cli() works correctly when given minimal args."""
254 juju = FakeJuju("/fake-juju", "1.25.6", "/x")
255 cli = juju.cli()
256
257 self.assertEqual(
258 cli._exe,
259 Executable("/fake-juju", dict(os.environ, **{
260 "FAKE_JUJU_FAILURES": "/x/juju-failures",
261 "FAKE_JUJU_LOGS_DIR": "/x",
262 "JUJU_HOME": "/x",
263 })),
264 )
265
266 def test_cli_juju1(self):
267 """FakeJuju.cli() works correctly for Juju 1.x."""
268 juju = FakeJuju.from_version("1.25.6", "/x")
269 cli = juju.cli()
270
271 self.assertEqual(cli._exe.envvars["JUJU_HOME"], "/x")
272 self.assertIsInstance(cli._juju, _juju1.CLIHooks)
273
274 def test_cli_juju2(self):
275 """FakeJuju.cli() works correctly for Juju 2.x."""
276 juju = FakeJuju.from_version("2.0.0", "/x")
277 cli = juju.cli()
278
279 self.assertEqual(cli._exe.envvars["JUJU_DATA"], "/x")
280 self.assertIsInstance(cli._juju, _juju2.CLIHooks)
0281
=== added file 'python/setup.py'
--- python/setup.py 1970-01-01 00:00:00 +0000
+++ python/setup.py 2016-10-06 22:49:20 +0000
@@ -0,0 +1,69 @@
1import os
2from importlib import import_module
3try:
4 from setuptools import setup
5except ImportError:
6 from distutils.core import setup
7
8
9basedir = os.path.abspath(os.path.dirname(__file__) or '.')
10
11# required data
12
13package_name = 'fakejuju'
14NAME = package_name
15SUMMARY = 'A limited adaptation of Juju\'s client, with testing hooks.'
16AUTHOR = 'Canonical Landscape team'
17EMAIL = 'juju@lists.ubuntu.com'
18PROJECT_URL = 'https://launchpad.net/fake-juju'
19LICENSE = 'LGPLv3'
20
21with open(os.path.join(basedir, 'README.md')) as readme_file:
22 DESCRIPTION = readme_file.read()
23
24# dymanically generated data
25
26VERSION = import_module(package_name).__version__
27
28# set up packages
29
30exclude_dirs = [
31 'tests',
32 ]
33
34PACKAGES = []
35for path, dirs, files in os.walk(package_name):
36 if "__init__.py" not in files:
37 continue
38 path = path.split(os.sep)
39 if path[-1] in exclude_dirs:
40 continue
41 PACKAGES.append(".".join(path))
42
43# dependencies
44
45DEPS = ['yaml',
46 # for testing
47 'txjuju',
48 'fixtures',
49 'testtools',
50 ]
51
52
53if __name__ == "__main__":
54 setup(name=NAME,
55 version=VERSION,
56 author=AUTHOR,
57 author_email=EMAIL,
58 url=PROJECT_URL,
59 license=LICENSE,
60 description=SUMMARY,
61 long_description=DESCRIPTION,
62 packages=PACKAGES,
63
64 # for distutils
65 requires=DEPS,
66
67 # for setuptools
68 install_requires=DEPS,
69 )

Subscribers

People subscribed via source and target branches

to all changes: