Skip to content

Commit 27fef21

Browse files
committed
fix: modify tests for PilotLoggingAgent
1 parent 95e2e33 commit 27fef21

File tree

2 files changed

+52
-34
lines changed

2 files changed

+52
-34
lines changed

src/DIRAC/WorkloadManagementSystem/Agent/PilotLoggingAgent.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def initialize(self):
4646
"""
4747
# pilot logs lifetime in days
4848
self.clearPilotsDelay = self.am_getOption("ClearPilotsDelay", self.clearPilotsDelay)
49-
# configured VOs and setup
49+
# configured VOs
5050
res = getVOs()
5151
if not res["OK"]:
5252
return res
@@ -64,7 +64,6 @@ def initialize(self):
6464
if pilotLogging:
6565
res = opsHelper.getOptionsDict("Shifter/DataManager")
6666
if not res["OK"]:
67-
# voRes[vo] = "No shifter defined - skipped"
6867
self.log.error(f"No shifter defined for VO: {vo} - skipping ...")
6968
continue
7069

@@ -84,6 +83,7 @@ def initialize(self):
8483
continue
8584
userDNs = result["Value"] # a same user may have more than one DN
8685
fd, filename = tempfile.mkstemp(prefix=vo + "__")
86+
print("filename", filename)
8787
os.close(fd)
8888
vomsAttr = getVOMSAttributeForGroup(proxyGroup)
8989
result = getProxy(userDNs, proxyGroup, vomsAttr=vomsAttr, proxyFilePath=filename)
@@ -92,7 +92,7 @@ def initialize(self):
9292
self.log.error(
9393
f"Could not download a proxy for DN {userDNs}, group {proxyGroup} for VO {vo}, skipped"
9494
)
95-
return result
95+
continue
9696
self.proxyDict[vo] = result["Value"]
9797

9898
return S_OK()

src/DIRAC/WorkloadManagementSystem/Agent/test/Test_Agent_PilotLoggingAgent.py

Lines changed: 49 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
# Mock Objects
1818
mockReply = MagicMock()
1919
mockReply1 = MagicMock()
20+
mockGetDNForUsername = MagicMock()
21+
mockGetVomsAttr = MagicMock()
22+
mockGetProxy = MagicMock()
2023
mockOperations = MagicMock()
2124
mockTornadoClient = MagicMock()
2225
mockDataManager = MagicMock()
@@ -48,19 +51,16 @@ def plaBase(mocker):
4851
mocker.patch(
4952
"DIRAC.WorkloadManagementSystem.Agent.PilotLoggingAgent.Operations.getOptionsDict", side_effect=mockReply1
5053
)
54+
mocker.patch(
55+
"DIRAC.WorkloadManagementSystem.Agent.PilotLoggingAgent.getDNForUsername", side_effect=mockGetDNForUsername
56+
)
57+
mocker.patch("DIRAC.WorkloadManagementSystem.Agent.PilotLoggingAgent.getProxy", side_effect=mockGetProxy)
5158
pla = PilotLoggingAgent()
5259
pla.log = gLogger
5360
pla._AgentModule__configDefaults = mockAM
5461
return pla
5562

5663

57-
@pytest.fixture
58-
def pla_initialised(mocker, plaBase):
59-
mocker.patch("DIRAC.WorkloadManagementSystem.Agent.PilotLoggingAgent.PilotLoggingAgent.executeForVO")
60-
plaBase.initialize()
61-
return plaBase
62-
63-
6464
@pytest.fixture
6565
def pla(mocker, plaBase):
6666
mocker.patch(
@@ -72,42 +72,60 @@ def pla(mocker, plaBase):
7272
"DIRAC.WorkloadManagementSystem.Agent.PilotLoggingAgent.DataManager",
7373
side_effect=mockDataManager,
7474
)
75-
plaBase.initialize()
7675
return plaBase
7776

7877

79-
def test_initialize(plaBase):
78+
@pytest.mark.parametrize(
79+
"remoteLogging, options, getDN, getVOMS, getProxy, resDict, expectedRes",
80+
[
81+
([True, False], upDict, S_OK(["myDN"]), S_OK(), S_OK("proxyfilename"), {"gridpp": "proxyfilename"}, S_OK()),
82+
([False, False], upDict, S_OK(["myDN"]), S_OK(), S_OK(), {}, S_OK()),
83+
([True, False], upDict, S_ERROR("Could not obtain a DN"), S_OK(), S_OK(), {}, S_OK()),
84+
([True, False], upDict, S_ERROR("Could not download proxy"), S_OK(), S_ERROR("Failure"), {}, S_OK()),
85+
],
86+
)
87+
def test_initialize(plaBase, remoteLogging, options, getDN, getVOMS, getProxy, resDict, expectedRes):
88+
"""
89+
After a successful initialisation the proxyDict should contain proxy filenames key by a VO name.
90+
test loops: gridpp enabled, lz disabled, proxy obtained, result proxyDict contains a proxy filename.
91+
both VOs disabled => proxyDict empty
92+
gridpp enabled, by getDNForUsername fails => proxyDict empty
93+
gridpp enabled, lz disabled, getProxy fails => proxyDict empty
94+
95+
"""
96+
mockReply.side_effect = remoteLogging # Operations.getValue("/Pilot/RemoteLogging", False)
97+
mockReply1.return_value = options # Operations.getOptionsDict("Shifter/DataManager")
98+
mockGetDNForUsername.return_value = getDN
99+
mockGetVomsAttr.return_value = getVOMS
100+
mockGetProxy.return_value = getProxy
101+
80102
res = plaBase.initialize()
103+
81104
assert plaBase.voList == plaModule.getVOs()["Value"]
82-
assert res == S_OK()
105+
assert resDict == plaBase.proxyDict
106+
assert res == expectedRes
83107

84108

85109
@pytest.mark.parametrize(
86-
"mockReplyInput, expected, expectedExecOut, expected2",
110+
"proxyDict, execVORes, expectedResult",
87111
[
88-
("/Pilot/RemoteLogging", [True, False], S_OK(), upDict),
89-
("/Pilot/RemoteLogging", [False, False], S_OK(), upDict),
90-
("/Pilot/RemoteLogging", [True, False], S_ERROR("Execute for VO failed"), upDict),
112+
({}, S_OK(), S_OK()),
113+
({"gridpp": "gridpp_proxyfile"}, S_OK(), S_OK()),
114+
(
115+
{"gridpp": "gridpp_proxyfile"},
116+
S_ERROR("Execute for VO failed"),
117+
S_ERROR("Agent cycle for some VO finished with errors"),
118+
),
91119
],
92120
)
93-
def test_execute(pla_initialised, mockReplyInput, expected, expectedExecOut, expected2):
121+
def test_execute(plaBase, proxyDict, execVORes, expectedResult):
94122
"""Testing a thin version of execute (executeForVO is mocked)"""
95-
assert pla_initialised.voList == plaModule.getVOs()["Value"]
96-
mockReply.side_effect = expected
97-
mockReply1.return_value = expected2
98-
# remote pilot logging on (gridpp only) and off.
99-
pla_initialised.executeForVO.return_value = expectedExecOut
100-
res = pla_initialised.execute()
101-
if not any(expected):
102-
pla_initialised.executeForVO.assert_not_called()
103-
else:
104-
assert pla_initialised.executeForVO.called
105-
pla_initialised.executeForVO.assert_called_with(
106-
"gridpp",
107-
proxyUserName=upDict["Value"]["User"],
108-
proxyUserGroup=upDict["Value"]["Group"],
109-
)
110-
assert res["OK"] == expectedExecOut["OK"]
123+
124+
plaBase.executeForVO = MagicMock()
125+
plaBase.proxyDict = proxyDict
126+
plaBase.executeForVO.return_value = execVORes
127+
res = plaBase.execute()
128+
assert res["OK"] == expectedResult["OK"]
111129

112130

113131
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)