summaryrefslogtreecommitdiff
path: root/tests/test_shell_task.py
blob: 407e335f2351159b2b77a3bae0186d0a2fcb991c (plain)
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# Copyright 2014 Rackspace Australia
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import base
import fakes
import json
import logging
import mock
import os
import uuid

from turbo_hipster.lib.models import ShellTask, Task


class TestTaskRunner(base.TestWithGearman):
    log = logging.getLogger("TestTaskRunner")

    def test_simple_job_passes(self):
        self.start_server()
        zuul = fakes.FakeZuul(self.config['zuul_server']['gearman_host'],
                              self.config['zuul_server']['gearman_port'])

        job_uuid = str(uuid.uuid1())[:8]
        data_req = {
            'ZUUL_UUID': job_uuid,
            'ZUUL_PROJECT': 'stackforge/turbo-hipster',
            'ZUUL_PIPELINE': 'check',
            'ZUUL_URL': 'git://git.openstack.org/',
            'BRANCH': 'master',
            'BASE_LOG_PATH': '56/123456/8',
            'LOG_PATH': '56/123456/8/check/job_name/%s' % job_uuid
        }

        zuul.submit_job('build:do_something_shelly', data_req)
        zuul.wait_for_completion()

        last_data = json.loads(zuul.job.data[-1])
        self.log.debug(last_data)

        self.assertTrue(zuul.job.complete)
        self.assertFalse(zuul.job.failure)
        self.assertEqual("SUCCESS", last_data['result'])

        task_output_file = open(os.path.join(
            self.config['publish_logs']['path'], data_req['LOG_PATH'],
            'task_output.log'
        ))

        self.assertIn("Step 1: Setup environment", task_output_file.readline())

        git_prep_file = open(os.path.join(
            self.config['publish_logs']['path'], data_req['LOG_PATH'],
            'git_prep.log'
        ))

        self.assertIn("gerrit-git-prep.sh", git_prep_file.readline())

        shell_output_file = open(os.path.join(
            self.config['publish_logs']['path'], data_req['LOG_PATH'],
            'shell_output.log'
        ))

        self.assertIn("ls -lah", shell_output_file.readline())

    def test_simple_job_fails(self):
        # Test when the script fails
        self.start_server()
        zuul = fakes.FakeZuul(self.config['zuul_server']['gearman_host'],
                              self.config['zuul_server']['gearman_port'])

        job_uuid = str(uuid.uuid1())[:8]
        data_req = {
            'ZUUL_UUID': job_uuid,
            'ZUUL_PROJECT': 'stackforge/turbo-hipster',
            'ZUUL_PIPELINE': 'check',
            'ZUUL_URL': 'git://git.openstack.org/',
            'BRANCH': 'master',
            'BASE_LOG_PATH': '56/123456/8',
            'LOG_PATH': '56/123456/8/check/job_name/%s' % job_uuid
        }

        # Modify the job to fail. The git_path, job_working_dir and unqiue_id
        # are all passed to the shell script. If we 'ls unique_id' it'll fail
        # since it doesn't exist.
        self.config['jobs'][0]['shell_script'] = 'ls -lah'

        zuul.submit_job('build:do_something_shelly', data_req)
        zuul.wait_for_completion()

        last_data = json.loads(zuul.job.data[-1])
        self.log.debug(last_data)

        self.assertTrue(zuul.job.complete)
        self.assertTrue(zuul.job.failure)
        self.assertEqual("Return code from test script was non-zero (2)",
                         last_data['result'])

        task_output_file = open(os.path.join(
            self.config['publish_logs']['path'], data_req['LOG_PATH'],
            'task_output.log'
        ))

        self.assertIn("Step 1: Setup environment", task_output_file.readline())

        git_prep_file = open(os.path.join(
            self.config['publish_logs']['path'], data_req['LOG_PATH'],
            'git_prep.log'
        ))

        self.assertIn("gerrit-git-prep.sh", git_prep_file.readline())

        shell_output_file = open(os.path.join(
            self.config['publish_logs']['path'], data_req['LOG_PATH'],
            'shell_output.log'
        ))

        self.assertIn("ls -lah", shell_output_file.readline())

    @mock.patch.object(ShellTask, '_parse_and_check_results')
    def test_logs_uploaded_during_failure(self,
                                          mocked_parse_and_check_results):
        # When turbo-hipster itself fails (eg analysing results) it should
        # still upload the python logging log if it can

        def side_effect():
            raise Exception('check results failed!')

        #ShellTask._parse_and_check_results = _fake_parse_and_check_results
        mocked_parse_and_check_results.side_effect = side_effect

        self.start_server()
        zuul = fakes.FakeZuul(self.config['zuul_server']['gearman_host'],
                              self.config['zuul_server']['gearman_port'])

        job_uuid = str(uuid.uuid1())[:8]
        data_req = {
            'ZUUL_UUID': job_uuid,
            'ZUUL_PROJECT': 'stackforge/turbo-hipster',
            'ZUUL_PIPELINE': 'check',
            'ZUUL_URL': 'git://git.openstack.org/',
            'BRANCH': 'master',
            'BASE_LOG_PATH': '56/123456/8',
            'LOG_PATH': '56/123456/8/check/job_name/%s' % job_uuid
        }

        zuul.submit_job('build:do_something_shelly', data_req)
        zuul.wait_for_completion()

        last_data = json.loads(zuul.job.data[-1])
        self.log.debug(last_data)

        self.assertTrue(zuul.job.complete)
        self.assertTrue(zuul.job.failure)
        self.assertEqual("FAILURE running the job\n"
                         "Exception: check results failed!",
                         last_data['result'])

        git_prep_file = open(os.path.join(
            self.config['publish_logs']['path'], data_req['LOG_PATH'],
            'git_prep.log'
        ))

        self.assertIn("gerrit-git-prep.sh", git_prep_file.readline())

        shell_output_file = open(os.path.join(
            self.config['publish_logs']['path'], data_req['LOG_PATH'],
            'shell_output.log'
        ))

        self.assertIn("ls -lah", shell_output_file.readline())

        task_output_file = open(os.path.join(
            self.config['publish_logs']['path'], data_req['LOG_PATH'],
            'task_output.log'
        ))

        task_output_lines = task_output_file.readlines()
        self.assertIn("Step 1: Setup environment", task_output_lines[0])
        self.assertIn("Something failed running the job!",
                      task_output_lines[6])
        self.assertIn("Exception: check results failed!",
                      task_output_lines[len(task_output_lines) - 1])

    @mock.patch.object(Task, '_upload_results')
    def test_exception_when_uploading_fails(self, mocked_upload_results):

        def side_effect():
            raise Exception('uploading results failed!')

        mocked_upload_results.side_effect = side_effect

        self.start_server()
        zuul = fakes.FakeZuul(self.config['zuul_server']['gearman_host'],
                              self.config['zuul_server']['gearman_port'])

        job_uuid = str(uuid.uuid1())[:8]
        data_req = {
            'ZUUL_UUID': job_uuid,
            'ZUUL_PROJECT': 'stackforge/turbo-hipster',
            'ZUUL_PIPELINE': 'check',
            'ZUUL_URL': 'git://git.openstack.org/',
            'BRANCH': 'master',
            'BASE_LOG_PATH': '56/123456/8',
            'LOG_PATH': '56/123456/8/check/job_name/%s' % job_uuid
        }

        zuul.submit_job('build:do_something_shelly', data_req)
        zuul.wait_for_completion()

        last_data = json.loads(zuul.job.data[-1])
        self.log.debug(last_data)

        self.assertTrue(zuul.job.complete)
        self.assertTrue(zuul.job.failure)
        self.assertEqual("FAILURE during cleanup and log upload\n"
                         "Exception: uploading results failed!",
                         last_data['result'])

    def test_failure_during_setup(self):
        pass