OLD | NEW |
| (Empty) |
1 # -*- coding: utf-8 -*- | |
2 # Copyright 2013 Google Inc. All Rights Reserved. | |
3 # | |
4 # Permission is hereby granted, free of charge, to any person obtaining a | |
5 # copy of this software and associated documentation files (the | |
6 # "Software"), to deal in the Software without restriction, including | |
7 # without limitation the rights to use, copy, modify, merge, publish, dis- | |
8 # tribute, sublicense, and/or sell copies of the Software, and to permit | |
9 # persons to whom the Software is furnished to do so, subject to the fol- | |
10 # lowing conditions: | |
11 # | |
12 # The above copyright notice and this permission notice shall be included | |
13 # in all copies or substantial portions of the Software. | |
14 # | |
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | |
16 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL- | |
17 # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT | |
18 # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | |
19 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | |
21 # IN THE SOFTWARE. | |
22 """Tests for gsutil utility functions.""" | |
23 | |
24 from __future__ import absolute_import | |
25 | |
26 import httplib2 | |
27 from gslib import util | |
28 import gslib.tests.testcase as testcase | |
29 from gslib.tests.util import SetEnvironmentForTest | |
30 from gslib.util import CompareVersions | |
31 | |
32 | |
33 class TestUtil(testcase.GsUtilUnitTestCase): | |
34 """Tests for utility functions.""" | |
35 | |
36 def test_MakeHumanReadable(self): | |
37 """Tests converting byte counts to human-readable strings.""" | |
38 self.assertEqual(util.MakeHumanReadable(0), '0 B') | |
39 self.assertEqual(util.MakeHumanReadable(1023), '1023 B') | |
40 self.assertEqual(util.MakeHumanReadable(1024), '1 KiB') | |
41 self.assertEqual(util.MakeHumanReadable(1024 ** 2), '1 MiB') | |
42 self.assertEqual(util.MakeHumanReadable(1024 ** 3), '1 GiB') | |
43 self.assertEqual(util.MakeHumanReadable(1024 ** 3 * 5.3), '5.3 GiB') | |
44 self.assertEqual(util.MakeHumanReadable(1024 ** 4 * 2.7), '2.7 TiB') | |
45 self.assertEqual(util.MakeHumanReadable(1024 ** 5), '1 PiB') | |
46 self.assertEqual(util.MakeHumanReadable(1024 ** 6), '1 EiB') | |
47 | |
48 def test_MakeBitsHumanReadable(self): | |
49 """Tests converting bit counts to human-readable strings.""" | |
50 self.assertEqual(util.MakeBitsHumanReadable(0), '0 bit') | |
51 self.assertEqual(util.MakeBitsHumanReadable(1023), '1023 bit') | |
52 self.assertEqual(util.MakeBitsHumanReadable(1024), '1 Kibit') | |
53 self.assertEqual(util.MakeBitsHumanReadable(1024 ** 2), '1 Mibit') | |
54 self.assertEqual(util.MakeBitsHumanReadable(1024 ** 3), '1 Gibit') | |
55 self.assertEqual(util.MakeBitsHumanReadable(1024 ** 3 * 5.3), '5.3 Gibit') | |
56 self.assertEqual(util.MakeBitsHumanReadable(1024 ** 4 * 2.7), '2.7 Tibit') | |
57 self.assertEqual(util.MakeBitsHumanReadable(1024 ** 5), '1 Pibit') | |
58 self.assertEqual(util.MakeBitsHumanReadable(1024 ** 6), '1 Eibit') | |
59 | |
60 def test_HumanReadableToBytes(self): | |
61 """Tests converting human-readable strings to byte counts.""" | |
62 self.assertEqual(util.HumanReadableToBytes('1'), 1) | |
63 self.assertEqual(util.HumanReadableToBytes('15'), 15) | |
64 self.assertEqual(util.HumanReadableToBytes('15.3'), 15) | |
65 self.assertEqual(util.HumanReadableToBytes('15.7'), 16) | |
66 self.assertEqual(util.HumanReadableToBytes('1023'), 1023) | |
67 self.assertEqual(util.HumanReadableToBytes('1k'), 1024) | |
68 self.assertEqual(util.HumanReadableToBytes('2048'), 2048) | |
69 self.assertEqual(util.HumanReadableToBytes('1 k'), 1024) | |
70 self.assertEqual(util.HumanReadableToBytes('1 K'), 1024) | |
71 self.assertEqual(util.HumanReadableToBytes('1 KB'), 1024) | |
72 self.assertEqual(util.HumanReadableToBytes('1 KiB'), 1024) | |
73 self.assertEqual(util.HumanReadableToBytes('1 m'), 1024 ** 2) | |
74 self.assertEqual(util.HumanReadableToBytes('1 M'), 1024 ** 2) | |
75 self.assertEqual(util.HumanReadableToBytes('1 MB'), 1024 ** 2) | |
76 self.assertEqual(util.HumanReadableToBytes('1 MiB'), 1024 ** 2) | |
77 self.assertEqual(util.HumanReadableToBytes('1 g'), 1024 ** 3) | |
78 self.assertEqual(util.HumanReadableToBytes('1 G'), 1024 ** 3) | |
79 self.assertEqual(util.HumanReadableToBytes('1 GB'), 1024 ** 3) | |
80 self.assertEqual(util.HumanReadableToBytes('1 GiB'), 1024 ** 3) | |
81 self.assertEqual(util.HumanReadableToBytes('1t'), 1024 ** 4) | |
82 self.assertEqual(util.HumanReadableToBytes('1T'), 1024 ** 4) | |
83 self.assertEqual(util.HumanReadableToBytes('1TB'), 1024 ** 4) | |
84 self.assertEqual(util.HumanReadableToBytes('1TiB'), 1024 ** 4) | |
85 self.assertEqual(util.HumanReadableToBytes('1\t p'), 1024 ** 5) | |
86 self.assertEqual(util.HumanReadableToBytes('1\t P'), 1024 ** 5) | |
87 self.assertEqual(util.HumanReadableToBytes('1\t PB'), 1024 ** 5) | |
88 self.assertEqual(util.HumanReadableToBytes('1\t PiB'), 1024 ** 5) | |
89 self.assertEqual(util.HumanReadableToBytes('1e'), 1024 ** 6) | |
90 self.assertEqual(util.HumanReadableToBytes('1E'), 1024 ** 6) | |
91 self.assertEqual(util.HumanReadableToBytes('1EB'), 1024 ** 6) | |
92 self.assertEqual(util.HumanReadableToBytes('1EiB'), 1024 ** 6) | |
93 | |
94 def test_CompareVersions(self): | |
95 """Tests CompareVersions for various use cases.""" | |
96 # CompareVersions(first, second) returns (g, m), where | |
97 # g is True if first known to be greater than second, else False. | |
98 # m is True if first known to be greater by at least 1 major version, | |
99 (g, m) = CompareVersions('3.37', '3.2') | |
100 self.assertTrue(g) | |
101 self.assertFalse(m) | |
102 (g, m) = CompareVersions('7', '2') | |
103 self.assertTrue(g) | |
104 self.assertTrue(m) | |
105 (g, m) = CompareVersions('3.32', '3.32pre') | |
106 self.assertTrue(g) | |
107 self.assertFalse(m) | |
108 (g, m) = CompareVersions('3.32pre', '3.31') | |
109 self.assertTrue(g) | |
110 self.assertFalse(m) | |
111 (g, m) = CompareVersions('3.4pre', '3.3pree') | |
112 self.assertTrue(g) | |
113 self.assertFalse(m) | |
114 | |
115 (g, m) = CompareVersions('3.2', '3.37') | |
116 self.assertFalse(g) | |
117 self.assertFalse(m) | |
118 (g, m) = CompareVersions('2', '7') | |
119 self.assertFalse(g) | |
120 self.assertFalse(m) | |
121 (g, m) = CompareVersions('3.32pre', '3.32') | |
122 self.assertFalse(g) | |
123 self.assertFalse(m) | |
124 (g, m) = CompareVersions('3.31', '3.32pre') | |
125 self.assertFalse(g) | |
126 self.assertFalse(m) | |
127 (g, m) = CompareVersions('3.3pre', '3.3pre') | |
128 self.assertFalse(g) | |
129 self.assertFalse(m) | |
130 | |
131 (g, m) = CompareVersions('foobar', 'baz') | |
132 self.assertFalse(g) | |
133 self.assertFalse(m) | |
134 (g, m) = CompareVersions('3.32', 'baz') | |
135 self.assertFalse(g) | |
136 self.assertFalse(m) | |
137 | |
138 (g, m) = CompareVersions('3.4', '3.3') | |
139 self.assertTrue(g) | |
140 self.assertFalse(m) | |
141 (g, m) = CompareVersions('3.3', '3.4') | |
142 self.assertFalse(g) | |
143 self.assertFalse(m) | |
144 (g, m) = CompareVersions('4.1', '3.33') | |
145 self.assertTrue(g) | |
146 self.assertTrue(m) | |
147 (g, m) = CompareVersions('3.10', '3.1') | |
148 self.assertTrue(g) | |
149 self.assertFalse(m) | |
150 | |
151 def _AssertProxyInfosEqual(self, pi1, pi2): | |
152 self.assertEqual(pi1.proxy_type, pi2.proxy_type) | |
153 self.assertEqual(pi1.proxy_host, pi2.proxy_host) | |
154 self.assertEqual(pi1.proxy_port, pi2.proxy_port) | |
155 self.assertEqual(pi1.proxy_rdns, pi2.proxy_rdns) | |
156 self.assertEqual(pi1.proxy_user, pi2.proxy_user) | |
157 self.assertEqual(pi1.proxy_pass, pi2.proxy_pass) | |
158 | |
159 def test_ProxyInfoFromEnvironmentVar(self): | |
160 """Tests ProxyInfoFromEnvironmentVar for various cases.""" | |
161 valid_variables = ['http_proxy', 'https_proxy'] | |
162 if not util.IS_WINDOWS: | |
163 # Dynamically set Windows environment variables are case-insensitive. | |
164 valid_variables.append('HTTPS_PROXY') | |
165 # Clear any existing environment variables for the duration of the test. | |
166 clear_dict = {} | |
167 for key in valid_variables: | |
168 clear_dict[key] = None | |
169 with SetEnvironmentForTest(clear_dict): | |
170 for env_var in valid_variables: | |
171 for url_string in ['hostname', 'http://hostname', 'https://hostname']: | |
172 with SetEnvironmentForTest({env_var: url_string}): | |
173 self._AssertProxyInfosEqual( | |
174 util.ProxyInfoFromEnvironmentVar(env_var), | |
175 httplib2.ProxyInfo( | |
176 httplib2.socks.PROXY_TYPE_HTTP, 'hostname', | |
177 443 if env_var.lower().startswith('https') else 80)) | |
178 # Shouldn't populate info for other variables | |
179 for other_env_var in valid_variables: | |
180 if other_env_var == env_var: continue | |
181 self._AssertProxyInfosEqual( | |
182 util.ProxyInfoFromEnvironmentVar(other_env_var), | |
183 httplib2.ProxyInfo(httplib2.socks.PROXY_TYPE_HTTP, None, 0)) | |
184 for url_string in ['1.2.3.4:50', 'http://1.2.3.4:50', | |
185 'https://1.2.3.4:50']: | |
186 with SetEnvironmentForTest({env_var: url_string}): | |
187 self._AssertProxyInfosEqual( | |
188 util.ProxyInfoFromEnvironmentVar(env_var), | |
189 httplib2.ProxyInfo(httplib2.socks.PROXY_TYPE_HTTP, '1.2.3.4', | |
190 50)) | |
191 for url_string in ['foo:bar@1.2.3.4:50', 'http://foo:bar@1.2.3.4:50', | |
192 'https://foo:bar@1.2.3.4:50']: | |
193 with SetEnvironmentForTest({env_var: url_string}): | |
194 self._AssertProxyInfosEqual( | |
195 util.ProxyInfoFromEnvironmentVar(env_var), | |
196 httplib2.ProxyInfo(httplib2.socks.PROXY_TYPE_HTTP, | |
197 '1.2.3.4', 50, proxy_user='foo', | |
198 proxy_pass='bar')) | |
199 for url_string in ['bar@1.2.3.4:50', 'http://bar@1.2.3.4:50', | |
200 'https://bar@1.2.3.4:50']: | |
201 with SetEnvironmentForTest({env_var: url_string}): | |
202 self._AssertProxyInfosEqual( | |
203 util.ProxyInfoFromEnvironmentVar(env_var), | |
204 httplib2.ProxyInfo(httplib2.socks.PROXY_TYPE_HTTP, '1.2.3.4', | |
205 50, proxy_pass='bar')) | |
206 for env_var in ['proxy', 'noproxy', 'garbage']: | |
207 for url_string in ['1.2.3.4:50', 'http://1.2.3.4:50']: | |
208 with SetEnvironmentForTest({env_var: url_string}): | |
209 self._AssertProxyInfosEqual( | |
210 util.ProxyInfoFromEnvironmentVar(env_var), | |
211 httplib2.ProxyInfo(httplib2.socks.PROXY_TYPE_HTTP, None, 0)) | |
OLD | NEW |