Esempio n. 1
0
class CPImplTest(unittest.TestCase):
    def setUp(self):
        self.CPImpl = CPImpl()

    def testCreateSourceName(self):
        self.assertEqual("name", self.CPImpl.createSourceName("", "name"))
        self.assertEqual("file:////name", self.CPImpl.createSourceName("", "file:////name"))

    @mock.patch('WMCore.Storage.Backends.CPImpl.CPImpl.run')
    def testCreateOutputDirectory_error0(self, mock_runCommand):
        mock_runCommand.return_value = 0
        self.CPImpl.createOutputDirectory("dir/file/test")
        mock_runCommand.assert_called_once_with("/bin/ls dir/file > /dev/null ")

    @mock.patch('WMCore.Storage.Backends.CPImpl.CPImpl.run')
    def testCreateOutputDirectory_error0Exception(self, mock_runCommand):
        mock_runCommand.side_effect = Exception("Im in test, yay!")
        self.CPImpl.createOutputDirectory("dir/file/test")
        mock_runCommand.assert_called_once_with("/bin/ls dir/file > /dev/null ")

    def testCreateOutputDirectory_error1Exception(self):
        self.CPImpl.run = Mock()
        self.CPImpl.run.return_value = [1, Exception("Failed to create directory")]
        self.CPImpl.createOutputDirectory("dir/file/test")
        self.CPImpl.run.assert_has_calls([call("/bin/ls dir/file > /dev/null "),
                                          call("umask 002 ; /bin/mkdir -p dir/file")])

    @mock.patch('WMCore.Storage.Backends.CPImpl.CPImpl.run')
    def testCreateOutputDirectory_error1(self, mock_runCommand):
        mock_runCommand.return_value = [1, 2]
        self.CPImpl.createOutputDirectory("dir/file/test")
        mock_runCommand.assert_has_calls([call("/bin/ls dir/file > /dev/null "),
                                          call("umask 002 ; /bin/mkdir -p dir/file")])

    def testCreateStageOutCommand_realFile(self):
        base = os.path.join(getTestBase(), "WMCore_t/Storage_t/ExecutableCommands.py")
        result = self.CPImpl.createStageOutCommand(base, "test")
        expectedResult = [" '642' -eq $DEST_SIZE", "DEST_SIZE=`/bin/ls -l test", base]
        for text in expectedResult:
            self.assertIn(text, result)

    @mock.patch('WMCore.Storage.Backends.CPImpl.os.stat')
    def testCreateStageOutCommand_noFile(self, mocked_stat):
        mocked_stat.return_value = [0, 1, 2, 3, 4, 5, 6]
        result = self.CPImpl.createStageOutCommand("sourcePFN", "targetPFN", options="optionsTest")
        expectedResult = [" '6' -eq $DEST_SIZE", "DEST_SIZE=`/bin/ls -l targetPFN", "sourcePFN", "optionsTest"]
        for text in expectedResult:
            self.assertIn(text, result)

    @mock.patch('WMCore.Storage.StageOutImpl.StageOutImpl.executeCommand')
    def testRemoveFile(self, mock_executeCommand):
        self.CPImpl.removeFile("file")
        mock_executeCommand.assert_called_with("/bin/rm file")
Esempio n. 2
0
class CPImplTest(unittest.TestCase):
    def setUp(self):
        self.CPImpl = CPImpl()

    def testCreateSourceName_simple(self):
        self.assertEqual("name", self.CPImpl.createSourceName("", "name"))

    @mock.patch('WMCore.Storage.Backends.CPImpl.os')
    def testCreateOutputDirectory_noDir(self, mock_os):
        mock_os.path.dirname.return_value = "/dir1/dir2"
        mock_os.path.isdir.return_value = False
        self.CPImpl.createOutputDirectory("/dir1/dir2/file")
        mock_os.path.dirname.assert_called_once_with("/dir1/dir2/file")
        mock_os.path.isdir.assert_called_once_with("/dir1/dir2")
        mock_os.makedirs.assert_called_once_with("/dir1/dir2")

    @mock.patch('WMCore.Storage.Backends.CPImpl.os')
    def testCreateOutputDirectory_withDir(self, mock_os):
        mock_os.path.dirname.return_value = "/dir1/dir2"
        mock_os.path.isdir.return_value = True
        self.CPImpl.createOutputDirectory("/dir1/dir2/file")
        mock_os.path.dirname.assert_called_once_with("/dir1/dir2/file")
        mock_os.path.isdir.assert_called_once_with("/dir1/dir2")
        mock_os.makedirs.assert_not_called()

    def testCreateStageOutCommand_noStageInNoOptionsNoChecksum(self):
        self.CPImpl.stageIn = False
        results = self.CPImpl.createStageOutCommand("sourcePFN", "targetPFN")
        expectedResults = self.createStageOutCommandResults(False, "sourcePFN", "targetPFN")
        self.assertEqual(expectedResults, results)

    def testCreateStageOutCommand_stageInNoOptionsNoChecksum(self):
        self.CPImpl.stageIn = True
        results = self.CPImpl.createStageOutCommand("sourcePFN", "targetPFN")
        expectedResults = self.createStageOutCommandResults(True, "sourcePFN", "targetPFN")
        self.assertEqual(expectedResults, results)

    def createStageOutCommandResults(self, stageIn, sourcePFN, targetPFN):
        copyCommand = ""
        if stageIn:
            remotePFN, localPFN = sourcePFN, targetPFN
        else:
            remotePFN, localPFN = targetPFN, sourcePFN
            copyCommand += "LOCAL_SIZE=`stat -c%%s %s`\n" % localPFN
            copyCommand += "echo \"Local File Size is: $LOCAL_SIZE\"\n"
        copyCommand += "cp %s %s\n" % (sourcePFN, targetPFN)
        if stageIn:
            copyCommand += "LOCAL_SIZE=`stat -c%%s %s`\n" % localPFN
            copyCommand += "echo \"Local File Size is: $LOCAL_SIZE\"\n"
            removeCommand = ""
        else:
            removeCommand = "rm %s;" % remotePFN
        copyCommand += "REMOTE_SIZE=`stat -c%%s %s`\n" % remotePFN
        copyCommand += "echo \"Remote File Size is: $REMOTE_SIZE\"\n"
        copyCommand += "if [ $REMOTE_SIZE ] && [ $LOCAL_SIZE == $REMOTE_SIZE ]; then exit 0; "
        copyCommand += "else echo \"ERROR: Size Mismatch between local and SE\"; %s exit 60311 ; fi" % removeCommand
        return copyCommand

    @mock.patch('WMCore.Storage.Backends.CPImpl.os')
    def testRemoveFile(self, mock_os):
        self.CPImpl.removeFile("file")
        mock_os.remove.assert_called_once_with("file")
Esempio n. 3
0
class CPImplTest(unittest.TestCase):
    def setUp(self):
        self.CPImpl = CPImpl()

    def testCreateSourceName_simple(self):
        self.assertEqual("name", self.CPImpl.createSourceName("", "name"))

    @mock.patch('WMCore.Storage.Backends.CPImpl.os')
    def testCreateOutputDirectory_noDir(self, mock_os):
        mock_os.path.dirname.return_value = "/dir1/dir2"
        mock_os.path.isdir.return_value = False
        self.CPImpl.createOutputDirectory("/dir1/dir2/file")
        mock_os.path.dirname.assert_called_once_with("/dir1/dir2/file")
        mock_os.path.isdir.assert_called_once_with("/dir1/dir2")
        mock_os.makedirs.assert_called_once_with("/dir1/dir2")

    @mock.patch('WMCore.Storage.Backends.CPImpl.os')
    def testCreateOutputDirectory_withDir(self, mock_os):
        mock_os.path.dirname.return_value = "/dir1/dir2"
        mock_os.path.isdir.return_value = True
        self.CPImpl.createOutputDirectory("/dir1/dir2/file")
        mock_os.path.dirname.assert_called_once_with("/dir1/dir2/file")
        mock_os.path.isdir.assert_called_once_with("/dir1/dir2")
        mock_os.makedirs.assert_not_called()

    def testCreateStageOutCommand_noStageInNoOptionsNoChecksum(self):
        self.CPImpl.stageIn = False
        results = self.CPImpl.createStageOutCommand("sourcePFN", "targetPFN")
        expectedResults = self.createStageOutCommandResults(
            False, "sourcePFN", "targetPFN")
        self.assertEqual(expectedResults, results)

    def testCreateStageOutCommand_stageInNoOptionsNoChecksum(self):
        self.CPImpl.stageIn = True
        results = self.CPImpl.createStageOutCommand("sourcePFN", "targetPFN")
        expectedResults = self.createStageOutCommandResults(
            True, "sourcePFN", "targetPFN")
        self.assertEqual(expectedResults, results)

    def createStageOutCommandResults(self, stageIn, sourcePFN, targetPFN):
        copyCommand = ""
        if stageIn:
            remotePFN, localPFN = sourcePFN, targetPFN
        else:
            remotePFN, localPFN = targetPFN, sourcePFN
            copyCommand += "LOCAL_SIZE=`stat -c%%s %s`\n" % localPFN
            copyCommand += "echo \"Local File Size is: $LOCAL_SIZE\"\n"
        copyCommand += "cp %s %s\n" % (sourcePFN, targetPFN)
        if stageIn:
            copyCommand += "LOCAL_SIZE=`stat -c%%s %s`\n" % localPFN
            copyCommand += "echo \"Local File Size is: $LOCAL_SIZE\"\n"
            removeCommand = ""
        else:
            removeCommand = "rm %s;" % remotePFN
        copyCommand += "REMOTE_SIZE=`stat -c%%s %s`\n" % remotePFN
        copyCommand += "echo \"Remote File Size is: $REMOTE_SIZE\"\n"
        copyCommand += "if [ $REMOTE_SIZE ] && [ $LOCAL_SIZE == $REMOTE_SIZE ]; then exit 0; "
        copyCommand += "else echo \"ERROR: Size Mismatch between local and SE\"; %s exit 60311 ; fi" % removeCommand
        return copyCommand

    @mock.patch('WMCore.Storage.Backends.CPImpl.os')
    def testRemoveFile(self, mock_os):
        self.CPImpl.removeFile("file")
        mock_os.remove.assert_called_once_with("file")
Esempio n. 4
0
class CPImplTest(unittest.TestCase):
    def setUp(self):
        self.CPImpl = CPImpl()

    def testCreateSourceName(self):
        self.assertEqual("name", self.CPImpl.createSourceName("", "name"))
        self.assertEqual("file:////name",
                         self.CPImpl.createSourceName("", "file:////name"))

    @mock.patch('WMCore.Storage.Backends.CPImpl.CPImpl.run')
    def testCreateOutputDirectory_error0(self, mock_runCommand):
        mock_runCommand.return_value = 0
        self.CPImpl.createOutputDirectory("dir/file/test")
        mock_runCommand.assert_called_once_with(
            "/bin/ls dir/file > /dev/null ")

    @mock.patch('WMCore.Storage.Backends.CPImpl.CPImpl.run')
    def testCreateOutputDirectory_error0Exception(self, mock_runCommand):
        mock_runCommand.side_effect = Exception("Im in test, yay!")
        self.CPImpl.createOutputDirectory("dir/file/test")
        mock_runCommand.assert_called_once_with(
            "/bin/ls dir/file > /dev/null ")

    def testCreateOutputDirectory_error1Exception(self):
        self.CPImpl.run = Mock()
        self.CPImpl.run.side_effect = [1, Exception()]
        self.CPImpl.createOutputDirectory("dir/file/test")
        self.CPImpl.run.assert_has_calls([
            call("/bin/ls dir/file > /dev/null "),
            call("umask 002 ; /bin/mkdir -p dir/file")
        ])

    @mock.patch('WMCore.Storage.Backends.CPImpl.CPImpl.run')
    def testCreateOutputDirectory_error1(self, mock_runCommand):
        mock_runCommand.return_value = 1
        self.CPImpl.createOutputDirectory("dir/file/test")
        mock_runCommand.assert_has_calls([
            call("/bin/ls dir/file > /dev/null "),
            call("umask 002 ; /bin/mkdir -p dir/file")
        ])

    def testCreateStageOutCommand_realFile(self):
        base = os.path.join(getTestBase(),
                            "WMCore_t/Storage_t/ExecutableCommands.py")
        result = self.CPImpl.createStageOutCommand(base, "test")
        expectedResult = [
            " '590' -eq $DEST_SIZE", "DEST_SIZE=`/bin/ls -l test", base
        ]
        for text in expectedResult:
            self.assertIn(text, result)

    @mock.patch('WMCore.Storage.Backends.CPImpl.os.stat')
    def testCreateStageOutCommand_noFile(self, mocked_stat):
        mocked_stat.return_value = [0, 1, 2, 3, 4, 5, 6]
        result = self.CPImpl.createStageOutCommand("sourcePFN",
                                                   "targetPFN",
                                                   options="optionsTest")
        expectedResult = [
            " '6' -eq $DEST_SIZE", "DEST_SIZE=`/bin/ls -l targetPFN",
            "sourcePFN", "optionsTest"
        ]
        for text in expectedResult:
            self.assertIn(text, result)

    @mock.patch('WMCore.Storage.StageOutImpl.StageOutImpl.executeCommand')
    def testRemoveFile(self, mock_executeCommand):
        self.CPImpl.removeFile("file")
        mock_executeCommand.assert_called_with("/bin/rm file")