Test for callback initiation.

Checks for presence of required atributes,
basic functionality of the init method
and the current_time module function.

Closes-Bug: #1922726

Signed-off-by: Jiri Podivin <jpodivin@redhat.com>
Change-Id: I2e9a4640fad0f9c44b5503d53ea2613a416c6e18
This commit is contained in:
Jiri Podivin 2021-03-16 17:12:46 +01:00
parent 97d82bfeaa
commit 29575dda05
1 changed files with 64 additions and 1 deletions

View File

@ -24,14 +24,77 @@ try:
except ImportError:
import mock
import re
from validations_common.tests import base
from validations_common.tests import fakes
import validations_common.library.reportentry as validation
from ansible.plugins.callback import CallbackBase
from validations_common.callback_plugins import http_json
def is_iso_time(time_string):
"""
Checks if string represents valid time in ISO format,
with the default delimiter.
Regex is somewhat convoluted, but general enough to last
at least until the 9999 AD.
Returns:
True if string matches the pattern.
False otherwise.
"""
match = re.match(
r'\d{4}-[01][0-9]-[0-3][0-9]T[0-3][0-9](:[0-5][0-9]){2}\.\d+Z',
time_string)
if match:
return True
else:
return False
class TestHttpJson(base.TestCase):
def setUp(self):
super(TestHttpJson, self).setUp()
def test_callback_instantiation(self):
"""
Verifying that the CallbackModule is instantiated properly.
Test checks presence of CallbackBase in the inheritance chain,
in order to ensure that folowing tests are performed with
the correct assumptions.
"""
callback = http_json.CallbackModule()
self.assertEqual(type(callback).__mro__[2], CallbackBase)
"""
Every ansible callback needs to define variable with name and version.
"""
self.assertIn('CALLBACK_NAME', dir(callback))
self.assertIn('CALLBACK_VERSION', dir(callback))
self.assertIn('CALLBACK_TYPE', dir(callback))
self.assertEqual(callback.CALLBACK_NAME, 'http_json')
self.assertIsInstance(callback.CALLBACK_VERSION, float)
self.assertEqual(callback.CALLBACK_TYPE, 'aggregate')
"""
Additionally, the 'http_json' callback performs several
other operations during instantiation.
"""
self.assertEqual(callback.env, {})
self.assertIsNone(callback.t0)
"""
Callback time sanity check only verifies general format
of the stored time to be iso format `YYYY-MM-DD HH:MM:SS.mmmmmm`
with 'T' as a separator.
For example: '2020-07-03T13:28:21.224103Z'
"""
self.assertTrue(is_iso_time(callback.current_time))