Changes in unit test

This commit is contained in:
Shreya 2021-01-14 18:13:43 +05:30
parent 9240408956
commit ca460b7348
1 changed files with 60 additions and 32 deletions

View File

@ -27,60 +27,88 @@ RESULT_STRINGS =\
'only_selected': "Only selected hosts can make connections (SSL or non-SSL).\n" 'only_selected': "Only selected hosts can make connections (SSL or non-SSL).\n"
} }
EXAMPLE_EXCEPTIONS =\ EXAMPLE_EXCEPTIONS_WITH_EXPECTED_RESULTS =\
[ {
RELEVANT_EXCEPTION_STRINGS['pwd_auth_failed'], # SSL not configured, all non-SSL allowed # SSL not configured, all non-SSL allowed, # SSL not configured, all non-SSL allowed
RELEVANT_EXCEPTION_STRINGS['pwd_auth_failed']: [
RESULT_STRINGS['ssl_not_conf'],
RESULT_STRINGS['all_non_ssl']
],
RELEVANT_EXCEPTION_STRINGS['ssl_off_entry_not_found'], # SSL not configured, selected non-SSL allowed # SSL not configured, selected non-SSL allowed
RELEVANT_EXCEPTION_STRINGS['ssl_off_entry_not_found']: [
RESULT_STRINGS['ssl_not_conf'],
RESULT_STRINGS['selected_non_ssl']
],
# all SSL allowed, all non-SSL allowed
'\n'.join([RELEVANT_EXCEPTION_STRINGS['pwd_auth_failed'], '\n'.join([RELEVANT_EXCEPTION_STRINGS['pwd_auth_failed'],
RELEVANT_EXCEPTION_STRINGS['pwd_auth_failed']]), # all SSL allowed, all non-SSL allowed RELEVANT_EXCEPTION_STRINGS['pwd_auth_failed']]): [
RESULT_STRINGS['ssl_conf'],
RESULT_STRINGS['all_ssl'],
RESULT_STRINGS['all_non_ssl']
],
# all SSL allowed, selected non-SSL allowed
'\n'.join([RELEVANT_EXCEPTION_STRINGS['pwd_auth_failed'], '\n'.join([RELEVANT_EXCEPTION_STRINGS['pwd_auth_failed'],
RELEVANT_EXCEPTION_STRINGS['ssl_off_entry_not_found']]), # all SSL allowed, selected non-SSL allowed RELEVANT_EXCEPTION_STRINGS['ssl_off_entry_not_found']]): [
RESULT_STRINGS['ssl_conf'],
RESULT_STRINGS['all_ssl'],
RESULT_STRINGS['selected_non_ssl']
],
# selected SSL allowed, all non-SSL allowed
'\n'.join([RELEVANT_EXCEPTION_STRINGS['ssl_on_entry_not_found'], '\n'.join([RELEVANT_EXCEPTION_STRINGS['ssl_on_entry_not_found'],
RELEVANT_EXCEPTION_STRINGS['pwd_auth_failed']]), # selected SSL allowed, all non-SSL allowed RELEVANT_EXCEPTION_STRINGS['pwd_auth_failed']]): [
RESULT_STRINGS['ssl_conf'],
RESULT_STRINGS['selected_ssl'],
RESULT_STRINGS['all_non_ssl']
],
# selected SSL allowed, selected non-SSL allowed
'\n'.join([RELEVANT_EXCEPTION_STRINGS['ssl_on_entry_not_found'], '\n'.join([RELEVANT_EXCEPTION_STRINGS['ssl_on_entry_not_found'],
RELEVANT_EXCEPTION_STRINGS['ssl_off_entry_not_found']]) # selected SSL allowed, selected non-SSL allowed RELEVANT_EXCEPTION_STRINGS['ssl_off_entry_not_found']]): [
] # don't change order! RESULT_STRINGS['ssl_conf'],
RESULT_STRINGS['only_selected']
]
}
EXPECTED_RESULTS =\ # EXPECTED_RESULTS =\
[ # [
[RESULT_STRINGS['ssl_not_conf'], # [RESULT_STRINGS['ssl_not_conf'],
RESULT_STRINGS['all_non_ssl']], # SSL not configured, all non-SSL allowed # RESULT_STRINGS['all_non_ssl']], # SSL not configured, all non-SSL allowed
[RESULT_STRINGS['ssl_not_conf'], # [RESULT_STRINGS['ssl_not_conf'],
RESULT_STRINGS['selected_non_ssl']], # SSL not configured, selected non-SSL allowed # RESULT_STRINGS['selected_non_ssl']], # SSL not configured, selected non-SSL allowed
[RESULT_STRINGS['ssl_conf'], # [RESULT_STRINGS['ssl_conf'],
RESULT_STRINGS['all_ssl'], # RESULT_STRINGS['all_ssl'],
RESULT_STRINGS['all_non_ssl']], # all SSL allowed, all non-SSL allowed # RESULT_STRINGS['all_non_ssl']], # all SSL allowed, all non-SSL allowed
[RESULT_STRINGS['ssl_conf'], # [RESULT_STRINGS['ssl_conf'],
RESULT_STRINGS['all_ssl'], # RESULT_STRINGS['all_ssl'],
RESULT_STRINGS['selected_non_ssl']], # all SSL allowed, selected non-SSL allowed # RESULT_STRINGS['selected_non_ssl']], # all SSL allowed, selected non-SSL allowed
[RESULT_STRINGS['ssl_conf'], # [RESULT_STRINGS['ssl_conf'],
RESULT_STRINGS['selected_ssl'], # RESULT_STRINGS['selected_ssl'],
RESULT_STRINGS['all_non_ssl']], # selected SSL allowed, all non-SSL allowed # RESULT_STRINGS['all_non_ssl']], # selected SSL allowed, all non-SSL allowed
[RESULT_STRINGS['ssl_conf'], # [RESULT_STRINGS['ssl_conf'],
RESULT_STRINGS['only_selected']] # selected SSL allowed, selected non-SSL allowed # RESULT_STRINGS['only_selected']] # selected SSL allowed, selected non-SSL allowed
] # don't change order! # ] # don't change order!
class TestPostgreSQLFinger(TestCase): class TestPostgreSQLFinger(TestCase):
def test_is_relevant_exception(self): def test_is_relevant_exception(self):
assert PostgreSQLFinger().is_relevant_exception(IRRELEVANT_EXCEPTION_STRING) is False assert PostgreSQLFinger().is_relevant_exception(IRRELEVANT_EXCEPTION_STRING) is False
for exception_string in EXAMPLE_EXCEPTIONS: for exception_string in EXAMPLE_EXCEPTIONS_WITH_EXPECTED_RESULTS:
assert PostgreSQLFinger().is_relevant_exception(exception_string) is True assert PostgreSQLFinger().is_relevant_exception(exception_string) is True
def test_analyze_operational_error(self): def test_analyze_operational_error(self):
host = Mock(['services']) host = Mock(['services'])
host.services = {} host.services = {}
for idx in range(len(EXAMPLE_EXCEPTIONS)): for exception_string in EXAMPLE_EXCEPTIONS_WITH_EXPECTED_RESULTS:
with self.subTest(msg=f"Checking result for exception: {EXAMPLE_EXCEPTIONS[idx]}"): with self.subTest(msg=f"Checking result for exception: {exception_string}"):
PostgreSQLFinger().analyze_operational_error(host, EXAMPLE_EXCEPTIONS[idx]) PostgreSQLFinger().analyze_operational_error(host, exception_string)
assert host.services['PostgreSQL']['communication_encryption_details'] == ''.join(EXPECTED_RESULTS[idx]) assert host.services['PostgreSQL']['communication_encryption_details'] ==\
''.join(EXAMPLE_EXCEPTIONS_WITH_EXPECTED_RESULTS[exception_string])