~allanlesage/uci-engine/coverage-extractor

« back to all changes in this revision

Viewing changes to tests/test_ppacreator.py

Merge with trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
# Ubuntu Continuous Integration Engine
 
3
# Copyright 2014 Canonical Ltd.
 
4
 
 
5
# This program is free software: you can redistribute it and/or modify it
 
6
# under the terms of the GNU Affero General Public License version 3, as
 
7
# published by the Free Software Foundation.
 
8
 
 
9
# This program is distributed in the hope that it will be useful, but
 
10
# WITHOUT ANY WARRANTY; without even the implied warranties of
 
11
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 
12
# PURPOSE.  See the GNU Affero General Public License for more details.
 
13
 
 
14
# You should have received a copy of the GNU Affero General Public License
 
15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
16
 
 
17
import json
 
18
import uuid
 
19
import unittest
 
20
import deployers
 
21
import deploy
 
22
 
 
23
from ci_utils import amqp_utils
 
24
from ucitests import (
 
25
    assertions,
 
26
    fixtures,
 
27
)
 
28
 
 
29
RABBIT_SERVICE = 'ci-airline-rabbit'
 
30
 
 
31
 
 
32
def run_rabbit_cmd(args):
 
33
    deployers.juju_run(RABBIT_SERVICE,
 
34
                       'sudo rabbitmqctl {}'.format(' '.join(args)))
 
35
 
 
36
 
 
37
def create_rabbit_user(user_name, password=None):
 
38
    if password is None:
 
39
        password = user_name
 
40
    run_rabbit_cmd(['add_user', user_name, password])
 
41
    run_rabbit_cmd(['set_permissions', '-p', '/', user_name, '".*" ".*" ".*"'])
 
42
 
 
43
 
 
44
def delete_rabbit_user(user_name):
 
45
    run_rabbit_cmd(['delete_user', user_name])
 
46
 
 
47
 
 
48
class TestPPACreator(deployers.DeployerTest):
 
49
    """Integration tests for ppacreator service run on a juju deployment"""
 
50
 
 
51
    def setUp(self):
 
52
        super(TestPPACreator, self).setUp()
 
53
        status = deploy.juju_status()
 
54
        units = status['services']['ci-airline-rabbit']['units']
 
55
        self.unit = 'ci-airline-rabbit/0'
 
56
        self.public_ip = units[self.unit]['public-address']
 
57
        # Expose rabbit service for this test.
 
58
        deployers.juju_expose(RABBIT_SERVICE)
 
59
        self.addCleanup(deployers.juju_expose, RABBIT_SERVICE, False)
 
60
 
 
61
        class AmqpConfig(object):
 
62
 
 
63
            def __init__(inner):
 
64
                inner.AMQP_HOST = self.public_ip
 
65
                inner.AMQP_VHOST = '/'
 
66
                inner.AMQP_USER = 'tester'
 
67
                inner.AMQP_PASSWORD = 's3cr3t'
 
68
        fixtures.patch(self, amqp_utils, 'get_config', AmqpConfig)
 
69
        create_rabbit_user('tester', 's3cr3t')
 
70
        self.addCleanup(delete_rabbit_user, 'tester')
 
71
 
 
72
    def drain_queue(self, queue_name):
 
73
        self.messages = []
 
74
 
 
75
        def on_message(msg):
 
76
            body = json.loads(msg.body)
 
77
            self.messages.append(body)
 
78
            msg.channel.basic_ack(msg.delivery_tag)
 
79
            if body.get('exit'):
 
80
                # this will kick us out of the run-forever worker loop, relying
 
81
                # on the worker last message setting 'exit'
 
82
                msg.channel.basic_cancel(msg.consumer_tag)
 
83
 
 
84
        conf = amqp_utils.get_config()
 
85
        # Process all messages and delete the queue when done
 
86
        amqp_utils.process_queue(conf, queue_name, on_message, delete=True)
 
87
        return self.messages
 
88
 
 
89
    def assertStatusMsg(self, expected, msg):
 
90
        self.assertEqual('STATUS', msg['state'])
 
91
        without_state = msg.copy()
 
92
        del without_state['state']
 
93
        self.assertDictEqual(expected, without_state)
 
94
 
 
95
    def test_worker_running(self):
 
96
        """Ensure the ppacreator worker is deployed and running."""
 
97
        self.assert_job_running('ci-airline-ppa-creator-worker')
 
98
 
 
99
    def test_create_ppa(self):
 
100
        """Ensure that ppa's can be created or their name can
 
101
           be retrieved if they are already existing"""
 
102
        unique_id = uuid.uuid1()
 
103
        progress_queue_name = 'progress-trigger'
 
104
        params = dict(
 
105
            name='ppacreator-inttest-{}'.format(unique_id),
 
106
            displayname='ppacreator-inttest-{}'.format(unique_id),
 
107
            description='int-test-ppa-description',
 
108
            progress_trigger=progress_queue_name,
 
109
            ticket_id='int-test-ppa-ticket-id',
 
110
            architectures=('all'),
 
111
            series='precise')
 
112
 
 
113
        amqp_utils.send(amqp_utils.PPA_CREATOR_QUEUE, json.dumps(params),
 
114
                        raise_errors=True)
 
115
        msgs = self.drain_queue(progress_queue_name)
 
116
        assertions.assertLength(self, 3, msgs)
 
117
        self.assertStatusMsg({u'message': u'Creating PPA {} for ticket, '
 
118
                              'int-test-ppa-ticket-id'.format(params['name'])},
 
119
                             msgs[1])
 
120
 
 
121
        final = msgs[2]
 
122
        self.assertEqual('COMPLETED', final['state'])
 
123
        self.assertEqual('PASSED', final['result'])
 
124
 
 
125
if __name__ == "__main__":
 
126
    unittest.main()