1#!/usr/bin/python 2# 3# Copyright (c) 2013 The Chromium OS Authors. All rights reserved. 4# Use of this source code is governed by a BSD-style license that can be 5# found in the LICENSE file. 6 7import datetime, unittest 8 9import mox 10 11import common 12# This must come before the import of utils in order to use the in memory 13# database. 14from autotest_lib.frontend import setup_django_readonly_environment 15from autotest_lib.frontend import setup_test_environment 16from autotest_lib.frontend.health import utils 17from autotest_lib.frontend.tko import models 18from django import test 19 20ERROR_STATUS = models.Status(status_idx=2, word='ERROR') 21ABORT_STATUS = models.Status(status_idx=3, word='ABORT') 22FAIL_STATUS = models.Status(status_idx=4, word='FAIL') 23WARN_STATUS = models.Status(status_idx=5, word='WARN') 24GOOD_STATUS = models.Status(status_idx=6, word='GOOD') 25ALERT_STATUS = models.Status(status_idx=7, word='ALERT') 26 27 28def add_statuses(): 29 """ 30 Save the statuses to the in-memory database. 31 32 These normally exist in the database and the code expects them. However, the 33 normal test database setup does not do this for us. 34 """ 35 ERROR_STATUS.save() 36 ABORT_STATUS.save() 37 FAIL_STATUS.save() 38 WARN_STATUS.save() 39 GOOD_STATUS.save() 40 ALERT_STATUS.save() 41 42 43class GetLastPassTimesTests(mox.MoxTestBase, test.TestCase): 44 """Tests the get_last_pass_times function.""" 45 46 def setUp(self): 47 super(GetLastPassTimesTests, self).setUp() 48 setup_test_environment.set_up() 49 add_statuses() 50 51 52 def tearDown(self): 53 setup_test_environment.tear_down() 54 super(GetLastPassTimesTests, self).tearDown() 55 56 57 def test_return_most_recent_pass(self): 58 """The last time a test passed should be returned.""" 59 # To add a test entry to the database, the test object has to 60 # be instantiated with various other model instances. We give these 61 # instances dummy id values. 62 job = models.Job(job_idx=1) 63 kernel = models.Kernel(kernel_idx=1) 64 machine = models.Machine(machine_idx=1) 65 66 early_pass = models.Test(job=job, status=GOOD_STATUS, 67 kernel=kernel, machine=machine, 68 test='test', 69 started_time=datetime.datetime(2012, 1, 1)) 70 early_pass.save() 71 late_pass = models.Test(job=job, status=GOOD_STATUS, 72 kernel=kernel, machine=machine, 73 test='test', 74 started_time=datetime.datetime(2012, 1, 2)) 75 late_pass.save() 76 77 results = utils.get_last_pass_times() 78 79 self.assertEquals(results, {'test': datetime.datetime(2012, 1, 2)}) 80 81 82 def test_only_return_passing_tests(self): 83 """Tests that only tests that have passed at some point are returned.""" 84 job = models.Job(job_idx=1) 85 kernel = models.Kernel(kernel_idx=1) 86 machine = models.Machine(machine_idx=1) 87 88 passing_test = models.Test(job=job, status=GOOD_STATUS, 89 kernel=kernel, machine=machine, 90 test='passing_test', 91 started_time=datetime.datetime(2012, 1, 1)) 92 passing_test.save() 93 failing_test = models.Test(job=job, status=FAIL_STATUS, 94 kernel=kernel, machine=machine, 95 test='failing_test', 96 started_time=datetime.datetime(2012, 1, 1)) 97 failing_test.save() 98 99 results = utils.get_last_pass_times() 100 101 self.assertEquals(results, 102 {'passing_test': datetime.datetime(2012, 1, 1)}) 103 104 105 def test_return_all_passing_tests(self): 106 """This function returns all tests that passed at least once.""" 107 job = models.Job(job_idx=1) 108 kernel = models.Kernel(kernel_idx=1) 109 machine = models.Machine(machine_idx=1) 110 111 test1 = models.Test(job=job, status=GOOD_STATUS, 112 kernel=kernel, machine=machine, 113 test='test1', 114 started_time=datetime.datetime(2012, 1, 1)) 115 test1.save() 116 test2 = models.Test(job=job, status=GOOD_STATUS, 117 kernel=kernel, machine=machine, 118 test='test2', 119 started_time=datetime.datetime(2012, 1, 2)) 120 test2.save() 121 122 results = utils.get_last_pass_times() 123 124 self.assertEquals(results, {'test1': datetime.datetime(2012, 1, 1), 125 'test2': datetime.datetime(2012, 1, 2)}) 126 127 128class GetLastFailTimesTests(mox.MoxTestBase, test.TestCase): 129 """Tests the get_last_fail_times function.""" 130 131 def setUp(self): 132 super(GetLastFailTimesTests, self).setUp() 133 setup_test_environment.set_up() 134 add_statuses() 135 136 137 def tearDown(self): 138 setup_test_environment.tear_down() 139 super(GetLastFailTimesTests, self).tearDown() 140 141 142 def test_return_most_recent_fail(self): 143 """The last time a test failed should be returned.""" 144 # To add a test entry to the database, the test object has to 145 # be instantiated with various other model instances. We give these 146 # instances dummy id values. 147 job = models.Job(job_idx=1) 148 kernel = models.Kernel(kernel_idx=1) 149 machine = models.Machine(machine_idx=1) 150 151 early_fail = models.Test(job=job, status=FAIL_STATUS, 152 kernel=kernel, machine=machine, 153 test='test', 154 started_time=datetime.datetime(2012, 1, 1)) 155 early_fail.save() 156 late_fail = models.Test(job=job, status=FAIL_STATUS, 157 kernel=kernel, machine=machine, 158 test='test', 159 started_time=datetime.datetime(2012, 1, 2)) 160 late_fail.save() 161 162 results = utils.get_last_fail_times() 163 164 self.assertEquals(results, {'test': datetime.datetime(2012, 1, 2)}) 165 166 167 def test_does_not_return_passing_tests(self): 168 """Test that passing test entries are not included.""" 169 job = models.Job(job_idx=1) 170 kernel = models.Kernel(kernel_idx=1) 171 machine = models.Machine(machine_idx=1) 172 173 passing_test = models.Test(job=job, status=GOOD_STATUS, 174 kernel=kernel, machine=machine, 175 test='passing_test', 176 started_time=datetime.datetime(2012, 1, 1)) 177 passing_test.save() 178 failing_test = models.Test(job=job, status=FAIL_STATUS, 179 kernel=kernel, machine=machine, 180 test='failing_test', 181 started_time=datetime.datetime(2012, 1, 1)) 182 failing_test.save() 183 184 results = utils.get_last_fail_times() 185 186 self.assertEquals(results, 187 {'failing_test': datetime.datetime(2012, 1, 1)}) 188 189 190 def test_return_all_failing_tests(self): 191 """This function returns all tests that failed at least once.""" 192 job = models.Job(job_idx=1) 193 kernel = models.Kernel(kernel_idx=1) 194 machine = models.Machine(machine_idx=1) 195 196 test1 = models.Test(job=job, status=FAIL_STATUS, 197 kernel=kernel, machine=machine, 198 test='test1', 199 started_time=datetime.datetime(2012, 1, 1)) 200 test1.save() 201 test2 = models.Test(job=job, status=FAIL_STATUS, 202 kernel=kernel, machine=machine, 203 test='test2', 204 started_time=datetime.datetime(2012, 1, 2)) 205 test2.save() 206 207 results = utils.get_last_fail_times() 208 209 self.assertEquals(results, {'test1': datetime.datetime(2012, 1, 1), 210 'test2': datetime.datetime(2012, 1, 2)}) 211 212 213 def test_returns_treats_error_status_as_failure(self): 214 """Error statuses should count as a failure.""" 215 job = models.Job(job_idx=1) 216 kernel = models.Kernel(kernel_idx=1) 217 machine = models.Machine(machine_idx=1) 218 219 test = models.Test(job=job, status=ERROR_STATUS, 220 kernel=kernel, machine=machine, 221 test='error', 222 started_time=datetime.datetime(2012, 1, 1)) 223 test.save() 224 225 results = utils.get_last_fail_times() 226 227 self.assertEquals(results, {'error': datetime.datetime(2012, 1, 1)}) 228 229 230 def test_returns_treats_abort_status_as_failure(self): 231 """ 232 Abort statuses should count as failures. 233 234 This should be changed once Abort only represents user caused aborts. 235 See issue crbug.com/188217. 236 """ 237 job = models.Job(job_idx=1) 238 kernel = models.Kernel(kernel_idx=1) 239 machine = models.Machine(machine_idx=1) 240 241 test = models.Test(job=job, status=ABORT_STATUS, 242 kernel=kernel, machine=machine, 243 test='abort', 244 started_time=datetime.datetime(2012, 1, 1)) 245 test.save() 246 247 results = utils.get_last_fail_times() 248 249 self.assertEquals(results, {'abort': datetime.datetime(2012, 1, 1)}) 250 251 252 def test_returns_treats_warn_status_as_failure(self): 253 """Warn statuses should count as failures.""" 254 job = models.Job(job_idx=1) 255 kernel = models.Kernel(kernel_idx=1) 256 machine = models.Machine(machine_idx=1) 257 258 test = models.Test(job=job, status=WARN_STATUS, 259 kernel=kernel, machine=machine, 260 test='warn', 261 started_time=datetime.datetime(2012, 1, 1)) 262 test.save() 263 264 results = utils.get_last_fail_times() 265 266 self.assertEquals(results, {'warn': datetime.datetime(2012, 1, 1)}) 267 268 269 def test_returns_treats_alert_status_as_failure(self): 270 """Alert statuses should count as failures.""" 271 job = models.Job(job_idx=1) 272 kernel = models.Kernel(kernel_idx=1) 273 machine = models.Machine(machine_idx=1) 274 275 test = models.Test(job=job, status=ALERT_STATUS, 276 kernel=kernel, machine=machine, 277 test='alert', 278 started_time=datetime.datetime(2012, 1, 1)) 279 test.save() 280 281 results = utils.get_last_fail_times() 282 283 self.assertEquals(results, {'alert': datetime.datetime(2012, 1, 1)}) 284 285 286if __name__ == '__main__': 287 unittest.main() 288