From 42cebaefeed6ca96873a847870861986e3bb9233 Mon Sep 17 00:00:00 2001 From: Arun Date: Mon, 3 Sep 2018 16:20:44 +0530 Subject: [PATCH] Added some mock test to cover 'sds_sync/__init__.py' file. Please note, this is not complete and has some failures. Some minor changes made to 'sds_sync/__init__.py' itself. --- .../gluster_integration/sds_sync/__init__.py | 2 +- .../tests/test_sds_sync.py | 66 ++++++++++++++++++- 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/tendrl/gluster_integration/sds_sync/__init__.py b/tendrl/gluster_integration/sds_sync/__init__.py index a14af45..fd5ee91 100644 --- a/tendrl/gluster_integration/sds_sync/__init__.py +++ b/tendrl/gluster_integration/sds_sync/__init__.py @@ -44,6 +44,7 @@ def __init__(self): self._complete = threading.Event() def run(self): + global VOLUME_TTL logger.log( "info", NS.publisher_id, @@ -245,7 +246,6 @@ def run(self): SYNC_TTL += 1 total_brick_count += b_count - 1 except KeyError: - global VOLUME_TTL # from second sync volume ttl is # SYNC_TTL + (no.volumes) * 20 + # (no.of.bricks) * 10 + 160 diff --git a/tendrl/gluster_integration/tests/test_sds_sync.py b/tendrl/gluster_integration/tests/test_sds_sync.py index 088a304..78c3df6 100644 --- a/tendrl/gluster_integration/tests/test_sds_sync.py +++ b/tendrl/gluster_integration/tests/test_sds_sync.py @@ -1,4 +1,7 @@ import etcd +import time +import maps +import subprocess import importlib from mock import MagicMock from mock import patch @@ -7,6 +10,9 @@ from tendrl.commons.utils import etcd_utils from tendrl.commons.utils import event_utils +from tendrl.commons.objects import node_context as node + +from tendrl.gluster_integration.tests.test_init import init @patch.object(BaseObject, "save") @patch.object(BaseObject, "load_all") @@ -14,7 +20,7 @@ @patch.object(BaseObject, "hash_compare_with_central_store") @patch.object(etcd_utils, "refresh") def test_brick_status_alert( - compare, refresh, emit_event, load_all, save + refresh, compare, emit_event, load_all, save ): compare.return_value = True refresh.return_value = True @@ -66,3 +72,61 @@ def test_brick_status_alert( sds_sync.brick_status_alert( "dhcp12-12.lab.abc.com" ) + + +@patch('tendrl.gluster_integration.' + + 'sds_sync.GlusterIntegrationSdsSyncStateThread.run') +def test_sds_sync_run_called(mock_run): + mock_run.return_value = None + sds_sync = importlib.import_module( + 'tendrl.gluster_integration.sds_sync' + ) + sds_sync.GlusterIntegrationSdsSyncStateThread().start() + mock_run.assert_called_once() + + +@patch.object(BaseObject, "load") +@patch.object(subprocess, 'call') +@patch.object(time, 'sleep') +def test_sds_sync_run(time_sleep, subproc_call, load): + time_sleep.return_value = None + subproc_call.return_value = 0 + init() + obj = NS.tendrl.objects.GlusterBrick( + integration_id="77deef29-b8e5-4dc5-8247-21e2a409a66a", + fqdn="dhcp12-12.lab.abc.com", + hostname="dhcp12-12.lab.abc.com", + status="started", + vol_name="v1", + brick_path="/gluster/b1", + node_id="3c4b48cc-1a61-4c64-90d6-eba840c00081" + ) + # reasoning of passing 'obj' + # load_all was providing list of objects, like [obj] (line# 37 in this file) + # so load should return a single value + load.retun_value = obj + + def dummy_callable(): + pass + setattr(NS, "tendrl_context", maps.NamedDict()) + NS.tendrl_context["integration_id"] = "int-id" + NS.tendrl_context["cluster_name"] = "cluster1" + NS.tendrl_context["sds_name"] = "gluster" + # in below line, passing a callable function to 'load' key, + # as the tox was complaining about the value being not callable + # assigning a MagicMock() object, making it indefinitely long + NS.tendrl_context["load"] = dummy_callable + NS.publisher_id = "gluster-integration" + sds_sync = importlib.import_module( + 'tendrl.gluster_integration.sds_sync' + ) + with patch.object(etcd_utils, "read") as utils_read: + # not sure why the below mock not working + utils_read.return_value = maps.NamedDict( + value='{"tags":[]}' + ) + sds_sync.GlusterIntegrationSdsSyncStateThread().run() + # below is a dummy assert, which should be replaced by + # other assertions to get the code coverage + # and check / mock some exceptions + subproc_call.assert_called_once()