Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added some mock test to cover 'sds_sync/__init__.py' file. #698

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tendrl/gluster_integration/sds_sync/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def __init__(self):
self._complete = threading.Event()

def run(self):
global VOLUME_TTL
logger.log(
"info",
NS.publisher_id,
Expand Down Expand Up @@ -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
Expand Down
66 changes: 65 additions & 1 deletion tendrl/gluster_integration/tests/test_sds_sync.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import etcd
import time
import maps
import subprocess
import importlib
from mock import MagicMock
from mock import patch
Expand All @@ -7,14 +10,17 @@
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")
@patch.object(event_utils, "emit_event")
@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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this have any effect by changing the order only?

):
compare.return_value = True
refresh.return_value = True
Expand Down Expand Up @@ -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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GowthamShanmugam can you please check this and suggest?

# 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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you planning to have another PR to take care?