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""" |
joychen | eaf4cfc | 2013-07-02 08:38:57 -0700 | [diff] [blame] | 47 | path = ('parrot-release', 'R27-2455.0.0', 'test') |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 48 | expected = ('parrot-release', 'R27-2455.0.0', 'test') |
joychen | eaf4cfc | 2013-07-02 08:38:57 -0700 | [diff] [blame] | 49 | self.assertEqual(self.mock_xb._InterpretPath(path_parts=path), expected) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 50 | |
joychen | eaf4cfc | 2013-07-02 08:38:57 -0700 | [diff] [blame] | 51 | path = ('parrot-release', 'R27-2455.0.0', 'full_payload') |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 52 | expected = ('parrot-release', 'R27-2455.0.0', 'full_payload') |
joychen | eaf4cfc | 2013-07-02 08:38:57 -0700 | [diff] [blame] | 53 | self.assertEqual(self.mock_xb._InterpretPath(path_parts=path), expected) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 54 | |
joychen | eaf4cfc | 2013-07-02 08:38:57 -0700 | [diff] [blame] | 55 | path = ('parrot-release', 'R27-2455.0.0') |
| 56 | expected = ('parrot-release', 'R27-2455.0.0', 'test') |
| 57 | self.assertEqual(self.mock_xb._InterpretPath(path_parts=path), expected) |
| 58 | |
| 59 | path = ('parrot-release', 'R27-2455.0.0', 'bad_alias') |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 60 | self.assertRaises(xbuddy.XBuddyException, |
| 61 | self.mock_xb._InterpretPath, |
joychen | eaf4cfc | 2013-07-02 08:38:57 -0700 | [diff] [blame] | 62 | path_parts=path) |
| 63 | |
| 64 | path = ('parrot-release', 'R27-2455.0.0', 'too', 'many', 'pieces') |
| 65 | self.assertRaises(xbuddy.XBuddyException, |
| 66 | self.mock_xb._InterpretPath, |
| 67 | path_parts=path) |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 68 | |
| 69 | def testUnpackArgsWithVersionAliases(self): |
| 70 | # TODO (joyc) |
| 71 | pass |
| 72 | |
| 73 | def testLookupVersion(self): |
| 74 | # TODO (joyc) |
| 75 | pass |
| 76 | |
| 77 | def testTimestampsAndList(self): |
| 78 | """Creation and listing of builds according to their timestamps.""" |
| 79 | # make 3 different timestamp files |
| 80 | build_id11 = 'b1/v1' |
| 81 | build_id12 = 'b1/v2' |
| 82 | build_id23 = 'b2/v3' |
| 83 | self.mock_xb._UpdateTimestamp(build_id11) |
| 84 | time.sleep(0.5) |
| 85 | self.mock_xb._UpdateTimestamp(build_id12) |
| 86 | time.sleep(0.5) |
| 87 | self.mock_xb._UpdateTimestamp(build_id23) |
| 88 | |
| 89 | # reference second one again |
| 90 | time.sleep(0.5) |
| 91 | self.mock_xb._UpdateTimestamp(build_id12) |
| 92 | |
| 93 | # check that list returns the same 3 things, in last referenced order |
| 94 | result = self.mock_xb._ListBuilds() |
| 95 | self.assertEqual(result[0][0], build_id12) |
| 96 | self.assertEqual(result[1][0], build_id23) |
| 97 | self.assertEqual(result[2][0], build_id11) |
| 98 | |
| 99 | ############### Public Methods |
| 100 | def testXBuddyCaching(self): |
| 101 | """Caching & replacement of timestamp files.""" |
| 102 | |
joychen | eaf4cfc | 2013-07-02 08:38:57 -0700 | [diff] [blame] | 103 | path_a = ('a', 'latest-local', 'test') |
| 104 | path_b = ('b', 'latest-local', 'test') |
| 105 | path_c = ('c', 'latest-local', 'test') |
| 106 | path_d = ('d', 'latest-local', 'test') |
| 107 | path_e = ('e', 'latest-local', 'test') |
| 108 | path_f = ('f', 'latest-local', 'test') |
joychen | 3cb228e | 2013-06-12 12:13:13 -0700 | [diff] [blame] | 109 | |
| 110 | self.mox.StubOutWithMock(self.mock_xb, '_ResolveVersion') |
| 111 | self.mox.StubOutWithMock(self.mock_xb, '_Download') |
| 112 | for _ in range(8): |
| 113 | self.mock_xb._ResolveVersion(mox.IsA(str), |
| 114 | mox.IsA(str)).AndReturn('latest-local') |
| 115 | self.mock_xb._Download(mox.IsA(str), mox.IsA(str)) |
| 116 | |
| 117 | self.mox.ReplayAll() |
| 118 | |
| 119 | # requires default capacity |
| 120 | self.assertEqual(self.mock_xb.Capacity(), '5') |
| 121 | |
| 122 | # Get 6 different images: a,b,c,d,e,f |
| 123 | self.mock_xb.Get(path_a, None) |
| 124 | time.sleep(0.5) |
| 125 | self.mock_xb.Get(path_b, None) |
| 126 | time.sleep(0.5) |
| 127 | self.mock_xb.Get(path_c, None) |
| 128 | time.sleep(0.5) |
| 129 | self.mock_xb.Get(path_d, None) |
| 130 | time.sleep(0.5) |
| 131 | self.mock_xb.Get(path_e, None) |
| 132 | time.sleep(0.5) |
| 133 | self.mock_xb.Get(path_f, None) |
| 134 | time.sleep(0.5) |
| 135 | |
| 136 | # check that b,c,d,e,f are still stored |
| 137 | result = self.mock_xb._ListBuilds() |
| 138 | self.assertEqual(len(result), 5) |
| 139 | self.assertEqual(result[4][0], 'b/latest-local') |
| 140 | self.assertEqual(result[3][0], 'c/latest-local') |
| 141 | self.assertEqual(result[2][0], 'd/latest-local') |
| 142 | self.assertEqual(result[1][0], 'e/latest-local') |
| 143 | self.assertEqual(result[0][0], 'f/latest-local') |
| 144 | |
| 145 | # Get b,a |
| 146 | self.mock_xb.Get(path_b, None) |
| 147 | time.sleep(0.5) |
| 148 | self.mock_xb.Get(path_a, None) |
| 149 | time.sleep(0.5) |
| 150 | |
| 151 | # check that d,e,f,b,a are still stored |
| 152 | result = self.mock_xb._ListBuilds() |
| 153 | self.assertEqual(len(result), 5) |
| 154 | self.assertEqual(result[4][0], 'd/latest-local') |
| 155 | self.assertEqual(result[3][0], 'e/latest-local') |
| 156 | self.assertEqual(result[2][0], 'f/latest-local') |
| 157 | self.assertEqual(result[1][0], 'b/latest-local') |
| 158 | self.assertEqual(result[0][0], 'a/latest-local') |
| 159 | |
| 160 | self.mox.VerifyAll() |
| 161 | |
| 162 | |
| 163 | if __name__ == '__main__': |
| 164 | unittest.main() |