OLD | NEW |
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 # | 2 # |
3 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 3 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
6 | 6 |
7 """Unit tests for ctest.""" | 7 """Unit tests for ctest.""" |
8 | 8 |
9 import ctest | |
10 import mox | 9 import mox |
11 import os | 10 import os |
12 import unittest | 11 import unittest |
13 import urllib | 12 import urllib |
14 | 13 |
| 14 import ctest |
| 15 |
15 _TEST_BOOT_DESC = """ | 16 _TEST_BOOT_DESC = """ |
16 --arch="x86" | 17 --arch="x86" |
17 --output_dir="/home/chrome-bot/0.8.70.5-a1" | 18 --output_dir="/home/chrome-bot/0.8.70.5-a1" |
18 --espfs_mountpoint="/home/chrome-bot/0.8.70.5-a1/esp" | 19 --espfs_mountpoint="/home/chrome-bot/0.8.70.5-a1/esp" |
19 --enable_rootfs_verification | 20 --enable_rootfs_verification |
20 """ | 21 """ |
21 | 22 |
22 class CrosTestTest(mox.MoxTestBase): | 23 class CrosTestTest(mox.MoxTestBase): |
23 """Test class for CTest.""" | 24 """Test class for CTest.""" |
24 | 25 |
25 def setUp(self): | 26 def setUp(self): |
26 mox.MoxTestBase.setUp(self) | 27 mox.MoxTestBase.setUp(self) |
27 self.board = 'test-board' | 28 self.board = 'test-board' |
28 self.channel = 'test-channel' | 29 self.channel = 'test-channel' |
29 self.version = '1.2.3.4.5' | 30 self.version = '1.2.3.4.5' |
30 self.revision = '7ghfa9999-12345' | 31 self.revision = '7ghfa9999-12345' |
31 self.image_name = 'TestOS-%s-%s' % (self.version, self.revision) | 32 self.image_name = 'TestOS-%s-%s' % (self.version, self.revision) |
32 self.download_folder = 'test_folder' | 33 self.download_folder = 'test_folder' |
33 self.latestbase = 'http://test-latest/TestOS' | 34 self.latestbase = 'http://test-latest/TestOS' |
34 self.zipbase = 'http://test-zips/archive/TestOS' | 35 self.zipbase = 'http://test-zips/archive/TestOS' |
35 self.image_url = '%s/%s/%s/%s/%s.zip' % (self.zipbase, self.channel, | 36 self.image_url = '%s/%s/%s/%s/%s.zip' % (self.zipbase, self.channel, |
36 self.board, self.version, | 37 self.board, self.version, |
37 self.image_name) | 38 self.image_name) |
| 39 self.test_regex = 'ChromeOS-\d+\.\d+\.\d+\.\d+-.*\.zip' |
38 | 40 |
39 def testModifyBootDesc(self): | 41 def testModifyBootDesc(self): |
40 """Tests to make sure we correctly modify a boot desc.""" | 42 """Tests to make sure we correctly modify a boot desc.""" |
41 in_chroot_path = ctest.ReinterpretPathForChroot(os.path.abspath( | 43 in_chroot_path = ctest.ReinterpretPathForChroot(os.path.abspath( |
42 self.download_folder)) | 44 self.download_folder)) |
43 self.mox.StubOutWithMock(__builtins__, 'open') | 45 self.mox.StubOutWithMock(__builtins__, 'open') |
44 self.mox.StubOutWithMock(ctest.fileinput, 'input') | 46 self.mox.StubOutWithMock(ctest.fileinput, 'input') |
45 m_file = self.mox.CreateMock(file) | 47 m_file = self.mox.CreateMock(file) |
46 | 48 |
47 mock_file = _TEST_BOOT_DESC.splitlines(True) | 49 mock_file = _TEST_BOOT_DESC.splitlines(True) |
(...skipping 21 matching lines...) Expand all Loading... |
69 self.board)).AndReturn(m_file) | 71 self.board)).AndReturn(m_file) |
70 m_file.read().AndReturn('%s.bin.gz' % self.image_name) | 72 m_file.read().AndReturn('%s.bin.gz' % self.image_name) |
71 m_file.close() | 73 m_file.close() |
72 | 74 |
73 self.mox.ReplayAll() | 75 self.mox.ReplayAll() |
74 self.assertEquals(ctest.GetLatestZipUrl(self.board, self.channel, | 76 self.assertEquals(ctest.GetLatestZipUrl(self.board, self.channel, |
75 self.latestbase, self.zipbase), | 77 self.latestbase, self.zipbase), |
76 self.image_url) | 78 self.image_url) |
77 self.mox.VerifyAll() | 79 self.mox.VerifyAll() |
78 | 80 |
| 81 def testGetLatestZipFromBadUrl(self): |
| 82 """Tests whether GetLatestZipUrl returns correct url given bad link.""" |
| 83 self.mox.StubOutWithMock(urllib, 'urlopen') |
| 84 self.mox.StubOutWithMock(ctest, 'GetNewestLinkFromZipBase') |
| 85 m_file = self.mox.CreateMock(file) |
| 86 |
| 87 urllib.urlopen('%s/%s/LATEST-%s' % (self.latestbase, self.channel, |
| 88 self.board)).AndRaise(IOError('Cannot open url.')) |
| 89 ctest.GetNewestLinkFromZipBase(self.board, self.channel, |
| 90 self.zipbase).AndReturn(self.image_url) |
| 91 |
| 92 self.mox.ReplayAll() |
| 93 self.assertEquals(ctest.GetLatestZipUrl(self.board, self.channel, |
| 94 self.latestbase, self.zipbase), |
| 95 self.image_url) |
| 96 self.mox.VerifyAll() |
| 97 |
79 def testGrabZipAndExtractImageUseCached(self): | 98 def testGrabZipAndExtractImageUseCached(self): |
80 """Test case where cache holds our image.""" | 99 """Test case where cache holds our image.""" |
81 self.mox.StubOutWithMock(os.path, 'exists') | 100 self.mox.StubOutWithMock(os.path, 'exists') |
82 self.mox.StubOutWithMock(__builtins__, 'open') | 101 self.mox.StubOutWithMock(__builtins__, 'open') |
83 m_file = self.mox.CreateMock(file) | 102 m_file = self.mox.CreateMock(file) |
84 | 103 |
85 os.path.exists('%s/%s' % ( | 104 os.path.exists('%s/%s' % ( |
86 self.download_folder, 'download_url')).AndReturn(True) | 105 self.download_folder, 'download_url')).AndReturn(True) |
87 | 106 |
88 open('%s/%s' % (self.download_folder, 'download_url')).AndReturn(m_file) | 107 open('%s/%s' % (self.download_folder, 'download_url')).AndReturn(m_file) |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 | 172 |
154 open('%s/%s' % (self.download_folder, 'download_url')).AndReturn(m_file) | 173 open('%s/%s' % (self.download_folder, 'download_url')).AndReturn(m_file) |
155 m_file.read().AndReturn(self.image_url) | 174 m_file.read().AndReturn(self.image_url) |
156 m_file.close() | 175 m_file.close() |
157 | 176 |
158 os.path.exists('%s/%s' % ( | 177 os.path.exists('%s/%s' % ( |
159 self.download_folder, ctest._IMAGE_TO_EXTRACT)).AndReturn(False) | 178 self.download_folder, ctest._IMAGE_TO_EXTRACT)).AndReturn(False) |
160 | 179 |
161 self.CommonDownloadAndExtractImage() | 180 self.CommonDownloadAndExtractImage() |
162 | 181 |
| 182 def testGetLatestLinkFromPage(self): |
| 183 """Tests whether we get the latest link from a url given a regex.""" |
| 184 test_url = 'test_url' |
| 185 test_html = """ |
| 186 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> |
| 187 <html> |
| 188 <body> |
| 189 <h1>Test Index</h1> |
| 190 <a href="ZsomeCruft">Cruft</a> |
| 191 <a href="YotherCruft">Cruft</a> |
| 192 <a href="ChromeOS-0.9.12.4-blahblah.zip">testlink1/</a> |
| 193 <a href="ChromeOS-0.9.12.4-blahblah.zip.other/">testlink2/</a> |
| 194 <a href="ChromeOS-Factory-0.9.12.4-blahblah.zip/">testlink3/</a> |
| 195 </body></html> |
| 196 """ |
| 197 self.mox.StubOutWithMock(urllib, 'urlopen') |
| 198 m_file = self.mox.CreateMock(file) |
| 199 |
| 200 urllib.urlopen(test_url).AndReturn(m_file) |
| 201 m_file.read().AndReturn(test_html) |
| 202 m_file.close() |
| 203 |
| 204 self.mox.ReplayAll() |
| 205 latest_link = ctest.GetLatestLinkFromPage(test_url, regex=self.test_regex) |
| 206 self.assertTrue(latest_link == 'ChromeOS-0.9.12.4-blahblah.zip') |
| 207 self.mox.VerifyAll() |
| 208 |
| 209 |
| 210 class HTMLDirectoryParserTest(unittest.TestCase): |
| 211 """Test class for HTMLDirectoryParser.""" |
| 212 |
| 213 def setUp(self): |
| 214 self.test_regex = '\d+\.\d+\.\d+\.\d+/' |
| 215 |
| 216 def testHandleStarttagGood(self): |
| 217 parser = ctest.HTMLDirectoryParser(regex=self.test_regex) |
| 218 parser.handle_starttag('a', [('href', '0.9.74.1/')]) |
| 219 self.assertTrue('0.9.74.1' in parser.link_list) |
| 220 |
| 221 def testHandleStarttagBad(self): |
| 222 parser = ctest.HTMLDirectoryParser(regex=self.test_regex) |
| 223 parser.handle_starttag('a', [('href', 'ZsomeCruft/')]) |
| 224 self.assertTrue('ZsomeCruft' not in parser.link_list) |
| 225 |
163 | 226 |
164 if __name__ == '__main__': | 227 if __name__ == '__main__': |
165 unittest.main() | 228 unittest.main() |
OLD | NEW |