Skip to content

Commit 212bad0

Browse files
committed
test: adding test specific for SandboxMetadataDB
1 parent bb95b7c commit 212bad0

File tree

4 files changed

+44
-22
lines changed

4 files changed

+44
-22
lines changed

src/DIRAC/WorkloadManagementSystem/ConfigTemplate.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ Services
101101
Enable = No
102102
PilotsLoggingQueue = serviceURL::QueueType::QueueName
103103
}
104+
##BEGIN SandboxStore
104105
SandboxStore
105106
{
106107
Port = 9196
@@ -121,6 +122,7 @@ Services
121122
}
122123
}
123124
}
125+
##END
124126
OptimizationMind
125127
{
126128
Port = 9175

src/DIRAC/WorkloadManagementSystem/DB/SandboxMetadataDB.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def __init__(self):
1212
DB.__init__(self, "SandboxMetadataDB", "WorkloadManagement/SandboxMetadataDB")
1313
result = self.__initializeDB()
1414
if not result["OK"]:
15-
raise RuntimeError(f"Can't create tables: {result['Message']}")
15+
raise RuntimeError(f"Can't create tables: {result['Message']}")
1616
self.__assignedSBGraceDays = 0
1717
self.__unassignedSBGraceDays = 15
1818

@@ -112,7 +112,7 @@ def registerAndGetSandbox(self, owner, ownerDN, ownerGroup, sbSE, sbPFN, size=0)
112112
Register a new sandbox in the metadata catalog
113113
Returns ( sbid, newSandbox )
114114
"""
115-
result = self.__registerAndGetOwnerId(owner, ownerDN, ownerGroup)
115+
result = self.__registerAndGetOwnerId(owner, ownerDN, ownerGroup)
116116
if not result["OK"]:
117117
return result
118118
ownerId = result["Value"]
@@ -135,7 +135,7 @@ def registerAndGetSandbox(self, owner, ownerDN, ownerGroup, sbSE, sbPFN, size=0)
135135
if not result["OK"]:
136136
return result
137137
if not result["Value"]:
138-
return S_ERROR("Location %s already exists but doesn't belong to the user or setup")
138+
return S_ERROR("SandBox already exists but doesn't belong to the user or setup")
139139
sbId = result["Value"][0][0]
140140
self.accessedSandboxById(sbId)
141141
return S_OK((sbId, False))
@@ -144,7 +144,7 @@ def registerAndGetSandbox(self, owner, ownerDN, ownerGroup, sbSE, sbPFN, size=0)
144144
return S_OK((result["lastRowId"], True))
145145
result = self._query("SELECT LAST_INSERT_ID()")
146146
if not result["OK"]:
147-
return S_ERROR("Can't determine sandbox id after insertion")
147+
return S_ERROR("Can't determine sandbox id after insertion")
148148
return S_OK((result["Value"][0][0], True))
149149

150150
def accessedSandboxById(self, sbId):
@@ -174,12 +174,12 @@ def assignSandboxesToEntities(self, enDict, requesterName, requesterGroup, enSet
174174
for entityId in enDict:
175175
for sbTuple in enDict[entityId]:
176176
if not isinstance(sbTuple, (tuple, list)):
177-
return S_ERROR("Entry for entity %s is not a itterable of tuples/lists" % entityId)
177+
return S_ERROR(f"Entry for entity {entityId} is not an iterable of tuples/lists")
178178
if len(sbTuple) != 2:
179-
return S_ERROR("SB definition is not ( SBLocation, Type )! It's '%s'" % str(sbTuple))
179+
return S_ERROR(f"SB definition is not ( SBLocation, Type )! It's {sbTuple}")
180180
SBLocation = sbTuple[0]
181181
if SBLocation.find("SB:") != 0:
182-
return S_ERROR("%s doesn't seem to be a sandbox" % SBLocation)
182+
return S_ERROR(f"{SBLocation} doesn't seem to be a sandbox")
183183
SBLocation = SBLocation[3:]
184184
splitted = List.fromChar(SBLocation, "|")
185185
if len(splitted) < 2:
@@ -197,7 +197,8 @@ def assignSandboxesToEntities(self, enDict, requesterName, requesterGroup, enSet
197197
insertValues = []
198198
if not result["OK"]:
199199
self.log.warn(
200-
"Cannot find id for %s:%s with requester %s@%s" % (SEName, SEPFN, requesterName, requesterGroup)
200+
f"Cannot find id for {SEName}:",
201+
f"{SEPFN} with requester {requesterName}@{requesterGroup}: {result['Message']}",
201202
)
202203
else:
203204
sbId = result["Value"]
@@ -273,7 +274,7 @@ def unassignEntities(self, entitiesDict, requesterName, requesterGroup):
273274
escapedSetup = self._escapeString(entitySetup)["Value"]
274275
result = self.__filterEntitiesByRequester(entitiesIds, escapedSetup, requesterName, requesterGroup)
275276
if not result["OK"]:
276-
gLogger.error(f"Cannot filter entities: {result['Message']}")
277+
gLogger.error("Cannot filter entities", result["Message"])
277278
continue
278279
ids = result["Value"]
279280
if not ids:
@@ -283,7 +284,7 @@ def unassignEntities(self, entitiesDict, requesterName, requesterGroup):
283284
sqlCmd = "DELETE FROM `sb_EntityMapping` WHERE %s" % " AND ".join(sqlCond)
284285
result = self._update(sqlCmd)
285286
if not result["OK"]:
286-
gLogger.error(f"Cannot unassign entities: {result['Message']}")
287+
gLogger.error("Cannot unassign entities", result["Message"])
287288
else:
288289
updated += 1
289290
return S_OK(updated)

src/DIRAC/WorkloadManagementSystem/Service/SandboxStoreHandler.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
""" SandboxHandler is the implementation of the Sandbox service
22
in the DISET framework
3+
4+
.. literalinclude:: ../ConfigTemplate.cfg
5+
:start-after: ##BEGIN SandboxStore
6+
:end-before: ##END
7+
:dedent: 2
8+
:caption: SandboxStore options
39
"""
410
import os
511
import time
@@ -36,7 +42,7 @@ def initializeHandler(cls, serviceInfoDict):
3642
cls.sandboxDB = result["Value"]()
3743

3844
except RuntimeError as excp:
39-
return S_ERROR("Can't connect to DB: %s" % excp)
45+
return S_ERROR("Can't connect to DB: %s" % repr(excp))
4046
return S_OK()
4147

4248
def initialize(self):
@@ -95,7 +101,7 @@ def transfer_fromClient(self, fileId, token, fileSize, fileHelper):
95101
else:
96102
extension = ""
97103
aHash = fileId
98-
gLogger.info("Upload requested for %s [%s]" % (aHash, extension))
104+
gLogger.info("Upload requested", f"for {aHash} [{extension}]")
99105

100106
credDict = self.getRemoteCredentials()
101107
sbPath = self.__getSandboxPath("%s.%s" % (aHash, extension))
@@ -123,10 +129,10 @@ def transfer_fromClient(self, fileId, token, fileSize, fileHelper):
123129
# Write to local file
124130
result = self.__networkToFile(fileHelper, hdPath)
125131
if not result["OK"]:
126-
gLogger.error("Error while receiving sandbox file", "%s" % result["Message"])
132+
gLogger.error("Error while receiving sandbox file", result["Message"])
127133
return result
128134
hdPath = result["Value"]
129-
gLogger.info("Wrote sandbox to file %s" % hdPath)
135+
gLogger.info("Wrote sandbox to file", hdPath)
130136
# Check hash!
131137
if fileHelper.getHash() != aHash:
132138
self.__secureUnlinkFile(hdPath)
@@ -141,7 +147,7 @@ def transfer_fromClient(self, fileId, token, fileSize, fileHelper):
141147
return result
142148
sbPath = result["Value"][1]
143149
# Register!
144-
gLogger.info("Registering sandbox in the DB with", "SB:%s|%s" % (self.__seNameToUse, sbPath))
150+
gLogger.info("Registering sandbox in the DB with", f"SB:{self.__seNameToUse}|{sbPath}")
145151
result = self.sandboxDB.registerAndGetSandbox(
146152
credDict["username"],
147153
credDict["DN"],
@@ -154,7 +160,7 @@ def transfer_fromClient(self, fileId, token, fileSize, fileHelper):
154160
self.__secureUnlinkFile(hdPath)
155161
return result
156162

157-
sbURL = "SB:%s|%s" % (self.__seNameToUse, sbPath)
163+
sbURL = f"SB:{self.__seNameToUse}|{sbPath}"
158164
assignTo = dict([(key, [(sbURL, assignTo[key])]) for key in assignTo])
159165
result = self.export_assignSandboxesToEntities(assignTo)
160166
if not result["OK"]:
@@ -521,7 +527,7 @@ def __deleteSandboxFromExternalBackend(self, SEName, SEPFN):
521527
return ReqClient().putRequest(request)
522528
except Exception as e:
523529
gLogger.exception("Exception while setting deletion request")
524-
return S_ERROR("Cannot set deletion request: %s" % str(e))
530+
return S_ERROR(f"Cannot set deletion request: {e}")
525531
else:
526532
gLogger.info("Deleting external Sandbox")
527533
try:

tests/Integration/WorkloadManagementSystem/Test_SandboxStoreClient.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,18 @@ def test_SSCChain():
5252
exeScriptLocation = find_all("exe-script.py", "..", "/DIRAC/tests/Integration")[0]
5353
fileList = [exeScriptLocation]
5454
res = ssc.uploadFilesAsSandbox(fileList)
55-
assert res["OK"] is True
55+
assert res["OK"] is True, res["Message"]
5656
# SEPFN = res['Value'].split( '|' )[1]
5757
res = ssc.uploadFilesAsSandboxForJob(fileList, 1, "Input")
58-
assert res["OK"] is True
58+
assert res["OK"] is True, res["Message"]
5959
res = ssc.downloadSandboxForJob(1, "Input") # to run this we need the RSS on
6060
print(res) # for debug...
61-
assert res["OK"] is True
61+
assert res["OK"] is True, res["Message"]
6262

6363
# only ones needing the DB
6464
res = smDB.getUnusedSandboxes()
6565
print(res)
66-
assert res["OK"] is True
66+
assert res["OK"] is True, res["Message"]
6767
# smDB.getSandboxId(SEName, SEPFN, requesterName, requesterGroup)
6868
# # cleaning
6969
# res = smDB.deleteSandboxes(SBIdList)
@@ -72,4 +72,17 @@ def test_SSCChain():
7272

7373
def test_SandboxMetadataDB():
7474
smDB = SandboxMetadataDB()
75-
smDB.registerAndGetSandbox("owner_1", "owner_1_DN", "owner_1_group", "sbSE", "/sb/pfn/1.tar.bz2")
75+
76+
seNameToUse = "ProductionSandboxSE"
77+
sbPath = "/sb/pfn/1.tar.bz2"
78+
assignTo = {"adminusername": "dirac_admin"}
79+
80+
res = smDB.registerAndGetSandbox(
81+
"adminusername", "/C=ch/O=DIRAC/OU=DIRAC CI/CN=ciuser", "dirac_admin", seNameToUse, sbPath, 123
82+
)
83+
assert res["OK"], res["Message"]
84+
85+
sbURL = f"SB:{seNameToUse}|{sbPath}"
86+
assignTo = dict([(key, [(sbURL, assignTo[key])]) for key in assignTo])
87+
res = smDB.assignSandboxesToEntities(assignTo, "adminusername", "dirac_admin", "enSetup")
88+
assert res["OK"], res["Message"]

0 commit comments

Comments
 (0)