123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460 |
- # -*- coding: utf-8 -*-
- '''
- :codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
- '''
- # Import Python libs
- from __future__ import absolute_import, print_function, unicode_literals
- from datetime import datetime
- # Import Salt Testing Libs
- from tests.support.mixins import LoaderModuleMockMixin
- from tests.support.unit import TestCase
- from tests.support.mock import (
- MagicMock)
- # Import Salt Libs
- import salt.modules.redismod as redismod
- class Mockredis(object):
- '''
- Mock redis class
- '''
- class ConnectionError(Exception):
- '''
- Mock ConnectionError class
- '''
- pass
- class MockConnect(object):
- '''
- Mock Connect class
- '''
- counter = 0
- def __init__(self):
- self.name = None
- self.pattern = None
- self.value = None
- self.key = None
- self.seconds = None
- self.timestamp = None
- self.field = None
- self.start = None
- self.stop = None
- self.master_host = None
- self.master_port = None
- @staticmethod
- def bgrewriteaof():
- '''
- Mock bgrewriteaof method
- '''
- return 'A'
- @staticmethod
- def bgsave():
- '''
- Mock bgsave method
- '''
- return 'A'
- def config_get(self, pattern):
- '''
- Mock config_get method
- '''
- self.pattern = pattern
- return 'A'
- def config_set(self, name, value):
- '''
- Mock config_set method
- '''
- self.name = name
- self.value = value
- return 'A'
- @staticmethod
- def dbsize():
- '''
- Mock dbsize method
- '''
- return 'A'
- @staticmethod
- def delete():
- '''
- Mock delete method
- '''
- return 'A'
- def exists(self, key):
- '''
- Mock exists method
- '''
- self.key = key
- return 'A'
- def expire(self, key, seconds):
- '''
- Mock expire method
- '''
- self.key = key
- self.seconds = seconds
- return 'A'
- def expireat(self, key, timestamp):
- '''
- Mock expireat method
- '''
- self.key = key
- self.timestamp = timestamp
- return 'A'
- @staticmethod
- def flushall():
- '''
- Mock flushall method
- '''
- return 'A'
- @staticmethod
- def flushdb():
- '''
- Mock flushdb method
- '''
- return 'A'
- def get(self, key):
- '''
- Mock get method
- '''
- self.key = key
- return 'A'
- def hget(self, key, field):
- '''
- Mock hget method
- '''
- self.key = key
- self.field = field
- return 'A'
- def hgetall(self, key):
- '''
- Mock hgetall method
- '''
- self.key = key
- return 'A'
- @staticmethod
- def info():
- '''
- Mock info method
- '''
- return 'A'
- def keys(self, pattern):
- '''
- Mock keys method
- '''
- self.pattern = pattern
- return 'A'
- def type(self, key):
- '''
- Mock type method
- '''
- self.key = key
- return 'A'
- @staticmethod
- def lastsave():
- '''
- Mock lastsave method
- '''
- return datetime.now()
- def llen(self, key):
- '''
- Mock llen method
- '''
- self.key = key
- return 'A'
- def lrange(self, key, start, stop):
- '''
- Mock lrange method
- '''
- self.key = key
- self.start = start
- self.stop = stop
- return 'A'
- @staticmethod
- def ping():
- '''
- Mock ping method
- '''
- MockConnect.counter = MockConnect.counter + 1
- if MockConnect.counter == 1:
- return 'A'
- elif MockConnect.counter in (2, 3, 5):
- raise Mockredis.ConnectionError('foo')
- @staticmethod
- def save():
- '''
- Mock save method
- '''
- return 'A'
- def set(self, key, value):
- '''
- Mock set method
- '''
- self.key = key
- self.value = value
- return 'A'
- @staticmethod
- def shutdown():
- '''
- Mock shutdown method
- '''
- return 'A'
- def slaveof(self, master_host, master_port):
- '''
- Mock slaveof method
- '''
- self.master_host = master_host
- self.master_port = master_port
- return 'A'
- def smembers(self, key):
- '''
- Mock smembers method
- '''
- self.key = key
- return 'A'
- @staticmethod
- def time():
- '''
- Mock time method
- '''
- return 'A'
- def zcard(self, key):
- '''
- Mock zcard method
- '''
- self.key = key
- return 'A'
- def zrange(self, key, start, stop):
- '''
- Mock zrange method
- '''
- self.key = key
- self.start = start
- self.stop = stop
- return 'A'
- class RedismodTestCase(TestCase, LoaderModuleMockMixin):
- '''
- Test cases for salt.modules.redismod
- '''
- def setup_loader_modules(self):
- return {
- redismod: {
- 'redis': Mockredis,
- '_connect': MagicMock(return_value=MockConnect())
- }
- }
- def test_bgrewriteaof(self):
- '''
- Test to asynchronously rewrite the append-only file
- '''
- self.assertEqual(redismod.bgrewriteaof(), 'A')
- def test_bgsave(self):
- '''
- Test to asynchronously save the dataset to disk
- '''
- self.assertEqual(redismod.bgsave(), 'A')
- def test_config_get(self):
- '''
- Test to get redis server configuration values
- '''
- self.assertEqual(redismod.config_get('*'), 'A')
- def test_config_set(self):
- '''
- Test to set redis server configuration values
- '''
- self.assertEqual(redismod.config_set('name', 'value'), 'A')
- def test_dbsize(self):
- '''
- Test to return the number of keys in the selected database
- '''
- self.assertEqual(redismod.dbsize(), 'A')
- def test_delete(self):
- '''
- Test to deletes the keys from redis, returns number of keys deleted
- '''
- self.assertEqual(redismod.delete(), 'A')
- def test_exists(self):
- '''
- Test to return true if the key exists in redis
- '''
- self.assertEqual(redismod.exists('key'), 'A')
- def test_expire(self):
- '''
- Test to set a keys time to live in seconds
- '''
- self.assertEqual(redismod.expire('key', 'seconds'), 'A')
- def test_expireat(self):
- '''
- Test to set a keys expire at given UNIX time
- '''
- self.assertEqual(redismod.expireat('key', 'timestamp'), 'A')
- def test_flushall(self):
- '''
- Test to remove all keys from all databases
- '''
- self.assertEqual(redismod.flushall(), 'A')
- def test_flushdb(self):
- '''
- Test to remove all keys from the selected database
- '''
- self.assertEqual(redismod.flushdb(), 'A')
- def test_get_key(self):
- '''
- Test to get redis key value
- '''
- self.assertEqual(redismod.get_key('key'), 'A')
- def test_hget(self):
- '''
- Test to get specific field value from a redis hash, returns dict
- '''
- self.assertEqual(redismod.hget('key', 'field'), 'A')
- def test_hgetall(self):
- '''
- Test to get all fields and values from a redis hash, returns dict
- '''
- self.assertEqual(redismod.hgetall('key'), 'A')
- def test_info(self):
- '''
- Test to get information and statistics about the server
- '''
- self.assertEqual(redismod.info(), 'A')
- def test_keys(self):
- '''
- Test to get redis keys, supports glob style patterns
- '''
- self.assertEqual(redismod.keys('pattern'), 'A')
- def test_key_type(self):
- '''
- Test to get redis key type
- '''
- self.assertEqual(redismod.key_type('key'), 'A')
- def test_lastsave(self):
- '''
- Test to get the UNIX time in seconds of the last successful
- save to disk
- '''
- self.assertTrue(redismod.lastsave())
- def test_llen(self):
- '''
- Test to get the length of a list in Redis
- '''
- self.assertEqual(redismod.llen('key'), 'A')
- def test_lrange(self):
- '''
- Test to get a range of values from a list in Redis
- '''
- self.assertEqual(redismod.lrange('key', 'start', 'stop'), 'A')
- def test_ping(self):
- '''
- Test to ping the server, returns False on connection errors
- '''
- self.assertEqual(redismod.ping(), 'A')
- self.assertFalse(redismod.ping())
- def test_save(self):
- '''
- Test to synchronously save the dataset to disk
- '''
- self.assertEqual(redismod.save(), 'A')
- def test_set_key(self):
- '''
- Test to set redis key value
- '''
- self.assertEqual(redismod.set_key('key', 'value'), 'A')
- def test_shutdown(self):
- '''
- Test to synchronously save the dataset to disk and then
- shut down the server
- '''
- self.assertFalse(redismod.shutdown())
- self.assertTrue(redismod.shutdown())
- self.assertFalse(redismod.shutdown())
- def test_slaveof(self):
- '''
- Test to make the server a slave of another instance, or
- promote it as master
- '''
- self.assertEqual(redismod.slaveof('master_host', 'master_port'), 'A')
- def test_smembers(self):
- '''
- Test to get members in a Redis set
- '''
- self.assertListEqual(redismod.smembers('key'), ['A'])
- def test_time(self):
- '''
- Test to return the current server UNIX time in seconds
- '''
- self.assertEqual(redismod.time(), 'A')
- def test_zcard(self):
- '''
- Test to get the length of a sorted set in Redis
- '''
- self.assertEqual(redismod.zcard('key'), 'A')
- def test_zrange(self):
- '''
- Test to get a range of values from a sorted set in Redis by index
- '''
- self.assertEqual(redismod.zrange('key', 'start', 'stop'), 'A')
|