Skip to content

Commit

Permalink
tests(agent): cover RacksDB unable errors
Browse files Browse the repository at this point in the history
  • Loading branch information
rezib committed Jan 30, 2025
1 parent 7d5ddca commit 3ba8d7d
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 2 deletions.
42 changes: 42 additions & 0 deletions slurmweb/tests/apps/test_agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Copyright (c) 2025 Rackslab
#
# This file is part of Slurm-web.
#
# SPDX-License-Identifier: GPL-3.0-or-later

import sys
from ..lib.agent import TestAgentBase


class TestAgentApp(TestAgentBase):

def test_app_loaded(self):
# No error log must be emitted in this case. Note that assertNoLogs is available
# starting from Python 3.10. For versions below, absence of logs is not checked.
if sys.version_info < (3, 10):
self.setup_client()
else:
with self.assertNoLogs("slurmweb", level="ERROR"):
self.setup_client()

def test_app_racksdb_format_error(self):
with self.assertLogs("slurmweb", level="ERROR") as cm:
self.setup_client(racksdb_format_error=True)
self.assertEqual(
cm.output,
[
"ERROR:slurmweb.apps.agent:Unable to load RacksDB database: fake db "
"format error"
],
)

def test_app_racksdb_schema_error(self):
with self.assertLogs("slurmweb", level="ERROR") as cm:
self.setup_client(racksdb_schema_error=True)
self.assertEqual(
cm.output,
[
"ERROR:slurmweb.apps.agent:Unable to load RacksDB schema: fake db "
"schema error"
],
)
15 changes: 13 additions & 2 deletions slurmweb/tests/lib/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from rfl.authentication.user import AuthenticatedUser
from slurmweb.apps import SlurmwebConfSeed
from slurmweb.apps.agent import SlurmwebAppAgent
from racksdb.errors import RacksDBFormatError, RacksDBSchemaError

from .utils import (
mock_slurmrestd_responses,
Expand Down Expand Up @@ -50,7 +51,12 @@ def basic(self):

class TestAgentBase(unittest.TestCase):

def setup_client(self, additional_conf=None):
def setup_client(
self,
additional_conf=None,
racksdb_format_error=False,
racksdb_schema_error=False,
):
# Generate JWT signing key
key = tempfile.NamedTemporaryFile(mode="w+")
key.write("hey")
Expand Down Expand Up @@ -81,7 +87,12 @@ def setup_client(self, additional_conf=None):

# Start the app with mocked RacksDB web blueprint
with mock.patch("slurmweb.apps.agent.RacksDBWebBlueprint") as m:
m.return_value = FakeRacksDBWebBlueprint()
if racksdb_format_error:
m.side_effect = RacksDBFormatError("fake db format error")
elif racksdb_schema_error:
m.side_effect = RacksDBSchemaError("fake db schema error")
else:
m.return_value = FakeRacksDBWebBlueprint()
self.app = SlurmwebAppAgent(
SlurmwebConfSeed(
debug=False,
Expand Down
20 changes: 20 additions & 0 deletions slurmweb/tests/views/test_agent_racksdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,26 @@ def test_request_racksdb(self):
)


class TestAgentRacksDBUnabledRequest(TestAgentBase):

def setUp(self):
self.setup_client(racksdb_format_error=True)

def test_request_racksdb(self):
# Check FakeRacksDBWebBlueprint is not registered when racksdb is unable to load
# database.
response = self.client.get("/racksdb/fake")
self.assertEqual(response.status_code, 404)
self.assertEqual(
response.json,
{
"code": 404,
"description": flask_404_description,
"name": "Not Found",
},
)


class TestAgentRacksDBDisabledRequest(TestAgentBase):

def setUp(self):
Expand Down

0 comments on commit 3ba8d7d

Please sign in to comment.