def test_build_stats_message_no_listener(self, mock_list_files, mock_get_stats, mock_is_running): mock_list_files.return_value = {LISTENER_ID1: 'TEST', LISTENER_ID2: 'TEST2'} mock_is_running.side_effect = [True, False] mock_get_stats.return_value = SAMPLE_STATS, SAMPLE_POOL_STATUS health_daemon.build_stats_message() self.assertEqual(1, mock_get_stats.call_count)
def test_build_stats_message_no_listener(self, mock_list_files, mock_get_stats, mock_is_running): mock_list_files.return_value = {LISTENER_ID1: 'TEST', LISTENER_ID2: 'TEST2'} mock_is_running.side_effect = [True, False] mock_get_stats.return_value = SAMPLE_STATS, SAMPLE_POOL_STATUS health_daemon.build_stats_message() self.assertEqual(mock_get_stats.call_count, 1)
def test_build_stats_message_no_listener(self, mock_list_files, mock_get_stats, mock_is_running): health_daemon.COUNTERS = None health_daemon.COUNTERS_FILE = None lb1_stats_socket = '/var/lib/octavia/{0}/haproxy.sock'.format(LB_ID1) mock_list_files.return_value = {LB_ID1: lb1_stats_socket} mock_is_running.return_value = False with mock.patch('os.open'), mock.patch.object( os, 'fdopen', self.mock_open) as mock_fdopen: health_daemon.build_stats_message() self.assertEqual(0, mock_get_stats.call_count) self.assertEqual(0, mock_fdopen().read.call_count)
def test_haproxy_restart(self, mock_list_files, mock_get_stats, mock_is_running): health_daemon.COUNTERS = None health_daemon.COUNTERS_FILE = None lb1_stats_socket = '/var/lib/octavia/{0}/haproxy.sock'.format(LB_ID1) mock_list_files.return_value = {LB_ID1: lb1_stats_socket} mock_is_running.return_value = True mock_get_stats.return_value = SAMPLE_STATS, SAMPLE_POOL_STATUS with mock.patch('os.open'), mock.patch.object( os, 'fdopen', self.mock_open) as mock_fdopen: mock_fdopen().read.return_value = simplejson.dumps({ LISTENER_ID1: { 'bin': 15, 'bout': 20 }, }) msg = health_daemon.build_stats_message() self.assertEqual(SAMPLE_MSG_HAPROXY_RESTART, msg) mock_get_stats.assert_any_call(lb1_stats_socket) mock_fdopen().write.assert_called_once_with( simplejson.dumps({ LISTENER_ID1: { 'bin': int(FRONTEND_STATS['bin']), 'bout': int(FRONTEND_STATS['bout']), 'ereq': int(FRONTEND_STATS['ereq']), 'stot': int(FRONTEND_STATS['stot']) } }))
def test_bulid_stats_message_with_udp_listener( self, mock_get_udp_listeners, mock_get_listener_stats, mock_get_pool_status): udp_listener_id1 = uuidutils.generate_uuid() udp_listener_id2 = uuidutils.generate_uuid() udp_listener_id3 = uuidutils.generate_uuid() pool_id = uuidutils.generate_uuid() member_id1 = uuidutils.generate_uuid() member_id2 = uuidutils.generate_uuid() mock_get_udp_listeners.return_value = [udp_listener_id1, udp_listener_id2, udp_listener_id3] mock_get_listener_stats.return_value = { udp_listener_id1: { 'status': constants.OPEN, 'stats': {'bin': 6387472, 'stot': 5, 'bout': 7490, 'ereq': 0, 'scur': 0}}, udp_listener_id3: { 'status': constants.DOWN, 'stats': {'bin': 0, 'stot': 0, 'bout': 0, 'ereq': 0, 'scur': 0}} } udp_pool_status = { 'lvs': { 'uuid': pool_id, 'status': constants.UP, 'members': {member_id1: constants.UP, member_id2: constants.UP}}} mock_get_pool_status.side_effect = ( lambda x: udp_pool_status if x == udp_listener_id1 else {}) # the first listener can get all necessary info. # the second listener can not get listener stats, so we won't report it # the third listener can get listener stats, but can not get pool # status, so the result will just contain the listener status for it. expected = { 'listeners': { udp_listener_id1: { 'status': constants.OPEN, 'pools': { pool_id: { 'status': constants.UP, 'members': { member_id1: constants.UP, member_id2: constants.UP}}}, 'stats': {'conns': 0, 'totconns': 5, 'ereq': 0, 'rx': 6387472, 'tx': 7490}}, udp_listener_id3: { 'status': constants.DOWN, 'pools': {}, 'stats': {'conns': 0, 'totconns': 0, 'ereq': 0, 'rx': 0, 'tx': 0}}}, 'id': None, 'seq': mock.ANY, 'ver': health_daemon.MSG_VER} msg = health_daemon.build_stats_message() self.assertEqual(expected, msg)
def test_build_stats_message_mismatch_pool(self, mock_list_files, mock_get_stats, mock_is_running): mock_list_files.return_value = {LISTENER_ID1: 'TEST', LISTENER_ID2: 'TEST2'} mock_is_running.return_value = True mock_get_stats.return_value = SAMPLE_STATS, SAMPLE_BOGUS_POOL_STATUS msg = health_daemon.build_stats_message() self.assertEqual(msg['listeners'][LISTENER_ID1]['pools'], {})
def test_build_stats_message_mismatch_pool(self, mock_list_files, mock_get_stats, mock_is_running): mock_list_files.return_value = {LISTENER_ID1: 'TEST', LISTENER_ID2: 'TEST2'} mock_is_running.return_value = True mock_get_stats.return_value = SAMPLE_STATS, SAMPLE_BOGUS_POOL_STATUS msg = health_daemon.build_stats_message() self.assertEqual(msg['listeners'][LISTENER_ID1]['pools'], {})
def test_build_stats_message(self, mock_list_files, mock_get_stats, mock_is_running): mock_list_files.return_value = {LISTENER_ID1: 'TEST', LISTENER_ID2: 'TEST2'} mock_is_running.return_value = True mock_get_stats.return_value = SAMPLE_STATS, SAMPLE_POOL_STATUS msg = health_daemon.build_stats_message() self.assertEqual(msg, SAMPLE_STATS_MSG) mock_get_stats.assert_any_call('TEST') mock_get_stats.assert_any_call('TEST2')
def test_build_stats_message(self, mock_list_files, mock_get_stats, mock_is_running): mock_list_files.return_value = {LISTENER_ID1: 'TEST', LISTENER_ID2: 'TEST2'} mock_is_running.return_value = True mock_get_stats.return_value = SAMPLE_STATS, SAMPLE_POOL_STATUS msg = health_daemon.build_stats_message() self.assertEqual(msg, SAMPLE_STATS_MSG) mock_get_stats.assert_any_call('TEST') mock_get_stats.assert_any_call('TEST2')
def test_build_stats_message_with_udp_listener(self, mock_get_udp_listeners, mock_get_listener_stats, mock_get_pool_status): health_daemon.COUNTERS = None health_daemon.COUNTERS_FILE = None udp_listener_id1 = uuidutils.generate_uuid() udp_listener_id2 = uuidutils.generate_uuid() udp_listener_id3 = uuidutils.generate_uuid() pool_id = uuidutils.generate_uuid() member_id1 = uuidutils.generate_uuid() member_id2 = uuidutils.generate_uuid() mock_get_udp_listeners.return_value = [ udp_listener_id1, udp_listener_id2, udp_listener_id3 ] mock_get_listener_stats.return_value = { udp_listener_id1: { 'status': constants.OPEN, 'stats': { 'bin': 5, 'stot': 5, 'bout': 10, 'ereq': 0, 'scur': 0 } }, udp_listener_id3: { 'status': constants.DOWN, 'stats': { 'bin': 0, 'stot': 0, 'bout': 0, 'ereq': 0, 'scur': 0 } } } udp_pool_status = { 'lvs': { 'uuid': pool_id, 'status': constants.UP, 'members': { member_id1: constants.UP, member_id2: constants.UP } } } mock_get_pool_status.side_effect = (lambda x: udp_pool_status if x == udp_listener_id1 else {}) # the first listener can get all necessary info. # the second listener can not get listener stats, so we won't report it # the third listener can get listener stats, but can not get pool # status, so the result will just contain the listener status for it. expected = { 'listeners': { udp_listener_id1: { 'status': constants.OPEN, 'stats': { 'conns': 0, 'totconns': 5, 'ereq': 0, 'rx': 4, 'tx': 8 } }, udp_listener_id3: { 'status': constants.DOWN, 'stats': { 'conns': 0, 'totconns': 0, 'ereq': 0, 'rx': 0, 'tx': 0 } } }, 'pools': { pool_id: { 'status': constants.UP, 'members': { member_id1: constants.UP, member_id2: constants.UP } } }, 'id': AMPHORA_ID, 'seq': mock.ANY, 'ver': health_daemon.MSG_VER } with mock.patch('os.open'), mock.patch.object( os, 'fdopen', self.mock_open) as mock_fdopen: mock_fdopen().read.return_value = simplejson.dumps({ udp_listener_id1: { 'bin': 1, 'bout': 2, "ereq": 0, "stot": 0 } }) msg = health_daemon.build_stats_message() self.assertEqual(expected, msg) mock_fdopen().write.assert_called_once_with( simplejson.dumps({ udp_listener_id1: { 'bin': 5, 'bout': 10, 'ereq': 0, 'stot': 5 }, udp_listener_id3: { 'bin': 0, 'bout': 0, 'ereq': 0, 'stot': 0 }, }))