~doanac/ubuntu-ci-services-itself/lander-integration-bugs

« back to all changes in this revision

Viewing changes to tests/ticket_system/test.py

  • Committer: Chris Johnston
  • Author(s): Andy Doan
  • Date: 2014-01-30 15:41:34 UTC
  • mfrom: (170.2.1 ubuntu-ci-services-itself)
  • Revision ID: chris_johnston-20140130154134-o9rqzbumvdne3otc
[r=Parameswaran Sivatharman, PS Jenkins bot] amulet: get a common base test class

We are starting to have to copying around amulet changes into each
test class and we need to write more test classes. This makes a common
base that's easy to extend from.
  from Andy Doan

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/usr/bin/env python
2
2
 
3
 
import base64
4
3
import json
5
4
import os
6
5
import unittest
7
6
 
8
7
import httplib2
9
 
import yaml
10
 
 
11
 
import amulet
12
 
 
13
 
 
14
 
opd = os.path.dirname
15
 
 
16
 
root_dir = opd(opd(opd((os.path.abspath(__file__)))))
17
 
 
18
 
 
19
 
class AmuletDeployment(object):
20
 
    """Fixture for a deployed set of juju services from a deployer config"""
21
 
 
22
 
    def __init__(self, deployer_cfg, juju_env=None, sentries=False):
23
 
        self.deployment = amulet.Deployment(juju_env, sentries=sentries)
24
 
        with open(deployer_cfg) as f:
25
 
            jd_script = yaml.safe_load(f.read())
26
 
        self.fixup_includes(os.path.dirname(deployer_cfg), jd_script)
27
 
        # Use juju_env given or defaulted above rather than taking the name
28
 
        # from the deployer config and assuming an env of that name exists
29
 
        script = {self.deployment.juju_env: jd_script.values()[0]}
30
 
        self.deployment.load(script)
31
 
 
32
 
    def fixup_includes(self, path, jd_script):
33
 
        # amulet doesn't deal with "include-base64" options, we we have to
34
 
        # patch the config ourselves
35
 
        for deployment in jd_script:
36
 
            for service in jd_script[deployment]['services'].values():
37
 
                for key, val in service.get('options', {}).iteritems():
38
 
                    include_token = 'include-base64://'
39
 
                    if type(val) is str and val.startswith(include_token):
40
 
                        val = val.replace(include_token, '')
41
 
                        path = os.path.join(path, val)
42
 
                        with open(path) as f:
43
 
                            val = base64.b64encode(f.read())
44
 
                        service['options'][key] = val
45
 
 
46
 
    def setUp(self):
47
 
        self.deployment.setup()
48
 
 
49
 
 
50
 
class TicketSystemTest(unittest.TestCase):
 
8
 
 
9
from tests.deployers import DEPLOYER_DIR, DeployerTest
 
10
 
 
11
 
 
12
class TicketSystemTest(DeployerTest):
51
13
    """Integration tests for ticket-system service run on a juju deployment"""
52
14
 
53
 
    def setUp(self):
54
 
        super(TicketSystemTest, self).setUp()
55
 
        self.deployer = AmuletDeployment(os.path.join(root_dir,
56
 
                                                      'juju-deployer',
57
 
                                                      'ticket-system.yaml'))
58
 
        self.deployer.setUp()
59
 
 
60
 
    def get_ip_and_port(self):
61
 
        self.status = amulet.waiter.status(self.deployer.deployment.juju_env)
62
 
        units = self.status['services']['ts-django']['units']
63
 
        ip = units['ts-django/0']['public-address']
64
 
        port, _ = units['ts-django/0']['open-ports'][0].split('/')
65
 
        return ip, port
 
15
    deployer_cfg = os.path.join(DEPLOYER_DIR, 'ticket-system.yaml')
66
16
 
67
17
    def get_server_status_and_content(self, url):
68
 
        final_url = url.format(*self.get_ip_and_port())
 
18
        final_url = url.format(*self.get_ip_and_port('ts-django'))
69
19
 
70
20
        # Issue a simple command against the ticket API
71
21
        client = httplib2.Http()
75
25
 
76
26
    def create_ticket(self, ticket_content):
77
27
        url = 'http://{}:{}/api/v1/ticket/'
78
 
        final_url = url.format(*self.get_ip_and_port())
 
28
        final_url = url.format(*self.get_ip_and_port('ts-django'))
79
29
 
80
30
        client = httplib2.Http()
81
31
        resp, content = client.request(