示例#1
0
    def test_read(self):
        with open(self.file, 'w') as file:
            file.write('B' * 512)

        raw = RawDirect(self.file)
        self.assertEquals(raw.read(512), 'B' * 512)
        raw.close()
示例#2
0
 def test_closed(self):
     raw = RawDirect(self.file)
     self.assertEquals(raw.closed, False)
     self.assertEquals(raw.writable(), True)
     raw.close()
     self.assertEquals(raw.closed, True)
     self.assertEquals(raw.writable(), False)
示例#3
0
    def test_write(self):
        raw = RawDirect(self.file)
        raw.write('A' * 512)
        raw.close()

        with open(self.file) as fd:
            self.assertEquals(fd.read(512), 'A' * 512)
示例#4
0
    def test_truncate(self):
        # Our test file should be 1MB
        self.assertEquals(os.stat(self.file).st_size, 1048576)

        raw = RawDirect(self.file)
        raw.truncate(512)
        raw.close()
        self.assertEquals(os.stat(self.file).st_size, 512)
示例#5
0
    def test_readinto(self):
        with open(self.file, 'w') as file:
            file.write('A' * 512)

        raw = RawDirect(self.file)
        buf = bytearray(512)
        self.assertEquals(raw.readinto(buf), 512)
        self.assertEquals(buf, 'A' * 512)
        raw.close()
示例#6
0
    def test_read_greater_than_page_size(self):
        with open(self.file, 'w') as file:
            file.write('J' * 8192)

        raw = RawDirect(self.file)
        # Ask to read 8 more bytes then the block size
        self.assertRaises(OSError, raw.read, 520)
        # Read 8192 ( double the typical page size )
        self.assertEquals(raw.read(8192), 'J' * 8192)
        raw.close()
示例#7
0
    def test_read_less_than_page_size(self):
        with open(self.file, 'w') as file:
            file.write('F' * 512)
            file.write('B' * 1024)

        raw = RawDirect(self.file)
        # Ask to only read 10 bytes
        self.assertRaises(OSError, raw.read, 10)
        # Read 512 bytes
        self.assertEquals(raw.read(512), 'F' * 512)
        # Read 1024
        self.assertEquals(raw.read(1024), 'B' * 1024)
        raw.close()
示例#8
0
    def test_read_past_eof(self):
        # Create a file that is less than the block size
        fd, file = tempfile.mkstemp(dir='/tmp')
        os.write(fd, 'A' * 511)
        os.close(fd)

        raw = RawDirect(file)
        # If requested read is past the EOF
        # return the bytes we actually read
        self.assertEquals(raw.read(512), ('A' * 511))
        # Empty string means we are at EOF
        self.assertEquals(raw.read(512), '')
        raw.close()
示例#9
0
    def test_write_past_eof(self):
        # Create a sparse file that is less than the block size
        fd, file = tempfile.mkstemp(dir='/tmp')
        os.ftruncate(fd, 511)
        os.close(fd)
        # Should be 511 in size
        self.assertEquals(os.stat(file).st_size, 511)

        raw = RawDirect(file)
        # the write should succeed
        self.assertEquals(raw.write('A' * 512), 512)
        raw.close()

        # writting past the end should grow the size of the file
        self.assertEquals(os.stat(file).st_size, 512)
示例#10
0
 def test_write_less_than_page_size(self):
     raw = RawDirect(self.file)
     # Write only 10 bytes
     self.assertRaises(OSError, raw.write, ('G' * 10))
     # Write 512
     self.assertEquals(raw.write('A' * 512), 512)
     # Write 1024
     self.assertEquals(raw.write('B' * 1024), 1024)
     # Ensure the data is there
     raw.seek(0)
     self.assertEquals(raw.read(512), 'A' * 512)
     self.assertEquals(raw.read(1024), 'B' * 1024)
     raw.close()
示例#11
0
    def test_write_past_device_eof(self):
        # Create a sparse file that is less than the block size
        fd, file = tempfile.mkstemp(dir='/tmp')
        os.ftruncate(fd, 511)
        os.close(fd)

        try:
            # Mount the file as a loopback device (simulate a block device)
            device = find_loopback_device()
            call("losetup %s %s" % (device, file), shell=True)

            raw = RawDirect(device)
            # Should raise IOError: [Errno 28] No space left on device
            self.assertRaises(OSError, raw.write, 'A' * 512)
            self.assertEquals(raw.tell(), 0)
            self.assertEquals(os.lseek(raw._fd, 0, os.SEEK_CUR), 0)
            raw.close()
        finally:
            call("losetup -d %s" % device, shell=True)
示例#12
0
 def test_readall(self):
     raw = RawDirect(self.file)
     # Read in the entire 1MB file
     buf = raw.readall()
     self.assertEquals(len(buf), 1048576)
     raw.close()
示例#13
0
 def test_tell(self):
     raw = RawDirect(self.file)
     raw.write('A' * 512)
     self.assertEquals(raw.tell(), 512)
     raw.write('B' * 512)
     self.assertEquals(raw.tell(), 1024)
     raw.seek(0)
     self.assertEquals(raw.read(512), ('A' * 512))
     self.assertEquals(raw.read(512), ('B' * 512))
     self.assertEquals(raw.tell(), 1024)
示例#14
0
 def test_seek(self):
     raw = RawDirect(self.file)
     self.assertEquals(raw.seek(4096, os.SEEK_SET), 4096)
     self.assertEquals(raw.seek(-512, os.SEEK_CUR), 3584)
示例#15
0
 def test_write_greater_than_page_size(self):
     raw = RawDirect(self.file)
     # Write only 8 bytes more than the block size
     self.assertRaises(OSError, raw.write, ('J' * 520))
     raw.close()