@@ -52,6 +52,13 @@ class AgentsTests(CliCommandTest):
5252 def setUp (self ):
5353 super (AgentsTests , self ).setUp ()
5454 self .use_manager ()
55+ self ._client_mocks = []
56+
57+ def tearDown (self ):
58+ super ().tearDown ()
59+ for patcher in self ._client_mocks :
60+ patcher .stop ()
61+ self ._client_mocks = []
5562
5663 @staticmethod
5764 def _agent_filters (node_ids = None , node_instance_ids = None ,
@@ -75,8 +82,8 @@ def _agent_filters(node_ids=None, node_instance_ids=None,
7582 ]
7683
7784 def mock_client (self , topology ):
78- def _topology_filter (predicate , ** kwargs ):
79- tenant_name = self . client . _client .headers .get (
85+ def _topology_filter (client_inst , predicate , ** kwargs ):
86+ tenant_name = client_inst . api .headers .get (
8087 CLOUDIFY_TENANT_HEADER )
8188 if not tenant_name :
8289 tenant_name = DEFAULT_TENANT_NAME
@@ -89,7 +96,7 @@ def _topology_filter(predicate, **kwargs):
8996 results .append (node_instance )
9097 return results
9198
92- def list_node_instances (** kwargs ):
99+ def list_node_instances (client_inst , ** kwargs ):
93100 def _matcher (node_instance ):
94101 ni_id = node_instance ['id' ]
95102 ni_node_id = node_instance ['node_id' ]
@@ -98,7 +105,7 @@ def _matcher(node_instance):
98105 ni_node_id in kwargs .get ('node_id' , [ni_node_id ]) and \
99106 ni_dep_id in kwargs .get ('deployment_id' , [ni_dep_id ])
100107
101- instances = _topology_filter (_matcher , ** kwargs )
108+ instances = _topology_filter (client_inst , _matcher , ** kwargs )
102109 total = len (instances )
103110 offset , size = kwargs .get ('_offset' , 0 ), kwargs .get ('_size' , 1000 )
104111 instances = instances [offset :offset + size ]
@@ -111,12 +118,12 @@ def _matcher(node_instance):
111118 }
112119 })
113120
114- def list_deployments (** kwargs ):
121+ def list_deployments (client_inst , ** kwargs ):
115122 tenant_name = self .client ._client .headers .get (
116123 CLOUDIFY_TENANT_HEADER )
117124 if not tenant_name :
118125 tenant_name = DEFAULT_TENANT_NAME
119- all_node_instances = _topology_filter (lambda x : True , ** kwargs )
126+ all_node_instances = _topology_filter (client_inst , lambda x : True , ** kwargs )
120127 deployments = {(x ['tenant_name' ], x ['deployment_id' ])
121128 for x in all_node_instances }
122129 deployments = [Deployment ({'id' : b , 'tenant_name' : a }) for a , b in
@@ -128,9 +135,9 @@ def list_deployments(**kwargs):
128135 results .append (dep )
129136 return ListResponse (results , {})
130137
131- def list_nodes (** kwargs ):
138+ def list_nodes (client_inst , ** kwargs ):
132139 node_ids = kwargs .get ('id' )
133- all_node_instances = _topology_filter (lambda x : True , ** kwargs )
140+ all_node_instances = _topology_filter (client_inst , lambda x : True , ** kwargs )
134141 nodes = {(x ['tenant_name' ], x ['deployment_id' ], x ['node_id' ])
135142 for x in all_node_instances }
136143 nodes = [Node ({'id' : c , 'deployment_id' : b , 'tenant_name' : a }) for
@@ -139,9 +146,19 @@ def list_nodes(**kwargs):
139146 nodes = [x for x in nodes if x ['id' ] in node_ids ]
140147 return ListResponse (nodes , {})
141148
142- self .client .node_instances .list = list_node_instances
143- self .client .deployments .list = list_deployments
144- self .client .nodes .list = list_nodes
149+ self ._client_mocks = [
150+ patch (
151+ 'cloudify_rest_client.node_instances.NodeInstancesClient.list' ,
152+ list_node_instances ,
153+ ),
154+ patch (
155+ 'cloudify_rest_client.deployments.DeploymentsClient.list' ,
156+ list_deployments ,
157+ ),
158+ patch ('cloudify_rest_client.nodes.NodesClient.list' , list_nodes ),
159+ ]
160+ for patcher in self ._client_mocks :
161+ patcher .start ()
145162
146163 def assert_execution_started (self , client_mock , deployment_id ,
147164 filters ):
@@ -392,9 +409,8 @@ def _wait_side_effect(*args, **kwargs):
392409 with patch ('cloudify_cli.commands.agents.wait_for_execution' ,
393410 return_value = PropertyMock (error = False ),
394411 side_effect = _wait_side_effect ), \
395- patch .object (ExecutionsClient , 'start' ,
396- _mock_execution_start ), \
397- patch ('cloudify_cli.commands.agents.time.sleep' ):
412+ patch .object (ExecutionsClient , 'start' , _mock_execution_start ), \
413+ patch ('cloudify_cli.commands.agents.time.sleep' ):
398414
399415 get_deployments_and_run_workers (
400416 self .client , self ._agent_filters (), True , self .logger ,
0 commit comments