blob: 517c5a777ef44ecd193d3e6fe9e414864c9f8ed1 [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
joychenc3944cb2013-08-19 10:42:07 -0700137 path = 'local/parrot/latest/ANY'
138 expected = ('ANY', 'parrot', 'latest', True)
139 self.assertEqual(self.mock_xb._InterpretPath(path=path), expected)
140
joychen7df67f72013-07-18 14:21:12 -0700141
joychen3cb228e2013-06-12 12:13:13 -0700142 def testTimestampsAndList(self):
143 """Creation and listing of builds according to their timestamps."""
144 # make 3 different timestamp files
joychen921e1fb2013-06-28 11:12:20 -0700145 b_id11 = 'b1/v1'
146 b_id12 = 'b1/v2'
147 b_id23 = 'b2/v3'
148 xbuddy.Timestamp.UpdateTimestamp(self.mock_xb._timestamp_folder, b_id11)
149 time.sleep(0.05)
150 xbuddy.Timestamp.UpdateTimestamp(self.mock_xb._timestamp_folder, b_id12)
151 time.sleep(0.05)
152 xbuddy.Timestamp.UpdateTimestamp(self.mock_xb._timestamp_folder, b_id23)
joychen3cb228e2013-06-12 12:13:13 -0700153
154 # reference second one again
joychen921e1fb2013-06-28 11:12:20 -0700155 time.sleep(0.05)
156 xbuddy.Timestamp.UpdateTimestamp(self.mock_xb._timestamp_folder, b_id12)
joychen3cb228e2013-06-12 12:13:13 -0700157
158 # check that list returns the same 3 things, in last referenced order
joychen921e1fb2013-06-28 11:12:20 -0700159 result = self.mock_xb._ListBuildTimes()
160 self.assertEqual(result[0][0], b_id12)
161 self.assertEqual(result[1][0], b_id23)
162 self.assertEqual(result[2][0], b_id11)
163
164 def testSyncRegistry(self):
165 # check that there are no builds initially
166 result = self.mock_xb._ListBuildTimes()
167 self.assertEqual(len(result), 0)
168
169 # set up the dummy build/images directory with images
170 boards = ['a', 'b']
171 versions = ['v1', 'v2']
172 for b in boards:
173 os.makedirs(os.path.join(self.mock_xb.images_dir, b))
174 for v in versions:
175 os.makedirs(os.path.join(self.mock_xb.images_dir, b, v))
176
177 # Sync and check that they've been added to xBuddy's registry
178 self.mock_xb._SyncRegistryWithBuildImages()
179 result = self.mock_xb._ListBuildTimes()
180 self.assertEqual(len(result), 4)
joychen3cb228e2013-06-12 12:13:13 -0700181
182 ############### Public Methods
183 def testXBuddyCaching(self):
184 """Caching & replacement of timestamp files."""
joychen121fc9b2013-08-02 14:30:30 -0700185 path_a = ('remote', 'a', 'R0', 'test')
186 path_b = ('remote', 'b', 'R0', 'test')
joychen3cb228e2013-06-12 12:13:13 -0700187
joychen3cb228e2013-06-12 12:13:13 -0700188 self.mox.StubOutWithMock(self.mock_xb, '_Download')
189 for _ in range(8):
joychen3cb228e2013-06-12 12:13:13 -0700190 self.mock_xb._Download(mox.IsA(str), mox.IsA(str))
191
192 self.mox.ReplayAll()
193
194 # requires default capacity
195 self.assertEqual(self.mock_xb.Capacity(), '5')
196
197 # Get 6 different images: a,b,c,d,e,f
joychen921e1fb2013-06-28 11:12:20 -0700198 images = ['a', 'b', 'c', 'd', 'e', 'f']
199 for c in images:
joychen562699a2013-08-13 15:22:14 -0700200 self.mock_xb.Get(('remote', c, 'R0', 'test'))
joychen921e1fb2013-06-28 11:12:20 -0700201 time.sleep(0.05)
joychen3cb228e2013-06-12 12:13:13 -0700202
203 # check that b,c,d,e,f are still stored
joychen921e1fb2013-06-28 11:12:20 -0700204 result = self.mock_xb._ListBuildTimes()
joychen3cb228e2013-06-12 12:13:13 -0700205 self.assertEqual(len(result), 5)
joychen921e1fb2013-06-28 11:12:20 -0700206
207 # Flip the list to get reverse chronological order
208 images.reverse()
209 for i in range(5):
joychenf8f07e22013-07-12 17:45:51 -0700210 self.assertEqual(result[i][0], '%s-release/R0' % images[i])
joychen3cb228e2013-06-12 12:13:13 -0700211
212 # Get b,a
joychen562699a2013-08-13 15:22:14 -0700213 self.mock_xb.Get(path_b)
joychen921e1fb2013-06-28 11:12:20 -0700214 time.sleep(0.05)
joychen562699a2013-08-13 15:22:14 -0700215 self.mock_xb.Get(path_a)
joychen921e1fb2013-06-28 11:12:20 -0700216 time.sleep(0.05)
joychen3cb228e2013-06-12 12:13:13 -0700217
218 # check that d,e,f,b,a are still stored
joychen921e1fb2013-06-28 11:12:20 -0700219 result = self.mock_xb._ListBuildTimes()
joychen3cb228e2013-06-12 12:13:13 -0700220 self.assertEqual(len(result), 5)
joychen921e1fb2013-06-28 11:12:20 -0700221 images_expected = ['a', 'b', 'f', 'e', 'd']
222 for i in range(5):
joychenf8f07e22013-07-12 17:45:51 -0700223 self.assertEqual(result[i][0], '%s-release/R0' % images_expected[i])
joychen3cb228e2013-06-12 12:13:13 -0700224
225 self.mox.VerifyAll()
226
227
228if __name__ == '__main__':
229 unittest.main()