Merge lp:~gz/ubuntu-ci-services-itself/intergration_test_fancy into lp:ubuntu-ci-services-itself

Proposed by Martin Packman
Status: Merged
Approved by: Vincent Ladeuil
Approved revision: 52
Merged at revision: 52
Proposed branch: lp:~gz/ubuntu-ci-services-itself/intergration_test_fancy
Merge into: lp:ubuntu-ci-services-itself
Prerequisite: lp:~vila/ubuntu-ci-services-itself/ppa-assigner-integration-test
Diff against target: 107 lines (+55/-35)
1 file modified
tests/ppa_assigner/test.py (+55/-35)
To merge this branch: bzr merge lp:~gz/ubuntu-ci-services-itself/intergration_test_fancy
Reviewer Review Type Date Requested Status
Evan (community) Approve
Review via email: mp+199637@code.launchpad.net

Commit message

Use python fixtures to isolate the deployer part into its own fixture.

Description of the change

Refactoring of new integration tests to use some testing niceties, which should make reuse of the concepts easier.

Implements the setup of a juju deployment in terms of a lp:python-fixtures Fixture, this means tests can be explicit about reusing the same environment, and has room for adding cleanUp() and reset() methods.

The test assertions are now bundled in a unittest testcase as previously planned.

To post a comment you must log in.
Revision history for this message
Evan (ev) wrote :

The import sys is no longer needed, otherwise +1.

review: Approve
Revision history for this message
Chris Johnston (cjohnston) wrote :
Revision history for this message
Chris Johnston (cjohnston) wrote :

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'tests/ppa_assigner/test.py'
2--- tests/ppa_assigner/test.py 2013-12-19 11:24:51 +0000
3+++ tests/ppa_assigner/test.py 2013-12-19 11:24:51 +0000
4@@ -1,48 +1,68 @@
5 #!/usr/bin/env python
6
7 import json
8+import os
9+import sys
10+import unittest
11+
12 import httplib2
13+import fixtures
14 import yaml
15-import os
16-import sys
17+
18+import amulet
19
20 opd = os.path.dirname
21
22 root_dir = opd(opd(opd((os.path.abspath(__file__)))))
23
24-import amulet
25-
26-def main():
27- # FIXME: Running amulet with sentries is vurrently bogus in multiple ways
28- # and we can get juju status ourselves -- vila 2013-12-18
29- depl = amulet.Deployment(sentries=False)
30- jd_path = os.path.join(root_dir, 'juju-deployer', 'ppa-assigner.yaml')
31- with open(jd_path) as f:
32- jd_script = yaml.safe_load(f.read())
33- # Override the juju env provided by ppa-assigner or amulet gets confused
34- script = {depl.juju_env: jd_script.values()[0]}
35- depl.load(script)
36- # This is a no-op if (as expected) the ppa-assigner has already been
37- # deployed, but it *will* be deployed if needed anyway.
38- depl.setup()
39-
40- status = amulet.waiter.status(depl.juju_env)
41- units = status['services']['django']['units']
42- ip = units['django/0']['public-address']
43- port, _ = units['django/0']['open-ports'][0].split('/')
44- # Issue a simple command against the ppa assigner API
45- url = 'http://{}:{}/api/v1/ppa'.format(ip, port)
46- client = httplib2.Http()
47- resp, content = client.request(url, 'GET')
48- jcontent = json.loads(content)
49- # Target
50- # self.assertEqual('200', resp['status'])
51- # self.assertNotEqual([], jcontent['objects'])
52- if '200' != resp['status']:
53- sys.exit(1)
54- if [] == jcontent['objects']:
55- sys.exit(2)
56+
57+class AmuletDeployment(fixtures.Fixture):
58+ """Fixture for a deployed set of juju services from a deployer config"""
59+
60+ def __init__(self, deployer_cfg, juju_env=None, sentries=False):
61+ self.deployment = amulet.Deployment(juju_env, sentries=sentries)
62+ with open(deployer_cfg) as f:
63+ jd_script = yaml.safe_load(f.read())
64+ # Use juju_env given or defaulted above rather than taking the name
65+ # from the deployer config and assuming an env of that name exists
66+ script = {self.deployment.juju_env: jd_script.values()[0]}
67+ self.deployment.load(script)
68+
69+ def setUp(self):
70+ super(AmuletDeployment, self).setUp()
71+ self.deployment.setup()
72+
73+
74+class PPAAssignerTest(fixtures.TestWithFixtures):
75+ """Integration tests for ppa-assigner service run on a juju deployment"""
76+
77+ @staticmethod
78+ def _get_deployer_cfg():
79+ return os.path.join(root_dir, 'juju-deployer', 'ppa-assigner.yaml')
80+
81+ def setUp(self):
82+ super(PPAAssignerTest, self).setUp()
83+ self.deployer_fixture = AmuletDeployment(self._get_deployer_cfg())
84+
85+ def get_ip_and_port(self):
86+ status = amulet.waiter.status(self.deployer_fixture.deployment.juju_env)
87+ units = status['services']['django']['units']
88+ ip = units['django/0']['public-address']
89+ port, _ = units['django/0']['open-ports'][0].split('/')
90+ return ip, port
91+
92+ def test_ppa_api(self):
93+ self.useFixture(self.deployer_fixture)
94+
95+ url = 'http://{}:{}/api/v1/ppa'.format(*self.get_ip_and_port())
96+
97+ # Issue a simple command against the ppa assigner API
98+ client = httplib2.Http()
99+ resp, content = client.request(url, 'GET')
100+ jcontent = json.loads(content)
101+ self.assertEqual('200', resp['status'])
102+ self.assertNotEqual([], jcontent['objects'])
103
104
105 if __name__ == "__main__":
106- main()
107+ unittest.main()

Subscribers

People subscribed via source and target branches