]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
pybind/mgr/smb: add unit tests file tests/test_rados_store.py
authorJohn Mulligan <jmulligan@redhat.com>
Wed, 13 Mar 2024 15:22:22 +0000 (11:22 -0400)
committerJohn Mulligan <jmulligan@redhat.com>
Thu, 25 Apr 2024 23:10:39 +0000 (19:10 -0400)
Signed-off-by: John Mulligan <jmulligan@redhat.com>
src/pybind/mgr/smb/tests/test_rados_store.py [new file with mode: 0644]

diff --git a/src/pybind/mgr/smb/tests/test_rados_store.py b/src/pybind/mgr/smb/tests/test_rados_store.py
new file mode 100644 (file)
index 0000000..ac807d2
--- /dev/null
@@ -0,0 +1,116 @@
+from unittest import mock
+
+import smb.rados_store
+
+
+def rados_mock():
+    import rados
+
+    m = mock.MagicMock()
+    m._fake_store = {}
+    ioctx = m.open_ioctx.return_value
+
+    def _read(key, *args):
+        return m._fake_store[ioctx._current_ns, key].encode('utf8')
+
+    def _set_ns(ns):
+        ioctx._current_ns = ns
+
+    def _write_full(key, data):
+        m._fake_store[ioctx._current_ns, key] = data
+
+    def _list(*args):
+        return [mock.MagicMock(nspace=a, key=b) for a, b in m._fake_store]
+
+    def _stat(key):
+        if (ioctx._current_ns, key) not in m._fake_store:
+            raise rados.ObjectNotFound
+
+    def _remove_object(key):
+        del m._fake_store[ioctx._current_ns, key]
+
+    ioctx.__enter__.return_value = ioctx
+    ioctx.list_objects.side_effect = _list
+    ioctx.set_namespace.side_effect = _set_ns
+    ioctx.read.side_effect = _read
+    ioctx.write_full.side_effect = _write_full
+    ioctx.stat.side_effect = _stat
+    ioctx.remove_object.side_effect = _remove_object
+
+    m._fake_store[
+        'foo', 'one'
+    ] = """
+        {"mocked": true, "silly": "very", "name": "one"}
+    """
+    m._fake_store[
+        'bar', 'two'
+    ] = """
+        {"mocked": true, "silly": "very", "name": "two"}
+    """
+    m._fake_store[
+        'bar', 'three'
+    ] = """
+        {"mocked": true, "silly": "very", "name": "three"}
+    """
+
+    return m
+
+
+def test_mocked_rados_store_iteration():
+    r = rados_mock()
+    store = smb.rados_store.RADOSConfigStore(r)
+    ekeys = list(store)
+    assert ekeys == [('foo', 'one'), ('bar', 'two'), ('bar', 'three')]
+    assert sorted(store.namespaces()) == ['bar', 'foo']
+    assert list(store.contents('foo')) == ['one']
+    assert list(store.contents('bar')) == ['two', 'three']
+
+
+def test_mocked_rados_store_get_entry():
+    r = rados_mock()
+    store = smb.rados_store.RADOSConfigStore(r)
+    entry = store['foo', 'one']
+    assert entry.uri == 'rados://.smb/foo/one'
+    assert entry.full_key == ('foo', 'one')
+    data = entry.get()
+    assert isinstance(data, dict)
+    assert data['mocked']
+    assert data['silly'] == 'very'
+    assert data['name'] == 'one'
+
+
+def test_mocked_rados_store_set_get_entry():
+    r = rados_mock()
+    store = smb.rados_store.RADOSConfigStore(r)
+    entry = store['foo', 'four']
+    assert entry.uri == 'rados://.smb/foo/four'
+    assert entry.full_key == ('foo', 'four')
+    entry.set(
+        dict(
+            mocked=False,
+            silly='walks',
+            name='four',
+        )
+    )
+    assert list(store.contents('foo')) == ['one', 'four']
+
+
+def test_mocked_rados_store_entry_exists():
+    r = rados_mock()
+    store = smb.rados_store.RADOSConfigStore(r)
+    entry = store['bar', 'two']
+    assert entry.uri == 'rados://.smb/bar/two'
+    assert entry.full_key == ('bar', 'two')
+    assert entry.exists()
+
+    entry = store['bar', 'seven']
+    assert entry.uri == 'rados://.smb/bar/seven'
+    assert entry.full_key == ('bar', 'seven')
+    assert not entry.exists()
+
+
+def test_mocked_rados_store_entry_remove():
+    r = rados_mock()
+    store = smb.rados_store.RADOSConfigStore(r)
+    store.remove(('bar', 'three'))
+    assert list(store) == [('foo', 'one'), ('bar', 'two')]