schedule_test.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. import unittest
  4. from plugins import base
  5. import uuid
  6. import logging
  7. from threading import Lock
  8. import datetime
  9. import schedule
  10. import time
  11. mock_lock = Lock()
  12. fail_num = success_num = 0
  13. class MockPlugin(base.Base):
  14. def __init__(self, *args, **kwargs):
  15. super(MockPlugin, self).__init__(*args, **kwargs)
  16. if kwargs.__contains__('name'):
  17. self.name = kwargs['name']
  18. else:
  19. self.name = 'ipc' + uuid.uuid1().hex[0:10]
  20. self.host = kwargs['host']
  21. self.port = kwargs['port']
  22. self.user = kwargs['username']
  23. self.password = kwargs['pwd']
  24. self.args = args
  25. self.kwargs = kwargs
  26. def name(self):
  27. return self.name
  28. def config_check(self, *args, **kwargs):
  29. if kwargs.__contains__('error'):
  30. return kwargs['error']
  31. return None
  32. @staticmethod
  33. def record(is_success):
  34. global success_num, fail_num
  35. mock_lock.acquire()
  36. if is_success:
  37. success_num += 1
  38. else:
  39. fail_num += 1
  40. mock_lock.release()
  41. def exec(self):
  42. err = self.config_check(**self.kwargs)
  43. if err is not None:
  44. logging.error('[MockPlugin] check config error: %s', err)
  45. self.record(False)
  46. return
  47. self.record(True)
  48. class ScheduleTest(unittest.TestCase):
  49. @staticmethod
  50. def generate_valid_config(success_count, fail_count):
  51. config = {
  52. 'id': int(datetime.datetime.now().timestamp())
  53. }
  54. data = []
  55. for i in range(success_count):
  56. item = {
  57. 'host': 'local.pc',
  58. 'port': 2000,
  59. 'dev_id': uuid.uuid1(),
  60. 'type': 'mock_plugin',
  61. 'schema': 'onvif',
  62. 'username': 'admin',
  63. 'pwd': '123456',
  64. 'expr': 10,
  65. 'kpi': {
  66. 'key1': 1
  67. }
  68. }
  69. data.append(item)
  70. for i in range(fail_count):
  71. item = {
  72. 'host': 'local.pc',
  73. 'port': 2000,
  74. 'dev_id': uuid.uuid1(),
  75. 'type': 'mock_plugin',
  76. 'schema': 'onvif',
  77. 'username': 'admin',
  78. 'pwd': '123456',
  79. 'expr': 10,
  80. 'kpi': {
  81. 'key1': 1
  82. },
  83. 'error': 'data error'
  84. }
  85. data.append(item)
  86. config['data'] = data
  87. return config
  88. @staticmethod
  89. def generate_invalid_config(count):
  90. config = {
  91. 'id': int(datetime.datetime.now().timestamp())
  92. }
  93. keys = ['host', 'port', 'dev_id', 'type', 'schema', 'username', 'pwd']
  94. data = []
  95. for count_index in range(count):
  96. item = {}
  97. index = count_index % len(keys)
  98. for i, k in enumerate(keys):
  99. if index == i:
  100. continue
  101. if k == 'port':
  102. item['port'] = 2000
  103. if k == 'type':
  104. item['type'] = 'invalid_type'
  105. else:
  106. item[k] = k
  107. data.append(item)
  108. config['data'] = data
  109. return config
  110. @staticmethod
  111. def generate_non_object(count):
  112. config = {
  113. 'id': int(datetime.datetime.now().timestamp())
  114. }
  115. data = []
  116. for index in range(count):
  117. data.append(index)
  118. config['data'] = data
  119. return config
  120. @staticmethod
  121. def reset():
  122. global success_num, fail_num
  123. success_num = 0
  124. fail_num = 0
  125. def test_schedule_serial(self):
  126. # config is None
  127. scheduler = schedule.Schedule(None)
  128. scheduler.plugins = {
  129. 'mock_plugin': MockPlugin
  130. }
  131. scheduler.deliver()
  132. time.sleep(1)
  133. self.assertEqual(0, success_num)
  134. self.assertEqual(0, fail_num)
  135. self.reset()
  136. # config have no field data
  137. scheduler = schedule.Schedule({'id': int(datetime.datetime.now().timestamp())})
  138. scheduler.plugins = {
  139. 'mock_plugin': MockPlugin
  140. }
  141. scheduler.deliver()
  142. time.sleep(1)
  143. self.assertEqual(0, success_num)
  144. self.assertEqual(0, fail_num)
  145. self.reset()
  146. # config.data is not list
  147. scheduler = schedule.Schedule({
  148. 'id': int(datetime.datetime.now().timestamp()),
  149. 'data': 10
  150. })
  151. scheduler.plugins = {
  152. 'mock_plugin': MockPlugin
  153. }
  154. scheduler.deliver()
  155. time.sleep(1)
  156. self.assertEqual(0, success_num)
  157. self.assertEqual(0, fail_num)
  158. self.reset()
  159. # valid config, 10 success, 10 fail.
  160. config = self.generate_valid_config(10, 10)
  161. scheduler = schedule.Schedule(config)
  162. scheduler.plugins = {
  163. 'mock_plugin': MockPlugin
  164. }
  165. scheduler.deliver()
  166. time.sleep(1)
  167. self.assertEqual(10, success_num)
  168. self.assertEqual(10, fail_num)
  169. self.reset()
  170. # invalid config, 0 success, 0 fail.
  171. config = self.generate_invalid_config(10)
  172. scheduler = schedule.Schedule(config)
  173. scheduler.plugins = {
  174. 'mock_plugin': MockPlugin
  175. }
  176. scheduler.deliver()
  177. time.sleep(1)
  178. # As invalid config, plugin won't be execute.
  179. self.assertEqual(0, success_num)
  180. self.assertEqual(0, fail_num)
  181. self.reset()
  182. # non-object config, 0 success, 0 fail.
  183. config = self.generate_non_object(10)
  184. scheduler = schedule.Schedule(config)
  185. scheduler.plugins = {
  186. 'mock_plugin': MockPlugin
  187. }
  188. scheduler.deliver()
  189. time.sleep(1)
  190. self.assertEqual(0, success_num)
  191. self.assertEqual(0, fail_num)
  192. self.reset()
  193. # invalid plugin type, 5 success, 0 fail
  194. config = self.generate_valid_config(10, 0)
  195. config_data = config['data']
  196. for index, item in enumerate(config_data):
  197. if index == 5:
  198. break
  199. config_data[index]['type'] = 'mock_type'
  200. del config_data[index]['expr']
  201. del config_data[index]['kpi']
  202. config['data'] = config_data
  203. scheduler = schedule.Schedule(config)
  204. scheduler.plugins = {
  205. 'mock_plugin': MockPlugin
  206. }
  207. scheduler.deliver()
  208. time.sleep(1)
  209. self.assertEqual(5, success_num)
  210. self.assertEqual(0, fail_num)
  211. self.reset()
  212. # multi config, 10 success, 10 fail
  213. config_data = [None]
  214. config = self.generate_valid_config(10, 10)
  215. invalid_config = self.generate_invalid_config(10)
  216. non_object_config = self.generate_non_object(10)
  217. invalid_type_config = self.generate_valid_config(10, 0)
  218. invalid_type_data = invalid_type_config['data']
  219. for index, item in enumerate(invalid_type_data):
  220. invalid_type_data[index]['type'] = 'mock_type'
  221. config_data = config_data + config['data']
  222. config_data = config_data + invalid_config['data']
  223. config_data = config_data + non_object_config['data']
  224. config_data = config_data + invalid_type_data
  225. config['data'] = config_data
  226. # check config
  227. self.assertEqual(len(config_data), 51)
  228. scheduler = schedule.Schedule(config)
  229. scheduler.plugins = {
  230. 'mock_plugin': MockPlugin
  231. }
  232. scheduler.deliver()
  233. time.sleep(1)
  234. self.assertEqual(10, success_num)
  235. self.assertEqual(10, fail_num)
  236. self.reset()
  237. def test_schedule_concurrent(self):
  238. pass
  239. if __name__ == '__main__':
  240. unittest.main()