示例#1
0
    def test_update_and_read(self):
        with temporaryPath() as srcPath, \
                directio.open(srcPath, "w") as f:
            f.write(self.DATA[:BLOCK_SIZE])

            with directio.open(srcPath, "r+") as f:
                f.seek(BLOCK_SIZE)
                f.write(self.DATA[BLOCK_SIZE:])

            with io.open(srcPath, "rb") as f:
                self.assertEqual(f.read(), self.DATA)
示例#2
0
    def test_special_volumes(self, size, loop_device):
        # Write some data to the device.
        with directio.open(loop_device.path, "r+") as f:
            f.write(b"x" * size * 2)

        # Zero size bytes
        blockdev.zero(loop_device.path, size=size)

        # Verify that size bytes were zeroed and the rest not modified.
        with directio.open(loop_device.path) as f:
            data = f.read(size)
            assert data == b"\0" * size
            data = f.read(size)
            assert data == b"x" * size
示例#3
0
    def test_size(self, loop_device):
        # Write some data to the device.
        with directio.open(loop_device.path, "r+") as f:
            f.write(DATA * 2)

        # Zero bytes at the start of the device.
        blockdev.zero(loop_device.path, size=len(ZERO))

        # Verify that the area was zeroed.
        with directio.open(loop_device.path) as f:
            data = f.read(len(ZERO))
            assert data == ZERO
            data = f.read(len(DATA))
            assert data == DATA
示例#4
0
    def test_special_volumes(self, size, loop_device):
        # Write some data to the device.
        with directio.open(loop_device.path, "r+") as f:
            f.write(b"x" * size * 2)

        # Zero size bytes
        blockdev.zero(loop_device.path, size=size)

        # Verify that size bytes were zeroed and the rest not modified.
        with directio.open(loop_device.path) as f:
            data = f.read(size)
            assert data == b"\0" * size
            data = f.read(size)
            assert data == b"x" * size
示例#5
0
    def test_size(self, loop_device):
        # Write some data to the device.
        with directio.open(loop_device.path, "r+") as f:
            f.write(DATA * 2)

        # Zero bytes at the start of the device.
        blockdev.zero(loop_device.path, size=len(ZERO))

        # Verify that the area was zeroed.
        with directio.open(loop_device.path) as f:
            data = f.read(len(ZERO))
            assert data == ZERO
            data = f.read(len(DATA))
            assert data == DATA
示例#6
0
    def test_entire_device(self, loop_device):
        # Write some data to the device.
        with directio.open(loop_device.path, "r+") as f:
            f.write(DATA)
            f.seek(FILE_SIZE - len(DATA))
            f.write(DATA)

        # Zero the entire device
        blockdev.zero(loop_device.path)

        # Verify that parts with data were zeroed.
        with directio.open(loop_device.path) as f:
            data = f.read(len(ZERO))
            assert data == ZERO
            f.seek(FILE_SIZE - len(ZERO))
            data = f.read(len(ZERO))
            assert data == ZERO
示例#7
0
    def test_entire_device(self, loop_device):
        # Write some data to the device.
        with directio.open(loop_device.path, "r+") as f:
            f.write(DATA)
            f.seek(FILE_SIZE - len(DATA))
            f.write(DATA)

        # Zero the entire device
        blockdev.zero(loop_device.path)

        # Verify that parts with data were zeroed.
        with directio.open(loop_device.path) as f:
            data = f.read(len(ZERO))
            assert data == ZERO
            f.seek(FILE_SIZE - len(ZERO))
            data = f.read(len(ZERO))
            assert data == ZERO
示例#8
0
    def test_small_writes(self):
        with temporaryPath() as srcPath, \
                directio.open(srcPath, "w") as f:
            f.write(self.DATA[:BLOCK_SIZE])
            f.write(self.DATA[BLOCK_SIZE:])

            with io.open(srcPath, "rb") as f:
                self.assertEqual(f.read(), self.DATA)
示例#9
0
    def test_supported(self, loop_device):
        # Write some data to the device.
        with directio.open(loop_device.path, "r+") as f:
            f.write(DATA)
            f.seek(FILE_SIZE - len(DATA))
            f.write(DATA)

        # Discard entire device.
        blockdev.discard(loop_device.path)

        # Check that all data was discarded.
        stat = os.stat(loop_device.backing_file)
        assert stat.st_blocks == 0
示例#10
0
    def test_supported(self, loop_device):
        # Write some data to the device.
        with directio.open(loop_device.path, "r+") as f:
            f.write(DATA)
            f.seek(FILE_SIZE - len(DATA))
            f.write(DATA)

        # Discard entire device.
        blockdev.discard(loop_device.path)

        # Check that all data was discarded.
        stat = os.stat(loop_device.backing_file)
        assert stat.st_blocks == 0
示例#11
0
    def test_supported(self, loop_device):
        # If the loop device backing file is on a file system that does not
        # support discard, discard is not supported.
        cp = subprocess.run(
            ["blkdiscard", loop_device.path], stderr=subprocess.PIPE)
        if cp.returncode != 0:
            pytest.skip("blkdiscard not supported: %s" % cp.stderr)

        # Write some data to the device.
        with directio.open(loop_device.path, "r+") as f:
            f.write(DATA)
            f.seek(FILE_SIZE - len(DATA))
            f.write(DATA)

        # Discard entire device.
        blockdev.discard(loop_device.path)

        # Check that all data was discarded.
        stat = os.stat(loop_device.backing_file)
        assert stat.st_blocks == 0
示例#12
0
 def test_read_all(self):
     with temporaryPath(data=self.DATA) as srcPath, \
             directio.open(srcPath) as direct_file, \
             io.open(srcPath, "rb") as buffered_file:
         self.assertEqual(direct_file.read(), buffered_file.read())
示例#13
0
 def test_write_unaligned(self):
     with temporaryPath(data=self.DATA) as srcPath, \
             directio.open(srcPath, "r+") as f:
         self.assertRaises(ValueError, f.write, "x" * 511)
         with io.open(srcPath, "rb") as f:
             self.assertEqual(f.read(), self.DATA)
示例#14
0
 def test_write(self):
     with temporaryPath() as srcPath, \
             directio.open(srcPath, "w") as f:
         f.write(self.DATA)
         with io.open(srcPath, "rb") as f:
             self.assertEqual(f.read(), self.DATA)
示例#15
0
 def test_seek_and_read(self, offset):
     with temporaryPath(data=self.DATA) as srcPath, \
             directio.open(srcPath) as f:
         f.seek(offset)
         self.assertEqual(f.read(), self.DATA[offset:])
示例#16
0
 def test_read(self, size):
     with temporaryPath(data=self.DATA) as srcPath, \
             directio.open(srcPath) as f:
         self.assertEqual(f.read(size), self.DATA[:size])