-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b235130
commit 3ce951e
Showing
15 changed files
with
409 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
Redis | ||
====== | ||
Used to mock a Redis data store dependency. | ||
|
||
## Specs | ||
|
||
* Name: redis | ||
* Dev Port: 6379 | ||
* Password: | ||
|
||
## Configuration | ||
|
||
N/A | ||
|
||
## Defaults Example | ||
|
||
```yaml | ||
--- | ||
objects: | ||
foo: bar | ||
baz: buzz | ||
``` | ||
## Usage Example | ||
```python | ||
# Set a value | ||
self.mocks.redis.setup().set('foo') | ||
|
||
# Get a value | ||
self.mocks.redis.verify().get('foo') | ||
|
||
# Get a JSON value as dict | ||
result: dict = self.mocks.redis.verify().get_json('foo') | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
--- | ||
objects: | ||
foo: bar | ||
baz: buzz |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import json | ||
|
||
from touchstone.lib.touchstone_test import TouchstoneTest | ||
|
||
|
||
class Set(TouchstoneTest): | ||
def given(self) -> object: | ||
pass | ||
|
||
def when(self, given) -> object: | ||
self.mocks.redis.setup().set('foo', 'bar') | ||
return None | ||
|
||
def then(self, given, result) -> bool: | ||
return self.mocks.redis.verify().value_matches('foo', 'bar') | ||
|
||
|
||
class ValueExists(TouchstoneTest): | ||
def given(self) -> object: | ||
pass | ||
|
||
def when(self, given) -> object: | ||
self.mocks.redis.setup().set('foo', 'bar') | ||
return None | ||
|
||
def then(self, given, result) -> bool: | ||
return self.mocks.redis.verify().value_exists('foo') | ||
|
||
|
||
class ValueMatches(TouchstoneTest): | ||
def given(self) -> object: | ||
pass | ||
|
||
def when(self, given) -> object: | ||
self.mocks.redis.setup().set('foo', 'bar') | ||
return None | ||
|
||
def then(self, given, result) -> bool: | ||
return self.mocks.redis.verify().value_matches('foo', 'bar') | ||
|
||
|
||
class ValueMatchesJson(TouchstoneTest): | ||
def given(self) -> object: | ||
return {'foo': 'bar'} | ||
|
||
def when(self, given) -> object: | ||
self.mocks.redis.setup().set('foo', json.dumps(given)) | ||
return None | ||
|
||
def then(self, given, result) -> bool: | ||
return self.mocks.redis.verify().value_matches_json('foo', given) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,3 +16,4 @@ mocks: | |
mysql: | ||
s3: | ||
filesystem: | ||
redis: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import abc | ||
|
||
from touchstone.lib.nodes.mocks.behaviors.i_behavior import IBehavior | ||
|
||
|
||
class IRedisSetup(object): | ||
@abc.abstractmethod | ||
def set(self, key: str, value: str): | ||
"""Sets a key-value pair.""" | ||
pass | ||
|
||
|
||
class IRedisVerify(object): | ||
@abc.abstractmethod | ||
def value_exists(self, key: str) -> bool: | ||
"""Returns True if the given key exists.""" | ||
pass | ||
|
||
@abc.abstractmethod | ||
def value_matches(self, key: str, value: str) -> bool: | ||
"""Returns True if the given key matches the given value.""" | ||
|
||
@abc.abstractmethod | ||
def value_matches_json(self, key: str, value: dict) -> bool: | ||
"""Returns True if the given key matches the given JSON value.""" | ||
|
||
|
||
class IRedisBehavior(IBehavior): | ||
@abc.abstractmethod | ||
def setup(self) -> IRedisSetup: | ||
pass | ||
|
||
@abc.abstractmethod | ||
def verify(self) -> IRedisVerify: | ||
pass |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
from redis import Redis | ||
|
||
from touchstone.lib import exceptions | ||
from touchstone.lib.configurers.i_configurable import IConfigurable | ||
from touchstone.lib.managers.docker_manager import DockerManager | ||
from touchstone.lib.networking.docker_network import DockerNetwork | ||
from touchstone.lib.networking.i_network import INetwork | ||
from touchstone.lib.nodes.mocks.behaviors.i_behavior import IBehavior | ||
from touchstone.lib.nodes.mocks.behaviors.i_redis_behavior import IRedisBehavior, IRedisSetup, IRedisVerify | ||
from touchstone.lib.nodes.mocks.docker.i_runnable_docker import IRunnableDocker | ||
from touchstone.lib.nodes.mocks.docker.redis.docker_redis_setup import DockerRedisSetup | ||
from touchstone.lib.nodes.mocks.docker.redis.docker_redis_verify import DockerRedisVerify | ||
|
||
|
||
class DockerRedis(IRunnableDocker, IRedisBehavior): | ||
def __init__(self, defaults_configurer: IConfigurable, is_dev_mode: bool, setup: DockerRedisSetup, | ||
verify: DockerRedisVerify, docker_manager: DockerManager, docker_network: DockerNetwork): | ||
self.__defaults_configurer = defaults_configurer | ||
self.__is_dev_mode = is_dev_mode | ||
self.__setup = setup | ||
self.__verify = verify | ||
self.__docker_manager = docker_manager | ||
self.__docker_network = docker_network | ||
self.__ui_container_id = None | ||
|
||
def get_behavior(self) -> IBehavior: | ||
return self | ||
|
||
def get_network(self) -> INetwork: | ||
return self.__docker_network | ||
|
||
def initialize(self): | ||
redis_client = Redis(self.__docker_network.external_host(), self.__docker_network.external_port()) | ||
self.__setup.set_redis_client(redis_client) | ||
self.__verify.set_redis_client(redis_client) | ||
self.__setup.init(self.__defaults_configurer.get_config()) | ||
|
||
def start(self): | ||
run_result = self.__docker_manager.run_background_image('redis:6.2.5-alpine', port=6379) | ||
self.__docker_network.set_container_id(run_result.container_id) | ||
if self.__is_dev_mode: | ||
ui_run_result = self.__docker_manager.run_background_image( | ||
'rediscommander/redis-commander:redis-commander-210', ui_port=8081, | ||
environment_vars=[('REDIS_HOST', self.__docker_network.internal_host())]) | ||
self.__ui_container_id = ui_run_result.container_id | ||
self.__docker_network.set_ui_port(ui_run_result.ui_port) | ||
self.__docker_network.set_internal_port(run_result.internal_port) | ||
self.__docker_network.set_external_port(run_result.external_port) | ||
|
||
def stop(self): | ||
if self.__docker_network.container_id(): | ||
self.__docker_manager.stop_container(self.__docker_network.container_id()) | ||
if self.__ui_container_id: | ||
self.__docker_manager.stop_container(self.__ui_container_id) | ||
|
||
def reset(self): | ||
self.__setup.init(self.__defaults_configurer.get_config()) | ||
|
||
def is_healthy(self) -> bool: | ||
try: | ||
client = Redis(self.__docker_network.external_host(), self.__docker_network.external_port()) | ||
client.info() | ||
return True | ||
except Exception: | ||
return False | ||
|
||
def setup(self) -> IRedisSetup: | ||
if not self.__setup: | ||
raise exceptions.MockException('Setup unavailable. Mock is still starting.') | ||
return self.__setup | ||
|
||
def verify(self) -> IRedisVerify: | ||
if not self.__verify: | ||
raise exceptions.MockException('Verify unavailable. Mock is still starting.') | ||
return self.__verify |
22 changes: 22 additions & 0 deletions
22
touchstone/lib/nodes/mocks/docker/redis/docker_redis_setup.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from redis import Redis | ||
|
||
from touchstone.lib import exceptions | ||
from touchstone.lib.nodes.mocks.behaviors.i_redis_behavior import IRedisSetup | ||
|
||
|
||
class DockerRedisSetup(IRedisSetup): | ||
def __init__(self): | ||
self.__redis_client: Redis = None | ||
|
||
def set_redis_client(self, redis_client: Redis): | ||
self.__redis_client = redis_client | ||
|
||
def init(self, defaults: dict): | ||
self.__redis_client.flushall() | ||
for key, value in defaults.get('objects', []).items(): | ||
self.__redis_client.set(key, value) | ||
|
||
def set(self, key: str, value: str): | ||
if not isinstance(value, str): | ||
raise exceptions.MockException('Value must be a str.') | ||
return self.__redis_client.set(key, value) |
24 changes: 24 additions & 0 deletions
24
touchstone/lib/nodes/mocks/docker/redis/docker_redis_verify.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import json | ||
|
||
from redis import Redis | ||
|
||
from touchstone.helpers import validation | ||
from touchstone.lib.nodes.mocks.behaviors.i_redis_behavior import IRedisVerify | ||
|
||
|
||
class DockerRedisVerify(IRedisVerify): | ||
def __init__(self): | ||
self.__redis_client: Redis = None | ||
|
||
def set_redis_client(self, redis_client: Redis): | ||
self.__redis_client = redis_client | ||
|
||
def value_exists(self, key: str) -> bool: | ||
return self.__redis_client.get(key) is not None | ||
|
||
def value_matches(self, key: str, value: str) -> bool: | ||
return self.__redis_client.get(key).decode('utf-8') == value | ||
|
||
def value_matches_json(self, key: str, value: dict) -> bool: | ||
value_json = json.loads(self.__redis_client.get(key).decode('utf-8')) | ||
return validation.matches(value, value_json) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters