Skip to content

Commit

Permalink
Merge pull request Tendrl#985 from anmolsachan/Fix_unit_tests
Browse files Browse the repository at this point in the history
Fixed unit tests
  • Loading branch information
r0h4n authored Jun 13, 2018
2 parents 84857e0 + 19c9601 commit 517875b
Show file tree
Hide file tree
Showing 20 changed files with 626 additions and 133 deletions.
6 changes: 4 additions & 2 deletions tendrl/commons/tests/fixtures/nodecontext.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
class NodeContext(object):

def __init__(self, *args):
self.tags = ""
def __init__(self, node_id=None, *args):
self.tags = "test_tag"
self.fqdn = "test_fqdn"
self.node_id = node_id

def load(self):
return self
4 changes: 2 additions & 2 deletions tendrl/commons/tests/fixtures/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ def setup_gluster_node(self, *args, **kwargs):
return True

def create_gluster_cluster(self, *args, **kwargs):
if args[0][0] == "test":
if args[0][0] == "test_ip":
return False
return True

def expand_gluster_cluster(self, *args, **kwargs):
if args[0][0] == "test":
if args[0][0] == "test_ip":
return False
return True

Expand Down
6 changes: 5 additions & 1 deletion tendrl/commons/tests/fixtures/tendrlcontext.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
class TendrlContext(object):

def __init__(self, *args):
pass
self.integration_id = "test_integration_id"
self.cluster_name = "test_name"

def load(self):
return self
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@
import mock
import pytest

from mock import patch

import tendrl.commons.objects.cluster as cluster

from tendrl.commons.flows.exceptions import FlowExecutionFailedError
from tendrl.commons.flows.expand_cluster import gluster_help
from tendrl.commons.objects.cluster import Cluster
from tendrl.commons.utils import etcd_utils


'''Unit Test Cases'''

Expand All @@ -23,9 +29,15 @@ def test_get_node_ips():
mock.Mock(return_value=None))
@mock.patch('tendrl.commons.message.Message.__init__',
mock.Mock(return_value=None))
def test_expand_gluster():
@patch.object(etcd_utils, 'read')
@patch.object(cluster.Cluster, 'load')
def test_expand_gluster(patch_cluster_load, patch_etcd_utils_read):
setattr(__builtin__, "NS", maps.NamedDict())
setattr(NS, "_int", maps.NamedDict())
NS.publisher_id = "node_context"
NS.tendrl = maps.NamedDict()
NS.tendrl.objects = maps.NamedDict()
NS.tendrl.objects.Cluster = Cluster
NS.config = maps.NamedDict()
NS.config.data = maps.NamedDict()
NS.config.data['glusterfs_repo'] = "test_gluster"
Expand All @@ -43,4 +55,25 @@ def test_expand_gluster():
gluster_help.expand_gluster(param)
param["Cluster.node_configuration"] = {
"test_node": maps.NamedDict(role="mon", provisioning_ip="test")}
patch_etcd_utils_read.return_value = maps.NamedDict(
value='{"current_job": '
'{"status": "finished",'
'"job_id": "test_job_id",'
'"job_name": "ImportCluster"'
'},'
'"status": "",'
'"short_name": "t2",'
'"volume_profiling_flag": "enable",'
'"conf_overrides": "",'
'"integration_id": "test_integration_id",'
'"errors": "[]",'
'"node_configuration": "",'
'"locked_by": {},'
'"last_sync": "2018-06-07 13:25:25.943914+00:00",'
'"volume_profiling_state": "enabled",'
'"public_network": "",'
'"is_managed": "yes",'
'"node_identifier": "[]",'
'"cluster_network": "127.0.0.1/24"}'
)
gluster_help.expand_gluster(param)
34 changes: 32 additions & 2 deletions tendrl/commons/tests/flows/expand_cluster/test_ec_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import tendrl.commons.objects.node_context as node
from tendrl.commons import TendrlNS
from tendrl.commons.tests.fixtures.client import Client
from tendrl.commons.utils import etcd_utils


def read_failed(*args):
Expand All @@ -36,7 +37,13 @@ def get_parsed_defs():
@patch.object(etcd, "Client")
@patch.object(etcd.Client, "read")
@patch.object(node.NodeContext, '_get_node_id')
def init(patch_get_node_id, patch_read, patch_client):
@patch.object(etcd_utils, 'read')
@patch.object(node.NodeContext, 'load')
def init(patch_node_load,
patch_etcd_utils_read,
patch_get_node_id,
patch_read,
patch_client):
patch_get_node_id.return_value = 1
patch_read.return_value = etcd.Client()
patch_client.return_value = etcd.Client()
Expand All @@ -54,6 +61,17 @@ def init(patch_get_node_id, patch_read, patch_client):
NS.publisher_id = "node_context"
NS.config.data['etcd_port'] = 8085
NS.config.data['etcd_connection'] = "Test Connection"
patch_etcd_utils_read.return_value = maps.NamedDict(
value='{"status": "UP",'
'"pkey": "tendrl-node-test",'
'"node_id": "test_node_id",'
'"ipv4_addr": "test_ip",'
'"tags": "[\\"my_tag\\"]",'
'"sync_status": "done",'
'"locked_by": "fd",'
'"fqdn": "tendrl-node-test",'
'"last_sync": "date"}')
patch_node_load.return_value = node.NodeContext
tendrlNS = TendrlNS()
return tendrlNS

Expand All @@ -72,7 +90,8 @@ def init(patch_get_node_id, patch_read, patch_client):
mock.Mock(return_value=None))
@mock.patch('tendrl.commons.flows.utils.acquire_node_lock',
mock.Mock(return_value=None))
def test_expand_cluster():
@patch.object(etcd_utils, 'read')
def test_expand_cluster(patch_etcd_utils_read):
expand_cluster = ExpandCluster()
param = maps.NamedDict()
param['TendrlContext.integration_id'] = None
Expand All @@ -92,11 +111,22 @@ def test_expand_cluster():
param["flow_id"] = "test_flow_id"
param['Node[]'] = ['test_node']
param["job_id"] = "test_id"
param['TendrlContext.integration_id'] = None
NS._int.client = importlib.import_module(
"tendrl.commons.tests.fixtures.client").Client()
NS.gluster_provisioner = importlib.import_module(
"tendrl.commons.tests.fixtures.plugin").Plugin()
NS.tendrl_context = maps.NamedDict(integration_id="")
patch_etcd_utils_read.return_value = maps.NamedDict(
value='{"status": "UP",'
'"pkey": "tendrl-node-test",'
'"node_id": "test_node_id",'
'"ipv4_addr": "test_ip",'
'"tags": "[\\"my_tag\\"]",'
'"sync_status": "done",'
'"locked_by": "fd",'
'"fqdn": "tendrl-node-test",'
'"last_sync": "date"}')
with patch.object(Client, "read", read_failed):
with pytest.raises(FlowExecutionFailedError):
expand_cluster.run()
20 changes: 19 additions & 1 deletion tendrl/commons/tests/flows/import_cluster/test_gluster_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
from tendrl.commons import TendrlNS
from tendrl.commons.utils import ansible_module_runner
from tendrl.commons.utils import cmd_utils
from tendrl.commons.utils import etcd_utils
from tendrl.commons.utils.service_status import ServiceStatus


'''Dummy Functions'''


Expand All @@ -35,7 +37,11 @@ def open(*args, **kwargs):
@patch.object(etcd, "Client")
@patch.object(etcd.Client, "read")
@patch.object(node.NodeContext, '_get_node_id')
def init(patch_get_node_id, patch_read, patch_client):
@patch.object(etcd_utils, 'read')
def init(patch_etcd_utils_read,
patch_get_node_id,
patch_read,
patch_client):
patch_get_node_id.return_value = 1
patch_read.return_value = etcd.Client()
patch_client.return_value = etcd.Client()
Expand All @@ -52,12 +58,23 @@ def init(patch_get_node_id, patch_read, patch_client):
NS.config["data"] = maps.NamedDict(logging_socket_path="test/path")
NS.node_context = maps.NamedDict()
NS.node_context.node_id = 1
NS.node_context['fqdn'] = "test_fqdn"
NS.config.data['package_source_type'] = 'test pip'
NS.config.data['tags'] = "test"
NS.config.data['etcd_port'] = 8085
NS.config.data['etcd_connection'] = "Test Connection"
NS.config.data['sync_interval'] = 30
NS.compiled_definitions = mock.MagicMock()
patch_etcd_utils_read.return_value = maps.NamedDict(
value='{"status": "UP",'
'"pkey": "tendrl-node-test",'
'"node_id": "test_node_id",'
'"ipv4_addr": "test_ip",'
'"tags": "[\\"my_tag\\"]",'
'"sync_status": "done",'
'"locked_by": "fd",'
'"fqdn": "tendrl-node-test",'
'"last_sync": "date"}')
tendrlNS = TendrlNS()
return tendrlNS

Expand All @@ -70,6 +87,7 @@ def init(patch_get_node_id, patch_read, patch_client):
mock.Mock(return_value=None))
def test_import_gluster():
tendrlNS = init()
# import pdb; pdb.set_trace();
NS.compiled_definitions = tendrlNS.current_ns.definitions
parameters = maps.NamedDict(job_id=1, flow_id=1)
ret_val, err = gluster_help.import_gluster(parameters)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
from mock import patch
import pytest

import tendrl.commons.objects.node_context as node

from tendrl.commons.flows.exceptions import FlowExecutionFailedError
from tendrl.commons.flows.import_cluster import ImportCluster
from tendrl.commons import objects
from tendrl.commons.objects import AtomExecutionFailedError
from tendrl.commons.objects.node.atoms.cmd import Cmd
import tendrl.commons.objects.node_context as node
from tendrl.commons import TendrlNS
from tendrl.commons.utils import etcd_utils


def get_obj_definition(*args, **kwargs):
Expand Down Expand Up @@ -80,10 +82,33 @@ def return_job(param):
def read(key):
if key == 'indexes/tags/tendrl/integration/None':
raise etcd.EtcdKeyNotFound
elif key == 'indexes/tags/tendrl/integration/' \
'94ac63ba-de73-4e7f-8dfa-9010d9554084':
node_ids = maps.NamedDict()
node_ids['value'] = '["bc4cad92-b7e3-4c63-b820-a439db3a0516",' \
'"a71af0e5-5241-4856-9e9c-22627a466b8d"]'
return node_ids
else:
return maps.NamedDict(
value=u'["bc15f88b-7118-485e-ab5c-cf4b9e1c2ee5"]'
)
return etcd.EtcdResult(node={'newKey': False,
'raft_index': 449389,
'_children': [{u'createdIndex': 1657,
u'modifiedIndex': 1657,
u'dir': True,
u'key': u'/clusters/'
u'b7d4b5ae-d33d-'
u'49cf-ae6d-5d6bb'
u'494ece7'}
],
'createdIndex': 1657,
'modifiedIndex': 1657,
'value': None,
'etcd_index': 101021,
'expiration': None,
'key': u'/clusters',
'ttl': None,
'action': u'get',
'dir': True
})


def save(*args):
Expand All @@ -93,7 +118,11 @@ def save(*args):
@patch.object(etcd, "Client")
@patch.object(etcd.Client, "read")
@patch.object(node.NodeContext, '_get_node_id')
def init(patch_get_node_id, patch_read, patch_client):
@patch.object(etcd_utils, 'read')
def init(patch_etcd_utils_read,
patch_get_node_id,
patch_read,
patch_client):
patch_get_node_id.return_value = 1
patch_read.return_value = etcd.Client()
patch_client.return_value = etcd.Client()
Expand All @@ -111,6 +140,16 @@ def init(patch_get_node_id, patch_read, patch_client):
NS.publisher_id = "node_context"
NS.config.data['etcd_port'] = 8085
NS.config.data['etcd_connection'] = "Test Connection"
patch_etcd_utils_read.return_value = maps.NamedDict(
value='{"status": "UP",'
'"pkey": "tendrl-node-test",'
'"node_id": "test_node_id",'
'"ipv4_addr": "test_ip",'
'"tags": "[\\"my_tag\\"]",'
'"sync_status": "done",'
'"locked_by": "fd",'
'"fqdn": "tendrl-node-test",'
'"last_sync": "date"}')
tendrlNS = TendrlNS()
return tendrlNS

Expand All @@ -128,25 +167,27 @@ def test_run():
import_cluster = ImportCluster(parameters=param)
with patch.object(objects.BaseObject, 'load', return_fail):
with patch.object(NS._int.client, 'read', read):
with pytest.raises(FlowExecutionFailedError):
import_cluster.run()
with patch.object(etcd_utils, 'read', read):
with pytest.raises(FlowExecutionFailedError):
import_cluster.run()
param['TendrlContext.integration_id'] = \
'94ac63ba-de73-4e7f-8dfa-9010d9554084'
import_cluster._defs['pre_run'] = ['tendrl.objects.Node.atoms.Cmd']
with patch.object(NS._int.client, 'read', read):
with patch.object(objects.BaseObject, 'save', save):
with patch.object(Cmd, 'run', return_value=True):
with patch.object(
NS.tendrl.objects.Cluster,
'load',
return_pass
):
with patch.object(etcd_utils, 'read', read):
with patch.object(
NS.tendrl.objects.Job,
NS.tendrl.objects.Cluster,
'load',
return_job
return_pass
):
import_cluster.run()
with patch.object(
NS.tendrl.objects.Job,
'load',
return_job
):
import_cluster.run()
with patch.object(NS._int.client, 'read', read):
with patch.object(objects.BaseObject, 'save', save):
with patch.object(Cmd, 'run', return_value=False):
Expand Down
20 changes: 19 additions & 1 deletion tendrl/commons/tests/flows/test_flows_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from tendrl.commons.objects.node.atoms.cmd import Cmd
import tendrl.commons.objects.node_context as node
from tendrl.commons import TendrlNS
from tendrl.commons.utils import etcd_utils


''' Global Variables'''
Expand Down Expand Up @@ -117,7 +118,13 @@ def get_flow_definition(*args, **kwargs):
@patch.object(etcd, "Client")
@patch.object(Client, "read")
@patch.object(node.NodeContext, '_get_node_id')
def init(patch_get_node_id, patch_read, patch_client):
@patch.object(etcd_utils, 'read')
@patch.object(node.NodeContext, 'load')
def init(patch_node_load,
patch_etcd_utils_read,
patch_get_node_id,
patch_read,
patch_client):
patch_get_node_id.return_value = 1
patch_read.return_value = etcd.Client()
patch_client.return_value = etcd.Client()
Expand All @@ -133,6 +140,17 @@ def init(patch_get_node_id, patch_read, patch_client):
NS.config["data"] = maps.NamedDict()
NS.config.data['tags'] = "test"
NS.publisher_id = "node_context"
patch_etcd_utils_read.return_value = maps.NamedDict(
value='{"status": "UP",'
'"pkey": "tendrl-node-test",'
'"node_id": "test_node_id",'
'"ipv4_addr": "test_ip",'
'"tags": "[\\"my_tag\\"]",'
'"sync_status": "done",'
'"locked_by": "fd",'
'"fqdn": "tendrl-node-test",'
'"last_sync": "date"}')
patch_node_load.return_value = node.NodeContext
tendrlNS = TendrlNS()
return tendrlNS

Expand Down
Loading

0 comments on commit 517875b

Please sign in to comment.