summaryrefslogtreecommitdiff
path: root/buildscripts/resmokelib/testing/hooks/jsfile.py
blob: 76b2de31313badbbc3aab7c034eed37db40d837d (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
"""
Interface for customizing the behavior of a test fixture by executing a
JavaScript file.
"""

from __future__ import absolute_import

from . import interface
from ..testcases import jstest
from ... import errors
from ...utils import registry


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

    REGISTERED_NAME = registry.LEAVE_UNREGISTERED

    def __init__(self, hook_logger, fixture, js_filename, description, shell_options=None):
        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
        """
        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):
        if not self._should_run_after_test():
            return

        hook_test_case = DynamicJSTestCase.create_after_test(
            self.logger.test_case_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):
        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__(self, logger, test_name, description, base_test_name, hook, js_filename,
                 shell_options=None):
        interface.DynamicTestCase.__init__(self, logger, test_name, description, base_test_name,
                                           hook)
        self._js_test = jstest.JSTestCase(logger, js_filename, shell_options=shell_options)

    def override_logger(self, new_logger):
        interface.DynamicTestCase.override_logger(self, new_logger)
        self._js_test.override_logger(new_logger)

    def reset_logger(self):
        interface.DynamicTestCase.reset_logger(self)
        self._js_test.reset_logger()

    def configure(self, fixture, *args, **kwargs):  # pylint: disable=unused-argument
        interface.DynamicTestCase.configure(self, fixture, *args, **kwargs)
        self._js_test.configure(fixture, *args, **kwargs)

    def run_test(self):
        self._js_test.run_test()