def test_wait_for_haproxy_state_handles_timeout_0(): actual_result = updown_service.wait_for_haproxy_state( service='service_three.main', expected_state='down', timeout=0, wait_time=1) assert actual_result == 1
def test_wait_for_haproxy_with_healthcheck_pass_returns_zero(): mock_stats_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'haproxy_stats.csv') with open(mock_stats_path, 'rb') as fd: with mock.patch('urllib.request.urlopen', return_value=fd), mock.patch( 'subprocess.check_call', side_effect=Exception()), mock.patch( 'nerve_tools.updown_service.check_local_healthcheck', return_value=True): assert 0 == updown_service.wait_for_haproxy_state( 'service_three.main', 'up', 10, 1)
def test_wait_for_haproxy_with_healthcheck_pass_returns_zero(): mock_stats_path = os.path.join( os.path.dirname(os.path.realpath(__file__)), 'haproxy_stats.csv') with open(mock_stats_path) as fd: with contextlib.nested( mock.patch('urllib2.urlopen', return_value=fd), mock.patch('subprocess.check_call', side_effect=Exception()), mock.patch('nerve_tools.updown_service.check_local_healthcheck', return_value=True)) as (_, _, _): assert 0 == updown_service.wait_for_haproxy_state( 'service_three.main', 'up', 10, 1)
def test_wait_for_haproxy_with_healthcheck_fail_returns_one(): mock_stats_path = os.path.join( os.path.dirname(os.path.realpath(__file__)), 'haproxy_stats.csv') with contextlib.nested( mock.patch('urllib2.urlopen', side_effect=lambda _, timeout: open(mock_stats_path)), mock.patch('time.sleep'), mock.patch('subprocess.check_call', side_effect=Exception()), mock.patch('nerve_tools.updown_service.check_local_healthcheck', return_value=False)) as (_, _, _, _): assert 1 == updown_service.wait_for_haproxy_state( 'service_three.main', 'up', 10, 1)
def test_wait_for_haproxy_with_healthcheck_fail_returns_one(): mock_stats_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'haproxy_stats.csv') with mock.patch( 'urllib.request.urlopen', side_effect=lambda _, timeout: open(mock_stats_path, 'rb') ), mock.patch('time.sleep'), mock.patch( 'subprocess.check_call', side_effect=Exception()), mock.patch( 'nerve_tools.updown_service.check_local_healthcheck', return_value=False): assert 1 == updown_service.wait_for_haproxy_state( 'service_three.main', 'up', 10, 1)
def test_wait_for_haproxy_state(): tests = [ # Service is immediately in the expected state [[True], 0, 1], # Service never enters the expected state [10 * [False], 1, 10], # Service enters the expected state on third poll [[False, False, True], 0, 3], ] for test in tests: mock_check_haproxy_state, expected_result, expected_mock_sleep_call_count = test with mock.patch('time.sleep') as mock_sleep, mock.patch( 'subprocess.check_call'), mock.patch( 'nerve_tools.updown_service.check_haproxy_state', side_effect=mock_check_haproxy_state, ): actual_result = updown_service.wait_for_haproxy_state( 'service_three.main', 'down', 10, 1) assert expected_result == actual_result assert mock_sleep.call_count == expected_mock_sleep_call_count
def test_wait_for_haproxy_state(): tests = [ # Service is immediately in the expected state [[True], 0, 1], # Service never enters the expected state [10 * [False], 1, 10], # Service enters the expected state on third poll [[False, False, True], 0, 3], ] for test in tests: mock_check_haproxy_state, expected_result, expected_mock_sleep_call_count = test with contextlib.nested( mock.patch('time.sleep'), mock.patch('subprocess.check_call'), mock.patch('nerve_tools.updown_service.check_haproxy_state', side_effect=mock_check_haproxy_state)) as (mock_sleep, _, _): actual_result = updown_service.wait_for_haproxy_state( 'service_three.main', 'down', 10, 1) assert expected_result == actual_result assert mock_sleep.call_count == expected_mock_sleep_call_count