Skip to content

Commit

Permalink
tests(agent): racksdb routes when disabled/enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
rezib committed Jan 17, 2025
1 parent 7959e22 commit d0325fa
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
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
20 changes: 20 additions & 0 deletions slurmweb/tests/lib/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,26 @@ def inner(self, *args, **kwargs):
return inner


def flask_version():
import importlib

return tuple(
[int(digit) for digit in importlib.metadata.version("flask").split(".")]
)


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",
},
)

0 comments on commit d0325fa

Please sign in to comment.