Beispiel #1
0
    def test_rename_backup(self, exists, suffix):
        self.mox.StubOutWithMock(os.path, 'exists')
        self.mox.StubOutWithMock(os, 'rename')

        input = 'test.dat'
        if suffix:
            backup = 'test.dat.testsuffix'
        else:
            backup = 'test.dat.bak'

        if exists == 0:
            os.path.exists(backup).AndReturn(False)
            output = backup
        if exists == 1:
            os.path.exists(backup).AndReturn(True)
            os.path.exists(backup + '.1').AndReturn(False)
            output = backup + '.1'
        if exists == 2:
            os.path.exists(backup).AndReturn(True)
            os.path.exists(backup + '.1').AndReturn(True)
            os.path.exists(backup + '.2').AndReturn(False)
            output = backup + '.2'

        os.rename(input, output)
        self.mox.ReplayAll()

        if suffix:
            b = fileutils.rename_backup(input, '.testsuffix')
        else:
            b = fileutils.rename_backup(input)
        assert b == output
        self.mox.VerifyAll()
Beispiel #2
0
    def test_get_as_local_path(self, exists, remote, overwrite, httpauth):
        if remote:
            p = 'http://example.org/test.zip'
            expectedp = 'test.zip'
        else:
            p = '/example/test.zip'
            expectedp = p

        self.mox.StubOutWithMock(os.path, 'exists')
        self.mox.StubOutWithMock(os.path, 'isdir')
        self.mox.StubOutWithMock(fileutils, 'rename_backup')
        self.mox.StubOutWithMock(fileutils, 'download')

        if httpauth:
            kwargs = {'httpuser': '******', 'httppassword': '******'}
        else:
            kwargs = {'httpuser': None, 'httppassword': None}

        if remote:
            os.path.exists(expectedp).AndReturn(exists)

        if remote and exists and overwrite == 'backup':
            fileutils.rename_backup(expectedp)

        if (remote and exists and overwrite == 'backup') or (
                remote and not exists):
            fileutils.download(p, expectedp, 0, **kwargs)

        if not remote or (remote and not exists) or (
                remote and exists and overwrite != 'error'):
            os.path.isdir(expectedp).AndReturn(False)
            os.path.exists(expectedp).AndReturn(True)

        self.mox.ReplayAll()

        if remote and exists and overwrite == 'error':
            with pytest.raises(fileutils.FileException):
                fileutils.get_as_local_path(p, overwrite, **kwargs)
        else:
            ptype, lpath = fileutils.get_as_local_path(p, overwrite, **kwargs)
            assert ptype == 'file'
            assert lpath == expectedp

        self.mox.VerifyAll()