def test_parse_retry_policy(self): value = { 'Value': """{ "hosts":{ "default":{ "profiles":{ "default":{ "max_timeout_tries":"2", "request_timeout_sec":"2", "connect_timeout_sec":"0.1", "retry_policy":{ "503":{ "idempotent":"true" }, "599":{ "idempotent":"false" } }, "max_tries":"3" } } } } }""" } config = parse_consul_upstream_config(value) upstream = Upstream('some_upstream', config, []) self.assertEqual(upstream.retry_policy.statuses, { 503: True, 599: False })
def test_slow_start_on_server(self): servers = [Server('1', 1, dc='test'), Server('2', 1, dc='test')] upstream = Upstream('upstream', {'slow_start_interval_sec': 10}, servers) self.assertIsNotNone(upstream.servers[0].join_strategy) self.assertIsNotNone(upstream.servers[1].join_strategy)
def test_ignoring_parameters(self): config, servers = Upstream.parse_config('abb=2|server=bla-bla some_parameter=434') self.assertEqual('2', config['abb']) self.assertEqual(1, len(servers)) server = servers[0] self.assertEqual('bla-bla', server.address)
def test_single_server_without_last_separator(self): config, servers = Upstream.parse_config('|server=bla-bla fail_timeout_sec=1 max_fails=30 weight=1 ') self.assertEqual(0, len(config)) self.assertEqual(1, len(servers)) server = servers[0] self.assertEqual('bla-bla', server.address) self.assertEqual(1, server.weight)
def test_defaults(self): config, servers = Upstream.parse_config('|server=bla-bla') self.assertEqual(0, len(config)) self.assertEqual(1, len(servers)) server = servers[0] self.assertEqual('bla-bla', server.address) self.assertEqual(1, server.weight)
def test_multiple_servers(self): config, servers = Upstream.parse_config('|server=bla-bla weight=1 | server=someserver weight=2') self.assertEqual(0, len(config)) self.assertEqual(2, len(servers)) server = servers[0] self.assertEqual('bla-bla', server.address) self.assertEqual(1, server.weight) server = servers[1] self.assertEqual('someserver', server.address) self.assertEqual(2, server.weight)
def test_single_server(self): config, servers = Upstream.parse_config( 'max_tries=1 fail_timeout_sec=1 request_timeout_sec=0.2 max_fails=30 |server=172.17.0.1:2800 weight=100|') self.assertEqual('1', config['max_tries']) self.assertEqual('30', config['max_fails']) self.assertEqual('1', config['fail_timeout_sec']) self.assertEqual('0.2', config['request_timeout_sec']) self.assertEqual(1, len(servers)) server = servers[0] self.assertEqual('172.17.0.1:2800', server.address) self.assertEqual(100, server.weight)
def test_rack_and_datacenter_parameters(self): config, servers = Upstream.parse_config('|server=s1 rack=r1 dc=dc1|server=s2 rack=r2 dc=dc2') self.assertEqual(2, len(servers)) server = servers[0] self.assertEqual('s1', server.address) self.assertEqual('r1', server.rack) self.assertEqual('dc1', server.datacenter) server = servers[1] self.assertEqual('s2', server.address) self.assertEqual('r2', server.rack) self.assertEqual('dc2', server.datacenter)
def test_curl_string_put(self): request = BalancedHttpRequest( 'http://test.com', Upstream.get_single_host_upstream(), 'test', '/path', 'test', data='DATA', method='PUT', content_type=media_types.TEXT_PLAIN).make_request() self.assertEqual( request_to_curl_string(request), "curl -X PUT 'http://test.com/path' -H 'Content-Length: 4' -H 'Content-Type: text/plain' " "-H '{}: test' --data 'DATA'".format(USER_AGENT_HEADER))
def test_curl_string_binary(self): request = BalancedHttpRequest( 'http://test.com', Upstream.get_single_host_upstream(), 'test', '/path', 'test', data='тест', method='POST', content_type=media_types.TEXT_PLAIN).make_request() self.assertEqual( request_to_curl_string(request), "echo -e '\\xd1\\x82\\xd0\\xb5\\xd1\\x81\\xd1\\x82' | " "curl -X POST 'http://test.com/path' -H 'Content-Length: 8' -H 'Content-Type: text/plain' " "-H '{}: test' --data-binary @-".format(USER_AGENT_HEADER))
def test_curl_string_post(self): request = BalancedHttpRequest('http://test.com', Upstream.get_single_host_upstream(), 'test', '/path', 'test', data={ 'param': 'value' }, method='POST').make_request() self.assertEqual( request_to_curl_string(request), "curl -X POST 'http://test.com/path' -H 'Content-Length: 11' " "-H 'Content-Type: application/x-www-form-urlencoded' " "-H '{}: test' --data 'param=value'".format(USER_AGENT_HEADER))
def test_curl_string_get(self): request = BalancedHttpRequest('http://test.com', Upstream.get_single_host_upstream(), 'test', '/path', 'test', data={ 'param': 'value' }, headers={ 'Accept': media_types.APPLICATION_JSON }).make_request() self.assertEqual( request_to_curl_string(request), "curl -X GET 'http://test.com/path?param=value' " "-H 'Accept: application/json' -H '{}: test'".format( USER_AGENT_HEADER))
def test_session_required_false(self): servers = [Server('1', 1, dc='test'), Server('2', 1, dc='test')] upstream = Upstream('upstream', {}, servers) self.assertEqual(upstream.session_required, False)
def test_session_required_true(self): servers = [Server('1', 1, dc='test'), Server('2', 1, dc='test')] upstream = Upstream('upstream', {'session_required': 'true'}, servers) self.assertEqual(upstream.session_required, True)
def _upstream(servers, config=None): return Upstream('upstream', {} if config is None else config, servers)
def register_ports_for_upstream(self, *ports): upstream = Upstream( 'test', self.get_upstream_config(), [Server(f'127.0.0.1:{port}', dc='test') for port in ports]) self.http_client_factory.upstreams[upstream.name] = upstream
def _upstream(weights): return Upstream( 'upstream', {}, [Server(str(weight), weight, dc='test') for weight in weights])