blob: eb60552a889063e61aeed931def6617e9f480ee3 [file] [log] [blame]
Joshua Heskethd5d7a212014-10-29 17:42:59 +11001# Copyright 2014 Rackspace Australia
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may
4# not use this file except in compliance with the License. You may obtain
5# a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations
13# under the License.
14
15import base
16import fakes
17import json
18import logging
19import mock
20import os
21import uuid
22
23from turbo_hipster.lib.models import ShellTask, Task
24
25
26class TestTaskRunner(base.TestWithGearman):
27 log = logging.getLogger("TestTaskRunner")
28
29 def test_simple_job_passes(self):
30 self.start_server()
31 zuul = fakes.FakeZuul(self.config['zuul_server']['gearman_host'],
32 self.config['zuul_server']['gearman_port'])
33
34 job_uuid = str(uuid.uuid1())[:8]
35 data_req = {
36 'ZUUL_UUID': job_uuid,
37 'ZUUL_PROJECT': 'stackforge/turbo-hipster',
38 'ZUUL_PIPELINE': 'check',
Ian Wienandaa710662019-03-24 20:36:22 +000039 'ZUUL_URL': 'https://git.openstack.org/',
Joshua Heskethd5d7a212014-10-29 17:42:59 +110040 'BRANCH': 'master',
41 'BASE_LOG_PATH': '56/123456/8',
42 'LOG_PATH': '56/123456/8/check/job_name/%s' % job_uuid
43 }
44
45 zuul.submit_job('build:do_something_shelly', data_req)
46 zuul.wait_for_completion()
47
48 last_data = json.loads(zuul.job.data[-1])
49 self.log.debug(last_data)
50
51 self.assertTrue(zuul.job.complete)
52 self.assertFalse(zuul.job.failure)
53 self.assertEqual("SUCCESS", last_data['result'])
54
55 task_output_file = open(os.path.join(
56 self.config['publish_logs']['path'], data_req['LOG_PATH'],
57 'task_output.log'
58 ))
59
60 self.assertIn("Step 1: Setup environment", task_output_file.readline())
61
62 git_prep_file = open(os.path.join(
63 self.config['publish_logs']['path'], data_req['LOG_PATH'],
64 'git_prep.log'
65 ))
66
67 self.assertIn("gerrit-git-prep.sh", git_prep_file.readline())
68
69 shell_output_file = open(os.path.join(
70 self.config['publish_logs']['path'], data_req['LOG_PATH'],
71 'shell_output.log'
72 ))
73
74 self.assertIn("ls -lah", shell_output_file.readline())
75
76 def test_simple_job_fails(self):
77 # Test when the script fails
78 self.start_server()
79 zuul = fakes.FakeZuul(self.config['zuul_server']['gearman_host'],
80 self.config['zuul_server']['gearman_port'])
81
82 job_uuid = str(uuid.uuid1())[:8]
83 data_req = {
84 'ZUUL_UUID': job_uuid,
85 'ZUUL_PROJECT': 'stackforge/turbo-hipster',
86 'ZUUL_PIPELINE': 'check',
Ian Wienandaa710662019-03-24 20:36:22 +000087 'ZUUL_URL': 'https://git.openstack.org/',
Joshua Heskethd5d7a212014-10-29 17:42:59 +110088 'BRANCH': 'master',
89 'BASE_LOG_PATH': '56/123456/8',
90 'LOG_PATH': '56/123456/8/check/job_name/%s' % job_uuid
91 }
92
93 # Modify the job to fail. The git_path, job_working_dir and unqiue_id
94 # are all passed to the shell script. If we 'ls unique_id' it'll fail
95 # since it doesn't exist.
Joshua Hesketh62721fb2014-12-02 13:22:10 +110096 self.config['jobs'][0]['shell_script'] = 'ls -lah'
Joshua Heskethd5d7a212014-10-29 17:42:59 +110097
98 zuul.submit_job('build:do_something_shelly', data_req)
99 zuul.wait_for_completion()
100
101 last_data = json.loads(zuul.job.data[-1])
102 self.log.debug(last_data)
103
104 self.assertTrue(zuul.job.complete)
105 self.assertTrue(zuul.job.failure)
106 self.assertEqual("Return code from test script was non-zero (2)",
107 last_data['result'])
108
109 task_output_file = open(os.path.join(
110 self.config['publish_logs']['path'], data_req['LOG_PATH'],
111 'task_output.log'
112 ))
113
114 self.assertIn("Step 1: Setup environment", task_output_file.readline())
115
116 git_prep_file = open(os.path.join(
117 self.config['publish_logs']['path'], data_req['LOG_PATH'],
118 'git_prep.log'
119 ))
120
121 self.assertIn("gerrit-git-prep.sh", git_prep_file.readline())
122
123 shell_output_file = open(os.path.join(
124 self.config['publish_logs']['path'], data_req['LOG_PATH'],
125 'shell_output.log'
126 ))
127
128 self.assertIn("ls -lah", shell_output_file.readline())
129
130 @mock.patch.object(ShellTask, '_parse_and_check_results')
131 def test_logs_uploaded_during_failure(self,
132 mocked_parse_and_check_results):
133 # When turbo-hipster itself fails (eg analysing results) it should
134 # still upload the python logging log if it can
135
136 def side_effect():
137 raise Exception('check results failed!')
138
Joshua Hesketha4b178d2015-06-04 14:13:31 +1000139 # ShellTask._parse_and_check_results = _fake_parse_and_check_results
Joshua Heskethd5d7a212014-10-29 17:42:59 +1100140 mocked_parse_and_check_results.side_effect = side_effect
141
142 self.start_server()
143 zuul = fakes.FakeZuul(self.config['zuul_server']['gearman_host'],
144 self.config['zuul_server']['gearman_port'])
145
146 job_uuid = str(uuid.uuid1())[:8]
147 data_req = {
148 'ZUUL_UUID': job_uuid,
149 'ZUUL_PROJECT': 'stackforge/turbo-hipster',
150 'ZUUL_PIPELINE': 'check',
Ian Wienandaa710662019-03-24 20:36:22 +0000151 'ZUUL_URL': 'https://git.openstack.org/',
Joshua Heskethd5d7a212014-10-29 17:42:59 +1100152 'BRANCH': 'master',
153 'BASE_LOG_PATH': '56/123456/8',
154 'LOG_PATH': '56/123456/8/check/job_name/%s' % job_uuid
155 }
156
157 zuul.submit_job('build:do_something_shelly', data_req)
158 zuul.wait_for_completion()
159
160 last_data = json.loads(zuul.job.data[-1])
161 self.log.debug(last_data)
162
163 self.assertTrue(zuul.job.complete)
164 self.assertTrue(zuul.job.failure)
165 self.assertEqual("FAILURE running the job\n"
166 "Exception: check results failed!",
167 last_data['result'])
168
169 git_prep_file = open(os.path.join(
170 self.config['publish_logs']['path'], data_req['LOG_PATH'],
171 'git_prep.log'
172 ))
173
174 self.assertIn("gerrit-git-prep.sh", git_prep_file.readline())
175
176 shell_output_file = open(os.path.join(
177 self.config['publish_logs']['path'], data_req['LOG_PATH'],
178 'shell_output.log'
179 ))
180
181 self.assertIn("ls -lah", shell_output_file.readline())
182
183 task_output_file = open(os.path.join(
184 self.config['publish_logs']['path'], data_req['LOG_PATH'],
185 'task_output.log'
186 ))
187
188 task_output_lines = task_output_file.readlines()
189 self.assertIn("Step 1: Setup environment", task_output_lines[0])
190 self.assertIn("Something failed running the job!",
Joshua Hesketh9b2c3122015-02-26 13:18:37 +1100191 task_output_lines[6])
Joshua Heskethd5d7a212014-10-29 17:42:59 +1100192 self.assertIn("Exception: check results failed!",
193 task_output_lines[len(task_output_lines) - 1])
194
195 @mock.patch.object(Task, '_upload_results')
196 def test_exception_when_uploading_fails(self, mocked_upload_results):
197
198 def side_effect():
199 raise Exception('uploading results failed!')
200
201 mocked_upload_results.side_effect = side_effect
202
203 self.start_server()
204 zuul = fakes.FakeZuul(self.config['zuul_server']['gearman_host'],
205 self.config['zuul_server']['gearman_port'])
206
207 job_uuid = str(uuid.uuid1())[:8]
208 data_req = {
209 'ZUUL_UUID': job_uuid,
210 'ZUUL_PROJECT': 'stackforge/turbo-hipster',
211 'ZUUL_PIPELINE': 'check',
Ian Wienandaa710662019-03-24 20:36:22 +0000212 'ZUUL_URL': 'https://git.openstack.org/',
Joshua Heskethd5d7a212014-10-29 17:42:59 +1100213 'BRANCH': 'master',
214 'BASE_LOG_PATH': '56/123456/8',
215 'LOG_PATH': '56/123456/8/check/job_name/%s' % job_uuid
216 }
217
218 zuul.submit_job('build:do_something_shelly', data_req)
219 zuul.wait_for_completion()
220
221 last_data = json.loads(zuul.job.data[-1])
222 self.log.debug(last_data)
223
224 self.assertTrue(zuul.job.complete)
225 self.assertTrue(zuul.job.failure)
226 self.assertEqual("FAILURE during cleanup and log upload\n"
227 "Exception: uploading results failed!",
228 last_data['result'])
229
230 def test_failure_during_setup(self):
231 pass