PageRenderTime 99ms CodeModel.GetById 40ms RepoModel.GetById 0ms app.codeStats 0ms

/test/python/WMCore_t/Storage_t/Backends_t/LCGImpl_t.py

https://github.com/dmwm/WMCore
Python | 160 lines | 152 code | 8 blank | 0 comment | 8 complexity | 97edbd9fe7ddb7c4f03aab2149fda2bf MD5 | raw file
  1. from __future__ import (print_function, division)
  2. import os
  3. import unittest
  4. from mock import mock
  5. from WMCore.Storage.Backends.LCGImpl import LCGImpl
  6. class LCGImplTest(unittest.TestCase):
  7. def setUp(self):
  8. self.LCGImpl = LCGImpl()
  9. self.LCGImpl.setups = self.setups = "test setups"
  10. self.timeoutOptions = self.LCGImpl.timeoutOptions = \
  11. '--srm-timeout 600 --sendreceive-timeout 600 --connect-timeout 300'
  12. @mock.patch('WMCore.Storage.Backends.LCGImpl.os')
  13. def testInit(self, mock_os):
  14. mock_os.environ.get.side_effect = lambda x: True if x == "GLITE_LOCATION" or x == "GRID_ENV_LOCATION" else False
  15. mock_os.path.isfile.return_value = True
  16. mock_os.path.normpath.side_effect = lambda x: "home" + x
  17. mock_os.path.join.side_effect = lambda a, b: os.path.join("", b)
  18. testLCGImpl = LCGImpl(True)
  19. self.assertTrue(testLCGImpl.stageIn)
  20. setups = []
  21. setups += ['source home/../etc/profile.d/grid-env.sh; ']
  22. setups += ['source home/grid-env.sh; ']
  23. setups += ['date "+%Y-%m-%dT%H:%M:%S"; ']
  24. self.assertTrue(testLCGImpl.stageIn)
  25. for setup in setups:
  26. self.assertIn(setup, testLCGImpl.setups)
  27. def testCreateSourceName_startswithSlash(self):
  28. self.assertEqual("file://name", self.LCGImpl.createSourceName("protocol", "//name"))
  29. @mock.patch('WMCore.Storage.Backends.LCGImpl.os.path')
  30. def testCreateSourceName_isfile(self, mock_path):
  31. mock_path.isfile.return_value = True
  32. mock_path.abspath.return_value = "/some/path"
  33. self.assertEqual("file:/some/path", self.LCGImpl.createSourceName("protocol", "name"))
  34. def testCreateSourceName_simple(self):
  35. self.assertEqual("name", self.LCGImpl.createSourceName("protocol", "name"))
  36. def testCreateStageOutCommand_stageInFile(self):
  37. self.LCGImpl.stageIn = True
  38. result = self.LCGImpl.createStageOutCommand("srm://sourcePFN", "file://targetPFN")
  39. expectedResults = self.getStageOutCommandResults("srm://sourcePFN", "file://targetPFN", "srm://sourcePFN",
  40. "//targetPFN", "/bin/rm -f //targetPFN", False)
  41. self.assertEqual(expectedResults, result)
  42. def testCreateStageOutCommand_stageISrmCvmfs(self):
  43. self.LCGImpl.stageIn = True
  44. result = self.LCGImpl.createStageOutCommand("srm://sourcePFN", "srm://targetPFN", options="cvmfs")
  45. createRemoveFileCommandResult = "%s lcg-del -b -l -D srmv2 %s --vo cms srm://targetPFN" % \
  46. (self.setups, self.timeoutOptions)
  47. expectedResults = self.getStageOutCommandResults("srm://sourcePFN", "srm://targetPFN", "srm://sourcePFN",
  48. "srm://targetPFN", createRemoveFileCommandResult, True)
  49. self.assertEqual(expectedResults, result)
  50. @mock.patch('WMCore.Storage.Backends.LCGImpl.StageOutImpl.createRemoveFileCommand')
  51. def testCreateStageOutCommand_options(self, mock_createRemoveFileCommand):
  52. mock_createRemoveFileCommand.return_value = "command"
  53. result = self.LCGImpl.createStageOutCommand("file://sourcePFN", "targetPFN", options="test")
  54. expectedResults = self.getStageOutCommandResults("file://sourcePFN", "targetPFN",
  55. "targetPFN", "//sourcePFN", "command", False, options="test")
  56. self.assertEqual(expectedResults, result)
  57. @mock.patch('WMCore.Storage.Backends.LCGImpl.StageOutImpl.createRemoveFileCommand')
  58. def testCreateStageOutCommand_checksum(self, mock_createRemoveFileCommand):
  59. mock_createRemoveFileCommand.return_value = "command"
  60. result = self.LCGImpl.createStageOutCommand("file://sourcePFN", "targetPFN", checksums={"adler32": "32"})
  61. expectedResults = self.getStageOutCommandResults("file://sourcePFN", "targetPFN",
  62. "targetPFN", "//sourcePFN", "command", False,
  63. checksums="00000032")
  64. self.assertEqual(expectedResults, result)
  65. def getStageOutCommandResults(self, sourcePFN, targetPFN, remotePFN, localPFN,
  66. createRemoveFileCommandResult, useCVMFS, options=None, checksums=None):
  67. result = "#!/bin/sh\n"
  68. copyCommand = "lcg-cp -b -D srmv2 --vo cms --srm-timeout 2400 --sendreceive-timeout 2400 --connect-timeout 300 --verbose"
  69. if options != None:
  70. copyCommand += " %s " % options
  71. copyCommand += " %s " % sourcePFN
  72. copyCommand += " %s 2> stageout.log" % targetPFN
  73. if useCVMFS:
  74. result += "(\n"
  75. result += "echo Modifying PATH and LD_LIBRARY_PATH to remove /cvmfs/cms.cern.ch elements\n"
  76. result += "export PATH=`echo $PATH | sed -e 's+:*/cvmfs/cms.cern.ch/[^:]*++'g`\n"
  77. result += "export LD_LIBRARY_PATH=`echo $LD_LIBRARY_PATH | sed -e 's+:*/cvmfs/cms.cern.ch/[^:]*++'g`\n"
  78. result += "echo Sourcing CVMFS UI setup script\n"
  79. result += ". /cvmfs/grid.cern.ch/emi3ui-latest/etc/profile.d/setup-ui-example.sh\n"
  80. result += copyCommand
  81. else:
  82. result += self.setups
  83. result += copyCommand
  84. result += """
  85. EXIT_STATUS=$?
  86. cat stageout.log
  87. echo -e "\nlcg-cp exit status: $EXIT_STATUS"
  88. if [[ $EXIT_STATUS != 0 ]]; then
  89. echo "ERROR: lcg-cp exited with $EXIT_STATUS"
  90. echo "Cleaning up failed file:"
  91. %s
  92. exit $EXIT_STATUS
  93. fi
  94. """ % createRemoveFileCommandResult
  95. result += "FILE_SIZE=`stat -c %s"
  96. result += " %s`\n" % localPFN
  97. result += "echo \"Local File Size is: $FILE_SIZE\"\n"
  98. if checksums:
  99. checksumCommand = \
  100. """
  101. if [[ "X$SRM_CHECKSUM" != "X" ]]; then
  102. if [[ "$SRM_CHECKSUM" == "%s" ]]; then
  103. exit 0
  104. else
  105. echo "ERROR: Checksum Mismatch between local and SE"
  106. echo "Cleaning up failed file"
  107. %s
  108. exit 60311
  109. fi
  110. fi
  111. exit 0
  112. """ % (checksums, createRemoveFileCommandResult)
  113. else:
  114. checksumCommand = "exit 0"
  115. metadataCheck = \
  116. """
  117. LCG_OUTPUT=`lcg-ls -l -b -D srmv2 %s %s 2>/dev/null`
  118. SRM_SIZE=`echo "$LCG_OUTPUT" | awk 'NR==1{print $5}'`
  119. SRM_CHECKSUM=`echo "$LCG_OUTPUT" | sed -nr 's/^.*\s([a-f0-9]{8})\s*\([aA][dD][lL][eE][rR]32\)\s*$/\\1/p'`
  120. echo "Remote File Size is: $SRM_SIZE"
  121. echo "Remote Checksum is: $SRM_CHECKSUM"
  122. if [[ $SRM_SIZE == $FILE_SIZE ]]; then
  123. %s
  124. else
  125. echo $LCG_OUTPUT
  126. echo "ERROR: Size Mismatch between local and SE. Cleaning up failed file..."
  127. %s
  128. exit 60311
  129. fi
  130. """ % (self.timeoutOptions, remotePFN, checksumCommand, createRemoveFileCommandResult)
  131. result += metadataCheck
  132. if useCVMFS:
  133. result += ")\n"
  134. return result
  135. @mock.patch('WMCore.Storage.StageOutImpl.StageOutImpl.executeCommand')
  136. def testRemoveFile(self, mock_executeCommand):
  137. self.LCGImpl.removeFile("file")
  138. mock_executeCommand.assert_called_with("%s lcg-del -b -l -D srmv2 %s --vo cms file" %
  139. (self.setups, self.timeoutOptions))