-
Notifications
You must be signed in to change notification settings - Fork 20
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
aruniiird
wants to merge
1
commit into
Tendrl:master
Choose a base branch
from
aruniiird:novice_mocker
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -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 | ||
): | ||
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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you planning to have another PR to take care? |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?