blob: 38eae340c52ff17a118212f3d39c21b919af1dd6 [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
17import xbuddy
18
19#pylint: disable=W0212
20class xBuddyTest(mox.MoxTestBase):
21 """Regression tests for xbuddy."""
22 def setUp(self):
23 mox.MoxTestBase.setUp(self)
24
joychen921e1fb2013-06-28 11:12:20 -070025 self.static_image_dir = tempfile.mkdtemp('xbuddy_unittest_static')
26 self.root_dir = tempfile.mkdtemp('xbuddy_unittest_ds_root')
joychen3cb228e2013-06-12 12:13:13 -070027
joychen921e1fb2013-06-28 11:12:20 -070028 self.mock_xb = xbuddy.XBuddy(
29 root_dir=self.root_dir,
30 static_dir=self.static_image_dir
31 )
32 self.images_dir = tempfile.mkdtemp('xbuddy_unittest_images')
33 self.mock_xb.images_dir = self.images_dir
joychen3cb228e2013-06-12 12:13:13 -070034
35 def tearDown(self):
36 """Removes testing files."""
37 shutil.rmtree(self.static_image_dir)
joychen921e1fb2013-06-28 11:12:20 -070038 shutil.rmtree(self.images_dir)
joychen3cb228e2013-06-12 12:13:13 -070039
40 def testParseBoolean(self):
41 """Check that some common True/False strings are handled."""
42 self.assertEqual(xbuddy.XBuddy.ParseBoolean(None), False)
43 self.assertEqual(xbuddy.XBuddy.ParseBoolean('false'), False)
44 self.assertEqual(xbuddy.XBuddy.ParseBoolean('bs'), False)
45 self.assertEqual(xbuddy.XBuddy.ParseBoolean('true'), True)
46 self.assertEqual(xbuddy.XBuddy.ParseBoolean('y'), True)
47
48 def _testResolveVersion(self):
49 # TODO (joyc)
50 pass
51
52 def testBasicInterpretPath(self):
53 """Basic checks for splitting a path"""
joycheneaf4cfc2013-07-02 08:38:57 -070054 path = ('parrot-release', 'R27-2455.0.0', 'test')
joychen3cb228e2013-06-12 12:13:13 -070055 expected = ('parrot-release', 'R27-2455.0.0', 'test')
joychen921e1fb2013-06-28 11:12:20 -070056 self.assertEqual(self.mock_xb._InterpretPath(path_list=path), expected)
joychen3cb228e2013-06-12 12:13:13 -070057
joycheneaf4cfc2013-07-02 08:38:57 -070058 path = ('parrot-release', 'R27-2455.0.0', 'full_payload')
joychen3cb228e2013-06-12 12:13:13 -070059 expected = ('parrot-release', 'R27-2455.0.0', 'full_payload')
joychen921e1fb2013-06-28 11:12:20 -070060 self.assertEqual(self.mock_xb._InterpretPath(path_list=path), expected)
joychen3cb228e2013-06-12 12:13:13 -070061
joycheneaf4cfc2013-07-02 08:38:57 -070062 path = ('parrot-release', 'R27-2455.0.0')
63 expected = ('parrot-release', 'R27-2455.0.0', 'test')
joychen921e1fb2013-06-28 11:12:20 -070064 self.assertEqual(self.mock_xb._InterpretPath(path_list=path), expected)
joycheneaf4cfc2013-07-02 08:38:57 -070065
66 path = ('parrot-release', 'R27-2455.0.0', 'too', 'many', 'pieces')
67 self.assertRaises(xbuddy.XBuddyException,
68 self.mock_xb._InterpretPath,
joychen921e1fb2013-06-28 11:12:20 -070069 path_list=path)
joychen3cb228e2013-06-12 12:13:13 -070070
joychen3cb228e2013-06-12 12:13:13 -070071
72 def testLookupVersion(self):
73 # TODO (joyc)
74 pass
75
76 def testTimestampsAndList(self):
77 """Creation and listing of builds according to their timestamps."""
78 # make 3 different timestamp files
joychen921e1fb2013-06-28 11:12:20 -070079 b_id11 = 'b1/v1'
80 b_id12 = 'b1/v2'
81 b_id23 = 'b2/v3'
82 xbuddy.Timestamp.UpdateTimestamp(self.mock_xb._timestamp_folder, b_id11)
83 time.sleep(0.05)
84 xbuddy.Timestamp.UpdateTimestamp(self.mock_xb._timestamp_folder, b_id12)
85 time.sleep(0.05)
86 xbuddy.Timestamp.UpdateTimestamp(self.mock_xb._timestamp_folder, b_id23)
joychen3cb228e2013-06-12 12:13:13 -070087
88 # reference second one again
joychen921e1fb2013-06-28 11:12:20 -070089 time.sleep(0.05)
90 xbuddy.Timestamp.UpdateTimestamp(self.mock_xb._timestamp_folder, b_id12)
joychen3cb228e2013-06-12 12:13:13 -070091
92 # check that list returns the same 3 things, in last referenced order
joychen921e1fb2013-06-28 11:12:20 -070093 result = self.mock_xb._ListBuildTimes()
94 self.assertEqual(result[0][0], b_id12)
95 self.assertEqual(result[1][0], b_id23)
96 self.assertEqual(result[2][0], b_id11)
97
98 def testSyncRegistry(self):
99 # check that there are no builds initially
100 result = self.mock_xb._ListBuildTimes()
101 self.assertEqual(len(result), 0)
102
103 # set up the dummy build/images directory with images
104 boards = ['a', 'b']
105 versions = ['v1', 'v2']
106 for b in boards:
107 os.makedirs(os.path.join(self.mock_xb.images_dir, b))
108 for v in versions:
109 os.makedirs(os.path.join(self.mock_xb.images_dir, b, v))
110
111 # Sync and check that they've been added to xBuddy's registry
112 self.mock_xb._SyncRegistryWithBuildImages()
113 result = self.mock_xb._ListBuildTimes()
114 self.assertEqual(len(result), 4)
joychen3cb228e2013-06-12 12:13:13 -0700115
116 ############### Public Methods
117 def testXBuddyCaching(self):
118 """Caching & replacement of timestamp files."""
joychen921e1fb2013-06-28 11:12:20 -0700119 path_a = ('a', 'R0', 'test')
120 path_b = ('b', 'R0', 'test')
joychen3cb228e2013-06-12 12:13:13 -0700121
122 self.mox.StubOutWithMock(self.mock_xb, '_ResolveVersion')
123 self.mox.StubOutWithMock(self.mock_xb, '_Download')
124 for _ in range(8):
125 self.mock_xb._ResolveVersion(mox.IsA(str),
joychen921e1fb2013-06-28 11:12:20 -0700126 mox.IsA(str)).AndReturn('R0')
joychen3cb228e2013-06-12 12:13:13 -0700127 self.mock_xb._Download(mox.IsA(str), mox.IsA(str))
128
129 self.mox.ReplayAll()
130
131 # requires default capacity
132 self.assertEqual(self.mock_xb.Capacity(), '5')
133
134 # Get 6 different images: a,b,c,d,e,f
joychen921e1fb2013-06-28 11:12:20 -0700135 images = ['a', 'b', 'c', 'd', 'e', 'f']
136 for c in images:
137 self.mock_xb.Get((c, 'R0', 'test'), None)
138 time.sleep(0.05)
joychen3cb228e2013-06-12 12:13:13 -0700139
140 # check that b,c,d,e,f are still stored
joychen921e1fb2013-06-28 11:12:20 -0700141 result = self.mock_xb._ListBuildTimes()
joychen3cb228e2013-06-12 12:13:13 -0700142 self.assertEqual(len(result), 5)
joychen921e1fb2013-06-28 11:12:20 -0700143
144 # Flip the list to get reverse chronological order
145 images.reverse()
146 for i in range(5):
147 self.assertEqual(result[i][0], '%s/R0' % images[i])
joychen3cb228e2013-06-12 12:13:13 -0700148
149 # Get b,a
150 self.mock_xb.Get(path_b, None)
joychen921e1fb2013-06-28 11:12:20 -0700151 time.sleep(0.05)
joychen3cb228e2013-06-12 12:13:13 -0700152 self.mock_xb.Get(path_a, None)
joychen921e1fb2013-06-28 11:12:20 -0700153 time.sleep(0.05)
joychen3cb228e2013-06-12 12:13:13 -0700154
155 # check that d,e,f,b,a are still stored
joychen921e1fb2013-06-28 11:12:20 -0700156 result = self.mock_xb._ListBuildTimes()
joychen3cb228e2013-06-12 12:13:13 -0700157 self.assertEqual(len(result), 5)
joychen921e1fb2013-06-28 11:12:20 -0700158 images_expected = ['a', 'b', 'f', 'e', 'd']
159 for i in range(5):
160 self.assertEqual(result[i][0], '%s/R0' % images_expected[i])
joychen3cb228e2013-06-12 12:13:13 -0700161
162 self.mox.VerifyAll()
163
164
165if __name__ == '__main__':
166 unittest.main()