blob: f36bea6a8f4776d256d802597ccf93d76f78db0b [file] [log] [blame]
mblighb62f7242009-07-29 14:34:30 +00001#!/usr/bin/python
2# Copyright 2009 Google Inc. Released under the GPL v2
3
mbligh811e38f2010-03-11 18:23:59 +00004import unittest, cStringIO, httplib, time, os
mblighb62f7242009-07-29 14:34:30 +00005
6import common
7from autotest_lib.mirror import source
8from autotest_lib.client.common_lib.test_utils import mock
9
10
11class rsync_source_unittest(unittest.TestCase):
12 _cmd_template = '/usr/bin/rsync -rltz --no-motd %s %s/%s'
13 _prefix = 'rsync://rsync.kernel.org/pub/linux/kernel'
14 _path1 = 'v2.6/patch-2.6.*.bz2'
15 _path2 = 'v2.6/testing/patch*.bz2'
16
17 _output1 = """\
18-rw-rw-r-- 10727 2003/12/17 19:04:34 patch-2.6.0.bz2
19-rw-rw-r-- 777959 2004/01/08 23:31:48 patch-2.6.1.bz2
20-rw-rw-r-- 4851041 2004/12/24 14:38:58 patch-2.6.10.bz2
21-rw-r--r-- 713 2005/03/08 16:59:09 patch-2.6.11.1.bz2
22-rw-r--r-- 15141 2005/05/16 11:17:23 patch-2.6.11.10.bz2
23-rw-rw-r-- 20868 2005/05/26 22:51:21 patch-2.6.11.11.bz2
24-rw-rw-r-- 23413 2005/06/11 19:57:26 patch-2.6.11.12.bz2
25-rw-r--r-- 1010 2005/03/12 22:55:52 patch-2.6.11.2.bz2
26"""
27 _output2 = """\
28-rw-rw-r-- 10462721 2009/04/07 15:45:35 patch-2.6.30-rc1.bz2
29-rw-rw-r-- 10815919 2009/04/14 19:01:40 patch-2.6.30-rc2.bz2
30-rw-rw-r-- 11032734 2009/04/21 20:28:11 patch-2.6.30-rc3.bz2
31"""
32 _output_excluded = """\
33-rw-rw-r-- 10462721 2009/04/07 15:45:35 patch-2.6.30-rc1.bz2
34-rw-rw-r-- 11032734 2009/04/21 20:28:11 patch-2.6.30-rc3.bz2
35"""
36 _known_files = {
37 'v2.6/patch-2.6.1.bz2': source.database.item(
38 'v2.6/patch-2.6.1.bz2', 777959, 1073633508),
39 'v2.6/patch-2.6.11.10.bz2': source.database.item(
40 'v2.6/patch-2.6.11.10.bz2', 15141, 1116267443),
41 'v2.6/testing/patch-2.6.30-rc1.bz2': source.database.item(
42 'v2.6/testing/patch-2.6.30-rc1.bz2', 10462721, 1239144335),
43 }
44 _result = {
45 'v2.6/patch-2.6.0.bz2': source.database.item(
46 'v2.6/patch-2.6.0.bz2', 10727, 1071716674),
47 'v2.6/patch-2.6.10.bz2': source.database.item(
48 'v2.6/patch-2.6.10.bz2', 4851041, 1103927938),
49 'v2.6/patch-2.6.11.12.bz2': source.database.item(
50 'v2.6/patch-2.6.11.12.bz2', 23413, 1118545046),
51 'v2.6/patch-2.6.11.11.bz2': source.database.item(
52 'v2.6/patch-2.6.11.11.bz2', 20868, 1117173081),
53 'v2.6/patch-2.6.11.2.bz2': source.database.item(
54 'v2.6/patch-2.6.11.2.bz2', 1010, 1110696952),
55 'v2.6/patch-2.6.11.1.bz2': source.database.item(
56 'v2.6/patch-2.6.11.1.bz2', 713, 1110329949),
57 'v2.6/testing/patch-2.6.30-rc3.bz2': source.database.item(
58 'v2.6/testing/patch-2.6.30-rc3.bz2', 11032734, 1240370891),
59 'v2.6/testing/patch-2.6.30-rc2.bz2': source.database.item(
60 'v2.6/testing/patch-2.6.30-rc2.bz2', 10815919, 1239760900),
61 }
62
63 def setUp(self):
64 self.god = mock.mock_god()
65 self.db_mock = self.god.create_mock_class(
66 source.database.database, 'database')
67 self.god.stub_function(source.utils, 'system_output')
mbligh811e38f2010-03-11 18:23:59 +000068 self.old_tz = getattr(os.environ, 'TZ', '')
69 os.environ['TZ'] = 'America/Los_Angeles'
70 time.tzset()
mblighb62f7242009-07-29 14:34:30 +000071
72
73 def tearDown(self):
74 self.god.unstub_all()
mbligh811e38f2010-03-11 18:23:59 +000075 os.environ['TZ'] = self.old_tz
76 time.tzset()
mblighb62f7242009-07-29 14:34:30 +000077
78
79 def test_simple(self):
80 # record
81 (source.utils.system_output.expect_call(
82 self._cmd_template % ('', self._prefix, self._path1))
83 .and_return(self._output1))
84 (source.utils.system_output.expect_call(
85 self._cmd_template % ('', self._prefix, self._path2))
86 .and_return(self._output2))
87 self.db_mock.get_dictionary.expect_call().and_return(self._known_files)
88
89 # playback
90 s = source.rsync_source(self.db_mock, self._prefix)
91 s.add_path('v2.6/patch-2.6.*.bz2', 'v2.6')
92 s.add_path('v2.6/testing/patch*.bz2', 'v2.6/testing')
93 self.assertEquals(s.get_new_files(), self._result)
94 self.god.check_playback()
95
96
97 def test_exclusions(self):
98 # setup
99 exclude_str = '--exclude "2.6.30-rc2"'
100 excluded_result = dict(self._result)
101 del excluded_result['v2.6/testing/patch-2.6.30-rc2.bz2']
102
103 # record
104 (source.utils.system_output.expect_call(
105 self._cmd_template % (exclude_str, self._prefix, self._path1))
106 .and_return(self._output1))
107 (source.utils.system_output.expect_call(
108 self._cmd_template % (exclude_str, self._prefix, self._path2))
109 .and_return(self._output_excluded))
110 self.db_mock.get_dictionary.expect_call().and_return(self._known_files)
111
112 # playback
113 s = source.rsync_source(self.db_mock, self._prefix,
114 excludes=('2.6.30-rc2',))
115 s.add_path('v2.6/patch-2.6.*.bz2', 'v2.6')
116 s.add_path('v2.6/testing/patch*.bz2', 'v2.6/testing')
117 self.assertEquals(s.get_new_files(), excluded_result)
118 self.god.check_playback()
119
120
121class url_source_unittest(unittest.TestCase):
122 _prefix = 'http://www.kernel.org/pub/linux/kernel/'
123
124 _path1 = 'v2.6/'
125 _full_path1 = '%s%s' % (_prefix, _path1)
126
127 _path2 = 'v2.6/testing'
128 _full_path2 = '%s%s/' % (_prefix, _path2)
129
130 _output1 = """\
131<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
132<html>
133 <head>
134 <title>Index of /pub/linux/kernel/v2.6</title>
135 </head>
136 <body>
137<h1>Index of /pub/linux/kernel/v2.6</h1>
138<pre><a href="?C=N;O=D">Name</a> <a href="?C=M;O=A">Last modified</a> <a href="?C=S;O=A">Size</a> <hr><a href="/pub/linux/kernel/">Parent Directory</a> -
139<a href="incr/">incr/</a> 23-Mar-2009 22:13 -
140<a href="pre-releases/">pre-releases/</a> 18-Dec-2003 15:50 -
141<a href="snapshots/">snapshots/</a> 25-Apr-2009 00:18 -
142<a href="stable-review/">stable-review/</a> 23-Apr-2009 07:51 -
143<a href="testing/">testing/</a> 22-Apr-2009 03:31 -
144<a href="ChangeLog-2.6.0">ChangeLog-2.6.0</a> 18-Dec-2003 03:04 12K
145<a href="ChangeLog-2.6.1">ChangeLog-2.6.1</a> 09-Jan-2004 07:08 189K
146<a href="ChangeLog-2.6.2">ChangeLog-2.6.2</a> 04-Feb-2004 04:06 286K
147<a href="patch-2.6.19.6.bz2.sign">patch-2.6.19.6.bz2.sign</a> 03-Mar-2007 01:06 248
148<a href="patch-2.6.19.6.gz">patch-2.6.19.6.gz</a> 03-Mar-2007 01:06 68K
149<a href="patch-2.6.19.6.gz.sign">patch-2.6.19.6.gz.sign</a> 03-Mar-2007 01:06 248
150<a href="patch-2.6.19.6.sign">patch-2.6.19.6.sign</a> 03-Mar-2007 01:06 248
151<a href="patch-2.6.19.7.bz2">patch-2.6.19.7.bz2</a> 03-Mar-2007 05:29 62K
152<a href="patch-2.6.19.7.bz2.sign">patch-2.6.19.7.bz2.sign</a> 03-Mar-2007 05:29 248
153<a href="linux-2.6.28.1.tar.sign">linux-2.6.28.1.tar.sign</a> 18-Jan-2009 18:48 248
154<a href="linux-2.6.28.2.tar.bz2">linux-2.6.28.2.tar.bz2</a> 25-Jan-2009 00:47 50M
155<a href="linux-2.6.28.2.tar.bz2.sign">linux-2.6.28.2.tar.bz2.sign</a> 25-Jan-2009 00:47 248
156<a href="linux-2.6.28.2.tar.gz">linux-2.6.28.2.tar.gz</a> 25-Jan-2009 00:47 64M
157<a href="linux-2.6.28.2.tar.gz.sign">linux-2.6.28.2.tar.gz.sign</a> 25-Jan-2009 00:47 248
158<a href="linux-2.6.28.2.tar.sign">linux-2.6.28.2.tar.sign</a> 25-Jan-2009 00:47 248
159<a href="linux-2.6.28.3.tar.bz2">linux-2.6.28.3.tar.bz2</a> 02-Feb-2009 18:21 50M
160<hr></pre>
161</body></html>
162"""
163 _output2 = """\
164<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
165<html>
166 <head>
167 <title>Index of /pub/linux/kernel/v2.6/testing</title>
168 </head>
169 <body>
170<h1>Index of /pub/linux/kernel/v2.6/testing</h1>
171<pre><a href="?C=N;O=D">Name</a> <a href="?C=M;O=A">Last modified</a> <a href="?C=S;O=A">Size</a> <hr><a href="/pub/linux/kernel/v2.6/">Parent Directory</a> -
172<a href="cset/">cset/</a> 04-Apr-2005 17:12 -
173<a href="incr/">incr/</a> 22-Apr-2009 03:30 -
174<a href="old/">old/</a> 14-Jul-2003 16:06 -
175<a href="v2.6.1/">v2.6.1/</a> 15-Feb-2008 21:47 -
176<a href="v2.6.2/">v2.6.2/</a> 15-Feb-2008 21:47 -
177<a href="LATEST-IS-2.6.30-rc3">LATEST-IS-2.6.30-rc3</a> 22-Apr-2009 03:13 0
178<a href="linux-2.6.30-rc1.tar.bz2">linux-2.6.30-rc1.tar.bz2</a> 07-Apr-2009 22:43 57M
179<a href="linux-2.6.30-rc1.tar.bz2.sign">linux-2.6.30-rc1.tar.bz2.sign</a> 07-Apr-2009 22:43 248
180<a href="linux-2.6.30-rc3.tar.gz.sign">linux-2.6.30-rc3.tar.gz.sign</a> 22-Apr-2009 03:25 248
181<a href="linux-2.6.30-rc3.tar.sign">linux-2.6.30-rc3.tar.sign</a> 22-Apr-2009 03:25 248
182<a href="patch-2.6.30-rc1.bz2">patch-2.6.30-rc1.bz2</a> 07-Apr-2009 22:45 10M
183<a href="patch-2.6.30-rc1.bz2.sign">patch-2.6.30-rc1.bz2.sign</a> 07-Apr-2009 22:45 248
184<hr></pre>
185</body></html>
186"""
187 _extracted_links1 = (
188 (_full_path1 + 'patch-2.6.19.6.gz', '70021',
189 (2007, 3, 3, 1, 6, 0, 0, 1, 0)),
190 (_full_path1 + 'patch-2.6.19.7.bz2', '63424',
191 (2007, 3, 3, 5, 29, 0, 0, 1, 0)),
192 (_full_path1 + 'linux-2.6.28.2.tar.bz2', '52697313',
193 (2009, 1, 25, 0, 47, 0, 0, 1, 0)),
194 (_full_path1 + 'linux-2.6.28.2.tar.gz', '66781113',
195 (2009, 1, 25, 0, 47, 0, 0, 1, 0)),
196 (_full_path1 + 'linux-2.6.28.3.tar.bz2', '52703558',
197 (2009, 2, 2, 18, 21, 0, 0, 1, 0)),
198 )
199
200 _extracted_links2 = (
201 (_full_path2 + 'patch-2.6.30-rc1.bz2', '10462721',
202 (2009, 4, 7, 22, 43, 0, 0, 1, 0)),
203 )
204
205 _known_files = {
206 _full_path1 + 'linux-2.6.28.2.tar.gz': source.database.item(
207 _full_path1 + 'linux-2.6.28.2.tar.gz', 66781113, 1232873220),
208 }
209
210 _result = {
211 _full_path1 + 'linux-2.6.28.3.tar.bz2': source.database.item(
212 _full_path1 + 'linux-2.6.28.3.tar.bz2', 52703558, 1233627660),
213 _full_path2 + 'patch-2.6.30-rc1.bz2': source.database.item(
214 _full_path2 + 'patch-2.6.30-rc1.bz2', 10462721, 1239172980),
215 _full_path1 + 'patch-2.6.19.7.bz2': source.database.item(
216 _full_path1 + 'patch-2.6.19.7.bz2', 63424, 1172928540),
217 _full_path1 + 'linux-2.6.28.2.tar.bz2': source.database.item(
218 _full_path1 + 'linux-2.6.28.2.tar.bz2', 52697313, 1232873220),
219 _full_path1 + 'patch-2.6.19.6.gz': source.database.item(
220 _full_path1 + 'patch-2.6.19.6.gz', 70021, 1172912760),
221 }
222
223 def setUp(self):
224 self.god = mock.mock_god()
225 self.db_mock = self.god.create_mock_class(
226 source.database.database, 'database')
227 self.god.stub_function(source.urllib2, 'urlopen')
228 self.addinfourl_mock = self.god.create_mock_class(
229 source.urllib2.addinfourl, 'addinfourl')
230 self.mime_mock = self.god.create_mock_class(
231 httplib.HTTPMessage, 'HTTPMessage')
mbligh811e38f2010-03-11 18:23:59 +0000232 self.old_tz = getattr(os.environ, 'TZ', '')
233 os.environ['TZ'] = 'America/Los_Angeles'
234 time.tzset()
mblighb62f7242009-07-29 14:34:30 +0000235
236
237 def tearDown(self):
238 self.god.unstub_all()
mbligh811e38f2010-03-11 18:23:59 +0000239 os.environ['TZ'] = self.old_tz
240 time.tzset()
mblighb62f7242009-07-29 14:34:30 +0000241
242
243 def test_get_new_files(self):
244 # record
245 (source.urllib2.urlopen.expect_call(self._full_path1)
246 .and_return(cStringIO.StringIO(self._output1)))
247 for link, size, time in self._extracted_links1:
248 (source.urllib2.urlopen.expect_call(link)
249 .and_return(self.addinfourl_mock))
250 self.addinfourl_mock.info.expect_call().and_return(self.mime_mock)
251 self.mime_mock.get.expect_call('content-length').and_return(size)
252 self.mime_mock.getdate.expect_call('date').and_return(time)
253
254 (source.urllib2.urlopen.expect_call(self._full_path2)
255 .and_return(cStringIO.StringIO(self._output2)))
256 for link, size, time in self._extracted_links2:
257 (source.urllib2.urlopen.expect_call(link)
258 .and_return(self.addinfourl_mock))
259 self.addinfourl_mock.info.expect_call().and_return(self.mime_mock)
260 self.mime_mock.get.expect_call('content-length').and_return(size)
261 self.mime_mock.getdate.expect_call('date').and_return(time)
262
263 self.db_mock.get_dictionary.expect_call().and_return(self._known_files)
264
265 # playback
266 s = source.url_source(self.db_mock, self._prefix)
267 s.add_url(self._path1, r'.*\.(gz|bz2)$')
268 s.add_url(self._path2, r'.*patch-[0-9.]+(-rc[0-9]+)?\.bz2$')
269 self.assertEquals(s.get_new_files(), self._result)
270 self.god.check_playback()
271
272
273if __name__ == "__main__":
274 unittest.main()