Skip to content

Commit

Permalink
Merge pull request #317 from giffels/fix/CodeQL-suggestions
Browse files Browse the repository at this point in the history
Fix suggestions from CodeQL scan 
  • Loading branch information
giffels authored Nov 21, 2023
2 parents 17f8097 + 880c5e0 commit 242e4c0
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 5 deletions.
2 changes: 1 addition & 1 deletion tardis/plugins/sqliteregistry.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def _deploy_db_schema(self) -> None:
"CONSTRAINT unique_machine_type_per_site UNIQUE (machine_type, site_id)", # noqa B950
],
"Resources": [
"id INTEGER PRIMARY KEY AUTOINCREMENT,"
"id INTEGER PRIMARY KEY AUTOINCREMENT",
"remote_resource_uuid VARCHAR(255)",
"drone_uuid VARCHAR(255) UNIQUE",
"state_id INTEGER",
Expand Down
12 changes: 12 additions & 0 deletions tardis/utilities/asynccachemap.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,15 @@ def __getitem__(self, item):

def __len__(self):
return len(self._data)

def __eq__(self, other):
if not isinstance(other, AsyncCacheMap):
return False

return (
self._update_coroutine == other._update_coroutine
and self._max_age == other._max_age
and self._last_update == other._last_update
and self._data == other._data
and self._lock == other._lock
)
5 changes: 5 additions & 0 deletions tardis/utilities/staticmapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@ def __iter__(self):

def __len__(self):
return len(self._data)

def __eq__(self, other):
if not isinstance(other, StaticMapping):
return False
return self._data == other._data
5 changes: 3 additions & 2 deletions tests/plugins_t/test_prometheusmonitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ def tearDownClass(cls):

@patch("tardis.plugins.prometheusmonitoring.logging", Mock())
def setUp(self):
ip = "127.0.0.1"
self.config = self.mock_config.return_value
self.config.Plugins.PrometheusMonitoring.addr = "127.0.0.1"
self.config.Plugins.PrometheusMonitoring.port = get_free_port()
self.config.Plugins.PrometheusMonitoring.addr = ip
self.config.Plugins.PrometheusMonitoring.port = get_free_port(ip)

self.plugin = PrometheusMonitoring()

Expand Down
4 changes: 2 additions & 2 deletions tests/utilities/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ def async_return(*args, return_value=None, **kwargs):
return f


def get_free_port(): # from https://gist.github.com/dbrgn/3979133
def get_free_port(ip: str): # from https://gist.github.com/dbrgn/3979133
s = socket.socket()
s.bind(("", 0))
s.bind((ip, 0))
port = s.getsockname()[1]
s.close()
return port
Expand Down
30 changes: 30 additions & 0 deletions tests/utilities_t/test_asynccachemap.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,33 @@ def test_last_update(self):
self.assertTrue(
datetime.now() - self.async_cache_map.last_update < timedelta(seconds=1)
)

def test_eq_async_cache_map(self):
test_cache_map = AsyncCacheMap(
update_coroutine=self.async_cache_map._update_coroutine
)
# Since both objects have been recently initialized, all values (self._max_age,
# self._last_update, self._data and self._lock) are still the defaults
self.assertTrue(self.async_cache_map == test_cache_map)

# Test the opposite
self.assertFalse(self.async_cache_map != test_cache_map)

# change default values
run_async(self.async_cache_map.update_status)
self.assertFalse(self.async_cache_map == test_cache_map)

# update default values, self._last_update, self._lock still differ
run_async(test_cache_map.update_status)
self.assertFalse(self.async_cache_map == test_cache_map)

# Assimilate lock, self._last_update still differs
test_cache_map._lock = self.async_cache_map._lock
self.assertFalse(self.async_cache_map == test_cache_map)

# Make them equal again
test_cache_map._last_update = self.async_cache_map._last_update
self.assertTrue(self.async_cache_map == test_cache_map)

# Test different class
self.assertFalse(self.async_cache_map == self.test_data)
14 changes: 14 additions & 0 deletions tests/utilities_t/test_staticmapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,17 @@ def test_modify_static_mapping(self):
self.static_map["testB"] = 456
with self.assertRaises(TypeError):
self.static_map["testC"] = 456

def test_eq_async_cache_map(self):
test_static_map = StaticMapping(**self.test_data)
self.assertTrue(self.static_map == test_static_map)

# Test the opposite
self.assertFalse(self.static_map != test_static_map)

# Change the data dictionary
test_static_map = StaticMapping(test=123)
self.assertFalse(self.static_map == test_static_map)

# Test different class
self.assertFalse(self.static_map == self.test_data)

0 comments on commit 242e4c0

Please sign in to comment.