<commit_before><commit_msg>some tests for the new mon_errors check<commit_after>from ceph_deploy import mon
from mock import Mock


# NOTE: If at some point we re-use this helper, move it out
# and make it even more generic

def make_fake_conn(receive_returns=None):
    receive_returns = receive_returns or (['{}'], '', 0)
    conn = Mock()
    conn.return_value = conn
    conn.execute = conn
    conn.receive = Mock(return_value=receive_returns)
    conn.result = Mock(return_value=conn)
    return conn


class TestCatchCommonErrors(object):

    def setup(self):
        self.logger = Mock()

    def assert_logger_message(self, logger, msg):
        calls = logger.call_args_list
        for log_call in calls:
            if msg in log_call[0][0]:
                return True
        raise AssertionError('"%s" was not found in any of %s' % (msg, calls))

    def test_warn_if_no_intial_members(self):
        fake_conn = make_fake_conn()
        mon.catch_mon_errors(fake_conn, self.logger, 'host', {})
        expected_msg = 'is not defined in `mon initial members`'
        self.assert_logger_message(self.logger.warning, expected_msg)

    def test_warn_if_not_mon_in_monmap(self):
        fake_conn = make_fake_conn()
        mon.catch_mon_errors(fake_conn, self.logger, 'host', {})
        expected_msg = 'does not exist in monmap'
        self.assert_logger_message(self.logger.warning, expected_msg)

    def test_warn_if_not_public_addr_and_not_public_netw(self):
        fake_conn = make_fake_conn()
        mon.catch_mon_errors(fake_conn, self.logger, 'host', {'global': ''})
        expected_msg = 'neither `public_addr` nor `public_network`'
        self.assert_logger_message(self.logger.warning, expected_msg)
{'fraction_non_alphanumeric': 0.097631426920855, 'fraction_numerical': 0.0017331022530329288, 'mean_word_length': 3.463917525773196, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2948471', 'n_tokens_mistral': 549, 'n_tokens_neox': 539, 'n_words': 130}