~psivaa/uci-engine/lander-jenkins-with-proxy

« back to all changes in this revision

Viewing changes to tests/ticket_system/test.py

  • Committer: Parameswaran Sivatharman
  • Date: 2014-01-07 17:25:40 UTC
  • mto: This revision was merged to the branch mainline in revision 89.
  • Revision ID: para.siva@canonical.com-20140107172540-405mljjc60r3z9pq
Ticket-system basic juju tests

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
3
import json
 
4
import os
 
5
import unittest
 
6
 
 
7
import httplib2
 
8
import yaml
 
9
 
 
10
import amulet
 
11
 
 
12
 
 
13
opd = os.path.dirname
 
14
 
 
15
root_dir = opd(opd(opd((os.path.abspath(__file__)))))
 
16
 
 
17
 
 
18
class AmuletDeployment(object):
 
19
    """Fixture for a deployed set of juju services from a deployer config"""
 
20
 
 
21
    def __init__(self, deployer_cfg, juju_env=None, sentries=False):
 
22
        self.deployment = amulet.Deployment(juju_env, sentries=sentries)
 
23
        with open(deployer_cfg) as f:
 
24
            jd_script = yaml.safe_load(f.read())
 
25
        # Use juju_env given or defaulted above rather than taking the name
 
26
        # from the deployer config and assuming an env of that name exists
 
27
        script = {self.deployment.juju_env: jd_script.values()[0]}
 
28
        self.deployment.load(script)
 
29
 
 
30
    def setUp(self):
 
31
        self.deployment.setup()
 
32
 
 
33
 
 
34
class PPAAssignerTest(unittest.TestCase):
 
35
    """Integration tests for ppa-assigner service run on a juju deployment"""
 
36
 
 
37
    def setUp(self):
 
38
        super(PPAAssignerTest, self).setUp()
 
39
        self.deployer = AmuletDeployment(os.path.join(root_dir,
 
40
                                                      'juju-deployer',
 
41
                                                      'ticket_system.yaml'))
 
42
        self.deployer.setUp()
 
43
 
 
44
    def get_ip_and_port(self):
 
45
        self.status = amulet.waiter.status(self.deployer.deployment.juju_env)
 
46
        units = self.status['services']['ts-django']['units']
 
47
        ip = units['ts-django/0']['public-address']
 
48
        port, _ = units['ts-django/0']['open-ports'][0].split('/')
 
49
        return ip, port
 
50
 
 
51
    def get_server_status_and_content(self, url):
 
52
        final_url = url.format(*self.get_ip_and_port())
 
53
 
 
54
        # Issue a simple command against the ticket API
 
55
        client = httplib2.Http()
 
56
        resp, content = client.request(final_url, 'GET')
 
57
        jcontent = json.loads(content)
 
58
        return resp, jcontent
 
59
 
 
60
    def create_ticket(self, ticket_content):
 
61
        url = 'http://{}:{}/api/v1/ticket/'
 
62
        final_url = url.format(*self.get_ip_and_port())
 
63
 
 
64
        client = httplib2.Http()
 
65
        resp, content = client.request(
 
66
            uri=final_url,
 
67
            method='POST',
 
68
            headers={'Content-Type': 'application/json'},
 
69
            body=json.dumps(ticket_content),
 
70
        )
 
71
        return resp, content
 
72
 
 
73
    def test_ticket_sys_api(self):
 
74
        url = 'http://{}:{}/api/v1/ticket'
 
75
        resp, content = self.get_server_status_and_content(url)
 
76
 
 
77
        self.assertEqual('200', resp['status'])
 
78
 
 
79
    def test_create_ticket(self):
 
80
        ticket_content = {"owner": "create@example.com",
 
81
                          "title": "Created ticket",
 
82
                          "description": "This ticket is for. \
 
83
creattion test",
 
84
                          "bug_id": "111111",
 
85
                          "added_binaries": "binary-1,binary-2",
 
86
                          "removed_binaries": "binary-3,binary-4"}
 
87
        resp, content = self.create_ticket(ticket_content)
 
88
        self.assertEqual('201', resp['status'])
 
89
 
 
90
    def test_prohibit_delete(self):
 
91
        ticket_content = {'owner': "dont_delete@example.com",
 
92
                          "title": "Delete test ticket",
 
93
                          "description": "Attempt to delete ticket details",
 
94
                          "bug_id": "22222",
 
95
                          "added_binaries": "binary-4-delete-1",
 
96
                          "removed_binaries": "binary-4-delete-2"}
 
97
        resp, content = self.create_ticket(ticket_content)
 
98
        self.assertEqual('201', resp['status'])
 
99
        ticket_loc = resp['location']
 
100
 
 
101
        client = httplib2.Http()
 
102
        resp, content = client.request(
 
103
            uri=ticket_loc,
 
104
            method='DELETE',
 
105
        )
 
106
        self.assertEqual('405', resp['status'])
 
107
 
 
108
    def test_prihibit_change_ticket_details(self):
 
109
        ticket_content = {'owner': "before_change@example.com",
 
110
                          "title": "Change test ticket",
 
111
                          "description": "Attempt to change ticket details",
 
112
                          "bug_id": "33333",
 
113
                          "added_binaries": "binary-4-change-1",
 
114
                          "removed_binaries": "binary-4-change-2"}
 
115
        resp, content = self.create_ticket(ticket_content)
 
116
        self.assertEqual('201', resp['status'])
 
117
        ticket_loc = resp['location']
 
118
 
 
119
        client = httplib2.Http()
 
120
        resp, content = client.request(
 
121
            uri=ticket_loc,
 
122
            method='PATCH',
 
123
            headers={'Content-Type': 'application/json'},
 
124
            body=json.dumps({'owner': "after_changed@example.com"}),
 
125
        )
 
126
        self.assertEqual('405', resp['status'])
 
127
 
 
128
if __name__ == "__main__":
 
129
    unittest.main()