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

« back to all changes in this revision

Viewing changes to test_runner/tstrun/run_worker.py

  • Committer: Ursula Junque (Ursinha)
  • Date: 2014-03-14 21:40:47 UTC
  • Revision ID: ursinha@canonical.com-20140314214047-oxu080435bqw0mor
Fixing tests to use right variable; Making it explicit the user choice for privacy

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
# Ubuntu CI 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
from ci_utils import (
 
18
    amqp_utils,
 
19
    amqp_worker,
 
20
)
 
21
import tstrun
 
22
from tstrun import testbed
 
23
 
 
24
 
 
25
class TestRunnerWorker(amqp_worker.AMQPWorker):
 
26
 
 
27
    def __init__(self):
 
28
        super(TestRunnerWorker, self).__init__('test_runner')
 
29
        self.data_store = None
 
30
 
 
31
    def save_subunit(self, log, package, stream, retval):
 
32
        # make this exception-safe since we can already report pass/fail
 
33
        # with or without this file
 
34
        try:
 
35
            log.info('saving subunit results for {}'.format(package))
 
36
            name = 'test-runner.{}-subunit-stream'.format(package)
 
37
            url = self.data_store.put_file(name, stream, 'text/plain')
 
38
            retval.setdefault('artifacts', []).append({
 
39
                'name': name,
 
40
                'reference': url,
 
41
                'type': 'RESULTS',
 
42
            })
 
43
        except:
 
44
            log.exception(
 
45
                'unable to upload subunit result for {}'.format(package))
 
46
 
 
47
    def save_testbed_console(self, log, test_bed, retval):
 
48
        # make this exception-safe since we can already report pass/fail
 
49
        # with or without this file
 
50
        try:
 
51
            log.info('saving testbed console}')
 
52
            name = 'test-runner.testbed-cloud-init.log'
 
53
            console = test_bed.get_cloud_init_console()
 
54
            url = self.data_store.put_file(name, console, 'text/plain')
 
55
            retval.setdefault('artifacts', []).append({
 
56
                'name': name,
 
57
                'reference': url,
 
58
                'type': 'LOGS',
 
59
            })
 
60
        except:
 
61
            log.exception('unable to upload testbed console')
 
62
 
 
63
    def handle_request(self, log, params):
 
64
        progress_queue = params['progress_trigger']
 
65
        image_id = params['image_id']
 
66
        package_list = params['package_list']
 
67
 
 
68
        def status_cb(msg):
 
69
            log.info(msg)
 
70
            amqp_utils.progress_update(progress_queue, {'message': msg})
 
71
 
 
72
        self.data_store = self._create_data_store(params['ticket_id'])
 
73
        # The tests will succeed unless they fail ;)
 
74
        notify = amqp_utils.progress_completed
 
75
        results = {}
 
76
        test_bed = None
 
77
        flavors = tstrun.get_auth_config()['tr_flavors']
 
78
        try:
 
79
            test_bed = testbed.TestBed('testbed-{}'.format(progress_queue),
 
80
                                       flavors, image_id, status_cb)
 
81
            test_bed.setup()
 
82
            for package in package_list:
 
83
                if params.get('cancelled'):
 
84
                    log.error('request has been cancelled, exiting')
 
85
                    return amqp_utils.progress_failed, results
 
86
                status_cb('testing %s' % package)
 
87
                # uci-vms shell adt-run ...  --- adt-virt-null
 
88
                return_code, subunit_stream = test_bed.run_test(package)
 
89
                # 0 is success, 8 is skipped and considered a success
 
90
                if return_code not in (0, 8):
 
91
                    # At least one test failed
 
92
                    notify = amqp_utils.progress_failed
 
93
                self.save_subunit(log, package, subunit_stream, results)
 
94
            return notify, results
 
95
        finally:
 
96
            if test_bed:
 
97
                self.save_testbed_console(log, test_bed, results)
 
98
                test_bed.teardown()
 
99
 
 
100
 
 
101
if __name__ == '__main__':
 
102
    TestRunnerWorker().main(tstrun.internal_rabbit_queue)