test_server_locations.py (3464B)
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 mozunit 8 import pytest 9 from mozprofile.permissions import ( 10 BadPortLocationError, 11 LocationsSyntaxError, 12 MissingPrimaryLocationError, 13 MultiplePrimaryLocationsError, 14 ServerLocations, 15 ) 16 17 LOCATIONS = """# This is the primary location from which tests run. 18 # 19 http://mochi.test:8888 primary,privileged 20 21 # a few test locations 22 http://127.0.0.1:80 privileged 23 http://127.0.0.1:8888 privileged 24 https://test:80 privileged 25 http://example.org:80 privileged 26 http://test1.example.org privileged 27 28 """ 29 30 LOCATIONS_NO_PRIMARY = """http://secondary.test:80 privileged 31 http://tertiary.test:8888 privileged 32 """ 33 34 LOCATIONS_BAD_PORT = """http://mochi.test:8888 primary,privileged 35 http://127.0.0.1:80 privileged 36 http://127.0.0.1:8888 privileged 37 http://test:badport privileged 38 http://example.org:80 privileged 39 """ 40 41 42 def compare_location(location, scheme, host, port, options): 43 assert location.scheme == scheme 44 assert location.host == host 45 assert location.port == port 46 assert location.options == options 47 48 49 @pytest.fixture 50 def create_temp_file(tmpdir): 51 def inner(contents): 52 f = tmpdir.mkdtemp().join("locations.txt") 53 f.write(contents) 54 return f.strpath 55 56 return inner 57 58 59 def test_server_locations(create_temp_file): 60 # write a permissions file 61 f = create_temp_file(LOCATIONS) 62 63 # read the locations 64 locations = ServerLocations(f) 65 66 # ensure that they're what we expect 67 assert len(locations) == 6 68 i = iter(locations) 69 compare_location(next(i), "http", "mochi.test", "8888", ["primary", "privileged"]) 70 compare_location(next(i), "http", "127.0.0.1", "80", ["privileged"]) 71 compare_location(next(i), "http", "127.0.0.1", "8888", ["privileged"]) 72 compare_location(next(i), "https", "test", "80", ["privileged"]) 73 compare_location(next(i), "http", "example.org", "80", ["privileged"]) 74 compare_location(next(i), "http", "test1.example.org", "8888", ["privileged"]) 75 76 locations.add_host("mozilla.org") 77 assert len(locations) == 7 78 compare_location(next(i), "http", "mozilla.org", "80", ["privileged"]) 79 80 # test some errors 81 with pytest.raises(MultiplePrimaryLocationsError): 82 locations.add_host("primary.test", options="primary") 83 84 # assert we don't throw DuplicateLocationError 85 locations.add_host("127.0.0.1") 86 87 with pytest.raises(BadPortLocationError): 88 locations.add_host("127.0.0.1", port="abc") 89 90 # test some errors in locations file 91 f = create_temp_file(LOCATIONS_NO_PRIMARY) 92 93 exc = None 94 try: 95 ServerLocations(f) 96 except LocationsSyntaxError as e: 97 exc = e 98 assert exc is not None 99 assert exc.err.__class__ == MissingPrimaryLocationError 100 assert exc.lineno == 3 101 102 # test bad port in a locations file to ensure lineno calculated 103 # properly. 104 f = create_temp_file(LOCATIONS_BAD_PORT) 105 106 exc = None 107 try: 108 ServerLocations(f) 109 except LocationsSyntaxError as e: 110 exc = e 111 assert exc is not None 112 assert exc.err.__class__ == BadPortLocationError 113 assert exc.lineno == 4 114 115 116 if __name__ == "__main__": 117 mozunit.main()