mbligh | b62f724 | 2009-07-29 14:34:30 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # Copyright 2009 Google Inc. Released under the GPL v2 |
| 3 | |
| 4 | import unittest, cStringIO, httplib |
| 5 | |
| 6 | import common |
| 7 | from autotest_lib.mirror import source |
| 8 | from autotest_lib.client.common_lib.test_utils import mock |
| 9 | |
| 10 | |
| 11 | class 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') |
| 68 | |
| 69 | |
| 70 | def tearDown(self): |
| 71 | self.god.unstub_all() |
| 72 | |
| 73 | |
| 74 | def test_simple(self): |
| 75 | # record |
| 76 | (source.utils.system_output.expect_call( |
| 77 | self._cmd_template % ('', self._prefix, self._path1)) |
| 78 | .and_return(self._output1)) |
| 79 | (source.utils.system_output.expect_call( |
| 80 | self._cmd_template % ('', self._prefix, self._path2)) |
| 81 | .and_return(self._output2)) |
| 82 | self.db_mock.get_dictionary.expect_call().and_return(self._known_files) |
| 83 | |
| 84 | # playback |
| 85 | s = source.rsync_source(self.db_mock, self._prefix) |
| 86 | s.add_path('v2.6/patch-2.6.*.bz2', 'v2.6') |
| 87 | s.add_path('v2.6/testing/patch*.bz2', 'v2.6/testing') |
| 88 | self.assertEquals(s.get_new_files(), self._result) |
| 89 | self.god.check_playback() |
| 90 | |
| 91 | |
| 92 | def test_exclusions(self): |
| 93 | # setup |
| 94 | exclude_str = '--exclude "2.6.30-rc2"' |
| 95 | excluded_result = dict(self._result) |
| 96 | del excluded_result['v2.6/testing/patch-2.6.30-rc2.bz2'] |
| 97 | |
| 98 | # record |
| 99 | (source.utils.system_output.expect_call( |
| 100 | self._cmd_template % (exclude_str, self._prefix, self._path1)) |
| 101 | .and_return(self._output1)) |
| 102 | (source.utils.system_output.expect_call( |
| 103 | self._cmd_template % (exclude_str, self._prefix, self._path2)) |
| 104 | .and_return(self._output_excluded)) |
| 105 | self.db_mock.get_dictionary.expect_call().and_return(self._known_files) |
| 106 | |
| 107 | # playback |
| 108 | s = source.rsync_source(self.db_mock, self._prefix, |
| 109 | excludes=('2.6.30-rc2',)) |
| 110 | s.add_path('v2.6/patch-2.6.*.bz2', 'v2.6') |
| 111 | s.add_path('v2.6/testing/patch*.bz2', 'v2.6/testing') |
| 112 | self.assertEquals(s.get_new_files(), excluded_result) |
| 113 | self.god.check_playback() |
| 114 | |
| 115 | |
| 116 | class url_source_unittest(unittest.TestCase): |
| 117 | _prefix = 'http://www.kernel.org/pub/linux/kernel/' |
| 118 | |
| 119 | _path1 = 'v2.6/' |
| 120 | _full_path1 = '%s%s' % (_prefix, _path1) |
| 121 | |
| 122 | _path2 = 'v2.6/testing' |
| 123 | _full_path2 = '%s%s/' % (_prefix, _path2) |
| 124 | |
| 125 | _output1 = """\ |
| 126 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> |
| 127 | <html> |
| 128 | <head> |
| 129 | <title>Index of /pub/linux/kernel/v2.6</title> |
| 130 | </head> |
| 131 | <body> |
| 132 | <h1>Index of /pub/linux/kernel/v2.6</h1> |
| 133 | <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> - |
| 134 | <a href="incr/">incr/</a> 23-Mar-2009 22:13 - |
| 135 | <a href="pre-releases/">pre-releases/</a> 18-Dec-2003 15:50 - |
| 136 | <a href="snapshots/">snapshots/</a> 25-Apr-2009 00:18 - |
| 137 | <a href="stable-review/">stable-review/</a> 23-Apr-2009 07:51 - |
| 138 | <a href="testing/">testing/</a> 22-Apr-2009 03:31 - |
| 139 | <a href="ChangeLog-2.6.0">ChangeLog-2.6.0</a> 18-Dec-2003 03:04 12K |
| 140 | <a href="ChangeLog-2.6.1">ChangeLog-2.6.1</a> 09-Jan-2004 07:08 189K |
| 141 | <a href="ChangeLog-2.6.2">ChangeLog-2.6.2</a> 04-Feb-2004 04:06 286K |
| 142 | <a href="patch-2.6.19.6.bz2.sign">patch-2.6.19.6.bz2.sign</a> 03-Mar-2007 01:06 248 |
| 143 | <a href="patch-2.6.19.6.gz">patch-2.6.19.6.gz</a> 03-Mar-2007 01:06 68K |
| 144 | <a href="patch-2.6.19.6.gz.sign">patch-2.6.19.6.gz.sign</a> 03-Mar-2007 01:06 248 |
| 145 | <a href="patch-2.6.19.6.sign">patch-2.6.19.6.sign</a> 03-Mar-2007 01:06 248 |
| 146 | <a href="patch-2.6.19.7.bz2">patch-2.6.19.7.bz2</a> 03-Mar-2007 05:29 62K |
| 147 | <a href="patch-2.6.19.7.bz2.sign">patch-2.6.19.7.bz2.sign</a> 03-Mar-2007 05:29 248 |
| 148 | <a href="linux-2.6.28.1.tar.sign">linux-2.6.28.1.tar.sign</a> 18-Jan-2009 18:48 248 |
| 149 | <a href="linux-2.6.28.2.tar.bz2">linux-2.6.28.2.tar.bz2</a> 25-Jan-2009 00:47 50M |
| 150 | <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 |
| 151 | <a href="linux-2.6.28.2.tar.gz">linux-2.6.28.2.tar.gz</a> 25-Jan-2009 00:47 64M |
| 152 | <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 |
| 153 | <a href="linux-2.6.28.2.tar.sign">linux-2.6.28.2.tar.sign</a> 25-Jan-2009 00:47 248 |
| 154 | <a href="linux-2.6.28.3.tar.bz2">linux-2.6.28.3.tar.bz2</a> 02-Feb-2009 18:21 50M |
| 155 | <hr></pre> |
| 156 | </body></html> |
| 157 | """ |
| 158 | _output2 = """\ |
| 159 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> |
| 160 | <html> |
| 161 | <head> |
| 162 | <title>Index of /pub/linux/kernel/v2.6/testing</title> |
| 163 | </head> |
| 164 | <body> |
| 165 | <h1>Index of /pub/linux/kernel/v2.6/testing</h1> |
| 166 | <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> - |
| 167 | <a href="cset/">cset/</a> 04-Apr-2005 17:12 - |
| 168 | <a href="incr/">incr/</a> 22-Apr-2009 03:30 - |
| 169 | <a href="old/">old/</a> 14-Jul-2003 16:06 - |
| 170 | <a href="v2.6.1/">v2.6.1/</a> 15-Feb-2008 21:47 - |
| 171 | <a href="v2.6.2/">v2.6.2/</a> 15-Feb-2008 21:47 - |
| 172 | <a href="LATEST-IS-2.6.30-rc3">LATEST-IS-2.6.30-rc3</a> 22-Apr-2009 03:13 0 |
| 173 | <a href="linux-2.6.30-rc1.tar.bz2">linux-2.6.30-rc1.tar.bz2</a> 07-Apr-2009 22:43 57M |
| 174 | <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 |
| 175 | <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 |
| 176 | <a href="linux-2.6.30-rc3.tar.sign">linux-2.6.30-rc3.tar.sign</a> 22-Apr-2009 03:25 248 |
| 177 | <a href="patch-2.6.30-rc1.bz2">patch-2.6.30-rc1.bz2</a> 07-Apr-2009 22:45 10M |
| 178 | <a href="patch-2.6.30-rc1.bz2.sign">patch-2.6.30-rc1.bz2.sign</a> 07-Apr-2009 22:45 248 |
| 179 | <hr></pre> |
| 180 | </body></html> |
| 181 | """ |
| 182 | _extracted_links1 = ( |
| 183 | (_full_path1 + 'patch-2.6.19.6.gz', '70021', |
| 184 | (2007, 3, 3, 1, 6, 0, 0, 1, 0)), |
| 185 | (_full_path1 + 'patch-2.6.19.7.bz2', '63424', |
| 186 | (2007, 3, 3, 5, 29, 0, 0, 1, 0)), |
| 187 | (_full_path1 + 'linux-2.6.28.2.tar.bz2', '52697313', |
| 188 | (2009, 1, 25, 0, 47, 0, 0, 1, 0)), |
| 189 | (_full_path1 + 'linux-2.6.28.2.tar.gz', '66781113', |
| 190 | (2009, 1, 25, 0, 47, 0, 0, 1, 0)), |
| 191 | (_full_path1 + 'linux-2.6.28.3.tar.bz2', '52703558', |
| 192 | (2009, 2, 2, 18, 21, 0, 0, 1, 0)), |
| 193 | ) |
| 194 | |
| 195 | _extracted_links2 = ( |
| 196 | (_full_path2 + 'patch-2.6.30-rc1.bz2', '10462721', |
| 197 | (2009, 4, 7, 22, 43, 0, 0, 1, 0)), |
| 198 | ) |
| 199 | |
| 200 | _known_files = { |
| 201 | _full_path1 + 'linux-2.6.28.2.tar.gz': source.database.item( |
| 202 | _full_path1 + 'linux-2.6.28.2.tar.gz', 66781113, 1232873220), |
| 203 | } |
| 204 | |
| 205 | _result = { |
| 206 | _full_path1 + 'linux-2.6.28.3.tar.bz2': source.database.item( |
| 207 | _full_path1 + 'linux-2.6.28.3.tar.bz2', 52703558, 1233627660), |
| 208 | _full_path2 + 'patch-2.6.30-rc1.bz2': source.database.item( |
| 209 | _full_path2 + 'patch-2.6.30-rc1.bz2', 10462721, 1239172980), |
| 210 | _full_path1 + 'patch-2.6.19.7.bz2': source.database.item( |
| 211 | _full_path1 + 'patch-2.6.19.7.bz2', 63424, 1172928540), |
| 212 | _full_path1 + 'linux-2.6.28.2.tar.bz2': source.database.item( |
| 213 | _full_path1 + 'linux-2.6.28.2.tar.bz2', 52697313, 1232873220), |
| 214 | _full_path1 + 'patch-2.6.19.6.gz': source.database.item( |
| 215 | _full_path1 + 'patch-2.6.19.6.gz', 70021, 1172912760), |
| 216 | } |
| 217 | |
| 218 | def setUp(self): |
| 219 | self.god = mock.mock_god() |
| 220 | self.db_mock = self.god.create_mock_class( |
| 221 | source.database.database, 'database') |
| 222 | self.god.stub_function(source.urllib2, 'urlopen') |
| 223 | self.addinfourl_mock = self.god.create_mock_class( |
| 224 | source.urllib2.addinfourl, 'addinfourl') |
| 225 | self.mime_mock = self.god.create_mock_class( |
| 226 | httplib.HTTPMessage, 'HTTPMessage') |
| 227 | |
| 228 | |
| 229 | def tearDown(self): |
| 230 | self.god.unstub_all() |
| 231 | |
| 232 | |
| 233 | def test_get_new_files(self): |
| 234 | # record |
| 235 | (source.urllib2.urlopen.expect_call(self._full_path1) |
| 236 | .and_return(cStringIO.StringIO(self._output1))) |
| 237 | for link, size, time in self._extracted_links1: |
| 238 | (source.urllib2.urlopen.expect_call(link) |
| 239 | .and_return(self.addinfourl_mock)) |
| 240 | self.addinfourl_mock.info.expect_call().and_return(self.mime_mock) |
| 241 | self.mime_mock.get.expect_call('content-length').and_return(size) |
| 242 | self.mime_mock.getdate.expect_call('date').and_return(time) |
| 243 | |
| 244 | (source.urllib2.urlopen.expect_call(self._full_path2) |
| 245 | .and_return(cStringIO.StringIO(self._output2))) |
| 246 | for link, size, time in self._extracted_links2: |
| 247 | (source.urllib2.urlopen.expect_call(link) |
| 248 | .and_return(self.addinfourl_mock)) |
| 249 | self.addinfourl_mock.info.expect_call().and_return(self.mime_mock) |
| 250 | self.mime_mock.get.expect_call('content-length').and_return(size) |
| 251 | self.mime_mock.getdate.expect_call('date').and_return(time) |
| 252 | |
| 253 | self.db_mock.get_dictionary.expect_call().and_return(self._known_files) |
| 254 | |
| 255 | # playback |
| 256 | s = source.url_source(self.db_mock, self._prefix) |
| 257 | s.add_url(self._path1, r'.*\.(gz|bz2)$') |
| 258 | s.add_url(self._path2, r'.*patch-[0-9.]+(-rc[0-9]+)?\.bz2$') |
| 259 | self.assertEquals(s.get_new_files(), self._result) |
| 260 | self.god.check_playback() |
| 261 | |
| 262 | |
| 263 | if __name__ == "__main__": |
| 264 | unittest.main() |