]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
mgr/telemetry: add test coverage for telemetry upgrade
authorYaarit Hatuka <yaarit@redhat.com>
Tue, 7 Dec 2021 23:17:13 +0000 (23:17 +0000)
committerYaarit Hatuka <yaarit@redhat.com>
Thu, 13 Jan 2022 21:54:07 +0000 (21:54 +0000)
Test the behavior of the module after an upgrade, as we shift from our
revision design to Collections.

Signed-off-by: Yaarit Hatuka <yaarit@redhat.com>
src/pybind/mgr/telemetry/__init__.py
src/pybind/mgr/telemetry/tests/__init__.py [new file with mode: 0644]
src/pybind/mgr/telemetry/tests/test_telemetry.py [new file with mode: 0644]
src/pybind/mgr/telemetry/tox.ini [new file with mode: 0644]

index 8f210ac9247ea49624b20582ca8206e56055fbcd..a79cfbcbb4c0439d2d31cf9f8225c96f8695e27c 100644 (file)
@@ -1 +1,9 @@
-from .module import Module
+import os
+
+if 'UNITTEST' in os.environ:
+    import tests
+
+try:
+    from .module import Module
+except ImportError:
+    pass
diff --git a/src/pybind/mgr/telemetry/tests/__init__.py b/src/pybind/mgr/telemetry/tests/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/src/pybind/mgr/telemetry/tests/test_telemetry.py b/src/pybind/mgr/telemetry/tests/test_telemetry.py
new file mode 100644 (file)
index 0000000..188d0ef
--- /dev/null
@@ -0,0 +1,121 @@
+import json
+import pytest
+import unittest
+from unittest import mock
+
+import telemetry
+from typing import cast, Any, DefaultDict, Dict, List, Optional, Tuple, TypeVar, TYPE_CHECKING, Union
+
+OptionValue = Optional[Union[bool, int, float, str]]
+
+Collection = telemetry.module.Collection
+ALL_CHANNELS = telemetry.module.ALL_CHANNELS
+MODULE_COLLECTION = telemetry.module.MODULE_COLLECTION
+
+COLLECTION_BASE = ["basic_base", "device_base", "crash_base", "ident_base"]
+
+class TestTelemetry:
+    @pytest.mark.parametrize("preconfig,postconfig,prestore,poststore,expected",
+            [
+                (
+                    # user is not opted-in
+                    {
+                        'last_opt_revision': 1,
+                        'enabled': False,
+                    },
+                    {
+                        'last_opt_revision': 1,
+                        'enabled': False,
+                    },
+                    {
+                        # None
+                    },
+                    {
+                        'collection': []
+                    },
+                    {
+                        'is_opted_in': False,
+                        'is_enabled_collection':
+                        {
+                            'basic_base': False,
+                            'basic_mds_metadata': False,
+                        },
+                    },
+                ),
+                (
+                    # user is opted-in to an old revision
+                    {
+                        'last_opt_revision': 2,
+                        'enabled': True,
+                    },
+                    {
+                        'last_opt_revision': 2,
+                        'enabled': True,
+                    },
+                    {
+                        # None
+                    },
+                    {
+                        'collection': []
+                    },
+                    {
+                        'is_opted_in': False,
+                        'is_enabled_collection':
+                        {
+                            'basic_base': False,
+                            'basic_mds_metadata': False,
+                        },
+                    },
+                ),
+                (
+                    # user is opted-in to the latest revision
+                    {
+                        'last_opt_revision': 3,
+                        'enabled': True,
+                    },
+                    {
+                        'last_opt_revision': 3,
+                        'enabled': True,
+                    },
+                    {
+                        # None
+                    },
+                    {
+                        'collection': COLLECTION_BASE
+                    },
+                    {
+                        'is_opted_in': True,
+                        'is_enabled_collection':
+                        {
+                            'basic_base': True,
+                            'basic_mds_metadata': False,
+                        },
+                    },
+                ),
+            ])
+    def test_upgrade(self,
+                preconfig: Dict[str, Any], \
+                postconfig: Dict[str, Any], \
+                prestore: Dict[str, Any], \
+                poststore: Dict[str, Any], \
+                expected: Dict[str, Any]) -> None:
+
+        m = telemetry.Module('telemetry', '', '')
+
+        if preconfig is not None:
+            for k, v in preconfig.items():
+                # no need to mock.patch since _ceph_set_module_option() which
+                # is called from set_module_option() is already mocked for
+                # tests, and provides setting default values for all module
+                # options
+                m.set_module_option(k, v)
+
+        m.config_update_module_option()
+        m.load()
+
+        collection = json.loads(m.get_store('collection'))
+
+        assert collection == poststore['collection']
+        assert m.is_opted_in() == expected['is_opted_in']
+        assert m.is_enabled_collection(Collection.basic_base) == expected['is_enabled_collection']['basic_base']
+        assert m.is_enabled_collection(Collection.basic_mds_metadata) == expected['is_enabled_collection']['basic_mds_metadata']
diff --git a/src/pybind/mgr/telemetry/tox.ini b/src/pybind/mgr/telemetry/tox.ini
new file mode 100644 (file)
index 0000000..a887590
--- /dev/null
@@ -0,0 +1,12 @@
+[tox]
+envlist =
+    py3
+    mypy
+skipsdist = true
+
+[testenv]
+deps =
+    mock
+    pytest
+commands =
+    pytest {posargs}