Skip to content

Commit

Permalink
Merge pull request #862 from Ostorlab/feature/key-exists-check-persis…
Browse files Browse the repository at this point in the history
…t-mixin

Add method to check if a key is already mapped in Redis
  • Loading branch information
3asm authored Feb 5, 2025
2 parents 42f669e + d969905 commit 7a062c1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
14 changes: 13 additions & 1 deletion src/ostorlab/agent/mixins/agent_persist_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,18 @@ def get(self, key: Union[bytes, str]) -> Optional[bytes]:
"""
return self._redis_client.get(key)

def exists(self, key: Union[bytes, str]) -> bool:
"""Helper function that checks if a given key exists in the Redis database.
Args:
key: The key to check for existence.
Returns:
bool: True if the key exists, False otherwise.
"""

return self._redis_client.exists(key) == 1

def hash_add(
self,
hash_name: Union[bytes, str],
Expand All @@ -123,7 +135,7 @@ def hash_add(
Args:
hash_name: Name of the hash.
mapping: Dict of key/value pairs that will be hadded to hash_name
mapping: Dict of key/value pairs that will be added to hash_name
Returns:
True if the key does not exist, False otherwise
Expand Down
24 changes: 22 additions & 2 deletions tests/agent/mixins/agent_persist_mixin_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Tests for AgentPersistMixin module."""

import ipaddress
from typing import Generator

import pytest
from pytest_mock import plugin
Expand All @@ -14,10 +15,10 @@
@pytest.mark.asyncio
@pytest.mark.docker
async def testAgentPersistMixin_whenSetIsAdded_setIsPersisted(
mocker, redis_service, clean_redis_data
redis_service, clean_redis_data
):
"""Test proper storage and access of set API."""
del mocker, redis_service, clean_redis_data
del redis_service, clean_redis_data
settings = runtime_definitions.AgentSettings(
key="agent/ostorlab/debug", redis_url="redis://localhost:6379"
)
Expand All @@ -39,6 +40,25 @@ async def testAgentPersistMixin_whenSetIsAdded_setIsPersisted(
assert mixin.value_type("myKey1") == "none"


@pytest.mark.parametrize("clean_redis_data", ["redis://localhost:6379"], indirect=True)
@pytest.mark.asyncio
@pytest.mark.docker
async def testAgentPersistMixinExists_whenKeyExists_returnTrue(
redis_service: Generator[None, None, None],
clean_redis_data: Generator[local_redis_service.LocalRedis, None, None],
) -> None:
"""Test AgentPersistMixin exists method."""
settings = runtime_definitions.AgentSettings(
key="agent/ostorlab/debug", redis_url="redis://localhost:6379"
)
mixin = agent_persist_mixin.AgentPersistMixin(settings)

key = "thekey"
assert mixin.exists(key) is False
mixin.add(key, b"the_value")
assert mixin.exists(key) is True


@pytest.mark.parametrize("clean_redis_data", ["redis://localhost:6379"], indirect=True)
@pytest.mark.asyncio
@pytest.mark.docker
Expand Down

0 comments on commit 7a062c1

Please sign in to comment.