HCE Project Python language Distributed Tasks Manager Application, Distributed Crawler Application and client API bindings.  2.0.0-chaika
Hierarchical Cluster Engine Python language binding
test_drce_CommandExecutor.py
Go to the documentation of this file.
1 '''
2 Created on Feb 13, 2014
3 
4 @author: igor
5 '''
6 import unittest
7 from mock import MagicMock
8 from mock import call
9 from mock import ANY
10 
11 
12 from drce.CommandExecutor import CommandExecutor
13 from drce.CommandExecutor import CommandExecutorErr
14 from drce.CommandConvertor import CommandConvertor
15 from drce.CommandConvertor import CommandConvertorError
16 from drce.Commands import TaskCheckRequest
17 from transport.Connection import Connection
18 from transport.Connection import ConnectionTimeout
19 from transport.Response import Response
20 from transport.Response import ResponseFormatErr
21 
22 import drce.Consts as consts
23 
24 
25 class TestCommandExecutor(unittest.TestCase):
26 
27 
28  def setUp(self):
29  self.connect_mock = MagicMock(spec=Connection)
31 
32 
34  response = Response([
35  "1",
36  """
37  {
38  "error_code":0,
39  "error_message" :"msg",
40  "time":10,
41  "state":1,
42  "pid":101,
43  "data":[
44  {"files":[{"name":"f1", "data":"data1", "action":12}],
45  "stdout":"out",
46  "stderror":"error",
47  "exit_status":5,
48  "node":"n1",
49  "time":100
50  }
51  ]
52  }
53  """ ])
54 
55  connect_mock_cfg = {"send.return_value": None,
56  "recv.return_value": response}
57  self.connect_mock.configure_mock(**connect_mock_cfg)
58 
59  #check
60  connect_expect_calls = [call.send(ANY), call.recv(ANY)]
61 
62  task_request = TaskCheckRequest("task_id", consts.EXTEND_STATUS_INFO)
63  task_response = self.cmd_executor.execute(task_request)
64 
65  self.connect_mock.assert_has_calls(connect_expect_calls)
66 
67  test_err_str = "json parsing is failed"
68  self.assertEqual(task_response.error_code, 0, test_err_str)
69  self.assertEqual(task_response.error_msg, "msg", test_err_str )
70  self.assertEqual(task_response.state, 1, test_err_str)
71  self.assertEqual(len(task_response.data), 1, test_err_str)
72  self.assertEqual(task_response.pid, 101, test_err_str)
73 
74 
75 
77  self.connect_mock.send.return_value = None
78  self.connect_mock.recv.side_effect = ConnectionTimeout("Boom")
79  task_request = TaskCheckRequest("task_id", consts.EXTEND_STATUS_INFO)
80 
81  with self.assertRaises(ConnectionTimeout):
82  list(self.cmd_executor.execute(task_request))
83 
84 
85 
87  self.connect_mock.send.return_value = None
88  self.connect_mock.recv.side_effect = ResponseFormatErr("Boom")
89 
90  task_request = TaskCheckRequest("task_id", consts.EXTEND_STATUS_INFO)
91 
92  with self.assertRaises(CommandExecutorErr):
93  list(self.cmd_executor.execute(task_request))
94 
95 
97  self.connect_mock.send.return_value = None
98  self.connect_mock.recv.side_effect = CommandConvertorError("Boom")
99 
100  task_request = TaskCheckRequest("task_id", consts.EXTEND_STATUS_INFO)
101 
102  with self.assertRaises(CommandExecutorErr):
103  list(self.cmd_executor.execute(task_request))
104 
Check task request.
Definition: Commands.py:169
It's a wrapper similar to zmsg.hpp in sense of encapsulation of hce response message structure...
Definition: Response.py:20
def execute(self, commands, nodes)
execute method execute incoming commands on nodes, keepts reult in responses and responsesDicts field...
Definition: NodeManager.py:63
Exception which inform about errors which appeared during parsing procedure of drce message protocol...
Convertor which used to convert Task*Reques to json and TaskResponse from json.
Exception which inform about errors which appeared during the hce protocol parsing procedure...
Definition: Response.py:11