<s> import os <EOL> import mock <EOL> import requests <EOL> from paasta_tools . monitoring . replication_utils import backend_is_up <EOL> from paasta_tools . monitoring . replication_utils import get_registered_marathon_tasks <EOL> from paasta_tools . monitoring . replication_utils import get_replication_for_services <EOL> from paasta_tools . monitoring . replication_utils import ip_port_hostname_from_svname <EOL> from paasta_tools . monitoring . replication_utils import match_backends_and_tasks <EOL> from paasta_tools . utils import DEFAULT_SYNAPSE_HAPROXY_URL_FORMAT <EOL> def test_get_replication_for_service ( ) : <EOL> testdir = os . path . dirname ( os . path . realpath ( __file__ ) ) <EOL> testdata = os . path . join ( testdir , '<STR_LIT>' ) <EOL> with open ( testdata , '<STR_LIT:r>' ) as fd : <EOL> mock_haproxy_data = fd . read ( ) <EOL> mock_response = mock . Mock ( ) <EOL> mock_response . text = mock_haproxy_data <EOL> mock_get = mock . Mock ( return_value = ( mock_response ) ) <EOL> with mock . patch . object ( requests . Session , '<STR_LIT>' , mock_get ) : <EOL> replication_result = get_replication_for_services ( <EOL> '<STR_LIT>' , <EOL> <NUM_LIT> , <EOL> DEFAULT_SYNAPSE_HAPROXY_URL_FORMAT , <EOL> [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> ) <EOL> expected = { <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:3> <EOL> } <EOL> assert expected == replication_result <EOL> def test_get_registered_marathon_tasks ( ) : <EOL> backends = [ <EOL> { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT:status>" : "<STR_LIT>" } , <EOL> { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT:status>" : "<STR_LIT>" } , <EOL> { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT:status>" : "<STR_LIT>" } , <EOL> { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT:status>" : "<STR_LIT>" } , <EOL> { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT:status>" : "<STR_LIT>" } , <EOL> ] <EOL> hostnames = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> good_task1 = mock . Mock ( host = '<STR_LIT>' , ports = [ <NUM_LIT> ] ) <EOL> good_task2 = mock . Mock ( host = '<STR_LIT>' , ports = [ <NUM_LIT> ] ) <EOL> bad_task = mock . Mock ( host = '<STR_LIT>' , ports = [ <NUM_LIT> ] ) <EOL> marathon_tasks = [ <EOL> good_task1 , <EOL> good_task2 , <EOL> bad_task , <EOL> ] <EOL> with mock . patch ( <EOL> '<STR_LIT>' , <EOL> return_value = backends <EOL> ) as mock_get_multiple_backends : <EOL> with mock . patch ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> side_effect = lambda x : hostnames [ x ] , <EOL> ) : <EOL> actual = get_registered_marathon_tasks ( <EOL> '<STR_LIT>' , <EOL> <NUM_LIT> , <EOL> DEFAULT_SYNAPSE_HAPROXY_URL_FORMAT , <EOL> '<STR_LIT>' , <EOL> marathon_tasks , <EOL> ) <EOL> expected = [ good_task1 , good_task2 ] <EOL> assert actual == expected <EOL> mock_get_multiple_backends . assert_called_once_with ( <EOL> [ '<STR_LIT>' ] , <EOL> synapse_host = '<STR_LIT>' , <EOL> synapse_port = <NUM_LIT> , <EOL> synapse_haproxy_url_format = DEFAULT_SYNAPSE_HAPROXY_URL_FORMAT , <EOL> ) <EOL> def test_backend_is_up ( ) : <EOL> assert True is backend_is_up ( { "<STR_LIT:status>" : "<STR_LIT>" } ) <EOL> assert True is backend_is_up ( { "<STR_LIT:status>" : "<STR_LIT>" } ) <EOL> assert False is backend_is_up ( { "<STR_LIT:status>" : "<STR_LIT>" } ) <EOL> assert False is backend_is_up ( { "<STR_LIT:status>" : "<STR_LIT>" } ) <EOL> assert False is backend_is_up ( { "<STR_LIT:status>" : "<STR_LIT>" } ) <EOL> def test_ip_port_hostname_from_svname ( ) : <EOL> assert ( "<STR_LIT>" , <NUM_LIT:5> , "<STR_LIT>" ) == ip_port_hostname_from_svname ( "<STR_LIT>" ) <EOL> def test_match_backends_and_tasks ( ) : <EOL> backends = [ <EOL> { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT:status>" : "<STR_LIT>" } , <EOL> { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT:status>" : "<STR_LIT>" } , <EOL> { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT:status>" : "<STR_LIT>" } , <EOL> { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT:status>" : "<STR_LIT>" } , <EOL> { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT:status>" : "<STR_LIT>" } , <EOL> ] <EOL> hostnames = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> good_task1 = mock . Mock ( host = '<STR_LIT>' , ports = [ <NUM_LIT> ] ) <EOL> good_task2 = mock . Mock ( host = '<STR_LIT>' , ports = [ <NUM_LIT> ] ) <EOL> bad_task = mock . Mock ( host = '<STR_LIT>' , ports = [ <NUM_LIT> ] ) <EOL> tasks = [ good_task1 , good_task2 , bad_task ] <EOL> with mock . patch ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> side_effect = lambda x : hostnames [ x ] , <EOL> ) : <EOL> expected = [ <EOL> ( backends [ <NUM_LIT:0> ] , good_task1 ) , <EOL> ( backends [ <NUM_LIT:1> ] , good_task2 ) , <EOL> ( None , bad_task ) , <EOL> ( backends [ <NUM_LIT:2> ] , None ) , <EOL> ( backends [ <NUM_LIT:3> ] , None ) , <EOL> ( backends [ <NUM_LIT:4> ] , None ) , <EOL> ] <EOL> actual = match_backends_and_tasks ( backends , tasks ) <EOL> assert sorted ( actual ) == sorted ( expected ) </s>
