blob: 24c765f2db1607d8ca1c14a07cf7146b1c88ed20 [file] [log] [blame]
joychen3cb228e2013-06-12 12:13:13 -07001#!/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
9import os
10import shutil
joychen921e1fb2013-06-28 11:12:20 -070011import tempfile
joychen3cb228e2013-06-12 12:13:13 -070012import time
13import unittest
14
15import mox
16
joychenf8f07e22013-07-12 17:45:51 -070017import gsutil_util
joychen3cb228e2013-06-12 12:13:13 -070018import xbuddy
19
20#pylint: disable=W0212
21class xBuddyTest(mox.MoxTestBase):
22 """Regression tests for xbuddy."""
23 def setUp(self):
24 mox.MoxTestBase.setUp(self)
25
joychen921e1fb2013-06-28 11:12:20 -070026 self.static_image_dir = tempfile.mkdtemp('xbuddy_unittest_static')
27 self.root_dir = tempfile.mkdtemp('xbuddy_unittest_ds_root')
joychen3cb228e2013-06-12 12:13:13 -070028
joychen921e1fb2013-06-28 11:12:20 -070029 self.mock_xb = xbuddy.XBuddy(
joychen5260b9a2013-07-16 14:48:01 -070030 True,
joychen921e1fb2013-06-28 11:12:20 -070031 root_dir=self.root_dir,
32 static_dir=self.static_image_dir
33 )
34 self.images_dir = tempfile.mkdtemp('xbuddy_unittest_images')
35 self.mock_xb.images_dir = self.images_dir
joychen3cb228e2013-06-12 12:13:13 -070036
37 def tearDown(self):
38 """Removes testing files."""
39 shutil.rmtree(self.static_image_dir)
joychen921e1fb2013-06-28 11:12:20 -070040 shutil.rmtree(self.images_dir)
joychen3cb228e2013-06-12 12:13:13 -070041
42 def testParseBoolean(self):
43 """Check that some common True/False strings are handled."""
44 self.assertEqual(xbuddy.XBuddy.ParseBoolean(None), False)
45 self.assertEqual(xbuddy.XBuddy.ParseBoolean('false'), False)
46 self.assertEqual(xbuddy.XBuddy.ParseBoolean('bs'), False)
47 self.assertEqual(xbuddy.XBuddy.ParseBoolean('true'), True)
48 self.assertEqual(xbuddy.XBuddy.ParseBoolean('y'), True)
49
joychenf8f07e22013-07-12 17:45:51 -070050 def testLookupOfficial(self):
51 """Basic test of _LookupOfficial. Checks that a given suffix is handled."""
52 self.mox.StubOutWithMock(gsutil_util, 'GSUtilRun')
53 gsutil_util.GSUtilRun(mox.IgnoreArg(),
54 mox.IgnoreArg()).AndReturn('v')
55 expected = 'b-s/v'
56 self.mox.ReplayAll()
57 self.assertEqual(self.mock_xb._LookupOfficial('b', 's'), expected)
58 self.mox.VerifyAll()
59
60 def testLookupChannel(self):
61 """Basic test of _LookupChannel. Checks that a given suffix is handled."""
62 self.mox.StubOutWithMock(gsutil_util, 'GetLatestVersionFromGSDir')
63 mock_data1 = '4100.68.0'
joychen562699a2013-08-13 15:22:14 -070064 gsutil_util.GetLatestVersionFromGSDir(
65 mox.IgnoreArg(), with_release=False).AndReturn(mock_data1)
joychenf8f07e22013-07-12 17:45:51 -070066 mock_data2 = 'R28-4100.68.0'
67 gsutil_util.GetLatestVersionFromGSDir(mox.IgnoreArg()).AndReturn(mock_data2)
68 self.mox.ReplayAll()
69 expected = 'b-release/R28-4100.68.0'
70 self.assertEqual(self.mock_xb._LookupChannel('b'), expected)
71 self.mox.VerifyAll()
72
73 def testResolveVersionToUrl_Official(self):
74 """Check _ResolveVersionToUrl recognizes aliases for official builds."""
75 board = 'b'
76
77 # aliases that should be redirected to LookupOfficial
78 self.mox.StubOutWithMock(self.mock_xb, '_LookupOfficial')
79 self.mock_xb._LookupOfficial(board)
80 self.mock_xb._LookupOfficial(board, 'paladin')
81
82 self.mox.ReplayAll()
83 version = 'latest-official'
84 self.mock_xb._ResolveVersionToUrl(board, version)
85 version = 'latest-official-paladin'
86 self.mock_xb._ResolveVersionToUrl(board, version)
87 self.mox.VerifyAll()
88
89 def testResolveVersionToUrl_Channel(self):
90 """Check _ResolveVersionToUrl recognizes aliases for channels."""
91 board = 'b'
92
93 # aliases that should be redirected to LookupChannel
94 self.mox.StubOutWithMock(self.mock_xb, '_LookupChannel')
95 self.mock_xb._LookupChannel(board)
96 self.mock_xb._LookupChannel(board, 'dev')
97
98 self.mox.ReplayAll()
99 version = 'latest'
100 self.mock_xb._ResolveVersionToUrl(board, version)
101 version = 'latest-dev'
102 self.mock_xb._ResolveVersionToUrl(board, version)
103 self.mox.VerifyAll()
joychen3cb228e2013-06-12 12:13:13 -0700104
105 def testBasicInterpretPath(self):
106 """Basic checks for splitting a path"""
joychen121fc9b2013-08-02 14:30:30 -0700107 path = 'parrot/R27-2455.0.0/test'
joychen7df67f72013-07-18 14:21:12 -0700108 expected = ('test', 'parrot', 'R27-2455.0.0', True)
joychen121fc9b2013-08-02 14:30:30 -0700109 self.assertEqual(self.mock_xb._InterpretPath(path=path), expected)
joychen7df67f72013-07-18 14:21:12 -0700110
joychen121fc9b2013-08-02 14:30:30 -0700111 path = 'parrot/R27-2455.0.0/full_payload'
112 expected = ('full_payload', 'parrot', 'R27-2455.0.0', True)
113 self.assertEqual(self.mock_xb._InterpretPath(path=path), expected)
114
115 path = 'parrot/R27-2455.0.0'
116 expected = ('test', 'parrot', 'R27-2455.0.0', True)
117 self.assertEqual(self.mock_xb._InterpretPath(path=path), expected)
118
119 path = 'remote/parrot/R27-2455.0.0'
120 expected = ('test', 'parrot', 'R27-2455.0.0', False)
121 self.assertEqual(self.mock_xb._InterpretPath(path=path), expected)
122
123 path = 'local/parrot/R27-2455.0.0'
124 expected = ('test', 'parrot', 'R27-2455.0.0', True)
125 self.assertEqual(self.mock_xb._InterpretPath(path=path), expected)
126
127 path = ''
joycheneaf4cfc2013-07-02 08:38:57 -0700128 self.assertRaises(xbuddy.XBuddyException,
129 self.mock_xb._InterpretPath,
joychen121fc9b2013-08-02 14:30:30 -0700130 path=path)
joychen3cb228e2013-06-12 12:13:13 -0700131
joychen121fc9b2013-08-02 14:30:30 -0700132 path = 'local'
joychen7df67f72013-07-18 14:21:12 -0700133 self.assertRaises(xbuddy.XBuddyException,
134 self.mock_xb._InterpretPath,
joychen121fc9b2013-08-02 14:30:30 -0700135 path=path)
joychen7df67f72013-07-18 14:21:12 -0700136
137
joychen3cb228e2013-06-12 12:13:13 -0700138 def testTimestampsAndList(self):
139 """Creation and listing of builds according to their timestamps."""
140 # make 3 different timestamp files
joychen921e1fb2013-06-28 11:12:20 -0700141 b_id11 = 'b1/v1'
142 b_id12 = 'b1/v2'
143 b_id23 = 'b2/v3'
144 xbuddy.Timestamp.UpdateTimestamp(self.mock_xb._timestamp_folder, b_id11)
145 time.sleep(0.05)
146 xbuddy.Timestamp.UpdateTimestamp(self.mock_xb._timestamp_folder, b_id12)
147 time.sleep(0.05)
148 xbuddy.Timestamp.UpdateTimestamp(self.mock_xb._timestamp_folder, b_id23)
joychen3cb228e2013-06-12 12:13:13 -0700149
150 # reference second one again
joychen921e1fb2013-06-28 11:12:20 -0700151 time.sleep(0.05)
152 xbuddy.Timestamp.UpdateTimestamp(self.mock_xb._timestamp_folder, b_id12)
joychen3cb228e2013-06-12 12:13:13 -0700153
154 # check that list returns the same 3 things, in last referenced order
joychen921e1fb2013-06-28 11:12:20 -0700155 result = self.mock_xb._ListBuildTimes()
156 self.assertEqual(result[0][0], b_id12)
157 self.assertEqual(result[1][0], b_id23)
158 self.assertEqual(result[2][0], b_id11)
159
160 def testSyncRegistry(self):
161 # check that there are no builds initially
162 result = self.mock_xb._ListBuildTimes()
163 self.assertEqual(len(result), 0)
164
165 # set up the dummy build/images directory with images
166 boards = ['a', 'b']
167 versions = ['v1', 'v2']
168 for b in boards:
169 os.makedirs(os.path.join(self.mock_xb.images_dir, b))
170 for v in versions:
171 os.makedirs(os.path.join(self.mock_xb.images_dir, b, v))
172
173 # Sync and check that they've been added to xBuddy's registry
174 self.mock_xb._SyncRegistryWithBuildImages()
175 result = self.mock_xb._ListBuildTimes()
176 self.assertEqual(len(result), 4)
joychen3cb228e2013-06-12 12:13:13 -0700177
178 ############### Public Methods
179 def testXBuddyCaching(self):
180 """Caching & replacement of timestamp files."""
joychen121fc9b2013-08-02 14:30:30 -0700181 path_a = ('remote', 'a', 'R0', 'test')
182 path_b = ('remote', 'b', 'R0', 'test')
joychen3cb228e2013-06-12 12:13:13 -0700183
joychen3cb228e2013-06-12 12:13:13 -0700184 self.mox.StubOutWithMock(self.mock_xb, '_Download')
185 for _ in range(8):
joychen3cb228e2013-06-12 12:13:13 -0700186 self.mock_xb._Download(mox.IsA(str), mox.IsA(str))
187
188 self.mox.ReplayAll()
189
190 # requires default capacity
191 self.assertEqual(self.mock_xb.Capacity(), '5')
192
193 # Get 6 different images: a,b,c,d,e,f
joychen921e1fb2013-06-28 11:12:20 -0700194 images = ['a', 'b', 'c', 'd', 'e', 'f']
195 for c in images:
joychen562699a2013-08-13 15:22:14 -0700196 self.mock_xb.Get(('remote', c, 'R0', 'test'))
joychen921e1fb2013-06-28 11:12:20 -0700197 time.sleep(0.05)
joychen3cb228e2013-06-12 12:13:13 -0700198
199 # check that b,c,d,e,f are still stored
joychen921e1fb2013-06-28 11:12:20 -0700200 result = self.mock_xb._ListBuildTimes()
joychen3cb228e2013-06-12 12:13:13 -0700201 self.assertEqual(len(result), 5)
joychen921e1fb2013-06-28 11:12:20 -0700202
203 # Flip the list to get reverse chronological order
204 images.reverse()
205 for i in range(5):
joychenf8f07e22013-07-12 17:45:51 -0700206 self.assertEqual(result[i][0], '%s-release/R0' % images[i])
joychen3cb228e2013-06-12 12:13:13 -0700207
208 # Get b,a
joychen562699a2013-08-13 15:22:14 -0700209 self.mock_xb.Get(path_b)
joychen921e1fb2013-06-28 11:12:20 -0700210 time.sleep(0.05)
joychen562699a2013-08-13 15:22:14 -0700211 self.mock_xb.Get(path_a)
joychen921e1fb2013-06-28 11:12:20 -0700212 time.sleep(0.05)
joychen3cb228e2013-06-12 12:13:13 -0700213
214 # check that d,e,f,b,a are still stored
joychen921e1fb2013-06-28 11:12:20 -0700215 result = self.mock_xb._ListBuildTimes()
joychen3cb228e2013-06-12 12:13:13 -0700216 self.assertEqual(len(result), 5)
joychen921e1fb2013-06-28 11:12:20 -0700217 images_expected = ['a', 'b', 'f', 'e', 'd']
218 for i in range(5):
joychenf8f07e22013-07-12 17:45:51 -0700219 self.assertEqual(result[i][0], '%s-release/R0' % images_expected[i])
joychen3cb228e2013-06-12 12:13:13 -0700220
221 self.mox.VerifyAll()
222
223
224if __name__ == '__main__':
225 unittest.main()