Skip to content

Commit

Permalink
Make the code compatible with Python 2
Browse files Browse the repository at this point in the history
  • Loading branch information
freeekanayaka committed Oct 31, 2016
1 parent ff3b617 commit 0fb5d0d
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
dist
env
__pycache__
*.pyc
.coverage
.tox
AUTHORS
Expand Down
61 changes: 61 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
Charm Test
==========

.. image:: https://img.shields.io/pypi/v/charm-test.svg
:target: https://pypi.python.org/pypi/charm-test
:alt: Latest Version

.. image:: https://travis-ci.org/freeekanayaka/charm-test.svg?branch=master
:target: https://travis-ci.org/freeekanayaka/charm-test
:alt: Build Status

.. image:: https://coveralls.io/repos/github/freeekanayaka/charm-test/badge.svg?branch=master
:target: https://coveralls.io/github/freeekanayaka/charm-test?branch=master
:alt: Coverage

This package sports a collection of helpers for unit-testing Juju charms.

In particular, it extends systemfixtures_ by faking out hook tools
processes (`config-get`, `juju-log`, etc), so authors have a complete suite
of fakes for the typical "boundaries" of a Juju charm.

.. code:: python
>>> from testtools.matchers import DirExists
>>>
>>> from charmtest import CharmTest
>>>
>>> from charmhelpers.core import hookenv
>>>
>>>
>>> def example_charm_logic():
... return {
... "service-name": hookenv.service_name(),
... "local-unit": hookenv.local_unit(),
... "charm-dir": hookenv.charm_dir(),
... }
>>>
>>>
>>> class ExampleCharmTest(CharmTest):
...
... def test_charm_logic(self):
... result = example_charm_logic()
... self.assertEqual("test", result["service-name"])
... self.assertEqual("test/0", result["local-unit"])
... self.assertThat(result["charm-dir"], DirExists())
>>>
>>>
>>> ExampleCharmTest(methodName="test_charm_logic").run().wasSuccessful()
True
Support and Documentation
-------------------------

See the `online documentation <http://pythonhosted.org/charm-test/>`_ for
a complete reference.

Developing and Contributing
---------------------------

See the `GitHub project <https://github.com/freeekanayaka/charm-test>`_. Bugs
can be filed in the issues tracker.
13 changes: 3 additions & 10 deletions charmtest/juju/fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from testtools import try_import

from charmtest.paths import find_code_dir
from charmtest.juju.hooktools import (
ConfigGet,
JujuLog,
Expand All @@ -20,12 +21,12 @@
class FakeJuju(Fixture):

def __init__(self, filesystem, processes):
super().__init__()
super(Fixture, self).__init__()
self._fs = filesystem
self._processes = processes

def _setUp(self):
code_dir = self._find_code_dir()
code_dir = find_code_dir()
unit_name = self._unit_name(code_dir)
charm_dir = self._charm_dir(unit_name)

Expand All @@ -48,14 +49,6 @@ def _setUp(self):
# If charmhelpers is around, clear its config cache.
hookenv and hookenv.cache.clear()

def _find_code_dir(self):
directory = os.getcwd()
while directory != "/":
if os.path.exists(os.path.join(directory, "metadata.yaml")):
return directory
else: # pragma: no cover
raise RuntimeError("This doesn't seem to be a charm code tree")

def _unit_name(self, code_dir):
with open(os.path.join(code_dir, "metadata.yaml")) as fd:
metadata = yaml.safe_load(fd)
Expand Down
2 changes: 1 addition & 1 deletion charmtest/juju/tests/test_fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
class FakeJujuTest(TestCase):

def setUp(self):
super().setUp()
super(FakeJujuTest, self).setUp()
self.fs = self.useFixture(FakeFilesystem())
self.processes = self.useFixture(FakeProcesses())
self.juju = self.useFixture(FakeJuju(self.fs, self.processes))
Expand Down
4 changes: 2 additions & 2 deletions charmtest/juju/tests/test_hooktools.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class ConfigGetTest(TestCase):

def setUp(self):
super().setUp()
super(ConfigGetTest, self).setUp()
self.config = {}
self.process = ConfigGet(self.config)

Expand All @@ -22,7 +22,7 @@ def test_invoke(self):
class JujuLogTest(TestCase):

def setUp(self):
super().setUp()
super(JujuLogTest, self).setUp()
self.log = []
self.process = JujuLog(self.log)

Expand Down
10 changes: 10 additions & 0 deletions charmtest/paths.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import os


def find_code_dir():
directory = os.getcwd()
while directory != "/":
if os.path.exists(os.path.join(directory, "metadata.yaml")):
return directory
else: # pragma: no cover
raise RuntimeError("This doesn't seem to be a charm code tree")
2 changes: 1 addition & 1 deletion charmtest/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
class CharmTest(TestCase):

def setUp(self):
super().setUp()
super(CharmTest, self).setUp()
self.fakes = self.useFixture(CharmFakes())
2 changes: 1 addition & 1 deletion charmtest/tests/test_fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
class CharmFakesTest(TestCase):

def setUp(self):
super().setUp()
super(CharmFakesTest, self).setUp()
self.fakes = self.useFixture(CharmFakes())

def test_attributes(self):
Expand Down

0 comments on commit 0fb5d0d

Please sign in to comment.