blob: d4f90de8842841d74489872ac9b795bd0bdcbec2 [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
Simran Basi99e63c02014-05-20 10:39:52 -070021
22GS_ALTERNATE_DIR = 'gs://chromeos-alternate-archive/'
23
24
joychen3cb228e2013-06-12 12:13:13 -070025class xBuddyTest(mox.MoxTestBase):
26 """Regression tests for xbuddy."""
27 def setUp(self):
28 mox.MoxTestBase.setUp(self)
29
joychen921e1fb2013-06-28 11:12:20 -070030 self.static_image_dir = tempfile.mkdtemp('xbuddy_unittest_static')
joychen3cb228e2013-06-12 12:13:13 -070031
joychen921e1fb2013-06-28 11:12:20 -070032 self.mock_xb = xbuddy.XBuddy(
joychen5260b9a2013-07-16 14:48:01 -070033 True,
joychen921e1fb2013-06-28 11:12:20 -070034 static_dir=self.static_image_dir
35 )
36 self.images_dir = tempfile.mkdtemp('xbuddy_unittest_images')
37 self.mock_xb.images_dir = self.images_dir
joychen3cb228e2013-06-12 12:13:13 -070038
39 def tearDown(self):
40 """Removes testing files."""
41 shutil.rmtree(self.static_image_dir)
joychen921e1fb2013-06-28 11:12:20 -070042 shutil.rmtree(self.images_dir)
joychen3cb228e2013-06-12 12:13:13 -070043
44 def testParseBoolean(self):
45 """Check that some common True/False strings are handled."""
46 self.assertEqual(xbuddy.XBuddy.ParseBoolean(None), False)
47 self.assertEqual(xbuddy.XBuddy.ParseBoolean('false'), False)
48 self.assertEqual(xbuddy.XBuddy.ParseBoolean('bs'), False)
49 self.assertEqual(xbuddy.XBuddy.ParseBoolean('true'), True)
50 self.assertEqual(xbuddy.XBuddy.ParseBoolean('y'), True)
51
joychenf8f07e22013-07-12 17:45:51 -070052 def testLookupOfficial(self):
53 """Basic test of _LookupOfficial. Checks that a given suffix is handled."""
54 self.mox.StubOutWithMock(gsutil_util, 'GSUtilRun')
55 gsutil_util.GSUtilRun(mox.IgnoreArg(),
56 mox.IgnoreArg()).AndReturn('v')
57 expected = 'b-s/v'
58 self.mox.ReplayAll()
Chris Sosaea734d92013-10-11 11:28:58 -070059 self.assertEqual(self.mock_xb._LookupOfficial('b', '-s'), expected)
joychenf8f07e22013-07-12 17:45:51 -070060 self.mox.VerifyAll()
61
62 def testLookupChannel(self):
63 """Basic test of _LookupChannel. Checks that a given suffix is handled."""
64 self.mox.StubOutWithMock(gsutil_util, 'GetLatestVersionFromGSDir')
65 mock_data1 = '4100.68.0'
joychen562699a2013-08-13 15:22:14 -070066 gsutil_util.GetLatestVersionFromGSDir(
67 mox.IgnoreArg(), with_release=False).AndReturn(mock_data1)
joychenf8f07e22013-07-12 17:45:51 -070068 mock_data2 = 'R28-4100.68.0'
69 gsutil_util.GetLatestVersionFromGSDir(mox.IgnoreArg()).AndReturn(mock_data2)
70 self.mox.ReplayAll()
71 expected = 'b-release/R28-4100.68.0'
Simran Basi99e63c02014-05-20 10:39:52 -070072 self.assertEqual(self.mock_xb._LookupChannel('b'),
73 expected)
joychenf8f07e22013-07-12 17:45:51 -070074 self.mox.VerifyAll()
75
Chris Sosaea734d92013-10-11 11:28:58 -070076 def testResolveVersionToBuildId_Official(self):
77 """Check _ResolveVersionToBuildId recognizes aliases for official builds."""
joychenf8f07e22013-07-12 17:45:51 -070078 board = 'b'
79
80 # aliases that should be redirected to LookupOfficial
Chris Sosaea734d92013-10-11 11:28:58 -070081
joychenf8f07e22013-07-12 17:45:51 -070082 self.mox.StubOutWithMock(self.mock_xb, '_LookupOfficial')
Simran Basi99e63c02014-05-20 10:39:52 -070083 self.mock_xb._LookupOfficial(board, image_dir=None)
84 self.mock_xb._LookupOfficial(board,
85 image_dir=GS_ALTERNATE_DIR)
86 self.mock_xb._LookupOfficial(board, 'paladin', image_dir=None)
87 self.mock_xb._LookupOfficial(board, 'paladin',
88 image_dir=GS_ALTERNATE_DIR)
joychenf8f07e22013-07-12 17:45:51 -070089
90 self.mox.ReplayAll()
91 version = 'latest-official'
Chris Sosaea734d92013-10-11 11:28:58 -070092 self.mock_xb._ResolveVersionToBuildId(board, version)
Simran Basi99e63c02014-05-20 10:39:52 -070093 self.mock_xb._ResolveVersionToBuildId(board, version,
94 image_dir=GS_ALTERNATE_DIR)
joychenf8f07e22013-07-12 17:45:51 -070095 version = 'latest-official-paladin'
Chris Sosaea734d92013-10-11 11:28:58 -070096 self.mock_xb._ResolveVersionToBuildId(board, version)
Simran Basi99e63c02014-05-20 10:39:52 -070097 self.mock_xb._ResolveVersionToBuildId(board, version,
98 image_dir=GS_ALTERNATE_DIR)
joychenf8f07e22013-07-12 17:45:51 -070099 self.mox.VerifyAll()
100
Chris Sosaea734d92013-10-11 11:28:58 -0700101 def testResolveVersionToBuildId_Channel(self):
102 """Check _ResolveVersionToBuildId recognizes aliases for channels."""
joychenf8f07e22013-07-12 17:45:51 -0700103 board = 'b'
104
105 # aliases that should be redirected to LookupChannel
106 self.mox.StubOutWithMock(self.mock_xb, '_LookupChannel')
Simran Basi99e63c02014-05-20 10:39:52 -0700107 self.mock_xb._LookupChannel(board, image_dir=None)
108 self.mock_xb._LookupChannel(board, image_dir=GS_ALTERNATE_DIR)
109 self.mock_xb._LookupChannel(board, 'dev', image_dir=None)
110 self.mock_xb._LookupChannel(board, 'dev', image_dir=GS_ALTERNATE_DIR)
joychenf8f07e22013-07-12 17:45:51 -0700111
112 self.mox.ReplayAll()
113 version = 'latest'
Chris Sosaea734d92013-10-11 11:28:58 -0700114 self.mock_xb._ResolveVersionToBuildId(board, version)
Simran Basi99e63c02014-05-20 10:39:52 -0700115 self.mock_xb._ResolveVersionToBuildId(board, version,
116 image_dir=GS_ALTERNATE_DIR)
joychenf8f07e22013-07-12 17:45:51 -0700117 version = 'latest-dev'
Chris Sosaea734d92013-10-11 11:28:58 -0700118 self.mock_xb._ResolveVersionToBuildId(board, version)
Simran Basi99e63c02014-05-20 10:39:52 -0700119 self.mock_xb._ResolveVersionToBuildId(board, version,
120 image_dir=GS_ALTERNATE_DIR)
joychenf8f07e22013-07-12 17:45:51 -0700121 self.mox.VerifyAll()
joychen3cb228e2013-06-12 12:13:13 -0700122
123 def testBasicInterpretPath(self):
124 """Basic checks for splitting a path"""
joychen121fc9b2013-08-02 14:30:30 -0700125 path = 'parrot/R27-2455.0.0/test'
joychen7df67f72013-07-18 14:21:12 -0700126 expected = ('test', 'parrot', 'R27-2455.0.0', True)
joychen121fc9b2013-08-02 14:30:30 -0700127 self.assertEqual(self.mock_xb._InterpretPath(path=path), expected)
joychen7df67f72013-07-18 14:21:12 -0700128
joychen121fc9b2013-08-02 14:30:30 -0700129 path = 'parrot/R27-2455.0.0/full_payload'
130 expected = ('full_payload', 'parrot', 'R27-2455.0.0', True)
131 self.assertEqual(self.mock_xb._InterpretPath(path=path), expected)
132
133 path = 'parrot/R27-2455.0.0'
Chris Sosa0eecf962014-02-03 14:14:39 -0800134 expected = ('ANY', 'parrot', 'R27-2455.0.0', True)
joychen121fc9b2013-08-02 14:30:30 -0700135 self.assertEqual(self.mock_xb._InterpretPath(path=path), expected)
136
137 path = 'remote/parrot/R27-2455.0.0'
138 expected = ('test', 'parrot', 'R27-2455.0.0', False)
139 self.assertEqual(self.mock_xb._InterpretPath(path=path), expected)
140
141 path = 'local/parrot/R27-2455.0.0'
Chris Sosa0eecf962014-02-03 14:14:39 -0800142 expected = ('ANY', 'parrot', 'R27-2455.0.0', True)
joychen121fc9b2013-08-02 14:30:30 -0700143 self.assertEqual(self.mock_xb._InterpretPath(path=path), expected)
144
145 path = ''
Chris Sosa0eecf962014-02-03 14:14:39 -0800146 expected = ('ANY', None, 'latest', True)
147 self.assertEqual(self.mock_xb._InterpretPath(path=path), expected)
joychen3cb228e2013-06-12 12:13:13 -0700148
joychen121fc9b2013-08-02 14:30:30 -0700149 path = 'local'
Chris Sosa0eecf962014-02-03 14:14:39 -0800150 expected = ('ANY', None, 'latest', True)
151 self.assertEqual(self.mock_xb._InterpretPath(path=path), expected)
joychen7df67f72013-07-18 14:21:12 -0700152
joychenc3944cb2013-08-19 10:42:07 -0700153 path = 'local/parrot/latest/ANY'
154 expected = ('ANY', 'parrot', 'latest', True)
155 self.assertEqual(self.mock_xb._InterpretPath(path=path), expected)
156
joychen7df67f72013-07-18 14:21:12 -0700157
joychen3cb228e2013-06-12 12:13:13 -0700158 def testTimestampsAndList(self):
159 """Creation and listing of builds according to their timestamps."""
160 # make 3 different timestamp files
joychen921e1fb2013-06-28 11:12:20 -0700161 b_id11 = 'b1/v1'
162 b_id12 = 'b1/v2'
163 b_id23 = 'b2/v3'
164 xbuddy.Timestamp.UpdateTimestamp(self.mock_xb._timestamp_folder, b_id11)
165 time.sleep(0.05)
166 xbuddy.Timestamp.UpdateTimestamp(self.mock_xb._timestamp_folder, b_id12)
167 time.sleep(0.05)
168 xbuddy.Timestamp.UpdateTimestamp(self.mock_xb._timestamp_folder, b_id23)
joychen3cb228e2013-06-12 12:13:13 -0700169
170 # reference second one again
joychen921e1fb2013-06-28 11:12:20 -0700171 time.sleep(0.05)
172 xbuddy.Timestamp.UpdateTimestamp(self.mock_xb._timestamp_folder, b_id12)
joychen3cb228e2013-06-12 12:13:13 -0700173
174 # check that list returns the same 3 things, in last referenced order
joychen921e1fb2013-06-28 11:12:20 -0700175 result = self.mock_xb._ListBuildTimes()
176 self.assertEqual(result[0][0], b_id12)
177 self.assertEqual(result[1][0], b_id23)
178 self.assertEqual(result[2][0], b_id11)
179
180 def testSyncRegistry(self):
181 # check that there are no builds initially
182 result = self.mock_xb._ListBuildTimes()
183 self.assertEqual(len(result), 0)
184
185 # set up the dummy build/images directory with images
186 boards = ['a', 'b']
187 versions = ['v1', 'v2']
188 for b in boards:
189 os.makedirs(os.path.join(self.mock_xb.images_dir, b))
190 for v in versions:
191 os.makedirs(os.path.join(self.mock_xb.images_dir, b, v))
192
193 # Sync and check that they've been added to xBuddy's registry
194 self.mock_xb._SyncRegistryWithBuildImages()
195 result = self.mock_xb._ListBuildTimes()
196 self.assertEqual(len(result), 4)
joychen3cb228e2013-06-12 12:13:13 -0700197
198 ############### Public Methods
199 def testXBuddyCaching(self):
200 """Caching & replacement of timestamp files."""
joychen121fc9b2013-08-02 14:30:30 -0700201 path_a = ('remote', 'a', 'R0', 'test')
202 path_b = ('remote', 'b', 'R0', 'test')
Chris Sosaea734d92013-10-11 11:28:58 -0700203 self.mox.StubOutWithMock(gsutil_util, 'GSUtilRun')
204 self.mox.StubOutWithMock(self.mock_xb, '_Download')
joychen3cb228e2013-06-12 12:13:13 -0700205 self.mox.StubOutWithMock(self.mock_xb, '_Download')
206 for _ in range(8):
Chris Sosa75490802013-09-30 17:21:45 -0700207 self.mock_xb._Download(mox.IsA(str), mox.In(mox.IsA(str)))
joychen3cb228e2013-06-12 12:13:13 -0700208
Chris Sosaea734d92013-10-11 11:28:58 -0700209 # All non-release urls are invalid so we can meet expectations.
210 gsutil_util.GSUtilRun(mox.Not(mox.StrContains('-release')),
211 None).MultipleTimes().AndRaise(
212 gsutil_util.GSUtilError('bad url'))
213 gsutil_util.GSUtilRun(mox.StrContains('-release'), None).MultipleTimes()
214
joychen3cb228e2013-06-12 12:13:13 -0700215 self.mox.ReplayAll()
216
217 # requires default capacity
218 self.assertEqual(self.mock_xb.Capacity(), '5')
219
220 # Get 6 different images: a,b,c,d,e,f
joychen921e1fb2013-06-28 11:12:20 -0700221 images = ['a', 'b', 'c', 'd', 'e', 'f']
222 for c in images:
joychen562699a2013-08-13 15:22:14 -0700223 self.mock_xb.Get(('remote', c, 'R0', 'test'))
joychen921e1fb2013-06-28 11:12:20 -0700224 time.sleep(0.05)
joychen3cb228e2013-06-12 12:13:13 -0700225
226 # check that b,c,d,e,f are still stored
joychen921e1fb2013-06-28 11:12:20 -0700227 result = self.mock_xb._ListBuildTimes()
joychen3cb228e2013-06-12 12:13:13 -0700228 self.assertEqual(len(result), 5)
joychen921e1fb2013-06-28 11:12:20 -0700229
230 # Flip the list to get reverse chronological order
231 images.reverse()
232 for i in range(5):
joychenf8f07e22013-07-12 17:45:51 -0700233 self.assertEqual(result[i][0], '%s-release/R0' % images[i])
joychen3cb228e2013-06-12 12:13:13 -0700234
235 # Get b,a
joychen562699a2013-08-13 15:22:14 -0700236 self.mock_xb.Get(path_b)
joychen921e1fb2013-06-28 11:12:20 -0700237 time.sleep(0.05)
joychen562699a2013-08-13 15:22:14 -0700238 self.mock_xb.Get(path_a)
joychen921e1fb2013-06-28 11:12:20 -0700239 time.sleep(0.05)
joychen3cb228e2013-06-12 12:13:13 -0700240
241 # check that d,e,f,b,a are still stored
joychen921e1fb2013-06-28 11:12:20 -0700242 result = self.mock_xb._ListBuildTimes()
joychen3cb228e2013-06-12 12:13:13 -0700243 self.assertEqual(len(result), 5)
joychen921e1fb2013-06-28 11:12:20 -0700244 images_expected = ['a', 'b', 'f', 'e', 'd']
245 for i in range(5):
joychenf8f07e22013-07-12 17:45:51 -0700246 self.assertEqual(result[i][0], '%s-release/R0' % images_expected[i])
joychen3cb228e2013-06-12 12:13:13 -0700247
248 self.mox.VerifyAll()
249
250
251if __name__ == '__main__':
252 unittest.main()