~ursinha/ubuntu-ci-services-itself/private-only-is-env-variable

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
#!/usr/bin/env python
# Ubuntu CI 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/>.

from ci_utils import (
    amqp_utils,
    amqp_worker,
)
import tstrun
from tstrun import testbed


class TestRunnerWorker(amqp_worker.AMQPWorker):

    def __init__(self):
        super(TestRunnerWorker, self).__init__('test_runner')
        self.data_store = None

    def save_subunit(self, log, package, stream, retval):
        # make this exception-safe since we can already report pass/fail
        # with or without this file
        try:
            log.info('saving subunit results for {}'.format(package))
            name = 'test-runner.{}-subunit-stream'.format(package)
            url = self.data_store.put_file(name, stream, 'text/plain')
            retval.setdefault('artifacts', []).append({
                'name': name,
                'reference': url,
                'type': 'RESULTS',
            })
        except:
            log.exception(
                'unable to upload subunit result for {}'.format(package))

    def save_testbed_console(self, log, test_bed, retval):
        # make this exception-safe since we can already report pass/fail
        # with or without this file
        try:
            log.info('saving testbed console}')
            name = 'test-runner.testbed-cloud-init.log'
            console = test_bed.get_cloud_init_console()
            url = self.data_store.put_file(name, console, 'text/plain')
            retval.setdefault('artifacts', []).append({
                'name': name,
                'reference': url,
                'type': 'LOGS',
            })
        except:
            log.exception('unable to upload testbed console')

    def handle_request(self, log, params):
        progress_queue = params['progress_trigger']
        image_id = params['image_id']
        package_list = params['package_list']

        def status_cb(msg):
            log.info(msg)
            amqp_utils.progress_update(progress_queue, {'message': msg})

        self.data_store = self._create_data_store(params['ticket_id'])
        # The tests will succeed unless they fail ;)
        notify = amqp_utils.progress_completed
        results = {}
        test_bed = None
        flavors = tstrun.get_auth_config()['tr_flavors']
        try:
            test_bed = testbed.TestBed('testbed-{}'.format(progress_queue),
                                       flavors, image_id, status_cb)
            test_bed.setup()
            for package in package_list:
                if params.get('cancelled'):
                    log.error('request has been cancelled, exiting')
                    return amqp_utils.progress_failed, results
                status_cb('testing %s' % package)
                # uci-vms shell adt-run ...  --- adt-virt-null
                return_code, subunit_stream = test_bed.run_test(package)
                # 0 is success, 8 is skipped and considered a success
                if return_code not in (0, 8):
                    # At least one test failed
                    notify = amqp_utils.progress_failed
                self.save_subunit(log, package, subunit_stream, results)
            return notify, results
        finally:
            if test_bed:
                self.save_testbed_console(log, test_bed, results)
                test_bed.teardown()


if __name__ == '__main__':
    TestRunnerWorker().main(tstrun.internal_rabbit_queue)