Ejemplo n.º 1
0
def test_readwrite_unicode(tmppath):
    """Test read/write unicode."""
    if sys.getdefaultencoding() != "ascii":
        # Python 2 only problem
        raise AssertionError(
            "Default system encoding is not ascii. This is likely due to some"
            " imported module changing it using sys.setdefaultencoding."
        )

    fd = get_tsta_file(tmppath)
    fb = get_copy_file(fd)
    fp, dummy = fd["full_path"], fd["contents"]
    fp2 = fb["full_path"]

    unicodestr = u"æøå"

    pfile = open(fp2, "w")
    xfile = XRootDPyFile(mkurl(fp), "w")
    pytest.raises(UnicodeEncodeError, pfile.write, unicodestr)
    pytest.raises(UnicodeEncodeError, xfile.write, unicodestr)
    xfile.close()

    xfile = XRootDPyFile(mkurl(fp), "w+", encoding="utf-8")
    xfile.write(unicodestr)
    xfile.flush()
    xfile.seek(0)
    assert unicodestr.encode("utf8") == xfile.read()
    xfile.close()

    xfile = XRootDPyFile(mkurl(fp), "w+", errors="ignore")
    xfile.write(unicodestr)
    xfile.flush()
    xfile.seek(0)
    assert unicodestr.encode("ascii", "ignore") == xfile.read()
    xfile.close()
Ejemplo n.º 2
0
def test_truncate5(tmppath):
    """Test truncate() (no arg)."""
    fd = get_tsta_file(tmppath)
    fb = get_copy_file(fd)
    fp, fc = fd['full_path'], fd['contents']
    fp2 = fb['full_path']

    xfa = XRootDPyFile(mkurl(fp), 'r+')
    xfb = XRootDPyFile(mkurl(fp2), 'r+')

    acnts = xfa.read()
    assert acnts == xfb.read()

    # internal pointer starts at 0 in all 'r' modes.
    xtell = xfa.tell()
    assert xfa.tell() == xfb.tell()
    # f.truncate() and f.truncate(self.tell()) should be equivalent
    xfa.truncate(), xfb.truncate(xfb.tell())
    assert xfa.size == xfb.size
    assert xfa.tell() == xtell
    assert xfb.tell() == xtell
    assert xfb.read() == u''
    assert xfa.read() == u''

    xfa.seek(0), xfb.seek(0)
    are = xfa.read()
    assert are == fc
    assert are == xfb.read()
Ejemplo n.º 3
0
def test__assert_mode(tmppath):
    """Tests for _assert_mode"""
    fd = get_tsta_file(tmppath)
    full_path, fc = fd["full_path"], fd["contents"]
    mode = "r"
    xfile = XRootDPyFile(mkurl(full_path), mode)

    assert xfile.mode == mode
    assert xfile._assert_mode(mode)
    delattr(xfile, "mode")
    pytest.raises(AttributeError, xfile._assert_mode, mode)

    xfile.close()
    xfile = XRootDPyFile(mkurl(full_path), "r")
    assert xfile._assert_mode("r")
    pytest.raises(IOError, xfile._assert_mode, "w")

    xfile.close()
    xfile = XRootDPyFile(mkurl(full_path), "w-")
    assert xfile._assert_mode("w-")
    pytest.raises(IOError, xfile._assert_mode, "r")

    xfile.close()
    xfile = XRootDPyFile(mkurl(full_path), "a")
    assert xfile._assert_mode("w")
    pytest.raises(IOError, xfile._assert_mode, "r")
Ejemplo n.º 4
0
def test_init_append(tmppath):
    """Test for files opened 'a'"""
    fd = get_tsta_file(tmppath)
    fp, fc = fd['full_path'], fd['contents']
    xfile = XRootDPyFile(mkurl(fp), 'a')
    assert xfile.mode == 'a'
    pytest.raises(IOError, xfile.read)
    assert xfile.tell() == len(fc)

    # Seeking is allowed, but writes still go on the end.
    xfile.seek(0)
    assert xfile.tell() == 0
    newcont = u'butterflies'
    xfile.write(newcont)
    assert xfile.tell() == len(fc) + len(newcont)
    # Can't read in this mode.
    xfile.close()
    xfile = XRootDPyFile(mkurl(fp), 'r')
    assert xfile.read() == fc + newcont

    xfile.close()
    xfile = XRootDPyFile(mkurl(fp), 'a')
    xfile.write(fc)
    xfile.seek(0)
    pytest.raises(IOError, xfile.read)
Ejemplo n.º 5
0
def test_init_append(tmppath):
    """Test for files opened 'a'"""
    fd = get_tsta_file(tmppath)
    fp, fc = fd['full_path'], fd['contents']
    xfile = XRootDPyFile(mkurl(fp), 'a')
    assert xfile.mode == 'a'
    pytest.raises(IOError, xfile.read)
    assert xfile.tell() == len(fc)

    # Seeking is allowed, but writes still go on the end.
    xfile.seek(0)
    assert xfile.tell() == 0
    newcont = u'butterflies'
    xfile.write(newcont)
    assert xfile.tell() == len(fc) + len(newcont)
    # Can't read in this mode.
    xfile.close()
    xfile = XRootDPyFile(mkurl(fp), 'r')
    assert xfile.read() == fc + newcont

    xfile.close()
    xfile = XRootDPyFile(mkurl(fp), 'a')
    xfile.write(fc)
    xfile.seek(0)
    pytest.raises(IOError, xfile.read)
Ejemplo n.º 6
0
def test_write(tmppath):
    """Test write()."""
    # With a new file.
    xfile = XRootDPyFile(mkurl(join(tmppath, 'data/nuts')), 'w+')
    assert xfile.size == 0
    conts = xfile.read()
    assert not conts

    nconts = 'Write.'
    xfile.write(nconts)
    assert xfile.tell() == len(nconts)
    assert not xfile.closed
    xfile.seek(0)
    assert xfile.size == len(nconts)
    assert xfile.read() == nconts.encode()
    xfile.close()

    # Verify persistence after closing.
    xfile = XRootDPyFile(mkurl(join(tmppath, 'data/nuts')), 'r+')
    assert xfile.size == len(nconts)
    assert xfile.read() == nconts.encode()

    # Seek(x>0) followed by a write
    nc2 = 'hello'
    cntr = len(nconts) // 2
    xfile.seek(cntr)
    xfile.write(nc2)
    assert xfile.tell() == len(nc2) + cntr
    xfile.seek(0)
    expected = nconts[:cntr] + nc2
    assert xfile.read() == expected.encode()
    xfile.close()

    # Seek(x>0) followed by a write of len < size-x
    fd = get_tsta_file(tmppath)
    fp, fc = fd['full_path'], fd['contents']
    xfile = XRootDPyFile(mkurl(fp), 'r+')
    assert xfile.read() == fc.encode()
    xfile.seek(2)
    nc = 'yo'
    xfile.write(nc)
    assert xfile.tell() == len(nc) + 2
    assert xfile.read() == fc[2 + len(nc):].encode()

    # run w/ flushing == true
    xfile.write('', True)

    # Mock an error, yayy!
    fake_status = {
        "status": 3,
        "code": 0,
        "ok": False,
        "errno": errno.EREMOTE,
        "error": True,
        "message": '[FATAL] Remote I/O Error',
        "fatal": True,
        "shellcode": 51
    }
    xfile._file.write = Mock(return_value=(XRootDStatus(fake_status), None))
    pytest.raises(IOError, xfile.write, '')
Ejemplo n.º 7
0
def test_flush(tmppath):
    """Tests for flush()"""
    # Mostly it just ensures calling it doesn't crash the program.
    fd = get_tsta_file(tmppath)
    full_path, fc = fd['full_path'], fd['contents']
    xfile = XRootDPyFile(mkurl(full_path), 'w')

    writestr = 'whut'

    xfile.flush()
    xfile.seek(0, SEEK_END)
    xfile.write(writestr)
    xfile.flush()
    xfile.close()

    xfile = XRootDPyFile(mkurl(full_path), 'r')
    assert xfile.read() == writestr

    # Fake/mock an error response
    fake_status = {
        "status": 3,
        "code": 0,
        "ok": False,
        "errno": errno.EREMOTE,
        "error": True,
        "message": '[FATAL] Remote I/O Error',
        "fatal": True,
        "shellcode": 51
    }
    # Assign mock return value to the file's sync() function
    # (which is called by flush())
    xfile._file.sync = Mock(return_value=(XRootDStatus(fake_status), None))
    pytest.raises(IOError, xfile.flush)
Ejemplo n.º 8
0
def test_write_binary(tmppath):
    """Tests for writing binary data to file."""
    fd = get_bin_testfile(tmppath)
    fp, fc = fd['full_path'], fd['contents']

    # Test w/ confirmed binary data read from a binary file
    xf_new = XRootDPyFile(mkurl(join(tmppath, 'data/tmp_bin')), 'wb+')
    xf_new.write(fc), xf_new.seek(0)
    assert xf_new.read() == fc

    xf_new.close()
    # Verify persistence.
    xf_new = XRootDPyFile(mkurl(join(tmppath, 'data/tmp_bin')), 'r+')
    assert xf_new.read() == fc

    # Test truncate
    xf_new.truncate()
    xf_new.seek(0)
    assert xf_new.read() == fc
    xf_new.close()

    # Test with bytearray
    xf_new = XRootDPyFile(mkurl(join(tmppath, 'data/tmp_bin')), 'wb+')
    barr = bytearray(range(0, 5))
    xf_new.write(barr), xf_new.seek(0)
    assert xf_new.read() == barr
    xf_new.close()

    # Verify persistence.
    xf_new = XRootDPyFile(mkurl(join(tmppath, 'data/tmp_bin')), 'r')
    assert xf_new.read() == barr
    xf_new.close()
Ejemplo n.º 9
0
def test_readwrite_unicode(tmppath):
    """Test read/write unicode."""
    if sys.getdefaultencoding() != 'ascii':
        # Python 2 only problem
        raise AssertionError(
            "Default system encoding is not ascii. This is likely due to some"
            " imported module changing it using sys.setdefaultencoding."
        )

    fd = get_tsta_file(tmppath)
    fb = get_copy_file(fd)
    fp, dummy = fd['full_path'], fd['contents']
    fp2 = fb['full_path']

    unicodestr = u"æøå"

    pfile = open(fp2, 'w')
    xfile = XRootDPyFile(mkurl(fp), 'w')
    pytest.raises(UnicodeEncodeError, pfile.write, unicodestr)
    pytest.raises(UnicodeEncodeError, xfile.write, unicodestr)
    xfile.close()

    xfile = XRootDPyFile(mkurl(fp), 'w+', encoding='utf-8')
    xfile.write(unicodestr)
    xfile.flush()
    xfile.seek(0)
    assert unicodestr.encode('utf8') == xfile.read()
    xfile.close()

    xfile = XRootDPyFile(mkurl(fp), 'w+', errors='ignore')
    xfile.write(unicodestr)
    xfile.flush()
    xfile.seek(0)
    assert unicodestr.encode('ascii', 'ignore') == xfile.read()
    xfile.close()
Ejemplo n.º 10
0
def test_flush(tmppath):
    """Tests for flush()"""
    # Mostly it just ensures calling it doesn't crash the program.
    fd = get_tsta_file(tmppath)
    full_path, fc = fd['full_path'], fd['contents']
    xfile = XRootDPyFile(mkurl(full_path), 'w')

    writestr = 'whut'

    xfile.flush()
    xfile.seek(0, SEEK_END)
    xfile.write(writestr)
    xfile.flush()
    xfile.close()

    xfile = XRootDPyFile(mkurl(full_path), 'r')
    assert xfile.read() == writestr

    # Fake/mock an error response
    fake_status = {
        "status": 3,
        "code": 0,
        "ok": False,
        "errno": errno.EREMOTE,
        "error": True,
        "message": '[FATAL] Remote I/O Error',
        "fatal": True,
        "shellcode": 51
    }
    # Assign mock return value to the file's sync() function
    # (which is called by flush())
    xfile._file.sync = Mock(return_value=(XRootDStatus(fake_status), None))
    pytest.raises(IOError, xfile.flush)
Ejemplo n.º 11
0
def test_iterator_buffering(tmppath):
    "Test file iteration."
    f = "data/multiline.txt"
    xfile = XRootDPyFile(mkurl(join(tmppath, f)), 'r')
    assert len(list(iter(xfile))) == len(open(join(tmppath, f)).readlines())
    xfile = XRootDPyFile(mkurl(join(tmppath, f)), 'r', buffering=10)
    assert len(list(iter(xfile))) == int(math.ceil(xfile.size / 10.0))
Ejemplo n.º 12
0
def test_readwrite_unicode(tmppath):
    """Test read/write unicode."""
    if sys.getdefaultencoding() != 'ascii':
        # Python 2 only problem
        raise AssertionError(
            "Default system encoding is not ascii. This is likely due to some"
            " imported module changing it using sys.setdefaultencoding."
        )

    fd = get_tsta_file(tmppath)
    fb = get_copy_file(fd)
    fp, dummy = fd['full_path'], fd['contents']
    fp2 = fb['full_path']

    unicodestr = u"æøå"

    pfile = open(fp2, 'w')
    xfile = XRootDPyFile(mkurl(fp), 'w')
    pytest.raises(UnicodeEncodeError, pfile.write, unicodestr)
    pytest.raises(UnicodeEncodeError, xfile.write, unicodestr)
    xfile.close()

    xfile = XRootDPyFile(mkurl(fp), 'w+', encoding='utf-8')
    xfile.write(unicodestr)
    xfile.flush()
    xfile.seek(0)
    assert unicodestr.encode('utf8') == xfile.read()
    xfile.close()

    xfile = XRootDPyFile(mkurl(fp), 'w+', errors='ignore')
    xfile.write(unicodestr)
    xfile.flush()
    xfile.seek(0)
    assert unicodestr.encode('ascii', 'ignore') == xfile.read()
    xfile.close()
Ejemplo n.º 13
0
def test_write_binary(tmppath):
    """Tests for writing binary data to file."""
    fd = get_bin_testfile(tmppath)
    fp, fc = fd['full_path'], fd['contents']

    # Test w/ confirmed binary data read from a binary file
    xf_new = XRootDPyFile(mkurl(join(tmppath, 'data/tmp_bin')), 'wb+')
    xf_new.write(fc), xf_new.seek(0)
    assert xf_new.read() == fc

    xf_new.close()
    # Verify persistence.
    xf_new = XRootDPyFile(mkurl(join(tmppath, 'data/tmp_bin')), 'r+')
    assert xf_new.read() == fc

    # Test truncate
    xf_new.truncate()
    xf_new.seek(0)
    assert xf_new.read() == fc
    xf_new.close()

    # Test with bytearray
    xf_new = XRootDPyFile(mkurl(join(tmppath, 'data/tmp_bin')), 'wb+')
    barr = bytearray(range(0, 5))
    xf_new.write(barr), xf_new.seek(0)
    assert xf_new.read() == barr
    xf_new.close()

    # Verify persistence.
    xf_new = XRootDPyFile(mkurl(join(tmppath, 'data/tmp_bin')), 'r')
    assert xf_new.read() == barr
    xf_new.close()
Ejemplo n.º 14
0
def test_truncate5(tmppath):
    """Test truncate() (no arg)."""
    fd = get_tsta_file(tmppath)
    fb = get_copy_file(fd)
    fp, fc = fd['full_path'], fd['contents']
    fp2 = fb['full_path']

    xfa = XRootDPyFile(mkurl(fp), 'r+')
    xfb = XRootDPyFile(mkurl(fp2), 'r+')

    acnts = xfa.read()
    assert acnts == xfb.read()

    # internal pointer starts at 0 in all 'r' modes.
    xtell = xfa.tell()
    assert xfa.tell() == xfb.tell()
    # f.truncate() and f.truncate(self.tell()) should be equivalent
    xfa.truncate(), xfb.truncate(xfb.tell())
    assert xfa.size == xfb.size
    assert xfa.tell() == xtell
    assert xfb.tell() == xtell
    assert xfb.read() == u''
    assert xfa.read() == u''

    xfa.seek(0), xfb.seek(0)
    are = xfa.read()
    assert are == fc
    assert are == xfb.read()
Ejemplo n.º 15
0
def test_iterator_buffering(tmppath):
    "Test file iteration."
    f = "data/multiline.txt"
    xfile = XRootDPyFile(mkurl(join(tmppath, f)), 'r')
    assert len(list(iter(xfile))) == len(open(join(tmppath, f)).readlines())
    xfile = XRootDPyFile(mkurl(join(tmppath, f)), 'r', buffering=10)
    assert len(list(iter(xfile))) == int(math.ceil(xfile.size / 10.0))
Ejemplo n.º 16
0
def test_truncate1(tmppath):
    """Test truncate(0)."""
    fd = get_tsta_file(tmppath)
    full_path, fc = fd['full_path'], fd['contents']
    xfile = XRootDPyFile(mkurl(full_path), 'r+')
    # r+ opens for r/w, and won't truncate the file automatically.
    assert xfile.read() == fc
    assert xfile.tell() == len(fc)
    xfile.seek(0)  # Reset ipp.
    assert xfile.tell() == 0

    # Truncate it to size 0.
    xfile.truncate(0)
    assert xfile.size == 0
    assert xfile.tell() == 0
    assert xfile.read() == ''
    assert xfile.tell() == 0
    xfile.close()

    # Re-open same file.
    xfile = XRootDPyFile(mkurl(full_path), 'r+')
    assert xfile.size == 0
    assert xfile.read() == ''

    # Truncate it again!
    xfile.truncate(0)
    assert xfile.size == 0
    assert xfile.read() == ''

    # Truncate it twice.
    xfile.truncate(0)
    assert xfile.size == 0
    assert xfile.read() == ''

    # Truncate to 1.
    xfile.truncate(1)
    assert xfile.tell() == 0
    assert xfile.size == 1
    xfile.seek(0)
    assert xfile.read() == '\x00'
    assert xfile.tell() == 1
    xfile.close()

    xfile = XRootDPyFile(mkurl(full_path), 'r+')
    assert xfile.size == 1
    assert xfile.read() == '\x00'

    # Mock it.
    fake_status = {
        "status": 3,
        "code": 0,
        "ok": False,
        "errno": errno.EREMOTE,
        "error": True,
        "message": '[FATAL] Remote I/O Error',
        "fatal": True,
        "shellcode": 51
    }
    xfile._file.truncate = Mock(return_value=(XRootDStatus(fake_status), None))
    pytest.raises(IOError, xfile.truncate, 0)
Ejemplo n.º 17
0
def test_truncate1(tmppath):
    """Test truncate(0)."""
    fd = get_tsta_file(tmppath)
    full_path, fc = fd['full_path'], fd['contents']
    xfile = XRootDPyFile(mkurl(full_path), 'r+')
    # r+ opens for r/w, and won't truncate the file automatically.
    assert xfile.read() == fc
    assert xfile.tell() == len(fc)
    xfile.seek(0)  # Reset ipp.
    assert xfile.tell() == 0

    # Truncate it to size 0.
    xfile.truncate(0)
    assert xfile.size == 0
    assert xfile.tell() == 0
    assert xfile.read() == ''
    assert xfile.tell() == 0
    xfile.close()

    # Re-open same file.
    xfile = XRootDPyFile(mkurl(full_path), 'r+')
    assert xfile.size == 0
    assert xfile.read() == ''

    # Truncate it again!
    xfile.truncate(0)
    assert xfile.size == 0
    assert xfile.read() == ''

    # Truncate it twice.
    xfile.truncate(0)
    assert xfile.size == 0
    assert xfile.read() == ''

    # Truncate to 1.
    xfile.truncate(1)
    assert xfile.tell() == 0
    assert xfile.size == 1
    xfile.seek(0)
    assert xfile.read() == '\x00'
    assert xfile.tell() == 1
    xfile.close()

    xfile = XRootDPyFile(mkurl(full_path), 'r+')
    assert xfile.size == 1
    assert xfile.read() == '\x00'

    # Mock it.
    fake_status = {
        "status": 3,
        "code": 0,
        "ok": False,
        "errno": errno.EREMOTE,
        "error": True,
        "message": '[FATAL] Remote I/O Error',
        "fatal": True,
        "shellcode": 51
    }
    xfile._file.truncate = Mock(return_value=(XRootDStatus(fake_status), None))
    pytest.raises(IOError, xfile.truncate, 0)
Ejemplo n.º 18
0
def test_writelines(tmppath):
    """Test writelines()."""
    xfile = XRootDPyFile(mkurl(join(tmppath, "data/multiline.txt")), 'r')
    yfile = XRootDPyFile(mkurl(join(tmppath, "data/newfile.txt")), 'w+')
    yfile.writelines(xfile.xreadlines())
    xfile.seek(0), yfile.seek(0)
    assert xfile.readlines() == yfile.readlines()
Ejemplo n.º 19
0
def test__assert_mode(tmppath):
    """Tests for _assert_mode"""
    fd = get_tsta_file(tmppath)
    full_path, fc = fd['full_path'], fd['contents']
    mode = 'r'
    xfile = XRootDPyFile(mkurl(full_path), mode)

    assert xfile.mode == mode
    assert xfile._assert_mode(mode)
    delattr(xfile, 'mode')
    pytest.raises(AttributeError, xfile._assert_mode, mode)

    xfile.close()
    xfile = XRootDPyFile(mkurl(full_path), 'r')
    assert xfile._assert_mode('r')
    pytest.raises(IOError, xfile._assert_mode, 'w')

    xfile.close()
    xfile = XRootDPyFile(mkurl(full_path), 'w-')
    assert xfile._assert_mode('w-')
    pytest.raises(IOError, xfile._assert_mode, 'r')

    xfile.close()
    xfile = XRootDPyFile(mkurl(full_path), 'a')
    assert xfile._assert_mode('w')
    pytest.raises(IOError, xfile._assert_mode, 'r')
Ejemplo n.º 20
0
def test__assert_mode(tmppath):
    """Tests for _assert_mode"""
    fd = get_tsta_file(tmppath)
    full_path, fc = fd['full_path'], fd['contents']
    mode = 'r'
    xfile = XRootDPyFile(mkurl(full_path), mode)

    assert xfile.mode == mode
    assert xfile._assert_mode(mode)
    delattr(xfile, 'mode')
    pytest.raises(AttributeError, xfile._assert_mode, mode)

    xfile.close()
    xfile = XRootDPyFile(mkurl(full_path), 'r')
    assert xfile._assert_mode('r')
    pytest.raises(IOError, xfile._assert_mode, 'w')

    xfile.close()
    xfile = XRootDPyFile(mkurl(full_path), 'w-')
    assert xfile._assert_mode('w-')
    pytest.raises(IOError, xfile._assert_mode, 'r')

    xfile.close()
    xfile = XRootDPyFile(mkurl(full_path), 'a')
    assert xfile._assert_mode('w')
    pytest.raises(IOError, xfile._assert_mode, 'r')
Ejemplo n.º 21
0
def test_writelines(tmppath):
    """Test writelines()."""
    xfile = XRootDPyFile(mkurl(join(tmppath, "data/multiline.txt")), 'r')
    yfile = XRootDPyFile(mkurl(join(tmppath, "data/newfile.txt")), 'w+')
    yfile.writelines(xfile.xreadlines())
    xfile.seek(0), yfile.seek(0)
    assert xfile.readlines() == yfile.readlines()
Ejemplo n.º 22
0
def test_seekable(tmppath):
    """Test seekable."""
    assert XRootDPyFile(mkurl(join(tmppath, "data/testa.txt")), "r-").seekable() is False
    assert XRootDPyFile(mkurl(join(tmppath, "data/testa.txt")), "r").seekable() is True
    assert XRootDPyFile(mkurl(join(tmppath, "data/testa.txt")), "w").seekable() is True
    assert XRootDPyFile(mkurl(join(tmppath, "data/testa.txt")), "w-").seekable() is False
    assert XRootDPyFile(mkurl(join(tmppath, "data/testa.txt")), "r+").seekable() is True
Ejemplo n.º 23
0
def test_write(tmppath):
    """Test write()."""
    # With a new file.
    xfile = XRootDPyFile(mkurl(join(tmppath, 'data/nuts')), 'w+')
    assert xfile.size == 0
    conts = xfile.read()
    assert not conts

    nconts = 'Write.'
    xfile.write(nconts)
    assert xfile.tell() == len(nconts)
    assert not xfile.closed
    xfile.seek(0)
    assert xfile.size == len(nconts)
    assert xfile.read() == nconts
    xfile.close()

    # Verify persistence after closing.
    xfile = XRootDPyFile(mkurl(join(tmppath, 'data/nuts')), 'r+')
    assert xfile.size == len(nconts)
    assert xfile.read() == nconts

    # Seek(x>0) followed by a write
    nc2 = 'hello'
    cntr = len(nconts)//2
    xfile.seek(cntr)
    xfile.write(nc2)
    assert xfile.tell() == len(nc2) + cntr
    xfile.seek(0)
    assert xfile.read() == nconts[:cntr] + nc2
    xfile.close()

    # Seek(x>0) followed by a write of len < size-x
    fd = get_tsta_file(tmppath)
    fp, fc = fd['full_path'], fd['contents']
    xfile = XRootDPyFile(mkurl(fp), 'r+')
    assert xfile.read() == fc
    xfile.seek(2)
    nc = 'yo'
    xfile.write(nc)
    assert xfile.tell() == len(nc) + 2
    assert xfile.read() == fc[2+len(nc):]

    # run w/ flushing == true
    xfile.write('', True)

    # Mock an error, yayy!
    fake_status = {
        "status": 3,
        "code": 0,
        "ok": False,
        "errno": errno.EREMOTE,
        "error": True,
        "message": '[FATAL] Remote I/O Error',
        "fatal": True,
        "shellcode": 51
    }
    xfile._file.write = Mock(return_value=(XRootDStatus(fake_status), None))
    pytest.raises(IOError, xfile.write, '')
Ejemplo n.º 24
0
def test_init_notimplemented(tmppath):
    """Tests that specifying not-implemented args to XRDFile's constructor
       results in an error."""
    fd = get_tsta_file(tmppath)
    fp, fc = fd["full_path"], fd["contents"]

    pytest.raises(UnsupportedError, XRootDPyFile, mkurl(fp), "rb", buffering=1)
    pytest.raises(NotImplementedError, XRootDPyFile, mkurl(fp), line_buffering="")
Ejemplo n.º 25
0
def test_init_notimplemented(tmppath):
    """Tests that specifying not-implemented args to XRDFile's constructor
       results in an error."""
    fd = get_tsta_file(tmppath)
    fp, fc = fd['full_path'], fd['contents']

    pytest.raises(UnsupportedError, XRootDPyFile, mkurl(fp), 'rb',
                  buffering=1)
    pytest.raises(NotImplementedError, XRootDPyFile, mkurl(fp),
                  line_buffering='')
Ejemplo n.º 26
0
def test_truncate4(tmppath):
    """Verifies that truncate() raises errors on non-truncatable files."""
    fd = get_mltl_file(tmppath)
    full_path, fc = fd['full_path'], fd['contents']

    xfile = XRootDPyFile(mkurl(full_path), 'r')
    pytest.raises(IOError, xfile.truncate, 0)

    xfile.close()
    xfile = XRootDPyFile(mkurl(full_path), 'w-')
    pytest.raises(IOError, xfile.truncate, 0)
Ejemplo n.º 27
0
def test_truncate4(tmppath):
    """Verifies that truncate() raises errors on non-truncatable files."""
    fd = get_mltl_file(tmppath)
    full_path, fc = fd['full_path'], fd['contents']

    xfile = XRootDPyFile(mkurl(full_path), 'r')
    pytest.raises(IOError, xfile.truncate, 0)

    xfile.close()
    xfile = XRootDPyFile(mkurl(full_path), 'w-')
    pytest.raises(IOError, xfile.truncate, 0)
Ejemplo n.º 28
0
def test_getpathurl(tmppath):
    """Test getpathurl."""
    fs = XRootDFS(mkurl(tmppath))
    assert fs.getpathurl("data/testa.txt") == "root://localhost/{0}/{1}".format(tmppath, "data/testa.txt")

    fs = XRootDFS(mkurl(tmppath), query={"xrd.wantprot": "krb5"})

    assert fs.getpathurl("data/testa.txt") == "root://localhost/{0}/{1}".format(tmppath, "data/testa.txt")

    assert fs.getpathurl(
        "data/testa.txt", with_querystring=True
    ) == "root://localhost/{0}/{1}?xrd.wantprot=krb5".format(tmppath, "data/testa.txt")
Ejemplo n.º 29
0
def test_seekable(tmppath):
    """Test seekable."""
    assert XRootDPyFile(
        mkurl(join(tmppath, "data/testa.txt")), 'r-').seekable() is False
    assert XRootDPyFile(
        mkurl(join(tmppath, "data/testa.txt")), 'r').seekable() is True
    assert XRootDPyFile(
        mkurl(join(tmppath, "data/testa.txt")), 'w').seekable() is True
    assert XRootDPyFile(
        mkurl(join(tmppath, "data/testa.txt")), 'w-').seekable() is False
    assert XRootDPyFile(
        mkurl(join(tmppath, "data/testa.txt")), 'r+').seekable() is True
Ejemplo n.º 30
0
def test_seekable(tmppath):
    """Test seekable."""
    assert XRootDPyFile(
        mkurl(join(tmppath, "data/testa.txt")), 'r-').seekable() is False
    assert XRootDPyFile(
        mkurl(join(tmppath, "data/testa.txt")), 'r').seekable() is True
    assert XRootDPyFile(
        mkurl(join(tmppath, "data/testa.txt")), 'w').seekable() is True
    assert XRootDPyFile(
        mkurl(join(tmppath, "data/testa.txt")), 'w-').seekable() is False
    assert XRootDPyFile(
        mkurl(join(tmppath, "data/testa.txt")), 'r+').seekable() is True
Ejemplo n.º 31
0
def test_init_readmode_basic(tmppath):
    # Non-existing file causes what?
    # Resource not found error.
    fn, fp, fc = "nope", "data/", ""
    full_path = join(tmppath, fp, fn)
    pytest.raises(ResourceNotFoundError, XRootDPyFile, mkurl(full_path), mode="r")

    # Existing file can be read?
    fd = get_tsta_file(tmppath)
    full_path, fc = fd["full_path"], fd["contents"]
    xfile = XRootDPyFile(mkurl(full_path), mode="r")
    assert xfile
    assert xfile.read() == fc
Ejemplo n.º 32
0
def test_init_readmode_basic(tmppath):
    # Non-existing file causes what?
    # Resource not found error.
    fn, fp, fc = 'nope', 'data/', ''
    full_path = join(tmppath, fp, fn)
    pytest.raises(ResourceNotFoundError, XRootDPyFile, mkurl(full_path),
                  mode='r')

    # Existing file can be read?
    fd = get_tsta_file(tmppath)
    full_path, fc = fd['full_path'], fd['contents']
    xfile = XRootDPyFile(mkurl(full_path), mode='r')
    assert xfile
    assert xfile.read() == fc
Ejemplo n.º 33
0
def test_getpathurl(tmppath):
    """Test getpathurl."""
    fs = XRootDPyFS(mkurl(tmppath))
    assert fs.getpathurl("data/testa.txt") == \
        "root://localhost/{0}/{1}".format(tmppath, "data/testa.txt")

    fs = XRootDPyFS(mkurl(tmppath), query={'xrd.wantprot': 'krb5'})

    assert fs.getpathurl("data/testa.txt") == \
        "root://localhost/{0}/{1}".format(tmppath, "data/testa.txt")

    assert fs.getpathurl("data/testa.txt", with_querystring=True) == \
        "root://localhost/{0}/{1}?xrd.wantprot=krb5".format(
            tmppath, "data/testa.txt")
Ejemplo n.º 34
0
def test_init_readmode_basic(tmppath):
    # Non-existing file causes what?
    # Resource not found error.
    fn, fp, fc = 'nope', 'data/', ''
    full_path = join(tmppath, fp, fn)
    pytest.raises(ResourceNotFoundError, XRootDPyFile, mkurl(full_path),
                  mode='r')

    # Existing file can be read?
    fd = get_tsta_file(tmppath)
    full_path, fc = fd['full_path'], fd['contents']
    xfile = XRootDPyFile(mkurl(full_path), mode='r')
    assert xfile
    assert xfile.read() == fc
Ejemplo n.º 35
0
def test_init_newline(tmppath):
    """Tests fs.open() with specified newline parameter."""
    fd = get_tsta_file(tmppath)
    fp, fc = fd["full_path"], fd["contents"]

    xfile = XRootDPyFile(mkurl(fp))
    assert xfile._newline == "\n"
    xfile.close()

    xfile = XRootDPyFile(mkurl(fp), newline="\n")
    assert xfile._newline == "\n"
    xfile.close()

    pytest.raises(UnsupportedError, XRootDPyFile, mkurl(fp), mode="r", newline="what")
Ejemplo n.º 36
0
def test_init_newline(tmppath):
    """Tests fs.open() with specified newline parameter."""
    fd = get_tsta_file(tmppath)
    fp, fc = fd['full_path'], fd['contents']

    xfile = XRootDPyFile(mkurl(fp))
    assert xfile._newline == '\n'
    xfile.close()

    xfile = XRootDPyFile(mkurl(fp), newline='\n')
    assert xfile._newline == '\n'
    xfile.close()

    pytest.raises(UnsupportedError, XRootDPyFile, mkurl(fp), mode='r',
                  newline='what')
Ejemplo n.º 37
0
def test_init_newline(tmppath):
    """Tests fs.open() with specified newline parameter."""
    fd = get_tsta_file(tmppath)
    fp, fc = fd['full_path'], fd['contents']

    xfile = XRootDPyFile(mkurl(fp))
    assert xfile._newline == '\n'
    xfile.close()

    xfile = XRootDPyFile(mkurl(fp), newline='\n')
    assert xfile._newline == '\n'
    xfile.close()

    pytest.raises(UnsupportedError, XRootDPyFile, mkurl(fp), mode='r',
                  newline='what')
Ejemplo n.º 38
0
def test_copy_good(tmppath):
    """Test move file."""
    fs = XRootDFS(mkurl(tmppath))

    src_exists = "data/testa.txt"
    dst_exists = "data/multiline.txt"
    dst_new = "data/ok.txt"
    dst_folder_exists = "data/bfolder/"
    dst_folder_new = "data/anothernewfolder/"
    content = _get_content(fs, src_exists)

    assert fs.exists(dst_exists)
    assert not fs.exists(dst_new)
    assert fs.exists(dst_folder_exists)
    assert not fs.exists(dst_folder_new)

    fs.copy(src_exists, dst_new)
    assert fs.exists(src_exists) and fs.exists(dst_new)

    fs.copy(src_exists, dst_folder_new)
    assert fs.exists(src_exists) and fs.exists(dst_folder_new)

    fs.copy(src_exists, dst_exists, overwrite=True)
    assert fs.exists(src_exists) and fs.exists(dst_exists)
    assert content == _get_content(fs, dst_exists)

    fs.copy(src_exists, dst_folder_exists, overwrite=True)
    assert fs.exists(src_exists) and fs.exists(dst_folder_exists)
    assert content == _get_content(fs, dst_folder_exists)
Ejemplo n.º 39
0
def test_movedir_good(tmppath):
    """Test move file."""
    fs = XRootDFS(mkurl(tmppath))

    src_exists = "data/afolder/"
    dst_exists = "data/multiline.txt"
    dst_new = "data/ok.txt"
    dst_folder_exists = "data/bfolder/"
    dst_folder_new = "data/anothernewfolder/"

    assert fs.isdir(src_exists)
    assert fs.exists(dst_exists)
    assert not fs.exists(dst_new)
    assert fs.exists(dst_folder_exists)
    assert not fs.exists(dst_folder_new)

    fs.movedir(src_exists, dst_new)
    assert not fs.exists(src_exists) and fs.exists(dst_new)

    fs.movedir(dst_new, src_exists)
    fs.movedir(src_exists, dst_folder_new)
    assert not fs.exists(src_exists) and fs.exists(dst_folder_new)

    fs.movedir(dst_folder_new, src_exists)
    fs.movedir(src_exists, dst_exists, overwrite=True)
    assert not fs.exists(src_exists) and fs.exists(dst_exists)
    assert fs.isdir(dst_exists)

    fs.movedir(dst_exists, src_exists)
    fs.movedir(src_exists, dst_folder_exists, overwrite=True)
    assert not fs.exists(src_exists) and fs.exists(dst_folder_exists)
    assert fs.isdir(dst_folder_exists)
Ejemplo n.º 40
0
def test_checksum(tmppath):
    """Test checksum method."""
    fs = XRootDFS(mkurl(tmppath))

    # Local xrootd server does not support checksum operation
    pytest.raises(UnsupportedError, fs.xrd_checksum, "data/testa.txt")

    # Let's fake a success response
    fake_status = {
        "status": 0,
        "code": 0,
        "ok": True,
        "errno": 0,
        "error": False,
        "message": "[SUCCESS] ",
        "fatal": False,
        "shellcode": 0,
    }
    fs.xrd_client.query = Mock(return_value=(XRootDStatus(fake_status), "adler32 3836a69a\x00"))
    algo, val = fs.xrd_checksum("data/testa.txt")
    assert algo == "adler32" and val == "3836a69a"

    # Fake a bad response (e.g. on directory)
    fake_status = {
        "status": 1,
        "code": 400,
        "ok": False,
        "errno": 3011,
        "error": True,
        "message": "[ERROR] Server responded with an error: [3011] no such " "file or directory\n",
        "fatal": False,
        "shellcode": 54,
    }
    fs.xrd_client.query = Mock(return_value=(XRootDStatus(fake_status), None))
    pytest.raises(FSError, fs.xrd_checksum, "data/")
Ejemplo n.º 41
0
def test_getinfo(tmppath):
    """Test getinfo."""
    fs = XRootDPyFS(mkurl(tmppath))

    # Info for file
    f = "data/testa.txt"
    info = fs.getinfo(f)
    assert info['size'] == os.stat(join(tmppath, f)).st_size
    assert info['offline'] is False
    assert info['writable'] is True
    assert info['readable'] is True
    assert info['executable'] is False
    assert isinstance(info['created_time'], datetime)
    assert isinstance(info['modified_time'], datetime)
    assert isinstance(info['accessed_time'], datetime)

    # Info for directory
    f = "data/"
    info = fs.getinfo(f)
    assert info['size'] == os.stat(join(tmppath, f)).st_size
    assert info['offline'] is False
    assert info['writable'] is True
    assert info['readable'] is True
    assert info['executable'] is True
    assert isinstance(info['created_time'], datetime)
    assert isinstance(info['modified_time'], datetime)
    assert isinstance(info['accessed_time'], datetime)

    # Non existing path
    pytest.raises(ResourceNotFoundError, fs.getinfo, "invalidpath/")
Ejemplo n.º 42
0
def test_remove_dir_mock2(tmppath):
    """Test removedir."""
    fs = XRootDPyFS(mkurl(tmppath))

    status = XRootDStatus({
        "status": 3,
        "code": 101,
        "ok": False,
        "errno": 0,
        "error": True,
        "message": '[FATAL] Invalid address',
        "fatal": True,
        "shellcode": 51
    })

    def fail(f, fail_on):
        @wraps(f)
        def inner(path, **kwargs):
            if path == fail_on:
                return (status, None)
            return f(path, **kwargs)

        return inner

    fs.xrd_client.rmdir = fail(fs.xrd_client.rmdir, fs._p("data/bfolder/"))
    pytest.raises(ResourceError, fs.removedir, "data/", force=True)
Ejemplo n.º 43
0
def copydir_bad(tmppath, parallel):
    """Test copy directory."""
    fs = XRootDFS(mkurl(tmppath))

    src_exists = "data/testa.txt"
    src_new = "data/testb.txt"
    src_folder_exists = "data/afolder/"
    src_folder_new = "data/newfolder/"
    dst_exists = "data/multiline.txt"
    dst_new = "data/ok.txt"
    dst_folder_exists = "data/bfolder/"
    dst_folder_new = "data/anothernewfolder/"

    # Destination exists
    pytest.raises(DestinationExistsError, fs.copydir, src_folder_exists, dst_exists, parallel=parallel)
    pytest.raises(DestinationExistsError, fs.copydir, src_folder_exists, src_folder_exists, parallel=parallel)
    pytest.raises(DestinationExistsError, fs.copydir, src_folder_exists, dst_folder_exists, parallel=parallel)

    # Cannot move file
    pytest.raises(ResourceInvalidError, fs.copydir, src_exists, dst_new, parallel=parallel)
    pytest.raises(ResourceInvalidError, fs.copydir, src_exists, dst_folder_new, parallel=parallel)

    # Source doesn't exists
    pytest.raises(ResourceNotFoundError, fs.copydir, src_new, dst_exists, parallel=parallel)
    pytest.raises(ResourceNotFoundError, fs.copydir, src_new, dst_new, parallel=parallel)
    pytest.raises(ResourceNotFoundError, fs.copydir, src_new, dst_folder_exists, parallel=parallel)
    pytest.raises(ResourceNotFoundError, fs.copydir, src_new, dst_folder_new, parallel=parallel)

    pytest.raises(ResourceNotFoundError, fs.copydir, src_folder_new, dst_exists, parallel=parallel)
    pytest.raises(ResourceNotFoundError, fs.copydir, src_folder_new, dst_new, parallel=parallel)
    pytest.raises(ResourceNotFoundError, fs.copydir, src_folder_new, dst_folder_exists, parallel=parallel)
    pytest.raises(ResourceNotFoundError, fs.copydir, src_folder_new, dst_folder_new, parallel=parallel)
Ejemplo n.º 44
0
def test_read_existing(tmppath):
    """Test read() on an existing non-empty file."""
    fd = get_tsta_file(tmppath)
    full_path, fc = fd['full_path'], fd['contents']
    xfile = XRootDPyFile(mkurl(full_path))

    res = xfile.read()
    assert res == fc
    # After having read the entire file, the file pointer is at the
    # end of the file and consecutive reads return the empty string.
    assert xfile.read() == ''

    # reset ipp to start
    xfile.seek(0)
    assert xfile.read(1) == fc[0]
    assert xfile.read(2) == fc[1:3]
    overflow_read = xfile.read(len(fc))
    assert overflow_read == fc[3:]

    # Mock an error, yayy!
    fake_status = {
        "status": 3,
        "code": 0,
        "ok": False,
        "errno": errno.EREMOTE,
        "error": True,
        "message": '[FATAL] Remote I/O Error',
        "fatal": True,
        "shellcode": 51
    }
    xfile._file.read = Mock(return_value=(XRootDStatus(fake_status), None))
    pytest.raises(IOError, xfile.read)
Ejemplo n.º 45
0
def test_write_and_read(tmppath):
    """Tests that the XRootDPyFile behaves like a regular python file in w+."""
    fd = get_tsta_file(tmppath)
    fb = get_copy_file(fd)
    fp, fc = fd['full_path'], fd['contents']
    fp2 = fb['full_path']

    writestr = "Hello fair mare what fine stairs."
    seekpoint = len(writestr)//2
    # In 'w' (and variant modes) the file's contents are deleted upon opening.

    pfile = open(fp2, 'w+')
    xfile = XRootDPyFile(mkurl(fp), 'w+')

    assert xfile.tell() == pfile.tell()
    assert xfile.read() == pfile.read()
    assert xfile.tell() == pfile.tell()

    xfile.write(writestr), pfile.write(writestr)
    assert xfile.tell() == pfile.tell()
    assert xfile.read() == pfile.read()
    xfile.seek(0), pfile.seek(0)
    assert xfile.read() == pfile.read()
    assert xfile.tell() == pfile.tell()

    xfile.seek(seekpoint), pfile.seek(seekpoint)
    assert xfile.tell() == pfile.tell()
    assert xfile.read() == pfile.read()
    assert xfile.tell() == pfile.tell()
Ejemplo n.º 46
0
def test_read_and_write(tmppath):
    """Tests that the XRDFile behaves like a regular python file."""
    fd = get_tsta_file(tmppath)
    fb = get_copy_file(fd)
    fp, fc = fd['full_path'], fd['contents']
    fp2 = fb['full_path']

    seekpoint = len(fc)//2
    writestr = "Come what may in May this day says Ray all gay like Jay"

    pfile = open(fp2, 'r+')
    xfile = XRootDPyFile(mkurl(fp), 'r+')

    assert xfile.tell() == pfile.tell()
    assert xfile.read() == pfile.read()
    assert xfile.tell() == pfile.tell()

    xfile.seek(seekpoint), pfile.seek(seekpoint)
    assert xfile.tell() == pfile.tell()
    xfile.write(writestr), pfile.write(writestr)
    assert xfile.tell() == pfile.tell()
    assert xfile.read() == pfile.read()

    xfile.seek(0), pfile.seek(0)
    assert xfile.tell() == pfile.tell()
    assert xfile.read() == pfile.read()
Ejemplo n.º 47
0
def test_seek_args(tmppath):
    """Test seek() with a non-default whence argument."""
    fd = get_tsta_file(tmppath)
    fb = get_copy_file(fd)
    full_path, fc = fd['full_path'], fd['contents']

    xfile = XRootDPyFile(mkurl(full_path), 'r+')
    pfile = open(fb['full_path'], 'r+')

    xfile.truncate(3), pfile.truncate(3)
    xfile.seek(2, SEEK_END), pfile.seek(2, SEEK_END)
    assert xfile.tell() == pfile.tell()

    xfile.seek(3, SEEK_CUR), pfile.seek(3, SEEK_CUR)
    assert xfile.tell() == pfile.tell()

    xfile.seek(8, SEEK_SET), pfile.seek(8, SEEK_SET)
    assert xfile.tell() == pfile.tell()

    xfile.truncate(3), pfile.truncate(3)
    xfile.read(), pfile.read()
    assert xfile.tell() == pfile.tell()
    xfile.seek(8, SEEK_END), pfile.seek(8, SEEK_END)
    assert xfile.tell() == pfile.tell()

    xfile.seek(4, SEEK_CUR), pfile.seek(4, SEEK_CUR)
    assert xfile.tell() == pfile.tell()

    pytest.raises(NotImplementedError, xfile.seek, 0, 8)
Ejemplo n.º 48
0
def test_copy_bad(tmppath):
    """Test copy file."""
    fs = XRootDFS(mkurl(tmppath))

    src_exists = "data/testa.txt"
    src_new = "data/testb.txt"
    src_folder_exists = "data/afolder/"
    dst_exists = "data/multiline.txt"
    dst_new = "data/ok.txt"
    dst_folder_exists = "data/bfolder/"
    dst_folder_new = "data/anothernewfolder/"

    # Destination exists
    pytest.raises(DestinationExistsError, fs.copy, src_exists, dst_exists)
    pytest.raises(DestinationExistsError, fs.copy, src_exists, src_exists)
    pytest.raises(DestinationExistsError, fs.copy, src_exists, dst_folder_exists)

    # Cannot copy dir
    pytest.raises(ResourceInvalidError, fs.copy, src_folder_exists, dst_new)
    pytest.raises(ResourceInvalidError, fs.copy, src_folder_exists, dst_folder_new)

    # Source doesn't exists
    pytest.raises(ResourceNotFoundError, fs.copy, src_new, dst_exists)
    pytest.raises(ResourceNotFoundError, fs.copy, src_new, dst_new)
    pytest.raises(ResourceNotFoundError, fs.copy, src_new, dst_folder_exists)
    pytest.raises(ResourceNotFoundError, fs.copy, src_new, dst_folder_new)

    pytest.raises(ResourceNotFoundError, fs.copy, src_new, dst_exists)
    pytest.raises(ResourceNotFoundError, fs.copy, src_new, dst_new)
    pytest.raises(ResourceNotFoundError, fs.copy, src_new, dst_folder_exists)
    pytest.raises(ResourceNotFoundError, fs.copy, src_new, dst_folder_new)
Ejemplo n.º 49
0
def copydir_good(tmppath, parallel):
    """Test copy directory."""
    fs = XRootDFS(mkurl(tmppath))

    src_exists = "data/afolder/"
    dst_exists = "data/multiline.txt"
    dst_new = "data/ok.txt"
    dst_folder_exists = "data/bfolder/"
    dst_folder_new = "data/anothernewfolder/"

    assert fs.isdir(src_exists)
    assert fs.exists(dst_exists)
    assert not fs.exists(dst_new)
    assert fs.exists(dst_folder_exists)
    assert not fs.exists(dst_folder_new)

    fs.copydir(src_exists, dst_new, parallel=parallel)
    assert fs.exists(src_exists) and fs.exists(dst_new)

    fs.copydir(src_exists, dst_folder_new, parallel=parallel)
    assert fs.exists(src_exists) and fs.exists(dst_folder_new)

    fs.copydir(src_exists, dst_exists, overwrite=True, parallel=parallel)
    assert fs.exists(src_exists) and fs.exists(dst_exists)
    assert fs.isdir(dst_exists)

    fs.copydir(src_exists, dst_folder_exists, overwrite=True, parallel=parallel)
    assert fs.exists(src_exists) and fs.exists(dst_folder_exists)
    assert fs.isdir(dst_folder_exists)
Ejemplo n.º 50
0
def test_remove_dir_mock2(tmppath):
    """Test removedir."""
    fs = XRootDFS(mkurl(tmppath))

    status = XRootDStatus(
        {
            "status": 3,
            "code": 101,
            "ok": False,
            "errno": 0,
            "error": True,
            "message": "[FATAL] Invalid address",
            "fatal": True,
            "shellcode": 51,
        }
    )

    def fail(f, fail_on):
        @wraps(f)
        def inner(path, **kwargs):
            if path == fail_on:
                return (status, None)
            return f(path, **kwargs)

        return inner

    fs.xrd_client.rmdir = fail(fs.xrd_client.rmdir, fs._p("data/bfolder/"))
    pytest.raises(ResourceError, fs.removedir, "data/", force=True)
Ejemplo n.º 51
0
def test_makedir(tmppath):
    """Test makedir."""
    rooturl = mkurl(tmppath)

    # Dir in parent
    assert not XRootDPyFS(rooturl).exists("somedir")
    assert XRootDPyFS(rooturl).makedir("somedir")
    assert XRootDPyFS(rooturl).exists("somedir")
    assert exists(join(tmppath, "somedir"))

    # if the path is already a directory, and allow_recreate is False
    assert pytest.raises(DestinationExistsError,
                         XRootDPyFS(rooturl).makedir, "data")

    # allow_recreate
    assert XRootDPyFS(rooturl).makedir("data", allow_recreate=True)

    # if a containing directory is missing and recursive is False
    assert pytest.raises(ResourceNotFoundError,
                         XRootDPyFS(rooturl).makedir, "aa/bb/cc")

    # Recursive
    assert not XRootDPyFS(rooturl).exists("aa/bb/cc")
    assert XRootDPyFS(rooturl).makedir("aa/bb/cc", recursive=True)
    assert XRootDPyFS(rooturl).exists("aa/bb/cc")

    # if a path is an existing file
    assert pytest.raises(DestinationExistsError,
                         XRootDPyFS(rooturl).makedir, "data/testa.txt")
Ejemplo n.º 52
0
def test_getinfo(tmppath):
    """Test getinfo."""
    fs = XRootDFS(mkurl(tmppath))

    # Info for file
    f = "data/testa.txt"
    info = fs.getinfo(f)
    assert info["size"] == os.stat(join(tmppath, f)).st_size
    assert info["offline"] == False
    assert info["writable"] == True
    assert info["readable"] == True
    assert info["executable"] == False
    assert isinstance(info["created_time"], datetime)
    assert isinstance(info["modified_time"], datetime)
    assert isinstance(info["accessed_time"], datetime)

    # Info for directory
    f = "data/"
    info = fs.getinfo(f)
    assert info["size"] == os.stat(join(tmppath, f)).st_size
    assert info["offline"] == False
    assert info["writable"] == True
    assert info["readable"] == True
    assert info["executable"] == True
    assert isinstance(info["created_time"], datetime)
    assert isinstance(info["modified_time"], datetime)
    assert isinstance(info["accessed_time"], datetime)

    # Non existing path
    pytest.raises(ResourceNotFoundError, fs.getinfo, "invalidpath/")
Ejemplo n.º 53
0
def test_rename(tmppath):
    """Test rename."""
    fs = XRootDPyFS(mkurl(tmppath))

    pytest.raises(DestinationExistsError, fs.rename, "data/testa.txt",
                  "multiline.txt")
    pytest.raises(DestinationExistsError, fs.rename, "data/testa.txt",
                  "afolder/afile.txt")
    pytest.raises(DestinationExistsError, fs.rename, "data/afolder", "bfolder")
    pytest.raises(DestinationExistsError, fs.rename, "data/afolder",
                  "bfolder/bfile.txt")

    pytest.raises(ResourceNotFoundError, fs.rename, "data/invalid.txt",
                  "afolder/afile.txt")

    assert fs.exists("data/testa.txt") and not fs.exists("data/testb.txt")
    fs.rename("data/testa.txt", "testb.txt")
    assert fs.exists("data/testb.txt") and not fs.exists("data/testa.txt")

    assert fs.exists("data/afolder/") and not fs.exists("data/cfolder/")
    fs.rename("data/afolder/", "cfolder")
    assert fs.exists("data/cfolder") and not fs.exists("data/afolder")

    fs.rename("data/cfolder/", "a/b/c/test")
    assert fs.exists("data/a/b/c/test/")
Ejemplo n.º 54
0
def test_movedir_good(tmppath):
    """Test move file."""
    fs = XRootDPyFS(mkurl(tmppath))

    src_exists = "data/afolder/"
    dst_exists = "data/multiline.txt"
    dst_new = "data/ok.txt"
    dst_folder_exists = "data/bfolder/"
    dst_folder_new = "data/anothernewfolder/"

    assert fs.isdir(src_exists)
    assert fs.exists(dst_exists)
    assert not fs.exists(dst_new)
    assert fs.exists(dst_folder_exists)
    assert not fs.exists(dst_folder_new)

    fs.movedir(src_exists, dst_new)
    assert not fs.exists(src_exists) and fs.exists(dst_new)

    fs.movedir(dst_new, src_exists)
    fs.movedir(src_exists, dst_folder_new)
    assert not fs.exists(src_exists) and fs.exists(dst_folder_new)

    fs.movedir(dst_folder_new, src_exists)
    fs.movedir(src_exists, dst_exists, overwrite=True)
    assert not fs.exists(src_exists) and fs.exists(dst_exists)
    assert fs.isdir(dst_exists)

    fs.movedir(dst_exists, src_exists)
    fs.movedir(src_exists, dst_folder_exists, overwrite=True)
    assert not fs.exists(src_exists) and fs.exists(dst_folder_exists)
    assert fs.isdir(dst_folder_exists)
Ejemplo n.º 55
0
def test_init(tmppath):
    """Test initialization."""
    fs = XRootDFS("root://127.0.0.1//tmp/")
    assert fs.xrd_client
    assert fs.base_path == "//tmp/"
    assert fs.root_url == "root://127.0.0.1"

    XRootDFS("root://*****:*****@eosuser.cern.ch//")
    XRootDFS("root://eosuser.cern.ch//")
    XRootDFS("root://eosuser.cern.ch//")
    pytest.raises(InvalidPathError, XRootDFS, "http://localhost")
    pytest.raises(InvalidPathError, XRootDFS, "root://eosuser.cern.ch//lhc//")

    rooturl = mkurl(tmppath)
    fs = XRootDFS(rooturl)
    root_url, base_path, qargs = spliturl(rooturl)
    assert fs.xrd_client
    assert fs.base_path == base_path
    assert fs.root_url == root_url
    assert fs.queryargs is None

    qarg = "xrd.wantprot=krb5"
    fs = XRootDFS(rooturl + "?" + qarg)
    root_url, base_path, qargs = spliturl(rooturl + "?" + qarg)
    assert fs.base_path == base_path
    assert fs.root_url == root_url
    assert fs.queryargs == {"xrd.wantprot": "krb5"}
    assert qargs == qarg

    qarg = "xrd.wantprot=krb5"
    fs = XRootDFS(rooturl + "?" + qarg, query={"xrd.k5ccname": "/tmp/krb"})

    assert fs.queryargs == {"xrd.wantprot": "krb5", "xrd.k5ccname": "/tmp/krb"}

    pytest.raises(KeyError, XRootDFS, rooturl + "?" + qarg, query={"xrd.wantprot": "krb5"})
Ejemplo n.º 56
0
def test_copy_bad(tmppath):
    """Test copy file."""
    fs = XRootDPyFS(mkurl(tmppath))

    src_exists = "data/testa.txt"
    src_new = "data/testb.txt"
    src_folder_exists = "data/afolder/"
    dst_exists = "data/multiline.txt"
    dst_new = "data/ok.txt"
    dst_folder_exists = "data/bfolder/"
    dst_folder_new = "data/anothernewfolder/"

    # Destination exists
    pytest.raises(DestinationExistsError, fs.copy, src_exists, dst_exists)
    pytest.raises(DestinationExistsError, fs.copy, src_exists, src_exists)
    pytest.raises(DestinationExistsError, fs.copy, src_exists,
                  dst_folder_exists)

    # Cannot copy dir
    pytest.raises(ResourceInvalidError, fs.copy, src_folder_exists, dst_new)
    pytest.raises(ResourceInvalidError, fs.copy, src_folder_exists,
                  dst_folder_new)

    # Source doesn't exists
    pytest.raises(ResourceNotFoundError, fs.copy, src_new, dst_exists)
    pytest.raises(ResourceNotFoundError, fs.copy, src_new, dst_new)
    pytest.raises(ResourceNotFoundError, fs.copy, src_new, dst_folder_exists)
    pytest.raises(ResourceNotFoundError, fs.copy, src_new, dst_folder_new)

    pytest.raises(ResourceNotFoundError, fs.copy, src_new, dst_exists)
    pytest.raises(ResourceNotFoundError, fs.copy, src_new, dst_new)
    pytest.raises(ResourceNotFoundError, fs.copy, src_new, dst_folder_exists)
    pytest.raises(ResourceNotFoundError, fs.copy, src_new, dst_folder_new)
Ejemplo n.º 57
0
def test_copy_good(tmppath):
    """Test move file."""
    fs = XRootDPyFS(mkurl(tmppath))

    src_exists = "data/testa.txt"
    dst_exists = "data/multiline.txt"
    dst_new = "data/ok.txt"
    dst_folder_exists = "data/bfolder/"
    dst_folder_new = "data/anothernewfolder/"
    content = _get_content(fs, src_exists)

    assert fs.exists(dst_exists)
    assert not fs.exists(dst_new)
    assert fs.exists(dst_folder_exists)
    assert not fs.exists(dst_folder_new)

    fs.copy(src_exists, dst_new)
    assert fs.exists(src_exists) and fs.exists(dst_new)

    fs.copy(src_exists, dst_folder_new)
    assert fs.exists(src_exists) and fs.exists(dst_folder_new)

    fs.copy(src_exists, dst_exists, overwrite=True)
    assert fs.exists(src_exists) and fs.exists(dst_exists)
    assert content == _get_content(fs, dst_exists)

    fs.copy(src_exists, dst_folder_exists, overwrite=True)
    assert fs.exists(src_exists) and fs.exists(dst_folder_exists)
    assert content == _get_content(fs, dst_folder_exists)
Ejemplo n.º 58
0
def test_truncate_read_write2(tmppath):
    """Tests behaviour of writing after seek(0) after
       reading after truncating."""
    fd = get_tsta_file(tmppath)
    fb = get_copy_file(fd)
    fp, fc = fd['full_path'], fd['contents']
    fp2 = fb['full_path']

    sp = len(fc)//2
    wstr = "I am the string"

    pfile = open(fp2, 'r+')
    xfile = XRootDPyFile(mkurl(fp), 'r+')

    xfile.truncate(sp), pfile.truncate(sp)
    assert xfile.tell() == pfile.tell()
    assert xfile.read() == pfile.read()
    assert xfile.tell() == pfile.tell()

    xfile.seek(0), pfile.seek(0)
    assert xfile.tell() == pfile.tell()
    assert xfile.read() == pfile.read()
    xfile.seek(0), pfile.seek(0)

    xfile.write(wstr), pfile.write(wstr)
    assert xfile.tell() == pfile.tell()
    assert xfile.read() == pfile.read()
    xfile.seek(0), pfile.seek(0)
    assert xfile.read() == pfile.read()
Ejemplo n.º 59
0
def copydir_good(tmppath, parallel):
    """Test copy directory."""
    fs = XRootDPyFS(mkurl(tmppath))

    src_exists = "data/afolder/"
    dst_exists = "data/multiline.txt"
    dst_new = "data/ok.txt"
    dst_folder_exists = "data/bfolder/"
    dst_folder_new = "data/anothernewfolder/"

    assert fs.isdir(src_exists)
    assert fs.exists(dst_exists)
    assert not fs.exists(dst_new)
    assert fs.exists(dst_folder_exists)
    assert not fs.exists(dst_folder_new)

    fs.copydir(src_exists, dst_new, parallel=parallel)
    assert fs.exists(src_exists) and fs.exists(dst_new)

    fs.copydir(src_exists, dst_folder_new, parallel=parallel)
    assert fs.exists(src_exists) and fs.exists(dst_folder_new)

    fs.copydir(src_exists, dst_exists, overwrite=True, parallel=parallel)
    assert fs.exists(src_exists) and fs.exists(dst_exists)
    assert fs.isdir(dst_exists)

    fs.copydir(src_exists,
               dst_folder_exists,
               overwrite=True,
               parallel=parallel)
    assert fs.exists(src_exists) and fs.exists(dst_folder_exists)
    assert fs.isdir(dst_folder_exists)
Ejemplo n.º 60
0
def test_init_writemode_basic(tmppath):
    # Non-existing file is created.
    fn, fp, fc = 'nope', 'data/', ''
    full_path = join(tmppath, fp, fn)
    xfile = XRootDPyFile(mkurl(full_path), mode='w+')
    assert xfile
    assert xfile.read() == fc

    # Existing file is truncated
    fd = get_tsta_file(tmppath)
    full_path = fd['full_path']
    xfile = XRootDPyFile(mkurl(full_path), mode='w+')
    assert xfile
    assert xfile.read() == ''
    assert xfile.size == 0
    assert xfile.tell() == 0