1- # diracx-db/tests/sql/rss/test_rss.py
21from __future__ import annotations
32
43from datetime import datetime , timezone
54
65import pytest
76from sqlalchemy import insert
87
9- from diracx .core .models .rss import (
10- AllowedStatus ,
11- BannedStatus ,
12- ComputeElementStatus ,
13- FTSStatus ,
14- StorageElementStatus ,
15- map_status ,
16- )
17- from diracx .core .models .rss import SiteStatus as SiteStatusModel
8+ from diracx .core .exceptions import ResourceNotFoundError
189from diracx .db .sql .rss .db import ResourceStatusDB
1910
2011_NOW = datetime (2024 , 1 , 1 , tzinfo = timezone .utc )
@@ -30,27 +21,6 @@ async def rss_db(tmp_path):
3021 yield rss_db
3122
3223
33- @pytest .mark .parametrize ("status" , ["Active" , "Degraded" ])
34- async def test_map_status_allowed (status ):
35- assert bool (map_status (status , "" )) is True
36- assert isinstance (map_status (status , "" ), AllowedStatus )
37-
38-
39- @pytest .mark .parametrize ("status" , ["Banned" , "Probing" , "Error" , "Unknown" ])
40- async def test_map_status_banned (status ):
41- result = map_status (status , "CE banned" )
42- assert bool (result ) is False
43- assert isinstance (result , BannedStatus )
44- assert result .reason == "CE banned"
45-
46-
47- async def test_map_status_unknown_banned ():
48- result = map_status ("WeirdValue" , "" )
49- assert bool (result ) is False
50- assert isinstance (result , BannedStatus )
51- assert result .reason == "Unknown status: WeirdValue"
52-
53-
5424async def test_site_status (rss_db : ResourceStatusDB ):
5525 # Insert a test Site
5626 async with rss_db .engine .begin () as conn :
@@ -71,13 +41,12 @@ async def test_site_status(rss_db: ResourceStatusDB):
7141
7242 # Test with the test Site (should be found)
7343 async with rss_db as rss_db :
74- result = await rss_db .get_site_status ("TestSite" )
75- assert isinstance (result , SiteStatusModel )
76- assert isinstance (result .all , AllowedStatus )
77- assert bool (result .all ) is True
44+ status , reason = await rss_db .get_site_status ("TestSite" )
45+ assert status == "Active"
46+ assert reason == "All good"
7847
7948 # Test with an unknow Site (should not be found)
80- with pytest .raises (ValueError , match = "Site Unknown not found" ):
49+ with pytest .raises (ResourceNotFoundError ):
8150 async with rss_db as rss_db :
8251 await rss_db .get_site_status ("Unknown" )
8352
@@ -114,50 +83,7 @@ async def test_resource_status(rss_db: ResourceStatusDB):
11483 TokenOwner = "test" ,
11584 )
11685 )
117- # Insert a wrong test
118- await conn .execute (
119- insert (rss_db .metadata .tables ["ResourceStatus" ]).values (
120- Name = "WrongTest" ,
121- StatusType = "all" ,
122- VO = "all" ,
123- Status = "Active" ,
124- Reason = "All good" ,
125- DateEffective = _NOW ,
126- TokenExpiration = _FAR ,
127- LastCheckTime = _NOW ,
128- ElementType = "WrongTest" ,
129- TokenOwner = "WrongTest" ,
130- )
131- )
132-
133- # Test with the test Compute Element (should be found)
134- async with rss_db as rss_db :
135- result = await rss_db .get_resource_status ("TestCompute" )
136- assert isinstance (result , ComputeElementStatus )
137- assert isinstance (result .all , AllowedStatus )
138- assert bool (result .all ) is True
139-
140- # Test with the test FTS (should be found)
141- async with rss_db as rss_db :
142- result = await rss_db .get_resource_status ("TestFTS" )
143- assert isinstance (result , FTSStatus )
144- assert isinstance (result .all , AllowedStatus )
145- assert bool (result .all ) is True
146-
147- # Test with a wrong Resource type
148- with pytest .raises (ValueError , match = "not a valid ResourceType" ):
149- async with rss_db as rss_db :
150- await rss_db .get_resource_status ("WrongTest" )
151-
152- # Test with an unknow Resource (should not be found)
153- with pytest .raises (ValueError , match = "Resource Unknown not found" ):
154- async with rss_db as rss_db :
155- await rss_db .get_resource_status ("Unknown" )
156-
157-
158- async def test_storage_status (rss_db : ResourceStatusDB ):
159- # Insert a test Storage Element with all StatusType
160- async with rss_db .engine .begin () as conn :
86+ # Insert a test Storage Element with all StatusType
16187 for statustype in ["ReadAccess" , "WriteAccess" , "CheckAccess" , "RemoveAccess" ]:
16288 await conn .execute (
16389 insert (rss_db .metadata .tables ["ResourceStatus" ]).values (
@@ -174,20 +100,36 @@ async def test_storage_status(rss_db: ResourceStatusDB):
174100 )
175101 )
176102
103+ # Test with the test Compute Element (should be found)
104+ async with rss_db as rss_db :
105+ result = await rss_db .get_resource_status ("TestCompute" )
106+ assert "all" in result
107+ assert result ["all" ].Status == "Active"
108+ assert result ["all" ].Reason == "All good"
109+
110+ # Test with the test FTS (should be found)
111+ async with rss_db as rss_db :
112+ result = await rss_db .get_resource_status ("TestFTS" )
113+ assert "all" in result
114+ assert result ["all" ].Status == "Active"
115+ assert result ["all" ].Reason == "All good"
116+
177117 # Test with the test Storage Element (should be found)
178118 async with rss_db as rss_db :
179- result = await rss_db .get_storage_status ("TestStorage" )
180- assert isinstance (result , StorageElementStatus )
181- assert isinstance (result .read , AllowedStatus )
182- assert isinstance (result .write , AllowedStatus )
183- assert isinstance (result .check , AllowedStatus )
184- assert isinstance (result .remove , AllowedStatus )
185- assert bool (result .read ) is True
186- assert bool (result .write ) is True
187- assert bool (result .check ) is True
188- assert bool (result .remove ) is True
189-
190- # Test with an unknow Storage Element (should not be found)
191- with pytest .raises (ValueError , match = "StorageElement Unknown not found" ):
119+ result = await rss_db .get_resource_status (
120+ "TestStorage" , ["ReadAccess" , "WriteAccess" , "CheckAccess" , "RemoveAccess" ]
121+ )
122+ assert set (result .keys ()) == {
123+ "ReadAccess" ,
124+ "WriteAccess" ,
125+ "CheckAccess" ,
126+ "RemoveAccess" ,
127+ }
128+ for row in result .values ():
129+ assert row .Status == "Active"
130+ assert row .Reason == "All good"
131+
132+ # Test with an unknow Resource (should not be found)
133+ with pytest .raises (ResourceNotFoundError ):
192134 async with rss_db as rss_db :
193- await rss_db .get_storage_status ("Unknown" )
135+ await rss_db .get_resource_status ("Unknown" )
0 commit comments