Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip registering of RacksDB blueprint when disabled on agent #440

Merged
merged 2 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- gateway: Check RacksDB version executed by agent is greater or equal to the
minimal supported version specified in gateway configuration settings
(#415→#417).
- agent: Skip registering of RacksDB API endpoints when disabled (#440).
- frontend:
- Reduce height of error message container when unable to retrieve
infrastructure graphical representation from RacksDB in resources page.
Expand Down
45 changes: 24 additions & 21 deletions slurmweb/apps/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,27 +54,30 @@ class SlurmwebAppAgent(SlurmwebWebApp, RFLTokenizedRBACWebApp):
def __init__(self, seed):
SlurmwebWebApp.__init__(self, seed)

# Load RacksDB blueprint and fail with error if unable to load schema or
# database.
try:
self.register_blueprint(
RacksDBWebBlueprint(
db=self.settings.racksdb.db,
ext=self.settings.racksdb.extensions,
schema=self.settings.racksdb.schema,
drawings_schema=self.settings.racksdb.drawings_schema,
default_drawing_parameters={
"infrastructure": {"equipment_tags": self.settings.racksdb.tags}
},
),
url_prefix="/racksdb",
)
except RacksDBSchemaError as err:
logger.critical("Unable to load RacksDB schema: %s", err)
sys.exit(1)
except RacksDBFormatError as err:
logger.critical("Unable to load RacksDB database: %s", err)
sys.exit(1)
# If enabled, load RacksDB blueprint and fail with error if unable to load
# schema or database.
if self.settings.racksdb.enabled:
try:
self.register_blueprint(
RacksDBWebBlueprint(
db=self.settings.racksdb.db,
ext=self.settings.racksdb.extensions,
schema=self.settings.racksdb.schema,
drawings_schema=self.settings.racksdb.drawings_schema,
default_drawing_parameters={
"infrastructure": {
"equipment_tags": self.settings.racksdb.tags
}
},
),
url_prefix="/racksdb",
)
except RacksDBSchemaError as err:
logger.critical("Unable to load RacksDB schema: %s", err)
sys.exit(1)
except RacksDBFormatError as err:
logger.critical("Unable to load RacksDB database: %s", err)
sys.exit(1)

if self.settings.policy.roles.exists():
logger.debug("Select RBAC site roles policy %s", self.settings.policy.roles)
Expand Down
6 changes: 5 additions & 1 deletion slurmweb/tests/lib/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


import werkzeug
from flask import Blueprint
from flask import Blueprint, jsonify

from rfl.authentication.user import AuthenticatedUser
from slurmweb.apps import SlurmwebConfSeed
Expand Down Expand Up @@ -42,6 +42,10 @@ class FakeRacksDBWebBlueprint(Blueprint):

def __init__(self, **kwargs):
super().__init__("Fake RacksDB web blueprint", __name__)
self.add_url_rule("/fake", view_func=self.basic)

def basic(self):
return jsonify({"test": "ok"})


class TestAgentBase(unittest.TestCase):
Expand Down
26 changes: 26 additions & 0 deletions slurmweb/tests/lib/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,32 @@ def inner(self, *args, **kwargs):
return inner


def flask_version():
"""Return version of Flask package as a tuple of integers."""
import importlib

try:
version = importlib.metadata.version("flask")
except AttributeError:
version = flask.__version__
return tuple([int(digit) for digit in version.split(".")])


# Flask 404 description message has changed in recent versions (one space has been
# removed). Some tests check this message is used, they use this variable which is
# defined depending on the version of Flask package found in the environment.
if flask_version() < (1, 0, 0):
flask_404_description = (
"The requested URL was not found on the server. If you entered the URL "
"manually please check your spelling and try again."
)
else:
flask_404_description = (
"The requested URL was not found on the server. If you entered the URL "
"manually please check your spelling and try again."
)


class SlurmwebAssetUnavailable(Exception):
pass

Expand Down
53 changes: 53 additions & 0 deletions slurmweb/tests/views/test_agent_racksdb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Copyright (c) 2025 Rackslab
#
# This file is part of Slurm-web.
#
# SPDX-License-Identifier: GPL-3.0-or-later


import textwrap

from ..lib.agent import TestAgentBase
from ..lib.utils import flask_404_description


class TestAgentRacksDBEnabledRequest(TestAgentBase):

def setUp(self):
self.setup_client()

def test_request_racksdb(self):
# Check FakeRacksDBWebBlueprint is called when racksdb is enabled (by default).
response = self.client.get("/racksdb/fake")
self.assertEqual(response.status_code, 200)
self.assertEqual(
response.json,
{"test": "ok"},
)


class TestAgentRacksDBDisabledRequest(TestAgentBase):

def setUp(self):
self.setup_client(
additional_conf=textwrap.dedent(
"""

[racksdb]
enabled=no
"""
)
)

def test_request_racksdb(self):
# Check FakeRacksDBWebBlueprint is not registered when racksdb is disabled.
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",
},
)
Loading