Skip to content

Commit

Permalink
Fixing unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
anpicci committed Dec 17, 2024
1 parent 243db3d commit 24c1643
Showing 1 changed file with 48 additions and 17 deletions.
65 changes: 48 additions & 17 deletions test/python/WMCore_t/Storage_t/Backends_t/GFAL2Impl_t.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ def setUp(self):

def testInit(self):
testGFAL2Impl = GFAL2Impl()
# The default setup without a token
removeCommand = "env -i JOBSTARTDIR=$JOBSTARTDIR bash -c " \
"'. $JOBSTARTDIR/startup_environment.sh; date; gfal-rm -t 600 {}'"
"'. $JOBSTARTDIR/startup_environment.sh; date; gfal-rm -t 600 {}'"
copyCommand = "env -i JOBSTARTDIR=$JOBSTARTDIR bash -c '" \
". $JOBSTARTDIR/startup_environment.sh; date; gfal-copy -t 2400 -T 2400 -p " \
"-v --abort-on-failure {checksum} {options} {source} {destination}'"
". $JOBSTARTDIR/startup_environment.sh; date; gfal-copy -t 2400 -T 2400 -p " \
"-v --abort-on-failure {checksum} {options} {source} {destination}'"
self.assertEqual(removeCommand, testGFAL2Impl.removeCommand)
self.assertEqual(copyCommand, testGFAL2Impl.copyCommand)

Expand Down Expand Up @@ -79,10 +80,23 @@ def testCreateRemoveFileCommand_removeCommand(self, mock_path):
def testCreateStageOutCommand_stageIn(self, mock_createRemoveFileCommand):
self.GFAL2Impl.stageIn = True
mock_createRemoveFileCommand.return_value = "targetPFN2"
result = self.GFAL2Impl.createStageOutCommand("sourcePFN", "targetPFN")

# Call createStageOutCommand with auth_method='TOKEN'
result = self.GFAL2Impl.createStageOutCommand(
"sourcePFN", "targetPFN", auth_method='TOKEN'
)

# Generate the expected result with auth_method='TOKEN'
expectedResult = self.getStageOutCommandResult(
self.getCopyCommandDict("-K adler32", "", "sourcePFN", "targetPFN"), "targetPFN2")
self.getCopyCommandDict("-K adler32", "", "sourcePFN", "targetPFN"),
"targetPFN2",
auth_method="TOKEN"
)

# Assert that the removeFileCommand was called correctly
mock_createRemoveFileCommand.assert_called_with("targetPFN")

# Compare the expected and actual result
self.assertEqual(expectedResult, result)

@mock.patch('WMCore.Storage.Backends.GFAL2Impl.GFAL2Impl.createRemoveFileCommand')
Expand All @@ -94,20 +108,37 @@ def testCreateStageOutCommand_options(self, mock_createRemoveFileCommand):
mock_createRemoveFileCommand.assert_called_with("file:targetPFN")
self.assertEqual(expectedResult, result)

def getCopyCommandDict(self, checksum, options, source, destination):
copyCommandDict = {'checksum': '', 'options': '', 'source': '', 'destination': ''}
copyCommandDict['checksum'] = checksum
copyCommandDict['options'] = options
copyCommandDict['source'] = source
copyCommandDict['destination'] = destination
def getCopyCommandDict(self, checksum, options, source, destination, auth_method=None):
"""
Generate a dictionary for the gfal-copy command, dynamically adjusting for auth_method.
"""
copyCommandDict = {
'checksum': checksum,
'options': options,
'source': source,
'destination': destination
}
return copyCommandDict

def getStageOutCommandResult(self, copyCommandDict, createRemoveFileCommandResult):
def getStageOutCommandResult(self, copyCommandDict, createRemoveFileCommandResult, auth_method=None):
"""
Generate the expected result for the gfal-copy command, including dynamic adjustments for auth_method.
"""
# Adjust the setup based on auth_method
if auth_method == "X509":
setups = "env -i X509_USER_PROXY=$X509_USER_PROXY JOBSTARTDIR=$JOBSTARTDIR bash -c '{}'"
elif auth_method == "TOKEN":
setups = "env -i BEARER_TOKEN=$(cat $BEARER_TOKEN_FILE) JOBSTARTDIR=$JOBSTARTDIR bash -c '{}'"
else:
setups = "env -i JOBSTARTDIR=$JOBSTARTDIR bash -c '{}'"

# Build the copy command dynamically
copyOpts = '-t 2400 -T 2400 -p -v --abort-on-failure {checksum} {options} {source} {destination}'
copyCommand = setups.format('. $JOBSTARTDIR/startup_environment.sh; date; gfal-copy ' + copyOpts)

# Construct the full result
result = "#!/bin/bash\n"

copyCommand = self.copyCommand.format_map(copyCommandDict)
result += copyCommand

result += copyCommand.format_map(copyCommandDict)
result += """
EXIT_STATUS=$?
echo "gfal-copy exit status: $EXIT_STATUS"
Expand All @@ -118,7 +149,7 @@ def getStageOutCommandResult(self, copyCommandDict, createRemoveFileCommandResul
fi
exit $EXIT_STATUS
""".format(remove_command=createRemoveFileCommandResult)

return result

@mock.patch('WMCore.Storage.Backends.GFAL2Impl.os.path')
Expand Down

0 comments on commit 24c1643

Please sign in to comment.