1
0

test_redismod.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
  4. '''
  5. # Import Python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. from datetime import datetime
  8. # Import Salt Testing Libs
  9. from tests.support.mixins import LoaderModuleMockMixin
  10. from tests.support.unit import TestCase
  11. from tests.support.mock import (
  12. MagicMock)
  13. # Import Salt Libs
  14. import salt.modules.redismod as redismod
  15. class Mockredis(object):
  16. '''
  17. Mock redis class
  18. '''
  19. class ConnectionError(Exception):
  20. '''
  21. Mock ConnectionError class
  22. '''
  23. pass
  24. class MockConnect(object):
  25. '''
  26. Mock Connect class
  27. '''
  28. counter = 0
  29. def __init__(self):
  30. self.name = None
  31. self.pattern = None
  32. self.value = None
  33. self.key = None
  34. self.seconds = None
  35. self.timestamp = None
  36. self.field = None
  37. self.start = None
  38. self.stop = None
  39. self.master_host = None
  40. self.master_port = None
  41. @staticmethod
  42. def bgrewriteaof():
  43. '''
  44. Mock bgrewriteaof method
  45. '''
  46. return 'A'
  47. @staticmethod
  48. def bgsave():
  49. '''
  50. Mock bgsave method
  51. '''
  52. return 'A'
  53. def config_get(self, pattern):
  54. '''
  55. Mock config_get method
  56. '''
  57. self.pattern = pattern
  58. return 'A'
  59. def config_set(self, name, value):
  60. '''
  61. Mock config_set method
  62. '''
  63. self.name = name
  64. self.value = value
  65. return 'A'
  66. @staticmethod
  67. def dbsize():
  68. '''
  69. Mock dbsize method
  70. '''
  71. return 'A'
  72. @staticmethod
  73. def delete():
  74. '''
  75. Mock delete method
  76. '''
  77. return 'A'
  78. def exists(self, key):
  79. '''
  80. Mock exists method
  81. '''
  82. self.key = key
  83. return 'A'
  84. def expire(self, key, seconds):
  85. '''
  86. Mock expire method
  87. '''
  88. self.key = key
  89. self.seconds = seconds
  90. return 'A'
  91. def expireat(self, key, timestamp):
  92. '''
  93. Mock expireat method
  94. '''
  95. self.key = key
  96. self.timestamp = timestamp
  97. return 'A'
  98. @staticmethod
  99. def flushall():
  100. '''
  101. Mock flushall method
  102. '''
  103. return 'A'
  104. @staticmethod
  105. def flushdb():
  106. '''
  107. Mock flushdb method
  108. '''
  109. return 'A'
  110. def get(self, key):
  111. '''
  112. Mock get method
  113. '''
  114. self.key = key
  115. return 'A'
  116. def hget(self, key, field):
  117. '''
  118. Mock hget method
  119. '''
  120. self.key = key
  121. self.field = field
  122. return 'A'
  123. def hgetall(self, key):
  124. '''
  125. Mock hgetall method
  126. '''
  127. self.key = key
  128. return 'A'
  129. @staticmethod
  130. def info():
  131. '''
  132. Mock info method
  133. '''
  134. return 'A'
  135. def keys(self, pattern):
  136. '''
  137. Mock keys method
  138. '''
  139. self.pattern = pattern
  140. return 'A'
  141. def type(self, key):
  142. '''
  143. Mock type method
  144. '''
  145. self.key = key
  146. return 'A'
  147. @staticmethod
  148. def lastsave():
  149. '''
  150. Mock lastsave method
  151. '''
  152. return datetime.now()
  153. def llen(self, key):
  154. '''
  155. Mock llen method
  156. '''
  157. self.key = key
  158. return 'A'
  159. def lrange(self, key, start, stop):
  160. '''
  161. Mock lrange method
  162. '''
  163. self.key = key
  164. self.start = start
  165. self.stop = stop
  166. return 'A'
  167. @staticmethod
  168. def ping():
  169. '''
  170. Mock ping method
  171. '''
  172. MockConnect.counter = MockConnect.counter + 1
  173. if MockConnect.counter == 1:
  174. return 'A'
  175. elif MockConnect.counter in (2, 3, 5):
  176. raise Mockredis.ConnectionError('foo')
  177. @staticmethod
  178. def save():
  179. '''
  180. Mock save method
  181. '''
  182. return 'A'
  183. def set(self, key, value):
  184. '''
  185. Mock set method
  186. '''
  187. self.key = key
  188. self.value = value
  189. return 'A'
  190. @staticmethod
  191. def shutdown():
  192. '''
  193. Mock shutdown method
  194. '''
  195. return 'A'
  196. def slaveof(self, master_host, master_port):
  197. '''
  198. Mock slaveof method
  199. '''
  200. self.master_host = master_host
  201. self.master_port = master_port
  202. return 'A'
  203. def smembers(self, key):
  204. '''
  205. Mock smembers method
  206. '''
  207. self.key = key
  208. return 'A'
  209. @staticmethod
  210. def time():
  211. '''
  212. Mock time method
  213. '''
  214. return 'A'
  215. def zcard(self, key):
  216. '''
  217. Mock zcard method
  218. '''
  219. self.key = key
  220. return 'A'
  221. def zrange(self, key, start, stop):
  222. '''
  223. Mock zrange method
  224. '''
  225. self.key = key
  226. self.start = start
  227. self.stop = stop
  228. return 'A'
  229. class RedismodTestCase(TestCase, LoaderModuleMockMixin):
  230. '''
  231. Test cases for salt.modules.redismod
  232. '''
  233. def setup_loader_modules(self):
  234. return {
  235. redismod: {
  236. 'redis': Mockredis,
  237. '_connect': MagicMock(return_value=MockConnect())
  238. }
  239. }
  240. def test_bgrewriteaof(self):
  241. '''
  242. Test to asynchronously rewrite the append-only file
  243. '''
  244. self.assertEqual(redismod.bgrewriteaof(), 'A')
  245. def test_bgsave(self):
  246. '''
  247. Test to asynchronously save the dataset to disk
  248. '''
  249. self.assertEqual(redismod.bgsave(), 'A')
  250. def test_config_get(self):
  251. '''
  252. Test to get redis server configuration values
  253. '''
  254. self.assertEqual(redismod.config_get('*'), 'A')
  255. def test_config_set(self):
  256. '''
  257. Test to set redis server configuration values
  258. '''
  259. self.assertEqual(redismod.config_set('name', 'value'), 'A')
  260. def test_dbsize(self):
  261. '''
  262. Test to return the number of keys in the selected database
  263. '''
  264. self.assertEqual(redismod.dbsize(), 'A')
  265. def test_delete(self):
  266. '''
  267. Test to deletes the keys from redis, returns number of keys deleted
  268. '''
  269. self.assertEqual(redismod.delete(), 'A')
  270. def test_exists(self):
  271. '''
  272. Test to return true if the key exists in redis
  273. '''
  274. self.assertEqual(redismod.exists('key'), 'A')
  275. def test_expire(self):
  276. '''
  277. Test to set a keys time to live in seconds
  278. '''
  279. self.assertEqual(redismod.expire('key', 'seconds'), 'A')
  280. def test_expireat(self):
  281. '''
  282. Test to set a keys expire at given UNIX time
  283. '''
  284. self.assertEqual(redismod.expireat('key', 'timestamp'), 'A')
  285. def test_flushall(self):
  286. '''
  287. Test to remove all keys from all databases
  288. '''
  289. self.assertEqual(redismod.flushall(), 'A')
  290. def test_flushdb(self):
  291. '''
  292. Test to remove all keys from the selected database
  293. '''
  294. self.assertEqual(redismod.flushdb(), 'A')
  295. def test_get_key(self):
  296. '''
  297. Test to get redis key value
  298. '''
  299. self.assertEqual(redismod.get_key('key'), 'A')
  300. def test_hget(self):
  301. '''
  302. Test to get specific field value from a redis hash, returns dict
  303. '''
  304. self.assertEqual(redismod.hget('key', 'field'), 'A')
  305. def test_hgetall(self):
  306. '''
  307. Test to get all fields and values from a redis hash, returns dict
  308. '''
  309. self.assertEqual(redismod.hgetall('key'), 'A')
  310. def test_info(self):
  311. '''
  312. Test to get information and statistics about the server
  313. '''
  314. self.assertEqual(redismod.info(), 'A')
  315. def test_keys(self):
  316. '''
  317. Test to get redis keys, supports glob style patterns
  318. '''
  319. self.assertEqual(redismod.keys('pattern'), 'A')
  320. def test_key_type(self):
  321. '''
  322. Test to get redis key type
  323. '''
  324. self.assertEqual(redismod.key_type('key'), 'A')
  325. def test_lastsave(self):
  326. '''
  327. Test to get the UNIX time in seconds of the last successful
  328. save to disk
  329. '''
  330. self.assertTrue(redismod.lastsave())
  331. def test_llen(self):
  332. '''
  333. Test to get the length of a list in Redis
  334. '''
  335. self.assertEqual(redismod.llen('key'), 'A')
  336. def test_lrange(self):
  337. '''
  338. Test to get a range of values from a list in Redis
  339. '''
  340. self.assertEqual(redismod.lrange('key', 'start', 'stop'), 'A')
  341. def test_ping(self):
  342. '''
  343. Test to ping the server, returns False on connection errors
  344. '''
  345. self.assertEqual(redismod.ping(), 'A')
  346. self.assertFalse(redismod.ping())
  347. def test_save(self):
  348. '''
  349. Test to synchronously save the dataset to disk
  350. '''
  351. self.assertEqual(redismod.save(), 'A')
  352. def test_set_key(self):
  353. '''
  354. Test to set redis key value
  355. '''
  356. self.assertEqual(redismod.set_key('key', 'value'), 'A')
  357. def test_shutdown(self):
  358. '''
  359. Test to synchronously save the dataset to disk and then
  360. shut down the server
  361. '''
  362. self.assertFalse(redismod.shutdown())
  363. self.assertTrue(redismod.shutdown())
  364. self.assertFalse(redismod.shutdown())
  365. def test_slaveof(self):
  366. '''
  367. Test to make the server a slave of another instance, or
  368. promote it as master
  369. '''
  370. self.assertEqual(redismod.slaveof('master_host', 'master_port'), 'A')
  371. def test_smembers(self):
  372. '''
  373. Test to get members in a Redis set
  374. '''
  375. self.assertListEqual(redismod.smembers('key'), ['A'])
  376. def test_time(self):
  377. '''
  378. Test to return the current server UNIX time in seconds
  379. '''
  380. self.assertEqual(redismod.time(), 'A')
  381. def test_zcard(self):
  382. '''
  383. Test to get the length of a sorted set in Redis
  384. '''
  385. self.assertEqual(redismod.zcard('key'), 'A')
  386. def test_zrange(self):
  387. '''
  388. Test to get a range of values from a sorted set in Redis by index
  389. '''
  390. self.assertEqual(redismod.zrange('key', 'start', 'stop'), 'A')