J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Copyright 2015 The Chromium OS Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
Prathmesh Prabhu | 58aede8 | 2017-11-09 13:34:25 -0800 | [diff] [blame] | 6 | import collections |
J. Richard Barnette | f683928 | 2015-06-01 16:00:35 -0700 | [diff] [blame] | 7 | import itertools |
J. Richard Barnette | f60a1ee | 2015-06-02 10:52:37 -0700 | [diff] [blame] | 8 | import logging |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 9 | import os |
| 10 | import unittest |
| 11 | |
| 12 | import common |
Aviv Keshet | 7ee9586 | 2016-08-30 15:18:27 -0700 | [diff] [blame] | 13 | from autotest_lib.server.lib import status_history |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 14 | from autotest_lib.site_utils import lab_inventory |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 15 | |
| 16 | |
xixuan | 12ce04f | 2016-03-10 13:16:30 -0800 | [diff] [blame] | 17 | class _FakeHost(object): |
| 18 | """Class to mock `Host` in _FakeHostHistory for testing.""" |
| 19 | |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 20 | def __init__(self, hostname): |
xixuan | 12ce04f | 2016-03-10 13:16:30 -0800 | [diff] [blame] | 21 | self.hostname = hostname |
| 22 | |
| 23 | |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 24 | class _FakeHostHistory(object): |
| 25 | """Class to mock `HostJobHistory` for testing.""" |
| 26 | |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 27 | def __init__(self, model, pool, status, hostname=''): |
| 28 | self.host_model = model |
| 29 | self.host_board = model + '_board' |
Prathmesh Prabhu | 58aede8 | 2017-11-09 13:34:25 -0800 | [diff] [blame] | 30 | self.host_pool = pool |
| 31 | self.status = status |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 32 | self.host = _FakeHost(hostname) |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 33 | |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 34 | def last_diagnosis(self): |
| 35 | """Return the recorded diagnosis.""" |
Prathmesh Prabhu | 58aede8 | 2017-11-09 13:34:25 -0800 | [diff] [blame] | 36 | return self.status, None |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 37 | |
| 38 | |
J. Richard Barnette | f683928 | 2015-06-01 16:00:35 -0700 | [diff] [blame] | 39 | class _FakeHostLocation(object): |
| 40 | """Class to mock `HostJobHistory` for location sorting.""" |
| 41 | |
| 42 | _HOSTNAME_FORMAT = 'chromeos%d-row%d-rack%d-host%d' |
| 43 | |
J. Richard Barnette | f683928 | 2015-06-01 16:00:35 -0700 | [diff] [blame] | 44 | def __init__(self, location): |
| 45 | self.hostname = self._HOSTNAME_FORMAT % location |
| 46 | |
J. Richard Barnette | f683928 | 2015-06-01 16:00:35 -0700 | [diff] [blame] | 47 | @property |
| 48 | def host(self): |
| 49 | """Return a fake host object with a hostname.""" |
| 50 | return self |
| 51 | |
| 52 | |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 53 | # Status values that may be returned by `HostJobHistory`. |
| 54 | # |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 55 | # These merely rename the corresponding values in `status_history` |
| 56 | # for convenience. |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 57 | |
| 58 | _WORKING = status_history.WORKING |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 59 | _UNUSED = status_history.UNUSED |
| 60 | _BROKEN = status_history.BROKEN |
| 61 | _UNKNOWN = status_history.UNKNOWN |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 62 | |
| 63 | |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 64 | class HostSetInventoryTestCase(unittest.TestCase): |
| 65 | """Unit tests for class `_HostSetInventory`. |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 66 | |
| 67 | Coverage is quite basic: mostly just enough to make sure every |
| 68 | function gets called, and to make sure that the counting knows |
| 69 | the difference between 0 and 1. |
| 70 | |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 71 | The testing also ensures that all known status values that can be |
| 72 | returned by `HostJobHistory` are counted as expected. |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 73 | """ |
| 74 | |
| 75 | def setUp(self): |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 76 | super(HostSetInventoryTestCase, self).setUp() |
| 77 | self.histories = lab_inventory._HostSetInventory() |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 78 | |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 79 | def _add_host(self, status): |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 80 | fake = _FakeHostHistory('zebra', lab_inventory.SPARE_POOL, status) |
Prathmesh Prabhu | 0ecbf32 | 2017-11-08 17:04:24 -0800 | [diff] [blame] | 81 | self.histories.record_host(fake) |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 82 | |
xixuan | 12ce04f | 2016-03-10 13:16:30 -0800 | [diff] [blame] | 83 | def _check_counts(self, working, broken, idle): |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 84 | """Check that pool counts match expectations. |
| 85 | |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 86 | Asserts that `get_working()`, `get_broken()`, and `get_idle()` |
| 87 | return the given expected values. Also assert that |
| 88 | `get_total()` is the sum of all counts. |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 89 | |
| 90 | @param working The expected total of working devices. |
| 91 | @param broken The expected total of broken devices. |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 92 | @param idle The expected total of idle devices. |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 93 | """ |
Prathmesh Prabhu | 0ecbf32 | 2017-11-08 17:04:24 -0800 | [diff] [blame] | 94 | self.assertEqual(self.histories.get_working(), working) |
| 95 | self.assertEqual(self.histories.get_broken(), broken) |
| 96 | self.assertEqual(self.histories.get_idle(), idle) |
| 97 | self.assertEqual(self.histories.get_total(), |
xixuan | 12ce04f | 2016-03-10 13:16:30 -0800 | [diff] [blame] | 98 | working + broken + idle) |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 99 | |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 100 | def test_empty(self): |
| 101 | """Test counts when there are no DUTs recorded.""" |
xixuan | 12ce04f | 2016-03-10 13:16:30 -0800 | [diff] [blame] | 102 | self._check_counts(0, 0, 0) |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 103 | |
xixuan | 12ce04f | 2016-03-10 13:16:30 -0800 | [diff] [blame] | 104 | def test_broken(self): |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 105 | """Test counting for broken DUTs.""" |
xixuan | 12ce04f | 2016-03-10 13:16:30 -0800 | [diff] [blame] | 106 | self._add_host(_BROKEN) |
| 107 | self._check_counts(0, 1, 0) |
| 108 | |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 109 | def test_working(self): |
| 110 | """Test counting for working DUTs.""" |
| 111 | self._add_host(_WORKING) |
| 112 | self._check_counts(1, 0, 0) |
| 113 | |
xixuan | 12ce04f | 2016-03-10 13:16:30 -0800 | [diff] [blame] | 114 | def test_idle(self): |
| 115 | """Testing counting for idle status values.""" |
| 116 | self._add_host(_UNUSED) |
| 117 | self._check_counts(0, 0, 1) |
| 118 | self._add_host(_UNKNOWN) |
| 119 | self._check_counts(0, 0, 2) |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 120 | |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 121 | def test_working_then_broken(self): |
| 122 | """Test counts after adding a working and then a broken DUT.""" |
| 123 | self._add_host(_WORKING) |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 124 | self._add_host(_BROKEN) |
xixuan | 12ce04f | 2016-03-10 13:16:30 -0800 | [diff] [blame] | 125 | self._check_counts(1, 1, 0) |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 126 | |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 127 | def test_broken_then_working(self): |
| 128 | """Test counts after adding a broken and then a working DUT.""" |
| 129 | self._add_host(_BROKEN) |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 130 | self._add_host(_WORKING) |
xixuan | 12ce04f | 2016-03-10 13:16:30 -0800 | [diff] [blame] | 131 | self._check_counts(1, 1, 0) |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 132 | |
| 133 | |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 134 | class PoolSetInventoryTestCase(unittest.TestCase): |
| 135 | """Unit tests for class `_PoolSetInventory`. |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 136 | |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 137 | Coverage is quite basic: just enough to make sure every function |
| 138 | gets called, and to make sure that the counting knows the difference |
| 139 | between 0 and 1. |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 140 | |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 141 | The tests make sure that both individual pool counts and totals are |
| 142 | counted correctly. |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 143 | """ |
| 144 | |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 145 | _POOL_SET = ['humpty', 'dumpty'] |
| 146 | |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 147 | def setUp(self): |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 148 | super(PoolSetInventoryTestCase, self).setUp() |
| 149 | self._pool_histories = lab_inventory._PoolSetInventory(self._POOL_SET) |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 150 | |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 151 | def _add_host(self, pool, status): |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 152 | fake = _FakeHostHistory('zebra', pool, status) |
| 153 | self._pool_histories.record_host(fake) |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 154 | |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 155 | def _check_all_counts(self, working, broken): |
| 156 | """Check that total counts for all pools match expectations. |
| 157 | |
| 158 | Checks that `get_working()` and `get_broken()` return the |
| 159 | given expected values when called without a pool specified. |
| 160 | Also check that `get_total()` is the sum of working and |
| 161 | broken devices. |
| 162 | |
| 163 | Additionally, call the various functions for all the pools |
| 164 | individually, and confirm that the totals across pools match |
| 165 | the given expectations. |
| 166 | |
| 167 | @param working The expected total of working devices. |
| 168 | @param broken The expected total of broken devices. |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 169 | """ |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 170 | self.assertEqual(self._pool_histories.get_working(), working) |
| 171 | self.assertEqual(self._pool_histories.get_broken(), broken) |
| 172 | self.assertEqual(self._pool_histories.get_total(), |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 173 | working + broken) |
| 174 | count_working = 0 |
| 175 | count_broken = 0 |
| 176 | count_total = 0 |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 177 | for pool in self._POOL_SET: |
| 178 | count_working += self._pool_histories.get_working(pool) |
| 179 | count_broken += self._pool_histories.get_broken(pool) |
| 180 | count_total += self._pool_histories.get_total(pool) |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 181 | self.assertEqual(count_working, working) |
| 182 | self.assertEqual(count_broken, broken) |
| 183 | self.assertEqual(count_total, working + broken) |
| 184 | |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 185 | def _check_pool_counts(self, pool, working, broken): |
| 186 | """Check that counts for a given pool match expectations. |
| 187 | |
| 188 | Checks that `get_working()` and `get_broken()` return the |
| 189 | given expected values for the given pool. Also check that |
| 190 | `get_total()` is the sum of working and broken devices. |
| 191 | |
| 192 | @param pool The pool to be checked. |
| 193 | @param working The expected total of working devices. |
| 194 | @param broken The expected total of broken devices. |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 195 | """ |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 196 | self.assertEqual(self._pool_histories.get_working(pool), |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 197 | working) |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 198 | self.assertEqual(self._pool_histories.get_broken(pool), |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 199 | broken) |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 200 | self.assertEqual(self._pool_histories.get_total(pool), |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 201 | working + broken) |
| 202 | |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 203 | def test_empty(self): |
| 204 | """Test counts when there are no DUTs recorded.""" |
| 205 | self._check_all_counts(0, 0) |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 206 | for pool in self._POOL_SET: |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 207 | self._check_pool_counts(pool, 0, 0) |
| 208 | |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 209 | def test_all_working_then_broken(self): |
| 210 | """Test counts after adding a working and then a broken DUT. |
| 211 | |
| 212 | For each pool, add first a working, then a broken DUT. After |
| 213 | each DUT is added, check counts to confirm the correct values. |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 214 | """ |
| 215 | working = 0 |
| 216 | broken = 0 |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 217 | for pool in self._POOL_SET: |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 218 | self._add_host(pool, _WORKING) |
| 219 | working += 1 |
| 220 | self._check_pool_counts(pool, 1, 0) |
| 221 | self._check_all_counts(working, broken) |
| 222 | self._add_host(pool, _BROKEN) |
| 223 | broken += 1 |
| 224 | self._check_pool_counts(pool, 1, 1) |
| 225 | self._check_all_counts(working, broken) |
| 226 | |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 227 | def test_all_broken_then_working(self): |
| 228 | """Test counts after adding a broken and then a working DUT. |
| 229 | |
| 230 | For each pool, add first a broken, then a working DUT. After |
| 231 | each DUT is added, check counts to confirm the correct values. |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 232 | """ |
| 233 | working = 0 |
| 234 | broken = 0 |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 235 | for pool in self._POOL_SET: |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 236 | self._add_host(pool, _BROKEN) |
| 237 | broken += 1 |
| 238 | self._check_pool_counts(pool, 0, 1) |
| 239 | self._check_all_counts(working, broken) |
| 240 | self._add_host(pool, _WORKING) |
| 241 | working += 1 |
| 242 | self._check_pool_counts(pool, 1, 1) |
| 243 | self._check_all_counts(working, broken) |
| 244 | |
| 245 | |
J. Richard Barnette | f683928 | 2015-06-01 16:00:35 -0700 | [diff] [blame] | 246 | class LocationSortTests(unittest.TestCase): |
| 247 | """Unit tests for `_sort_by_location()`.""" |
| 248 | |
| 249 | def setUp(self): |
| 250 | super(LocationSortTests, self).setUp() |
| 251 | |
J. Richard Barnette | f683928 | 2015-06-01 16:00:35 -0700 | [diff] [blame] | 252 | def _check_sorting(self, *locations): |
| 253 | """Test sorting a given list of locations. |
| 254 | |
| 255 | The input is an already ordered list of lists of tuples with |
| 256 | row, rack, and host numbers. The test converts the tuples |
| 257 | to hostnames, preserving the original ordering. Then it |
| 258 | flattens and scrambles the input, runs it through |
| 259 | `_sort_by_location()`, and asserts that the result matches |
| 260 | the original. |
J. Richard Barnette | f683928 | 2015-06-01 16:00:35 -0700 | [diff] [blame] | 261 | """ |
| 262 | lab = 0 |
| 263 | expected = [] |
| 264 | for tuples in locations: |
| 265 | lab += 1 |
| 266 | expected.append( |
| 267 | [_FakeHostLocation((lab,) + t) for t in tuples]) |
| 268 | scrambled = [e for e in itertools.chain(*expected)] |
| 269 | scrambled = [e for e in reversed(scrambled)] |
| 270 | actual = lab_inventory._sort_by_location(scrambled) |
| 271 | # The ordering of the labs in the output isn't guaranteed, |
| 272 | # so we can't compare `expected` and `actual` directly. |
| 273 | # Instead, we create a dictionary keyed on the first host in |
| 274 | # each lab, and compare the dictionaries. |
| 275 | self.assertEqual({l[0]: l for l in expected}, |
| 276 | {l[0]: l for l in actual}) |
| 277 | |
J. Richard Barnette | f683928 | 2015-06-01 16:00:35 -0700 | [diff] [blame] | 278 | def test_separate_labs(self): |
| 279 | """Test that sorting distinguishes labs.""" |
| 280 | self._check_sorting([(1, 1, 1)], [(1, 1, 1)], [(1, 1, 1)]) |
| 281 | |
J. Richard Barnette | f683928 | 2015-06-01 16:00:35 -0700 | [diff] [blame] | 282 | def test_separate_rows(self): |
| 283 | """Test for proper sorting when only rows are different.""" |
| 284 | self._check_sorting([(1, 1, 1), (9, 1, 1), (10, 1, 1)]) |
| 285 | |
J. Richard Barnette | f683928 | 2015-06-01 16:00:35 -0700 | [diff] [blame] | 286 | def test_separate_racks(self): |
| 287 | """Test for proper sorting when only racks are different.""" |
| 288 | self._check_sorting([(1, 1, 1), (1, 9, 1), (1, 10, 1)]) |
| 289 | |
J. Richard Barnette | f683928 | 2015-06-01 16:00:35 -0700 | [diff] [blame] | 290 | def test_separate_hosts(self): |
| 291 | """Test for proper sorting when only hosts are different.""" |
| 292 | self._check_sorting([(1, 1, 1), (1, 1, 9), (1, 1, 10)]) |
| 293 | |
J. Richard Barnette | f683928 | 2015-06-01 16:00:35 -0700 | [diff] [blame] | 294 | def test_diagonal(self): |
| 295 | """Test for proper sorting when all parts are different.""" |
| 296 | self._check_sorting([(1, 1, 2), (1, 2, 1), (2, 1, 1)]) |
| 297 | |
| 298 | |
| 299 | class InventoryScoringTests(unittest.TestCase): |
| 300 | """Unit tests for `_score_repair_set()`.""" |
| 301 | |
| 302 | def setUp(self): |
| 303 | super(InventoryScoringTests, self).setUp() |
| 304 | |
J. Richard Barnette | f683928 | 2015-06-01 16:00:35 -0700 | [diff] [blame] | 305 | def _make_buffer_counts(self, *counts): |
| 306 | """Create a dictionary suitable as `buffer_counts`. |
| 307 | |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 308 | @param counts List of tuples with model count data. |
J. Richard Barnette | f683928 | 2015-06-01 16:00:35 -0700 | [diff] [blame] | 309 | """ |
| 310 | self._buffer_counts = dict(counts) |
| 311 | |
J. Richard Barnette | f683928 | 2015-06-01 16:00:35 -0700 | [diff] [blame] | 312 | def _make_history_list(self, repair_counts): |
| 313 | """Create a list suitable as `repair_list`. |
| 314 | |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 315 | @param repair_counts List of (model, count) tuples. |
J. Richard Barnette | f683928 | 2015-06-01 16:00:35 -0700 | [diff] [blame] | 316 | """ |
Kevin Cheng | cf0ad2b | 2016-04-19 14:51:39 -0700 | [diff] [blame] | 317 | pool = lab_inventory.SPARE_POOL |
J. Richard Barnette | f683928 | 2015-06-01 16:00:35 -0700 | [diff] [blame] | 318 | histories = [] |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 319 | for model, count in repair_counts: |
J. Richard Barnette | f683928 | 2015-06-01 16:00:35 -0700 | [diff] [blame] | 320 | for i in range(0, count): |
| 321 | histories.append( |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 322 | _FakeHostHistory(model, pool, _BROKEN)) |
J. Richard Barnette | f683928 | 2015-06-01 16:00:35 -0700 | [diff] [blame] | 323 | return histories |
| 324 | |
J. Richard Barnette | f683928 | 2015-06-01 16:00:35 -0700 | [diff] [blame] | 325 | def _check_better(self, repair_a, repair_b): |
| 326 | """Test that repair set A scores better than B. |
| 327 | |
| 328 | Contruct repair sets from `repair_a` and `repair_b`, |
| 329 | and score both of them using the pre-existing |
| 330 | `self._buffer_counts`. Assert that the score for A is |
| 331 | better than the score for B. |
| 332 | |
| 333 | @param repair_a Input data for repair set A |
| 334 | @param repair_b Input data for repair set B |
J. Richard Barnette | f683928 | 2015-06-01 16:00:35 -0700 | [diff] [blame] | 335 | """ |
| 336 | score_a = lab_inventory._score_repair_set( |
| 337 | self._buffer_counts, |
| 338 | self._make_history_list(repair_a)) |
| 339 | score_b = lab_inventory._score_repair_set( |
| 340 | self._buffer_counts, |
| 341 | self._make_history_list(repair_b)) |
| 342 | self.assertGreater(score_a, score_b) |
| 343 | |
J. Richard Barnette | f683928 | 2015-06-01 16:00:35 -0700 | [diff] [blame] | 344 | def _check_equal(self, repair_a, repair_b): |
| 345 | """Test that repair set A scores the same as B. |
| 346 | |
| 347 | Contruct repair sets from `repair_a` and `repair_b`, |
| 348 | and score both of them using the pre-existing |
| 349 | `self._buffer_counts`. Assert that the score for A is |
| 350 | equal to the score for B. |
| 351 | |
| 352 | @param repair_a Input data for repair set A |
| 353 | @param repair_b Input data for repair set B |
J. Richard Barnette | f683928 | 2015-06-01 16:00:35 -0700 | [diff] [blame] | 354 | """ |
| 355 | score_a = lab_inventory._score_repair_set( |
| 356 | self._buffer_counts, |
| 357 | self._make_history_list(repair_a)) |
| 358 | score_b = lab_inventory._score_repair_set( |
| 359 | self._buffer_counts, |
| 360 | self._make_history_list(repair_b)) |
| 361 | self.assertEqual(score_a, score_b) |
| 362 | |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 363 | def test_improve_worst_model(self): |
| 364 | """Test that improving the worst model improves scoring. |
J. Richard Barnette | f683928 | 2015-06-01 16:00:35 -0700 | [diff] [blame] | 365 | |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 366 | Construct a buffer counts dictionary with all models having |
J. Richard Barnette | f683928 | 2015-06-01 16:00:35 -0700 | [diff] [blame] | 367 | different counts. Assert that it is both necessary and |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 368 | sufficient to improve the count of the worst model in order |
J. Richard Barnette | f683928 | 2015-06-01 16:00:35 -0700 | [diff] [blame] | 369 | to improve the score. |
J. Richard Barnette | f683928 | 2015-06-01 16:00:35 -0700 | [diff] [blame] | 370 | """ |
| 371 | self._make_buffer_counts(('lion', 0), |
| 372 | ('tiger', 1), |
| 373 | ('bear', 2)) |
| 374 | self._check_better([('lion', 1)], [('tiger', 1)]) |
| 375 | self._check_better([('lion', 1)], [('bear', 1)]) |
| 376 | self._check_better([('lion', 1)], [('tiger', 2)]) |
| 377 | self._check_better([('lion', 1)], [('bear', 2)]) |
| 378 | self._check_equal([('tiger', 1)], [('bear', 1)]) |
| 379 | |
J. Richard Barnette | f683928 | 2015-06-01 16:00:35 -0700 | [diff] [blame] | 380 | def test_improve_worst_case_count(self): |
| 381 | """Test that improving the number of worst cases improves the score. |
| 382 | |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 383 | Construct a buffer counts dictionary with all models having |
| 384 | the same counts. Assert that improving two models is better |
| 385 | than improving one. Assert that improving any one model is |
J. Richard Barnette | f683928 | 2015-06-01 16:00:35 -0700 | [diff] [blame] | 386 | as good as any other. |
J. Richard Barnette | f683928 | 2015-06-01 16:00:35 -0700 | [diff] [blame] | 387 | """ |
| 388 | self._make_buffer_counts(('lion', 0), |
| 389 | ('tiger', 0), |
| 390 | ('bear', 0)) |
| 391 | self._check_better([('lion', 1), ('tiger', 1)], [('bear', 2)]) |
| 392 | self._check_equal([('lion', 2)], [('tiger', 1)]) |
| 393 | self._check_equal([('tiger', 1)], [('bear', 1)]) |
| 394 | |
| 395 | |
Prathmesh Prabhu | 58aede8 | 2017-11-09 13:34:25 -0800 | [diff] [blame] | 396 | # Each item is the number of DUTs in that status. |
| 397 | STATUS_CHOICES = (_WORKING, _BROKEN, _UNUSED) |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 398 | StatusCounts = collections.namedtuple('StatusCounts', ['good', 'bad', 'idle']) |
Prathmesh Prabhu | 58aede8 | 2017-11-09 13:34:25 -0800 | [diff] [blame] | 399 | # Each item is a StatusCounts tuple specifying the number of DUTs per status in |
| 400 | # the that pool. |
| 401 | CRITICAL_POOL = lab_inventory.CRITICAL_POOLS[0] |
| 402 | SPARE_POOL = lab_inventory.SPARE_POOL |
| 403 | POOL_CHOICES = (CRITICAL_POOL, SPARE_POOL) |
| 404 | PoolStatusCounts = collections.namedtuple('PoolStatusCounts', |
| 405 | ['critical', 'spare']) |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 406 | |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 407 | def create_inventory(data): |
| 408 | """Create a `_LabInventory` instance for testing. |
Prathmesh Prabhu | 58aede8 | 2017-11-09 13:34:25 -0800 | [diff] [blame] | 409 | |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 410 | This function allows the construction of a complete `_LabInventory` |
| 411 | object from a simplified input representation. |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 412 | |
| 413 | A single 'critical pool' is arbitrarily chosen for purposes of |
| 414 | testing; there's no coverage for testing arbitrary combinations |
| 415 | in more than one critical pool. |
| 416 | |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 417 | @param data: dict {key: PoolStatusCounts}. |
Prathmesh Prabhu | 58aede8 | 2017-11-09 13:34:25 -0800 | [diff] [blame] | 418 | @returns: lab_inventory._LabInventory object. |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 419 | """ |
Prathmesh Prabhu | 58aede8 | 2017-11-09 13:34:25 -0800 | [diff] [blame] | 420 | histories = [] |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 421 | for model, counts in data.iteritems(): |
Prathmesh Prabhu | 58aede8 | 2017-11-09 13:34:25 -0800 | [diff] [blame] | 422 | for p, pool in enumerate(POOL_CHOICES): |
| 423 | for s, status in enumerate(STATUS_CHOICES): |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 424 | fake_host = _FakeHostHistory(model, pool, status) |
| 425 | histories.extend([fake_host] * counts[p][s]) |
| 426 | inventory = lab_inventory._LabInventory( |
| 427 | histories, lab_inventory.MANAGED_POOLS) |
Prathmesh Prabhu | 58aede8 | 2017-11-09 13:34:25 -0800 | [diff] [blame] | 428 | return inventory |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 429 | |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 430 | |
Prathmesh Prabhu | 58aede8 | 2017-11-09 13:34:25 -0800 | [diff] [blame] | 431 | class LabInventoryTests(unittest.TestCase): |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 432 | """Tests for the basic functions of `_LabInventory`. |
| 433 | |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 434 | Contains basic coverage to show that after an inventory is created |
| 435 | and DUTs with known status are added, the inventory counts match the |
| 436 | counts of the added DUTs. |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 437 | """ |
| 438 | |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 439 | _MODEL_LIST = ['lion', 'tiger', 'bear'] # Oh, my! |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 440 | |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 441 | def _check_inventory_counts(self, inventory, data, msg=None): |
| 442 | """Check that all counts in the inventory match `data`. |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 443 | |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 444 | This asserts that the actual counts returned by the various |
| 445 | accessor functions for `inventory` match the values expected for |
| 446 | the given `data` that created the inventory. |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 447 | |
Prathmesh Prabhu | 58aede8 | 2017-11-09 13:34:25 -0800 | [diff] [blame] | 448 | @param inventory: _LabInventory object to check. |
| 449 | @param data Inventory data to check against. Same type as |
| 450 | `create_inventory`. |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 451 | """ |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 452 | self.assertEqual(set(inventory.keys()), set(data.keys())) |
| 453 | for model, histories in inventory.iteritems(): |
| 454 | expected_counts = data[model] |
| 455 | actual_counts = PoolStatusCounts( |
Prathmesh Prabhu | 58aede8 | 2017-11-09 13:34:25 -0800 | [diff] [blame] | 456 | StatusCounts( |
| 457 | histories.get_working(CRITICAL_POOL), |
| 458 | histories.get_broken(CRITICAL_POOL), |
| 459 | histories.get_idle(CRITICAL_POOL), |
| 460 | ), |
| 461 | StatusCounts( |
| 462 | histories.get_working(SPARE_POOL), |
| 463 | histories.get_broken(SPARE_POOL), |
| 464 | histories.get_idle(SPARE_POOL), |
| 465 | ), |
| 466 | ) |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 467 | self.assertEqual(actual_counts, expected_counts, msg) |
Prathmesh Prabhu | 58aede8 | 2017-11-09 13:34:25 -0800 | [diff] [blame] | 468 | |
| 469 | self.assertEqual(len(histories.get_working_list()), |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 470 | sum([p.good for p in expected_counts]), |
Prathmesh Prabhu | a5a0e3d | 2017-11-09 08:53:53 -0800 | [diff] [blame] | 471 | msg) |
Prathmesh Prabhu | 58aede8 | 2017-11-09 13:34:25 -0800 | [diff] [blame] | 472 | self.assertEqual(len(histories.get_broken_list()), |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 473 | sum([p.bad for p in expected_counts]), |
Prathmesh Prabhu | a5a0e3d | 2017-11-09 08:53:53 -0800 | [diff] [blame] | 474 | msg) |
Prathmesh Prabhu | 58aede8 | 2017-11-09 13:34:25 -0800 | [diff] [blame] | 475 | self.assertEqual(len(histories.get_idle_list()), |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 476 | sum([p.idle for p in expected_counts]), |
Prathmesh Prabhu | a5a0e3d | 2017-11-09 08:53:53 -0800 | [diff] [blame] | 477 | msg) |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 478 | |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 479 | def test_empty(self): |
| 480 | """Test counts when there are no DUTs recorded.""" |
Prathmesh Prabhu | 58aede8 | 2017-11-09 13:34:25 -0800 | [diff] [blame] | 481 | inventory = create_inventory({}) |
| 482 | self.assertEqual(inventory.get_num_duts(), 0) |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 483 | self.assertEqual(inventory.get_boards(), set()) |
| 484 | self._check_inventory_counts(inventory, {}) |
Prathmesh Prabhu | a5a0e3d | 2017-11-09 08:53:53 -0800 | [diff] [blame] | 485 | self.assertEqual(inventory.get_num_models(), 0) |
J. Richard Barnette | 6948ed3 | 2015-05-06 08:57:10 -0700 | [diff] [blame] | 486 | |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 487 | def _check_model_count(self, model_count): |
Prathmesh Prabhu | a5a0e3d | 2017-11-09 08:53:53 -0800 | [diff] [blame] | 488 | """Parameterized test for testing a specific number of models.""" |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 489 | msg = '[model: %d]' % (model_count,) |
| 490 | models = self._MODEL_LIST[:model_count] |
Prathmesh Prabhu | a5a0e3d | 2017-11-09 08:53:53 -0800 | [diff] [blame] | 491 | data = { |
| 492 | m: PoolStatusCounts( |
| 493 | StatusCounts(1, 1, 1), |
| 494 | StatusCounts(1, 1, 1), |
| 495 | ) |
| 496 | for m in models |
| 497 | } |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 498 | inventory = create_inventory(data) |
Prathmesh Prabhu | a5a0e3d | 2017-11-09 08:53:53 -0800 | [diff] [blame] | 499 | self.assertEqual(inventory.get_num_duts(), 6 * model_count, msg) |
| 500 | self.assertEqual(inventory.get_num_models(), model_count, msg) |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 501 | for pool in [CRITICAL_POOL, SPARE_POOL]: |
| 502 | self.assertEqual(set(inventory.get_pool_models(pool)), |
| 503 | set(models)) |
| 504 | self._check_inventory_counts(inventory, data, msg=msg) |
| 505 | |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 506 | def test_model_counts(self): |
| 507 | """Test counts for various numbers of models.""" |
| 508 | self.longMessage = True |
| 509 | for model_count in range(0, len(self._MODEL_LIST)): |
| 510 | self._check_model_count(model_count) |
| 511 | |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 512 | def _check_single_dut_counts(self, critical, spare): |
| 513 | """Parmeterized test for single dut counts.""" |
| 514 | self.longMessage = True |
| 515 | counts = PoolStatusCounts(critical, spare) |
| 516 | model = self._MODEL_LIST[0] |
| 517 | data = {model: counts} |
| 518 | msg = '[data: %s]' % (data,) |
| 519 | inventory = create_inventory(data) |
| 520 | self.assertEqual(inventory.get_num_duts(), 1, msg) |
| 521 | self.assertEqual(inventory.get_num_models(), 1, msg) |
| 522 | self._check_inventory_counts(inventory, data, msg=msg) |
Prathmesh Prabhu | a5a0e3d | 2017-11-09 08:53:53 -0800 | [diff] [blame] | 523 | |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 524 | def test_single_dut_counts(self): |
Prathmesh Prabhu | 58aede8 | 2017-11-09 13:34:25 -0800 | [diff] [blame] | 525 | """Test counts when there is a single DUT per board, and it is good.""" |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 526 | status_100 = StatusCounts(1, 0, 0) |
| 527 | status_010 = StatusCounts(0, 1, 0) |
| 528 | status_001 = StatusCounts(0, 0, 1) |
| 529 | status_null = StatusCounts(0, 0, 0) |
| 530 | self._check_single_dut_counts(status_100, status_null) |
| 531 | self._check_single_dut_counts(status_010, status_null) |
| 532 | self._check_single_dut_counts(status_001, status_null) |
| 533 | self._check_single_dut_counts(status_null, status_100) |
| 534 | self._check_single_dut_counts(status_null, status_010) |
| 535 | self._check_single_dut_counts(status_null, status_001) |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 536 | |
| 537 | |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 538 | # MODEL_MESSAGE_TEMPLATE - |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 539 | # This is a sample of the output text produced by |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 540 | # _generate_model_inventory_message(). This string is parsed by the |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 541 | # tests below to construct a sample inventory that should produce |
| 542 | # the output, and then the output is generated and checked against |
| 543 | # this original sample. |
| 544 | # |
| 545 | # Constructing inventories from parsed sample text serves two |
| 546 | # related purposes: |
| 547 | # - It provides a way to see what the output should look like |
| 548 | # without having to run the script. |
| 549 | # - It helps make sure that a human being will actually look at |
| 550 | # the output to see that it's basically readable. |
| 551 | # This should also help prevent test bugs caused by writing tests |
| 552 | # that simply parrot the original output generation code. |
| 553 | |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 554 | _MODEL_MESSAGE_TEMPLATE = ''' |
| 555 | Model Avail Bad Idle Good Spare Total |
xixuan | 12ce04f | 2016-03-10 13:16:30 -0800 | [diff] [blame] | 556 | lion -1 13 2 11 12 26 |
| 557 | tiger -1 5 2 9 4 16 |
| 558 | bear 0 5 2 10 5 17 |
| 559 | platypus 4 2 2 20 6 24 |
| 560 | aardvark 7 2 2 6 9 10 |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 561 | ''' |
| 562 | |
| 563 | |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 564 | class PoolSetInventoryTests(unittest.TestCase): |
| 565 | """Tests for `_generate_model_inventory_message()`. |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 566 | |
| 567 | The tests create various test inventories designed to match the |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 568 | counts in `_MODEL_MESSAGE_TEMPLATE`, and asserts that the |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 569 | generated message text matches the original message text. |
| 570 | |
| 571 | Message text is represented as a list of strings, split on the |
| 572 | `'\n'` separator. |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 573 | """ |
| 574 | |
| 575 | def setUp(self): |
Prathmesh Prabhu | 58aede8 | 2017-11-09 13:34:25 -0800 | [diff] [blame] | 576 | self.maxDiff = None |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 577 | lines = [x.strip() for x in _MODEL_MESSAGE_TEMPLATE.split('\n') if |
Prathmesh Prabhu | 58aede8 | 2017-11-09 13:34:25 -0800 | [diff] [blame] | 578 | x.strip()] |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 579 | self._header, self._model_lines = lines[0], lines[1:] |
| 580 | self._model_data = [] |
| 581 | for l in self._model_lines: |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 582 | items = l.split() |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 583 | model = items[0] |
Prathmesh Prabhu | 58aede8 | 2017-11-09 13:34:25 -0800 | [diff] [blame] | 584 | bad, idle, good, spare = [int(x) for x in items[2:-1]] |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 585 | self._model_data.append((model, (good, bad, idle, spare))) |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 586 | |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 587 | def _make_minimum_spares(self, counts): |
| 588 | """Create a counts tuple with as few spare DUTs as possible.""" |
xixuan | 12ce04f | 2016-03-10 13:16:30 -0800 | [diff] [blame] | 589 | good, bad, idle, spares = counts |
| 590 | if spares > bad + idle: |
Prathmesh Prabhu | 58aede8 | 2017-11-09 13:34:25 -0800 | [diff] [blame] | 591 | return PoolStatusCounts( |
| 592 | StatusCounts(good + bad +idle - spares, 0, 0), |
| 593 | StatusCounts(spares - bad - idle, bad, idle), |
| 594 | ) |
xixuan | 12ce04f | 2016-03-10 13:16:30 -0800 | [diff] [blame] | 595 | elif spares < bad: |
Prathmesh Prabhu | 58aede8 | 2017-11-09 13:34:25 -0800 | [diff] [blame] | 596 | return PoolStatusCounts( |
| 597 | StatusCounts(good, bad - spares, idle), |
| 598 | StatusCounts(0, spares, 0), |
| 599 | ) |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 600 | else: |
Prathmesh Prabhu | 58aede8 | 2017-11-09 13:34:25 -0800 | [diff] [blame] | 601 | return PoolStatusCounts( |
| 602 | StatusCounts(good, 0, idle + bad - spares), |
| 603 | StatusCounts(0, bad, spares - bad), |
| 604 | ) |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 605 | |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 606 | def _make_maximum_spares(self, counts): |
| 607 | """Create a counts tuple with as many spare DUTs as possible.""" |
xixuan | 12ce04f | 2016-03-10 13:16:30 -0800 | [diff] [blame] | 608 | good, bad, idle, spares = counts |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 609 | if good > spares: |
Prathmesh Prabhu | 58aede8 | 2017-11-09 13:34:25 -0800 | [diff] [blame] | 610 | return PoolStatusCounts( |
| 611 | StatusCounts(good - spares, bad, idle), |
| 612 | StatusCounts(spares, 0, 0), |
| 613 | ) |
xixuan | 12ce04f | 2016-03-10 13:16:30 -0800 | [diff] [blame] | 614 | elif good + bad > spares: |
Prathmesh Prabhu | 58aede8 | 2017-11-09 13:34:25 -0800 | [diff] [blame] | 615 | return PoolStatusCounts( |
| 616 | StatusCounts(0, good + bad - spares, idle), |
| 617 | StatusCounts(good, spares - good, 0), |
| 618 | ) |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 619 | else: |
Prathmesh Prabhu | 58aede8 | 2017-11-09 13:34:25 -0800 | [diff] [blame] | 620 | return PoolStatusCounts( |
| 621 | StatusCounts(0, 0, good + bad + idle - spares), |
| 622 | StatusCounts(good, bad, spares - good - bad), |
| 623 | ) |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 624 | |
Prathmesh Prabhu | 58aede8 | 2017-11-09 13:34:25 -0800 | [diff] [blame] | 625 | def _check_message(self, message): |
| 626 | """Checks that message approximately matches expected string.""" |
| 627 | message = [x.strip() for x in message.split('\n') if x.strip()] |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 628 | self.assertIn(self._header, message) |
Prathmesh Prabhu | 58aede8 | 2017-11-09 13:34:25 -0800 | [diff] [blame] | 629 | body = message[message.index(self._header) + 1:] |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 630 | self.assertEqual(body, self._model_lines) |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 631 | |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 632 | def test_minimum_spares(self): |
| 633 | """Test message generation when the spares pool is low.""" |
| 634 | data = { |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 635 | model: self._make_minimum_spares(counts) |
| 636 | for model, counts in self._model_data |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 637 | } |
Prathmesh Prabhu | 58aede8 | 2017-11-09 13:34:25 -0800 | [diff] [blame] | 638 | inventory = create_inventory(data) |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 639 | message = lab_inventory._generate_model_inventory_message(inventory) |
Prathmesh Prabhu | 58aede8 | 2017-11-09 13:34:25 -0800 | [diff] [blame] | 640 | self._check_message(message) |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 641 | |
| 642 | def test_maximum_spares(self): |
| 643 | """Test message generation when the critical pool is low.""" |
| 644 | data = { |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 645 | model: self._make_maximum_spares(counts) |
| 646 | for model, counts in self._model_data |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 647 | } |
Prathmesh Prabhu | 58aede8 | 2017-11-09 13:34:25 -0800 | [diff] [blame] | 648 | inventory = create_inventory(data) |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 649 | message = lab_inventory._generate_model_inventory_message(inventory) |
Prathmesh Prabhu | 58aede8 | 2017-11-09 13:34:25 -0800 | [diff] [blame] | 650 | self._check_message(message) |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 651 | |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 652 | def test_ignore_no_spares(self): |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 653 | """Test that messages ignore models with no spare pool.""" |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 654 | data = { |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 655 | model: self._make_maximum_spares(counts) |
| 656 | for model, counts in self._model_data |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 657 | } |
xixuan | 12ce04f | 2016-03-10 13:16:30 -0800 | [diff] [blame] | 658 | data['elephant'] = ((5, 4, 0), (0, 0, 0)) |
Prathmesh Prabhu | 58aede8 | 2017-11-09 13:34:25 -0800 | [diff] [blame] | 659 | inventory = create_inventory(data) |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 660 | message = lab_inventory._generate_model_inventory_message(inventory) |
Prathmesh Prabhu | 58aede8 | 2017-11-09 13:34:25 -0800 | [diff] [blame] | 661 | self._check_message(message) |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 662 | |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 663 | def test_ignore_no_critical(self): |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 664 | """Test that messages ignore models with no critical pools.""" |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 665 | data = { |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 666 | model: self._make_maximum_spares(counts) |
| 667 | for model, counts in self._model_data |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 668 | } |
xixuan | 12ce04f | 2016-03-10 13:16:30 -0800 | [diff] [blame] | 669 | data['elephant'] = ((0, 0, 0), (1, 5, 1)) |
Prathmesh Prabhu | 58aede8 | 2017-11-09 13:34:25 -0800 | [diff] [blame] | 670 | inventory = create_inventory(data) |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 671 | message = lab_inventory._generate_model_inventory_message(inventory) |
Prathmesh Prabhu | 58aede8 | 2017-11-09 13:34:25 -0800 | [diff] [blame] | 672 | self._check_message(message) |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 673 | |
J. Richard Barnette | ea5a4ba | 2016-02-18 16:34:50 -0800 | [diff] [blame] | 674 | def test_ignore_no_bad(self): |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 675 | """Test that messages ignore models with no bad DUTs.""" |
J. Richard Barnette | ea5a4ba | 2016-02-18 16:34:50 -0800 | [diff] [blame] | 676 | data = { |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 677 | model: self._make_maximum_spares(counts) |
| 678 | for model, counts in self._model_data |
J. Richard Barnette | ea5a4ba | 2016-02-18 16:34:50 -0800 | [diff] [blame] | 679 | } |
xixuan | 12ce04f | 2016-03-10 13:16:30 -0800 | [diff] [blame] | 680 | data['elephant'] = ((5, 0, 1), (5, 0, 1)) |
Prathmesh Prabhu | 58aede8 | 2017-11-09 13:34:25 -0800 | [diff] [blame] | 681 | inventory = create_inventory(data) |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 682 | message = lab_inventory._generate_model_inventory_message(inventory) |
Prathmesh Prabhu | 58aede8 | 2017-11-09 13:34:25 -0800 | [diff] [blame] | 683 | self._check_message(message) |
J. Richard Barnette | ea5a4ba | 2016-02-18 16:34:50 -0800 | [diff] [blame] | 684 | |
| 685 | |
xixuan | 12ce04f | 2016-03-10 13:16:30 -0800 | [diff] [blame] | 686 | class _PoolInventoryTestBase(unittest.TestCase): |
| 687 | """Parent class for tests relating to generating pool inventory messages. |
| 688 | |
| 689 | Func `setUp` in the class parses a given |message_template| to obtain |
| 690 | header and body. |
| 691 | """ |
Richard Barnette | df01f1b | 2018-04-20 14:44:40 -0400 | [diff] [blame] | 692 | |
xixuan | 12ce04f | 2016-03-10 13:16:30 -0800 | [diff] [blame] | 693 | def _read_template(self, message_template): |
| 694 | """Read message template for PoolInventoryTest and IdleInventoryTest. |
| 695 | |
| 696 | @param message_template: the input template to be parsed into: header |
| 697 | and content (report_lines). |
xixuan | 12ce04f | 2016-03-10 13:16:30 -0800 | [diff] [blame] | 698 | """ |
| 699 | message_lines = message_template.split('\n') |
| 700 | self._header = message_lines[1] |
| 701 | self._report_lines = message_lines[2:-1] |
| 702 | |
xixuan | 12ce04f | 2016-03-10 13:16:30 -0800 | [diff] [blame] | 703 | def _check_report_no_info(self, text): |
| 704 | """Test a message body containing no reported info. |
| 705 | |
Richard Barnette | df01f1b | 2018-04-20 14:44:40 -0400 | [diff] [blame] | 706 | The input `text` was created from a query to an inventory, which |
| 707 | has no objects meet the query and leads to an `empty` return. |
| 708 | Assert that the text consists of a single line starting with '(' |
| 709 | and ending with ')'. |
xixuan | 12ce04f | 2016-03-10 13:16:30 -0800 | [diff] [blame] | 710 | |
| 711 | @param text: Message body text to be tested. |
xixuan | 12ce04f | 2016-03-10 13:16:30 -0800 | [diff] [blame] | 712 | """ |
| 713 | self.assertTrue(len(text) == 1 and |
| 714 | text[0][0] == '(' and |
| 715 | text[0][-1] == ')') |
| 716 | |
xixuan | 12ce04f | 2016-03-10 13:16:30 -0800 | [diff] [blame] | 717 | def _check_report(self, text): |
| 718 | """Test a message against the passed |expected_content|. |
| 719 | |
| 720 | @param text: Message body text to be tested. |
| 721 | @param expected_content: The ground-truth content to be compared with. |
xixuan | 12ce04f | 2016-03-10 13:16:30 -0800 | [diff] [blame] | 722 | """ |
| 723 | self.assertEqual(text, self._report_lines) |
| 724 | |
| 725 | |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 726 | # _POOL_MESSAGE_TEMPLATE - |
| 727 | # This is a sample of the output text produced by |
| 728 | # _generate_pool_inventory_message(). This string is parsed by the |
| 729 | # tests below to construct a sample inventory that should produce |
| 730 | # the output, and then the output is generated and checked against |
| 731 | # this original sample. |
| 732 | # |
| 733 | # See the comments on _BOARD_MESSAGE_TEMPLATE above for the |
| 734 | # rationale on using sample text in this way. |
| 735 | |
| 736 | _POOL_MESSAGE_TEMPLATE = ''' |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 737 | Model Bad Idle Good Total |
xixuan | 12ce04f | 2016-03-10 13:16:30 -0800 | [diff] [blame] | 738 | lion 5 2 6 13 |
| 739 | tiger 4 1 5 10 |
| 740 | bear 3 0 7 10 |
| 741 | aardvark 2 0 0 2 |
| 742 | platypus 1 1 1 3 |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 743 | ''' |
| 744 | |
J. Richard Barnette | 4845fcf | 2015-04-20 14:26:25 -0700 | [diff] [blame] | 745 | _POOL_ADMIN_URL = 'http://go/cros-manage-duts' |
| 746 | |
| 747 | |
xixuan | 12ce04f | 2016-03-10 13:16:30 -0800 | [diff] [blame] | 748 | class PoolInventoryTests(_PoolInventoryTestBase): |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 749 | """Tests for `_generate_pool_inventory_message()`. |
| 750 | |
| 751 | The tests create various test inventories designed to match the |
| 752 | counts in `_POOL_MESSAGE_TEMPLATE`, and assert that the |
| 753 | generated message text matches the format established in the |
| 754 | original message text. |
| 755 | |
| 756 | The output message text is parsed against the following grammar: |
J. Richard Barnette | 4845fcf | 2015-04-20 14:26:25 -0700 | [diff] [blame] | 757 | <message> -> <intro> <pool> { "blank line" <pool> } |
| 758 | <intro> -> |
| 759 | Instructions to depty mentioning the admin page URL |
| 760 | A blank line |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 761 | <pool> -> |
| 762 | <description> |
| 763 | <header line> |
| 764 | <message body> |
| 765 | <description> -> |
| 766 | Any number of lines describing one pool |
| 767 | <header line> -> |
| 768 | The header line from `_POOL_MESSAGE_TEMPLATE` |
| 769 | <message body> -> |
| 770 | Any number of non-blank lines |
| 771 | |
| 772 | After parsing messages into the parts described above, various |
| 773 | assertions are tested against the parsed output, including |
| 774 | that the message body matches the body from |
| 775 | `_POOL_MESSAGE_TEMPLATE`. |
| 776 | |
| 777 | Parse message text is represented as a list of strings, split on |
| 778 | the `'\n'` separator. |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 779 | """ |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 780 | |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 781 | def setUp(self): |
xixuan | 12ce04f | 2016-03-10 13:16:30 -0800 | [diff] [blame] | 782 | super(PoolInventoryTests, self)._read_template(_POOL_MESSAGE_TEMPLATE) |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 783 | self._model_data = [] |
xixuan | 12ce04f | 2016-03-10 13:16:30 -0800 | [diff] [blame] | 784 | for l in self._report_lines: |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 785 | items = l.split() |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 786 | model = items[0] |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 787 | bad = int(items[1]) |
xixuan | 12ce04f | 2016-03-10 13:16:30 -0800 | [diff] [blame] | 788 | idle = int(items[2]) |
| 789 | good = int(items[3]) |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 790 | self._model_data.append((model, (good, bad, idle))) |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 791 | |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 792 | def _create_histories(self, pools, model_data): |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 793 | """Return a list suitable to create a `_LabInventory` object. |
| 794 | |
| 795 | Creates a list of `_FakeHostHistory` objects that can be |
| 796 | used to create a lab inventory. `pools` is a list of strings |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 797 | naming pools, and `model_data` is a list of tuples of the |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 798 | form |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 799 | `(model, (goodcount, badcount))` |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 800 | where |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 801 | `model` is a model name. |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 802 | `goodcount` is the number of working DUTs in the pool. |
| 803 | `badcount` is the number of broken DUTs in the pool. |
| 804 | |
| 805 | @param pools List of pools for which to create |
| 806 | histories. |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 807 | @param model_data List of tuples containing models and DUT |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 808 | counts. |
| 809 | @return A list of `_FakeHostHistory` objects that can be |
| 810 | used to create a `_LabInventory` object. |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 811 | """ |
| 812 | histories = [] |
xixuan | 12ce04f | 2016-03-10 13:16:30 -0800 | [diff] [blame] | 813 | status_choices = (_WORKING, _BROKEN, _UNUSED) |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 814 | for pool in pools: |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 815 | for model, counts in model_data: |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 816 | for status, count in zip(status_choices, counts): |
| 817 | for x in range(0, count): |
| 818 | histories.append( |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 819 | _FakeHostHistory(model, pool, status)) |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 820 | return histories |
| 821 | |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 822 | def _parse_pool_summaries(self, histories): |
| 823 | """Parse message output according to the grammar above. |
| 824 | |
| 825 | Create a lab inventory from the given `histories`, and |
| 826 | generate the pool inventory message. Then parse the message |
| 827 | and return a dictionary mapping each pool to the message |
| 828 | body parsed after that pool. |
| 829 | |
| 830 | Tests the following assertions: |
| 831 | * Each <description> contains a mention of exactly one |
Kevin Cheng | cf0ad2b | 2016-04-19 14:51:39 -0700 | [diff] [blame] | 832 | pool in the `CRITICAL_POOLS` list. |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 833 | * Each pool is mentioned in exactly one <description>. |
| 834 | Note that the grammar requires the header to appear once |
| 835 | for each pool, so the parsing implicitly asserts that the |
| 836 | output contains the header. |
| 837 | |
| 838 | @param histories Input used to create the test |
| 839 | `_LabInventory` object. |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 840 | @return A dictionary mapping model names to the output |
| 841 | (a list of lines) for the model. |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 842 | """ |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 843 | inventory = lab_inventory._LabInventory( |
| 844 | histories, lab_inventory.MANAGED_POOLS) |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 845 | message = lab_inventory._generate_pool_inventory_message( |
xixuan | 12ce04f | 2016-03-10 13:16:30 -0800 | [diff] [blame] | 846 | inventory).split('\n') |
Kevin Cheng | cf0ad2b | 2016-04-19 14:51:39 -0700 | [diff] [blame] | 847 | poolset = set(lab_inventory.CRITICAL_POOLS) |
J. Richard Barnette | 4845fcf | 2015-04-20 14:26:25 -0700 | [diff] [blame] | 848 | seen_url = False |
| 849 | seen_intro = False |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 850 | description = '' |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 851 | model_text = {} |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 852 | current_pool = None |
| 853 | for line in message: |
J. Richard Barnette | 4845fcf | 2015-04-20 14:26:25 -0700 | [diff] [blame] | 854 | if not seen_url: |
| 855 | if _POOL_ADMIN_URL in line: |
| 856 | seen_url = True |
| 857 | elif not seen_intro: |
| 858 | if not line: |
| 859 | seen_intro = True |
| 860 | elif current_pool is None: |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 861 | if line == self._header: |
| 862 | pools_mentioned = [p for p in poolset |
| 863 | if p in description] |
| 864 | self.assertEqual(len(pools_mentioned), 1) |
| 865 | current_pool = pools_mentioned[0] |
| 866 | description = '' |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 867 | model_text[current_pool] = [] |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 868 | poolset.remove(current_pool) |
| 869 | else: |
| 870 | description += line |
| 871 | else: |
| 872 | if line: |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 873 | model_text[current_pool].append(line) |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 874 | else: |
| 875 | current_pool = None |
| 876 | self.assertEqual(len(poolset), 0) |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 877 | return model_text |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 878 | |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 879 | def test_no_shortages(self): |
| 880 | """Test correct output when no pools have shortages.""" |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 881 | model_text = self._parse_pool_summaries([]) |
| 882 | for text in model_text.values(): |
xixuan | 12ce04f | 2016-03-10 13:16:30 -0800 | [diff] [blame] | 883 | self._check_report_no_info(text) |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 884 | |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 885 | def test_one_pool_shortage(self): |
| 886 | """Test correct output when exactly one pool has a shortage.""" |
Kevin Cheng | cf0ad2b | 2016-04-19 14:51:39 -0700 | [diff] [blame] | 887 | for pool in lab_inventory.CRITICAL_POOLS: |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 888 | histories = self._create_histories((pool,), |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 889 | self._model_data) |
| 890 | model_text = self._parse_pool_summaries(histories) |
Kevin Cheng | cf0ad2b | 2016-04-19 14:51:39 -0700 | [diff] [blame] | 891 | for checkpool in lab_inventory.CRITICAL_POOLS: |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 892 | text = model_text[checkpool] |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 893 | if checkpool == pool: |
xixuan | 12ce04f | 2016-03-10 13:16:30 -0800 | [diff] [blame] | 894 | self._check_report(text) |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 895 | else: |
xixuan | 12ce04f | 2016-03-10 13:16:30 -0800 | [diff] [blame] | 896 | self._check_report_no_info(text) |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 897 | |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 898 | def test_all_pool_shortages(self): |
| 899 | """Test correct output when all pools have a shortage.""" |
| 900 | histories = [] |
Kevin Cheng | cf0ad2b | 2016-04-19 14:51:39 -0700 | [diff] [blame] | 901 | for pool in lab_inventory.CRITICAL_POOLS: |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 902 | histories.extend( |
| 903 | self._create_histories((pool,), |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 904 | self._model_data)) |
| 905 | model_text = self._parse_pool_summaries(histories) |
Kevin Cheng | cf0ad2b | 2016-04-19 14:51:39 -0700 | [diff] [blame] | 906 | for pool in lab_inventory.CRITICAL_POOLS: |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 907 | self._check_report(model_text[pool]) |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 908 | |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 909 | def test_full_model_ignored(self): |
| 910 | """Test that models at full strength are not reported.""" |
Kevin Cheng | cf0ad2b | 2016-04-19 14:51:39 -0700 | [diff] [blame] | 911 | pool = lab_inventory.CRITICAL_POOLS[0] |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 912 | full_model = [('echidna', (5, 0, 0))] |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 913 | histories = self._create_histories((pool,), |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 914 | full_model) |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 915 | text = self._parse_pool_summaries(histories)[pool] |
xixuan | 12ce04f | 2016-03-10 13:16:30 -0800 | [diff] [blame] | 916 | self._check_report_no_info(text) |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 917 | model_data = self._model_data + full_model |
| 918 | histories = self._create_histories((pool,), model_data) |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 919 | text = self._parse_pool_summaries(histories)[pool] |
xixuan | 12ce04f | 2016-03-10 13:16:30 -0800 | [diff] [blame] | 920 | self._check_report(text) |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 921 | |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 922 | def test_spare_pool_ignored(self): |
| 923 | """Test that reporting ignores the spare pool inventory.""" |
Kevin Cheng | cf0ad2b | 2016-04-19 14:51:39 -0700 | [diff] [blame] | 924 | spare_pool = lab_inventory.SPARE_POOL |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 925 | spare_data = self._model_data + [('echidna', (0, 5, 0))] |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 926 | histories = self._create_histories((spare_pool,), |
| 927 | spare_data) |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 928 | model_text = self._parse_pool_summaries(histories) |
Kevin Cheng | cf0ad2b | 2016-04-19 14:51:39 -0700 | [diff] [blame] | 929 | for pool in lab_inventory.CRITICAL_POOLS: |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 930 | self._check_report_no_info(model_text[pool]) |
xixuan | 12ce04f | 2016-03-10 13:16:30 -0800 | [diff] [blame] | 931 | |
| 932 | |
| 933 | _IDLE_MESSAGE_TEMPLATE = ''' |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 934 | Hostname Model Pool |
xixuan | 12ce04f | 2016-03-10 13:16:30 -0800 | [diff] [blame] | 935 | chromeos4-row12-rack4-host7 tiger bvt |
| 936 | chromeos1-row3-rack1-host2 lion bvt |
| 937 | chromeos3-row2-rack2-host5 lion cq |
| 938 | chromeos2-row7-rack3-host11 platypus suites |
| 939 | ''' |
| 940 | |
| 941 | |
| 942 | class IdleInventoryTests(_PoolInventoryTestBase): |
| 943 | """Tests for `_generate_idle_inventory_message()`. |
| 944 | |
| 945 | The tests create idle duts that match the counts and pool in |
| 946 | `_IDLE_MESSAGE_TEMPLATE`. In test, it asserts that the generated |
| 947 | idle message text matches the format established in |
| 948 | `_IDLE_MESSAGE_TEMPLATE`. |
| 949 | |
| 950 | Parse message text is represented as a list of strings, split on |
| 951 | the `'\n'` separator. |
xixuan | 12ce04f | 2016-03-10 13:16:30 -0800 | [diff] [blame] | 952 | """ |
| 953 | |
| 954 | def setUp(self): |
| 955 | super(IdleInventoryTests, self)._read_template(_IDLE_MESSAGE_TEMPLATE) |
| 956 | self._host_data = [] |
| 957 | for h in self._report_lines: |
| 958 | items = h.split() |
| 959 | hostname = items[0] |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 960 | model = items[1] |
xixuan | 12ce04f | 2016-03-10 13:16:30 -0800 | [diff] [blame] | 961 | pool = items[2] |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 962 | self._host_data.append((hostname, model, pool)) |
xixuan | 12ce04f | 2016-03-10 13:16:30 -0800 | [diff] [blame] | 963 | self._histories = [] |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 964 | self._histories.append(_FakeHostHistory('echidna', 'bvt', _BROKEN)) |
| 965 | self._histories.append(_FakeHostHistory('lion', 'bvt', _WORKING)) |
xixuan | 12ce04f | 2016-03-10 13:16:30 -0800 | [diff] [blame] | 966 | |
xixuan | 12ce04f | 2016-03-10 13:16:30 -0800 | [diff] [blame] | 967 | def _add_idles(self): |
| 968 | """Add idle duts from `_IDLE_MESSAGE_TEMPLATE`.""" |
| 969 | idle_histories = [_FakeHostHistory( |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 970 | model, pool, _UNUSED, hostname) |
| 971 | for hostname, model, pool in self._host_data] |
xixuan | 12ce04f | 2016-03-10 13:16:30 -0800 | [diff] [blame] | 972 | self._histories.extend(idle_histories) |
| 973 | |
xixuan | 12ce04f | 2016-03-10 13:16:30 -0800 | [diff] [blame] | 974 | def _check_header(self, text): |
| 975 | """Check whether header in the template `_IDLE_MESSAGE_TEMPLATE` is in |
| 976 | passed text.""" |
| 977 | self.assertIn(self._header, text) |
| 978 | |
xixuan | 12ce04f | 2016-03-10 13:16:30 -0800 | [diff] [blame] | 979 | def _get_idle_message(self, histories): |
| 980 | """Generate idle inventory and obtain its message. |
| 981 | |
| 982 | @param histories: Used to create lab inventory. |
| 983 | |
| 984 | @return the generated idle message. |
xixuan | 12ce04f | 2016-03-10 13:16:30 -0800 | [diff] [blame] | 985 | """ |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 986 | inventory = lab_inventory._LabInventory( |
| 987 | histories, lab_inventory.MANAGED_POOLS) |
xixuan | 12ce04f | 2016-03-10 13:16:30 -0800 | [diff] [blame] | 988 | message = lab_inventory._generate_idle_inventory_message( |
| 989 | inventory).split('\n') |
| 990 | return message |
| 991 | |
xixuan | 12ce04f | 2016-03-10 13:16:30 -0800 | [diff] [blame] | 992 | def test_check_idle_inventory(self): |
| 993 | """Test that reporting all the idle DUTs for every pool, sorted by |
Kevin Cheng | cf0ad2b | 2016-04-19 14:51:39 -0700 | [diff] [blame] | 994 | lab_inventory.MANAGED_POOLS. |
xixuan | 12ce04f | 2016-03-10 13:16:30 -0800 | [diff] [blame] | 995 | """ |
| 996 | self._add_idles() |
| 997 | |
| 998 | message = self._get_idle_message(self._histories) |
| 999 | self._check_header(message) |
| 1000 | self._check_report(message[message.index(self._header) + 1 :]) |
| 1001 | |
xixuan | 12ce04f | 2016-03-10 13:16:30 -0800 | [diff] [blame] | 1002 | def test_no_idle_inventory(self): |
| 1003 | """Test that reporting no idle DUTs.""" |
| 1004 | message = self._get_idle_message(self._histories) |
| 1005 | self._check_header(message) |
| 1006 | self._check_report_no_info( |
| 1007 | message[message.index(self._header) + 1 :]) |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 1008 | |
| 1009 | |
| 1010 | class CommandParsingTests(unittest.TestCase): |
| 1011 | """Tests for command line argument parsing in `_parse_command()`.""" |
| 1012 | |
Richard Barnette | 7bfcb03 | 2018-02-26 11:46:56 -0800 | [diff] [blame] | 1013 | # At least one of these options must be specified on every command |
| 1014 | # line; otherwise, the command line parsing will fail. |
Richard Barnette | 1ca30e6 | 2018-04-09 16:45:58 -0700 | [diff] [blame] | 1015 | _REPORT_OPTIONS = [ |
| 1016 | '--model-notify=', '--pool-notify=', '--report-untestable' |
| 1017 | ] |
J. Richard Barnette | 02e8243 | 2015-10-13 16:02:47 -0700 | [diff] [blame] | 1018 | |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 1019 | def setUp(self): |
| 1020 | dirpath = '/usr/local/fubar' |
| 1021 | self._command_path = os.path.join(dirpath, |
| 1022 | 'site_utils', |
| 1023 | 'arglebargle') |
| 1024 | self._logdir = os.path.join(dirpath, lab_inventory._LOGDIR) |
| 1025 | |
Richard Barnette | 7bfcb03 | 2018-02-26 11:46:56 -0800 | [diff] [blame] | 1026 | def _parse_arguments(self, argv): |
| 1027 | """Test parsing with explictly passed report options.""" |
| 1028 | full_argv = [self._command_path] + argv |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 1029 | return lab_inventory._parse_command(full_argv) |
| 1030 | |
Richard Barnette | 7bfcb03 | 2018-02-26 11:46:56 -0800 | [diff] [blame] | 1031 | def _parse_non_report_arguments(self, argv): |
| 1032 | """Test parsing for non-report command-line options.""" |
| 1033 | return self._parse_arguments(argv + self._REPORT_OPTIONS) |
| 1034 | |
Richard Barnette | 7bfcb03 | 2018-02-26 11:46:56 -0800 | [diff] [blame] | 1035 | def _check_non_report_defaults(self, report_option): |
| 1036 | arguments = self._parse_arguments([report_option]) |
J. Richard Barnette | 02e8243 | 2015-10-13 16:02:47 -0700 | [diff] [blame] | 1037 | self.assertEqual(arguments.duration, |
| 1038 | lab_inventory._DEFAULT_DURATION) |
Richard Barnette | cf5d834 | 2017-10-24 18:13:11 -0700 | [diff] [blame] | 1039 | self.assertIsNone(arguments.recommend) |
J. Richard Barnette | 02e8243 | 2015-10-13 16:02:47 -0700 | [diff] [blame] | 1040 | self.assertFalse(arguments.debug) |
| 1041 | self.assertEqual(arguments.logdir, self._logdir) |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 1042 | self.assertEqual(arguments.modelnames, []) |
J. Richard Barnette | 02e8243 | 2015-10-13 16:02:47 -0700 | [diff] [blame] | 1043 | return arguments |
| 1044 | |
J. Richard Barnette | 02e8243 | 2015-10-13 16:02:47 -0700 | [diff] [blame] | 1045 | def test_empty_arguments(self): |
Richard Barnette | 7bfcb03 | 2018-02-26 11:46:56 -0800 | [diff] [blame] | 1046 | """Test that no reports requested is an error.""" |
| 1047 | arguments = self._parse_arguments([]) |
J. Richard Barnette | 02e8243 | 2015-10-13 16:02:47 -0700 | [diff] [blame] | 1048 | self.assertIsNone(arguments) |
| 1049 | |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 1050 | def test_argument_defaults(self): |
| 1051 | """Test that option defaults match expectations.""" |
Richard Barnette | 7bfcb03 | 2018-02-26 11:46:56 -0800 | [diff] [blame] | 1052 | for report in self._REPORT_OPTIONS: |
| 1053 | arguments = self._check_non_report_defaults(report) |
| 1054 | |
Richard Barnette | 7bfcb03 | 2018-02-26 11:46:56 -0800 | [diff] [blame] | 1055 | def test_model_notify_defaults(self): |
| 1056 | """Test defaults when `--model-notify` is specified alone.""" |
| 1057 | arguments = self._parse_arguments(['--model-notify=']) |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 1058 | self.assertEqual(arguments.model_notify, ['']) |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 1059 | self.assertEqual(arguments.pool_notify, []) |
Richard Barnette | 1ca30e6 | 2018-04-09 16:45:58 -0700 | [diff] [blame] | 1060 | self.assertFalse(arguments.report_untestable) |
Richard Barnette | 7bfcb03 | 2018-02-26 11:46:56 -0800 | [diff] [blame] | 1061 | |
Richard Barnette | 7bfcb03 | 2018-02-26 11:46:56 -0800 | [diff] [blame] | 1062 | def test_pool_notify_defaults(self): |
| 1063 | """Test defaults when `--pool-notify` is specified alone.""" |
| 1064 | arguments = self._parse_arguments(['--pool-notify=']) |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 1065 | self.assertEqual(arguments.model_notify, []) |
J. Richard Barnette | 02e8243 | 2015-10-13 16:02:47 -0700 | [diff] [blame] | 1066 | self.assertEqual(arguments.pool_notify, ['']) |
Richard Barnette | 1ca30e6 | 2018-04-09 16:45:58 -0700 | [diff] [blame] | 1067 | self.assertFalse(arguments.report_untestable) |
Richard Barnette | 7bfcb03 | 2018-02-26 11:46:56 -0800 | [diff] [blame] | 1068 | |
Richard Barnette | 1ca30e6 | 2018-04-09 16:45:58 -0700 | [diff] [blame] | 1069 | def test_report_untestable_defaults(self): |
| 1070 | """Test defaults when `--report-untestable` is specified alone.""" |
| 1071 | arguments = self._parse_arguments(['--report-untestable']) |
Richard Barnette | 7bfcb03 | 2018-02-26 11:46:56 -0800 | [diff] [blame] | 1072 | self.assertEqual(arguments.model_notify, []) |
| 1073 | self.assertEqual(arguments.pool_notify, []) |
Richard Barnette | 1ca30e6 | 2018-04-09 16:45:58 -0700 | [diff] [blame] | 1074 | self.assertTrue(arguments.report_untestable) |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 1075 | |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 1076 | def test_model_arguments(self): |
| 1077 | """Test that non-option arguments are returned in `modelnames`.""" |
| 1078 | modellist = ['aardvark', 'echidna'] |
Richard Barnette | 7bfcb03 | 2018-02-26 11:46:56 -0800 | [diff] [blame] | 1079 | arguments = self._parse_non_report_arguments(modellist) |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 1080 | self.assertEqual(arguments.modelnames, modellist) |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 1081 | |
Richard Barnette | cf5d834 | 2017-10-24 18:13:11 -0700 | [diff] [blame] | 1082 | def test_recommend_option(self): |
| 1083 | """Test parsing of the `--recommend` option.""" |
| 1084 | for opt in ['-r', '--recommend']: |
| 1085 | for recommend in ['5', '55']: |
Richard Barnette | 7bfcb03 | 2018-02-26 11:46:56 -0800 | [diff] [blame] | 1086 | arguments = self._parse_non_report_arguments([opt, recommend]) |
Richard Barnette | cf5d834 | 2017-10-24 18:13:11 -0700 | [diff] [blame] | 1087 | self.assertEqual(arguments.recommend, int(recommend)) |
| 1088 | |
J. Richard Barnette | 02e8243 | 2015-10-13 16:02:47 -0700 | [diff] [blame] | 1089 | def test_debug_option(self): |
| 1090 | """Test parsing of the `--debug` option.""" |
Richard Barnette | 7bfcb03 | 2018-02-26 11:46:56 -0800 | [diff] [blame] | 1091 | arguments = self._parse_non_report_arguments(['--debug']) |
J. Richard Barnette | 02e8243 | 2015-10-13 16:02:47 -0700 | [diff] [blame] | 1092 | self.assertTrue(arguments.debug) |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 1093 | |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 1094 | def test_duration(self): |
| 1095 | """Test parsing of the `--duration` option.""" |
Richard Barnette | cf5d834 | 2017-10-24 18:13:11 -0700 | [diff] [blame] | 1096 | for opt in ['-d', '--duration']: |
| 1097 | for duration in ['1', '11']: |
Richard Barnette | 7bfcb03 | 2018-02-26 11:46:56 -0800 | [diff] [blame] | 1098 | arguments = self._parse_non_report_arguments([opt, duration]) |
Richard Barnette | cf5d834 | 2017-10-24 18:13:11 -0700 | [diff] [blame] | 1099 | self.assertEqual(arguments.duration, int(duration)) |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 1100 | |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 1101 | def _check_email_option(self, option, getlist): |
| 1102 | """Test parsing of e-mail address options. |
| 1103 | |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 1104 | This is a helper function to test the `--model-notify` and |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 1105 | `--pool-notify` options. It tests the following cases: |
| 1106 | * `--option a1` gives the list [a1] |
| 1107 | * `--option ' a1 '` gives the list [a1] |
| 1108 | * `--option a1 --option a2` gives the list [a1, a2] |
| 1109 | * `--option a1,a2` gives the list [a1, a2] |
| 1110 | * `--option 'a1, a2'` gives the list [a1, a2] |
| 1111 | |
| 1112 | @param option The option to be tested. |
| 1113 | @param getlist A function to return the option's value from |
| 1114 | parsed command line arguments. |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 1115 | """ |
| 1116 | a1 = 'mumble@mumbler.com' |
| 1117 | a2 = 'bumble@bumbler.org' |
Richard Barnette | 7bfcb03 | 2018-02-26 11:46:56 -0800 | [diff] [blame] | 1118 | arguments = self._parse_arguments([option, a1]) |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 1119 | self.assertEqual(getlist(arguments), [a1]) |
Richard Barnette | 7bfcb03 | 2018-02-26 11:46:56 -0800 | [diff] [blame] | 1120 | arguments = self._parse_arguments([option, ' ' + a1 + ' ']) |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 1121 | self.assertEqual(getlist(arguments), [a1]) |
Richard Barnette | 7bfcb03 | 2018-02-26 11:46:56 -0800 | [diff] [blame] | 1122 | arguments = self._parse_arguments([option, a1, option, a2]) |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 1123 | self.assertEqual(getlist(arguments), [a1, a2]) |
| 1124 | arguments = self._parse_arguments( |
Richard Barnette | 7bfcb03 | 2018-02-26 11:46:56 -0800 | [diff] [blame] | 1125 | [option, ','.join([a1, a2])]) |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 1126 | self.assertEqual(getlist(arguments), [a1, a2]) |
| 1127 | arguments = self._parse_arguments( |
Richard Barnette | 7bfcb03 | 2018-02-26 11:46:56 -0800 | [diff] [blame] | 1128 | [option, ', '.join([a1, a2])]) |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 1129 | self.assertEqual(getlist(arguments), [a1, a2]) |
| 1130 | |
Richard Barnette | 5de01eb | 2017-12-15 09:53:42 -0800 | [diff] [blame] | 1131 | def test_model_notify(self): |
| 1132 | """Test parsing of the `--model-notify` option.""" |
| 1133 | self._check_email_option('--model-notify', |
| 1134 | lambda a: a.model_notify) |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 1135 | |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 1136 | def test_pool_notify(self): |
| 1137 | """Test parsing of the `--pool-notify` option.""" |
| 1138 | self._check_email_option('--pool-notify', |
| 1139 | lambda a: a.pool_notify) |
| 1140 | |
Richard Barnette | cf5d834 | 2017-10-24 18:13:11 -0700 | [diff] [blame] | 1141 | def test_logdir_option(self): |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 1142 | """Test parsing of the `--logdir` option.""" |
| 1143 | logdir = '/usr/local/whatsis/logs' |
Richard Barnette | 7bfcb03 | 2018-02-26 11:46:56 -0800 | [diff] [blame] | 1144 | arguments = self._parse_non_report_arguments(['--logdir', logdir]) |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 1145 | self.assertEqual(arguments.logdir, logdir) |
| 1146 | |
| 1147 | |
| 1148 | if __name__ == '__main__': |
J. Richard Barnette | f60a1ee | 2015-06-02 10:52:37 -0700 | [diff] [blame] | 1149 | # Some of the functions we test log messages. Prevent those |
| 1150 | # messages from showing up in test output. |
| 1151 | logging.getLogger().setLevel(logging.CRITICAL) |
J. Richard Barnette | 96db349 | 2015-03-27 17:23:52 -0700 | [diff] [blame] | 1152 | unittest.main() |