summaryrefslogtreecommitdiff
path: root/buildscripts/resmokelib/testing/hooks/jsfile.py
blob: 9e39057314e1cbef120bb1903441ec1c929c2dfc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
"""Interface for customizing the behavior of a test fixture by executing a JavaScript file."""

from buildscripts.resmokelib import errors
from buildscripts.resmokelib.testing.hooks import interface
from buildscripts.resmokelib.testing.testcases import jstest
from buildscripts.resmokelib.utils import registry


class JSHook(interface.Hook):
    """A hook with a static JavaScript file to execute."""

    REGISTERED_NAME = registry.LEAVE_UNREGISTERED

    def __init__(  # pylint: disable=too-many-arguments
            self, hook_logger, fixture, js_filename, description, shell_options=None):
        """Initialize JSHook."""
        interface.Hook.__init__(self, hook_logger, fixture, description)
        self._js_filename = js_filename
        self._shell_options = shell_options

    def _should_run_after_test(self):  # pylint: disable=no-self-use
        """Provide base callback.

        Callback that can be overrided by subclasses to indicate if the JavaScript file should be
        executed after the current test.
        """
        return True

    def after_test(self, test, test_report):
        """After test execution."""
        if not self._should_run_after_test():
            return

        hook_test_case = DynamicJSTestCase.create_after_test(test.logger, test, self,
                                                             self._js_filename, self._shell_options)
        hook_test_case.configure(self.fixture)
        hook_test_case.run_dynamic_test(test_report)


class DataConsistencyHook(JSHook):
    """
    A hook for running a static JavaScript file that checks data consistency of the server.

    If the mongo shell process running the JavaScript file exits with a non-zero return code, then
    an errors.ServerFailure exception is raised to cause resmoke.py's test execution to stop.
    """

    REGISTERED_NAME = registry.LEAVE_UNREGISTERED

    def after_test(self, test, test_report):
        """After test execution."""
        try:
            JSHook.after_test(self, test, test_report)
        except errors.TestFailure as err:
            raise errors.ServerFailure(err.args[0])


class DynamicJSTestCase(interface.DynamicTestCase):
    """A dynamic TestCase that runs a JavaScript file."""

    def __init__(  # pylint: disable=too-many-arguments
            self, logger, test_name, description, base_test_name, hook, js_filename,
            shell_options=None):
        """Initialize DynamicJSTestCase."""
        interface.DynamicTestCase.__init__(self, logger, test_name, description, base_test_name,
                                           hook)
        self._js_test_builder = jstest.JSTestCaseBuilder(logger, js_filename, self.id(),
                                                         shell_options=shell_options)
        self._js_test_case = None

    def override_logger(self, new_logger):
        """Override logger."""
        interface.DynamicTestCase.override_logger(self, new_logger)
        self._js_test_case.override_logger(new_logger)

    def reset_logger(self):
        """Reset the logger."""
        interface.DynamicTestCase.reset_logger(self)
        self._js_test_case.reset_logger()

    def configure(self, fixture, *args, **kwargs):  # pylint: disable=unused-argument
        """Configure the fixture."""
        super().configure(fixture, *args, **kwargs)
        self._js_test_builder.configure(fixture, *args, **kwargs)
        self._js_test_case = self._js_test_builder.create_test_case_for_thread(self.logger)

    def run_test(self):
        """Execute the test."""
        self._js_test_case.run_test()