joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame^] | 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 | |
| 7 | """Unit tests for xbuddy.py.""" |
| 8 | |
| 9 | import os |
| 10 | import shutil |
| 11 | import time |
| 12 | import unittest |
| 13 | |
| 14 | import mox |
| 15 | |
| 16 | import xbuddy |
| 17 | |
| 18 | #pylint: disable=W0212 |
| 19 | class xBuddyTest(mox.MoxTestBase): |
| 20 | """Regression tests for xbuddy.""" |
| 21 | def setUp(self): |
| 22 | mox.MoxTestBase.setUp(self) |
| 23 | |
| 24 | self.static_image_dir = '/tmp/static-dir/' |
| 25 | |
| 26 | self.mock_xb = xbuddy.XBuddy(self.static_image_dir) |
| 27 | os.makedirs(self.static_image_dir) |
| 28 | |
| 29 | def tearDown(self): |
| 30 | """Removes testing files.""" |
| 31 | shutil.rmtree(self.static_image_dir) |
| 32 | |
| 33 | def testParseBoolean(self): |
| 34 | """Check that some common True/False strings are handled.""" |
| 35 | self.assertEqual(xbuddy.XBuddy.ParseBoolean(None), False) |
| 36 | self.assertEqual(xbuddy.XBuddy.ParseBoolean('false'), False) |
| 37 | self.assertEqual(xbuddy.XBuddy.ParseBoolean('bs'), False) |
| 38 | self.assertEqual(xbuddy.XBuddy.ParseBoolean('true'), True) |
| 39 | self.assertEqual(xbuddy.XBuddy.ParseBoolean('y'), True) |
| 40 | |
| 41 | def _testResolveVersion(self): |
| 42 | # TODO (joyc) |
| 43 | pass |
| 44 | |
| 45 | def testBasicInterpretPath(self): |
| 46 | """Basic checks for splitting a path""" |
| 47 | path = "parrot-release/R27-2455.0.0/test" |
| 48 | expected = ('parrot-release', 'R27-2455.0.0', 'test') |
| 49 | self.assertEqual(self.mock_xb._InterpretPath(path=path), expected) |
| 50 | |
| 51 | path = "parrot-release/R27-2455.0.0/full_payload" |
| 52 | expected = ('parrot-release', 'R27-2455.0.0', 'full_payload') |
| 53 | self.assertEqual(self.mock_xb._InterpretPath(path=path), expected) |
| 54 | |
| 55 | path = "parrot-release/R27-2455.0.0/bad_alias" |
| 56 | self.assertRaises(xbuddy.XBuddyException, |
| 57 | self.mock_xb._InterpretPath, |
| 58 | path=path) |
| 59 | |
| 60 | def testUnpackArgsWithVersionAliases(self): |
| 61 | # TODO (joyc) |
| 62 | pass |
| 63 | |
| 64 | def testLookupVersion(self): |
| 65 | # TODO (joyc) |
| 66 | pass |
| 67 | |
| 68 | def testTimestampsAndList(self): |
| 69 | """Creation and listing of builds according to their timestamps.""" |
| 70 | # make 3 different timestamp files |
| 71 | build_id11 = 'b1/v1' |
| 72 | build_id12 = 'b1/v2' |
| 73 | build_id23 = 'b2/v3' |
| 74 | self.mock_xb._UpdateTimestamp(build_id11) |
| 75 | time.sleep(0.5) |
| 76 | self.mock_xb._UpdateTimestamp(build_id12) |
| 77 | time.sleep(0.5) |
| 78 | self.mock_xb._UpdateTimestamp(build_id23) |
| 79 | |
| 80 | # reference second one again |
| 81 | time.sleep(0.5) |
| 82 | self.mock_xb._UpdateTimestamp(build_id12) |
| 83 | |
| 84 | # check that list returns the same 3 things, in last referenced order |
| 85 | result = self.mock_xb._ListBuilds() |
| 86 | self.assertEqual(result[0][0], build_id12) |
| 87 | self.assertEqual(result[1][0], build_id23) |
| 88 | self.assertEqual(result[2][0], build_id11) |
| 89 | |
| 90 | ############### Public Methods |
| 91 | def testXBuddyCaching(self): |
| 92 | """Caching & replacement of timestamp files.""" |
| 93 | |
| 94 | path_a = "a/latest-local/test" |
| 95 | path_b = "b/latest-local/test" |
| 96 | path_c = "c/latest-local/test" |
| 97 | path_d = "d/latest-local/test" |
| 98 | path_e = "e/latest-local/test" |
| 99 | path_f = "f/latest-local/test" |
| 100 | |
| 101 | self.mox.StubOutWithMock(self.mock_xb, '_ResolveVersion') |
| 102 | self.mox.StubOutWithMock(self.mock_xb, '_Download') |
| 103 | for _ in range(8): |
| 104 | self.mock_xb._ResolveVersion(mox.IsA(str), |
| 105 | mox.IsA(str)).AndReturn('latest-local') |
| 106 | self.mock_xb._Download(mox.IsA(str), mox.IsA(str)) |
| 107 | |
| 108 | self.mox.ReplayAll() |
| 109 | |
| 110 | # requires default capacity |
| 111 | self.assertEqual(self.mock_xb.Capacity(), '5') |
| 112 | |
| 113 | # Get 6 different images: a,b,c,d,e,f |
| 114 | self.mock_xb.Get(path_a, None) |
| 115 | time.sleep(0.5) |
| 116 | self.mock_xb.Get(path_b, None) |
| 117 | time.sleep(0.5) |
| 118 | self.mock_xb.Get(path_c, None) |
| 119 | time.sleep(0.5) |
| 120 | self.mock_xb.Get(path_d, None) |
| 121 | time.sleep(0.5) |
| 122 | self.mock_xb.Get(path_e, None) |
| 123 | time.sleep(0.5) |
| 124 | self.mock_xb.Get(path_f, None) |
| 125 | time.sleep(0.5) |
| 126 | |
| 127 | # check that b,c,d,e,f are still stored |
| 128 | result = self.mock_xb._ListBuilds() |
| 129 | self.assertEqual(len(result), 5) |
| 130 | self.assertEqual(result[4][0], 'b/latest-local') |
| 131 | self.assertEqual(result[3][0], 'c/latest-local') |
| 132 | self.assertEqual(result[2][0], 'd/latest-local') |
| 133 | self.assertEqual(result[1][0], 'e/latest-local') |
| 134 | self.assertEqual(result[0][0], 'f/latest-local') |
| 135 | |
| 136 | # Get b,a |
| 137 | self.mock_xb.Get(path_b, None) |
| 138 | time.sleep(0.5) |
| 139 | self.mock_xb.Get(path_a, None) |
| 140 | time.sleep(0.5) |
| 141 | |
| 142 | # check that d,e,f,b,a are still stored |
| 143 | result = self.mock_xb._ListBuilds() |
| 144 | self.assertEqual(len(result), 5) |
| 145 | self.assertEqual(result[4][0], 'd/latest-local') |
| 146 | self.assertEqual(result[3][0], 'e/latest-local') |
| 147 | self.assertEqual(result[2][0], 'f/latest-local') |
| 148 | self.assertEqual(result[1][0], 'b/latest-local') |
| 149 | self.assertEqual(result[0][0], 'a/latest-local') |
| 150 | |
| 151 | self.mox.VerifyAll() |
| 152 | |
| 153 | |
| 154 | if __name__ == '__main__': |
| 155 | unittest.main() |