~vila/uci-engine/rtfd

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env python
# Ubuntu Continuous Integration Engine
# Copyright 2014 Canonical Ltd.

# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License version 3, as
# published by the Free Software Foundation.

# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
# PURPOSE.  See the GNU Affero General Public License for more details.

# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import re
import httplib2
import json
import os
import subprocess
import sys
import tempfile
import unittest

from ucitests import fixtures
import deployers


HERE = os.path.abspath(os.path.dirname(__file__))
sys.path.append(os.path.join(HERE, '..', 'ci-utils'))

from ci_utils import testing


class IntegrationTest(deployers.DeployerTest):
    """Integration tests for ticket-system service run on a juju deployment"""

    def get_server_status_and_content(self, url,
                                      service='ci-airline-webui-apache'):
        final_url = url.format(self.get_ip_and_port(service)[0])
        client = httplib2.Http()
        resp, content = client.request(final_url, 'GET')
        return resp, content

    def create_new_ticket(self, title, ci_url):
        """Create a new ticket using the CLI."""
        # We need the ci_utils and ci_cli modules paths for
        # running 'ubuntu-ci'.
        mod_paths = [os.path.realpath(os.path.join(HERE, '..', mod))
                     for mod in ('ci-utils', 'cli')]
        fixtures.override_env(self, 'PYTHONPATH', ':'.join(mod_paths))
        ci_url = 'http://{}'.format(
            self.get_ip_and_port('ci-airline-webui-apache')[0])

        # Build 'ubuntu-ci' arguments.
        changes_path = testing.get_test_file_path(
            'foobar_0.1-1_source.changes')
        cli_path = os.path.join(HERE, '../cli/ubuntu-ci')
        args = (
            'python',
            cli_path,
            '-u', ci_url,
            'create_ticket',
            '-t', title,
            '-d', 'This is a cool new feature',
            '-b', '1234',
            '-o', 'someone@example.com',
            '-a', 'foo',
            '-s', changes_path,
        )
        proc = subprocess.Popen(
            args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
        out, err = proc.communicate()
        self.assertEqual(
            0, proc.returncode, '{} failed:\n{}'.format(args, out))
        # The CLI output ends up with the following line:
        # '...Your ticket number is 5.'
        ticket_id = re.search('Your ticket number is (.*)\.$', out).group(1)
        return ticket_id

    def test_create_ticket_via_cli(self):
        # Create a testing ticket using CLI and check how it was stored
        # via the Ticket-System API.
        ci_url = 'http://{}'.format(
            self.get_ip_and_port('ci-airline-webui-apache')[0])
        ticket_title = 'Bazinga!'
        ticket_id = self.create_new_ticket(ticket_title, ci_url)

        url_spec = 'http://{}/api/v1/ticket/%s/' % ticket_id
        resp, content = self.get_server_status_and_content(url_spec)
        content = json.loads(content)

        self.assertEqual('200', resp['status'])
        self.assertEqual(ticket_title, content['title'])

    def test_webui_ticket_list_page(self):
        # Test that the WebUI main page shows up.
        url = 'http://{}/'
        resp, content = self.get_server_status_and_content(url)

        self.assertEqual(resp['status'], '200')
        self.assertIn('<a href="/" class="first active">Tickets</a>', content)


if __name__ == "__main__":
    unittest.main()