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_CommandConvertor.py
Go to the documentation of this file.
1 '''
2 Created on Feb 13, 2014
3 
4 @author: igor
5 '''
6 import json
7 import unittest
8 from drce.CommandConvertor import TaskExecuteStructEncoder
9 from drce.CommandConvertor import TaskExecuteRequestEncoder
10 from drce.CommandConvertor import CommandConvertor
11 from drce.CommandConvertor import CommandConvertorError
12 from drce.Commands import TaskExecuteStruct
13 from drce.Commands import TaskExecuteRequest
14 from drce.Commands import TaskCheckRequest, TaskTerminateRequest
15 import drce.Consts as consts
16 
17 
18 class TestTaskExecuteStruct(unittest.TestCase):
19 
20 
21  def teEst_json_encode(self):
22  expect_json = """{"files": [{"action": 22, "data": "-f", "name": "go.py"}], "input": "", "session": {"tmode": 0, "shell": "", "environment": {}, "user": "", "timeout": 0, "password": "", "type": 0, "port": 0}, "command": ""}"""
23 
24  task_exec_struct = TaskExecuteStruct()
25  task_exec_struct.add_files("go.py", "-f", 22)
26  res_json = json.dumps(task_exec_struct, cls= TaskExecuteStructEncoder)
27 
28  self.assertEqual(expect_json, res_json, "json edcoder is broken")
29 
30 
31 
32 class TestTaskExecuteRequest(unittest.TestCase):
33 
34 
36  execute_request = TaskExecuteRequest("id")
37  #last 2 params for pretty print
38  json.dumps(execute_request, cls = TaskExecuteRequestEncoder, sort_keys = False, indent = 4)
39  commandConvertor = CommandConvertor()
40  commandConvertor.to_json(execute_request)
41 
42 
43 
44 class TestTaskCheckRequest(unittest.TestCase):
45 
46 
47  def test_json_convert(self):
48  uid = "11"
49  check_request = TaskCheckRequest(uid, consts.SIMPLE_STATUS_INFO)
50  json.dumps(check_request.__dict__, indent=2)
51 
52 
53 
54 class TestTaskTerminateRequest(unittest.TestCase):
55 
56 
57  def test_json_convert(self):
58  uid = "11"
59  terminate_request = TaskTerminateRequest(uid)
60  json.dumps(terminate_request.__dict__, indent=2)
61 
62 
63 class TestCommandConvertor(unittest.TestCase):
64 
65 
67  json_s = """
68  {
69  "error_code":0,
70  "error_message" :"msg",
71  "time":10,
72  "state":1,
73  "pid":101,
74  "data":[
75  {"files":[{"name":"f1", "data":"data1", "action":12}],
76  "stdout":"out",
77  "stderror":"error",
78  "exit_status":5,
79  "node":"n1",
80  "time":100
81  }
82  ]
83  }
84  """
85  test_err_str = "json parsing is failed"
86  cmd_convertor = CommandConvertor()
87  task_response = cmd_convertor.from_json(json_s)
88 
89  self.assertEqual(task_response.error_code, 0, test_err_str)
90  self.assertEqual(task_response.error_msg, "msg", test_err_str )
91  self.assertEqual(task_response.state, 1, test_err_str)
92  self.assertEqual(task_response.pid, 101, test_err_str)
93  self.assertEqual(len(task_response.data), 1, test_err_str)
94  response_item = task_response.data[0]
95  self.assertEqual(len(response_item.files), 1, test_err_str)
96  self.assertEqual(response_item.files[0]["name"], "f1", test_err_str)
97  self.assertEqual(response_item.files[0]["data"], "data1", test_err_str)
98  self.assertEqual(response_item.files[0]["action"], 12, test_err_str)
99  self.assertEqual(response_item.stdout, "out", test_err_str)
100  self.assertEqual(response_item.stderror, "error", test_err_str)
101  self.assertEqual(response_item.exit_status, 5, test_err_str)
102  self.assertEqual(response_item.node, "n1", test_err_str)
103  self.assertEqual(response_item.time, 100, test_err_str)
104 
105 
106 
108  json_s = """{"some":1, "data":["1", "2"]} """
109 
110  cmd_convertor = CommandConvertor()
111  with self.assertRaises(CommandConvertorError):
112  list(cmd_convertor.from_json(json_s))
113 
114 
116  json_s = """
117  {
118  "error_code":0,
119  "error_message" :"msg",
120  "time":10,
121  "state":1,
122  "data":[
123  {"files":["f1", "f2"],
124  "stdout":"out",
125  "stderror":"error",
126  "time":100
127  }
128  ]
129  }
130  """
131  cmd_convertor = CommandConvertor()
132  with self.assertRaises(CommandConvertorError):
133  list(cmd_convertor.from_json(json_s))
134 
Check task request.
Definition: Commands.py:169
wrapper for TaskExecuteStruct
Definition: Commands.py:87
wrapper for task request
Definition: Commands.py:158
Convertor which used to convert Task*Reques to json and TaskResponse from json.
Terminate task request.
Definition: Commands.py:191