testing/mozbase/mozprofile/tests/server_locations.py

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:5c062d21bee5
1 #!/usr/bin/env python
2
3 # This Source Code Form is subject to the terms of the Mozilla Public
4 # License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 # You can obtain one at http://mozilla.org/MPL/2.0/.
6
7 import mozfile
8 import os
9 import shutil
10 import tempfile
11 import unittest
12 from mozprofile.permissions import ServerLocations, \
13 MissingPrimaryLocationError, MultiplePrimaryLocationsError, \
14 DuplicateLocationError, BadPortLocationError, LocationsSyntaxError
15
16 class ServerLocationsTest(unittest.TestCase):
17 """test server locations"""
18
19 locations = """# This is the primary location from which tests run.
20 #
21 http://mochi.test:8888 primary,privileged
22
23 # a few test locations
24 http://127.0.0.1:80 privileged
25 http://127.0.0.1:8888 privileged
26 https://test:80 privileged
27 http://example.org:80 privileged
28 http://test1.example.org privileged
29
30 """
31
32 locations_no_primary = """http://secondary.test:80 privileged
33 http://tertiary.test:8888 privileged
34 """
35
36 locations_bad_port = """http://mochi.test:8888 primary,privileged
37 http://127.0.0.1:80 privileged
38 http://127.0.0.1:8888 privileged
39 http://test:badport privileged
40 http://example.org:80 privileged
41 """
42
43 def compare_location(self, location, scheme, host, port, options):
44 self.assertEqual(location.scheme, scheme)
45 self.assertEqual(location.host, host)
46 self.assertEqual(location.port, port)
47 self.assertEqual(location.options, options)
48
49 def create_temp_file(self, contents):
50 f = mozfile.NamedTemporaryFile()
51 f.write(contents)
52 f.flush()
53 return f
54
55 def test_server_locations(self):
56 # write a permissions file
57 f = self.create_temp_file(self.locations)
58
59 # read the locations
60 locations = ServerLocations(f.name)
61
62 # ensure that they're what we expect
63 self.assertEqual(len(locations), 6)
64 i = iter(locations)
65 self.compare_location(i.next(), 'http', 'mochi.test', '8888',
66 ['primary', 'privileged'])
67 self.compare_location(i.next(), 'http', '127.0.0.1', '80',
68 ['privileged'])
69 self.compare_location(i.next(), 'http', '127.0.0.1', '8888',
70 ['privileged'])
71 self.compare_location(i.next(), 'https', 'test', '80', ['privileged'])
72 self.compare_location(i.next(), 'http', 'example.org', '80',
73 ['privileged'])
74 self.compare_location(i.next(), 'http', 'test1.example.org', '8888',
75 ['privileged'])
76
77 locations.add_host('mozilla.org')
78 self.assertEqual(len(locations), 7)
79 self.compare_location(i.next(), 'http', 'mozilla.org', '80',
80 ['privileged'])
81
82 # test some errors
83 self.assertRaises(MultiplePrimaryLocationsError, locations.add_host,
84 'primary.test', options='primary')
85
86 # We no longer throw these DuplicateLocation Error
87 try:
88 locations.add_host('127.0.0.1')
89 except DuplicateLocationError:
90 self.assertTrue(False, "Should no longer throw DuplicateLocationError")
91
92 self.assertRaises(BadPortLocationError, locations.add_host, '127.0.0.1',
93 port='abc')
94
95 # test some errors in locations file
96 f = self.create_temp_file(self.locations_no_primary)
97
98 exc = None
99 try:
100 ServerLocations(f.name)
101 except LocationsSyntaxError, e:
102 exc = e
103 self.assertNotEqual(exc, None)
104 self.assertEqual(exc.err.__class__, MissingPrimaryLocationError)
105 self.assertEqual(exc.lineno, 3)
106
107 # test bad port in a locations file to ensure lineno calculated
108 # properly.
109 f = self.create_temp_file(self.locations_bad_port)
110
111 exc = None
112 try:
113 ServerLocations(f.name)
114 except LocationsSyntaxError, e:
115 exc = e
116 self.assertNotEqual(exc, None)
117 self.assertEqual(exc.err.__class__, BadPortLocationError)
118 self.assertEqual(exc.lineno, 4)
119
120 def test_server_locations_callback(self):
121 class CallbackTest(object):
122 last_locations = None
123
124 def callback(self, locations):
125 self.last_locations = locations
126
127 c = CallbackTest()
128 f = self.create_temp_file(self.locations)
129 locations = ServerLocations(f.name, c.callback)
130
131 # callback should be for all locations in file
132 self.assertEqual(len(c.last_locations), 6)
133
134 # validate arbitrary one
135 self.compare_location(c.last_locations[2], 'http', '127.0.0.1', '8888',
136 ['privileged'])
137
138 locations.add_host('a.b.c')
139
140 # callback should be just for one location
141 self.assertEqual(len(c.last_locations), 1)
142 self.compare_location(c.last_locations[0], 'http', 'a.b.c', '80',
143 ['privileged'])
144
145 # read a second file, which should generate a callback with both
146 # locations.
147 f = self.create_temp_file(self.locations_no_primary)
148 locations.read(f.name)
149 self.assertEqual(len(c.last_locations), 2)
150
151
152 if __name__ == '__main__':
153 unittest.main()

mercurial