summaryrefslogtreecommitdiff
path: root/buildscripts/resmokelib/testing/hooks/enable_change_stream.py
blob: cafefff93703db3c7f4e5ffcd00b70be4f23df05 (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
"""Enable change stream hook.

A hook to enable change stream in the replica set and the sharded cluster in the multi-tenant
environment.
"""
from time import sleep
from pymongo import MongoClient

from buildscripts.resmokelib import config
from buildscripts.resmokelib.testing.hooks import interface


class EnableChangeStream(interface.Hook):
    """Enable change stream hook class.

    Enables change stream in the multi-tenant environment for the replica set and the sharded
    cluster.
    """

    IS_BACKGROUND = False

    def __init__(self, hook_logger, fixture):
        """Initialize the EnableChangeCollection."""
        description = "Enables the change stream in the multi-tenant environment."
        interface.Hook.__init__(self, hook_logger, fixture, description)
        self._fixture = fixture

    def before_test(self, test, test_report):
        """Enables change stream before test suite starts executing the test cases."""
        if hasattr(self._fixture, "mongos"):
            self.logger.info("Enabling change stream in the sharded cluster.")
            self._set_change_collection_state_in_sharded_cluster()
        else:
            self.logger.info("Enabling change stream in the replica sets.")
            self._set_change_stream_state(self._fixture, True)

        self.logger.info("Successfully enabled the change stream in the fixture.")

    def _set_change_collection_state_in_sharded_cluster(self):
        for shard in self._fixture.shards:
            EnableChangeStream._set_change_stream_state(shard, True)

        # TODO SERVER-68341 Remove the sleep. Sleep for some time such that periodic-noop entries
        # get written to change collections. This will ensure that the client open the change
        # stream cursor with the resume token whose timestamp is later than the change collection
        # first entry. Refer to the ticket for more details.
        sleep(5)

    @staticmethod
    def _set_change_stream_state(connection, enabled):
        # TODO SERVER-67267 Enable command overrides to use security token.
        client = connection.get_primary().mongo_client()
        client.get_database("admin").command({"setChangeStreamState": 1, "enabled": enabled})

        assert client.get_database("admin").command({"getChangeStreamState": 1
                                                     })["enabled"] is enabled