blob: ad908787ae2dc5b950079ae90d5874a7ca3e2963 [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(
joychen5260b9a2013-07-16 14:48:01 -070029 True,
joychen921e1fb2013-06-28 11:12:20 -070030 root_dir=self.root_dir,
31 static_dir=self.static_image_dir
32 )
33 self.images_dir = tempfile.mkdtemp('xbuddy_unittest_images')
34 self.mock_xb.images_dir = self.images_dir
joychen3cb228e2013-06-12 12:13:13 -070035
36 def tearDown(self):
37 """Removes testing files."""
38 shutil.rmtree(self.static_image_dir)
joychen921e1fb2013-06-28 11:12:20 -070039 shutil.rmtree(self.images_dir)
joychen3cb228e2013-06-12 12:13:13 -070040
41 def testParseBoolean(self):
42 """Check that some common True/False strings are handled."""
43 self.assertEqual(xbuddy.XBuddy.ParseBoolean(None), False)
44 self.assertEqual(xbuddy.XBuddy.ParseBoolean('false'), False)
45 self.assertEqual(xbuddy.XBuddy.ParseBoolean('bs'), False)
46 self.assertEqual(xbuddy.XBuddy.ParseBoolean('true'), True)
47 self.assertEqual(xbuddy.XBuddy.ParseBoolean('y'), True)
48
49 def _testResolveVersion(self):
50 # TODO (joyc)
51 pass
52
53 def testBasicInterpretPath(self):
54 """Basic checks for splitting a path"""
joycheneaf4cfc2013-07-02 08:38:57 -070055 path = ('parrot-release', 'R27-2455.0.0', 'test')
joychen3cb228e2013-06-12 12:13:13 -070056 expected = ('parrot-release', 'R27-2455.0.0', 'test')
joychen921e1fb2013-06-28 11:12:20 -070057 self.assertEqual(self.mock_xb._InterpretPath(path_list=path), expected)
joychen3cb228e2013-06-12 12:13:13 -070058
joycheneaf4cfc2013-07-02 08:38:57 -070059 path = ('parrot-release', 'R27-2455.0.0', 'full_payload')
joychen3cb228e2013-06-12 12:13:13 -070060 expected = ('parrot-release', 'R27-2455.0.0', 'full_payload')
joychen921e1fb2013-06-28 11:12:20 -070061 self.assertEqual(self.mock_xb._InterpretPath(path_list=path), expected)
joychen3cb228e2013-06-12 12:13:13 -070062
joycheneaf4cfc2013-07-02 08:38:57 -070063 path = ('parrot-release', 'R27-2455.0.0')
64 expected = ('parrot-release', 'R27-2455.0.0', 'test')
joychen921e1fb2013-06-28 11:12:20 -070065 self.assertEqual(self.mock_xb._InterpretPath(path_list=path), expected)
joycheneaf4cfc2013-07-02 08:38:57 -070066
67 path = ('parrot-release', 'R27-2455.0.0', 'too', 'many', 'pieces')
68 self.assertRaises(xbuddy.XBuddyException,
69 self.mock_xb._InterpretPath,
joychen921e1fb2013-06-28 11:12:20 -070070 path_list=path)
joychen3cb228e2013-06-12 12:13:13 -070071
joychen3cb228e2013-06-12 12:13:13 -070072
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
joychen921e1fb2013-06-28 11:12:20 -070080 b_id11 = 'b1/v1'
81 b_id12 = 'b1/v2'
82 b_id23 = 'b2/v3'
83 xbuddy.Timestamp.UpdateTimestamp(self.mock_xb._timestamp_folder, b_id11)
84 time.sleep(0.05)
85 xbuddy.Timestamp.UpdateTimestamp(self.mock_xb._timestamp_folder, b_id12)
86 time.sleep(0.05)
87 xbuddy.Timestamp.UpdateTimestamp(self.mock_xb._timestamp_folder, b_id23)
joychen3cb228e2013-06-12 12:13:13 -070088
89 # reference second one again
joychen921e1fb2013-06-28 11:12:20 -070090 time.sleep(0.05)
91 xbuddy.Timestamp.UpdateTimestamp(self.mock_xb._timestamp_folder, b_id12)
joychen3cb228e2013-06-12 12:13:13 -070092
93 # check that list returns the same 3 things, in last referenced order
joychen921e1fb2013-06-28 11:12:20 -070094 result = self.mock_xb._ListBuildTimes()
95 self.assertEqual(result[0][0], b_id12)
96 self.assertEqual(result[1][0], b_id23)
97 self.assertEqual(result[2][0], b_id11)
98
99 def testSyncRegistry(self):
100 # check that there are no builds initially
101 result = self.mock_xb._ListBuildTimes()
102 self.assertEqual(len(result), 0)
103
104 # set up the dummy build/images directory with images
105 boards = ['a', 'b']
106 versions = ['v1', 'v2']
107 for b in boards:
108 os.makedirs(os.path.join(self.mock_xb.images_dir, b))
109 for v in versions:
110 os.makedirs(os.path.join(self.mock_xb.images_dir, b, v))
111
112 # Sync and check that they've been added to xBuddy's registry
113 self.mock_xb._SyncRegistryWithBuildImages()
114 result = self.mock_xb._ListBuildTimes()
115 self.assertEqual(len(result), 4)
joychen3cb228e2013-06-12 12:13:13 -0700116
117 ############### Public Methods
118 def testXBuddyCaching(self):
119 """Caching & replacement of timestamp files."""
joychen921e1fb2013-06-28 11:12:20 -0700120 path_a = ('a', 'R0', 'test')
121 path_b = ('b', 'R0', 'test')
joychen3cb228e2013-06-12 12:13:13 -0700122
123 self.mox.StubOutWithMock(self.mock_xb, '_ResolveVersion')
124 self.mox.StubOutWithMock(self.mock_xb, '_Download')
125 for _ in range(8):
126 self.mock_xb._ResolveVersion(mox.IsA(str),
joychen921e1fb2013-06-28 11:12:20 -0700127 mox.IsA(str)).AndReturn('R0')
joychen3cb228e2013-06-12 12:13:13 -0700128 self.mock_xb._Download(mox.IsA(str), mox.IsA(str))
129
130 self.mox.ReplayAll()
131
132 # requires default capacity
133 self.assertEqual(self.mock_xb.Capacity(), '5')
134
135 # Get 6 different images: a,b,c,d,e,f
joychen921e1fb2013-06-28 11:12:20 -0700136 images = ['a', 'b', 'c', 'd', 'e', 'f']
137 for c in images:
138 self.mock_xb.Get((c, 'R0', 'test'), None)
139 time.sleep(0.05)
joychen3cb228e2013-06-12 12:13:13 -0700140
141 # check that b,c,d,e,f are still stored
joychen921e1fb2013-06-28 11:12:20 -0700142 result = self.mock_xb._ListBuildTimes()
joychen3cb228e2013-06-12 12:13:13 -0700143 self.assertEqual(len(result), 5)
joychen921e1fb2013-06-28 11:12:20 -0700144
145 # Flip the list to get reverse chronological order
146 images.reverse()
147 for i in range(5):
148 self.assertEqual(result[i][0], '%s/R0' % images[i])
joychen3cb228e2013-06-12 12:13:13 -0700149
150 # Get b,a
151 self.mock_xb.Get(path_b, None)
joychen921e1fb2013-06-28 11:12:20 -0700152 time.sleep(0.05)
joychen3cb228e2013-06-12 12:13:13 -0700153 self.mock_xb.Get(path_a, None)
joychen921e1fb2013-06-28 11:12:20 -0700154 time.sleep(0.05)
joychen3cb228e2013-06-12 12:13:13 -0700155
156 # check that d,e,f,b,a are still stored
joychen921e1fb2013-06-28 11:12:20 -0700157 result = self.mock_xb._ListBuildTimes()
joychen3cb228e2013-06-12 12:13:13 -0700158 self.assertEqual(len(result), 5)
joychen921e1fb2013-06-28 11:12:20 -0700159 images_expected = ['a', 'b', 'f', 'e', 'd']
160 for i in range(5):
161 self.assertEqual(result[i][0], '%s/R0' % images_expected[i])
joychen3cb228e2013-06-12 12:13:13 -0700162
163 self.mox.VerifyAll()
164
165
166if __name__ == '__main__':
167 unittest.main()