Example #1
0
class SwiftStorageTestCase(unittest.TestCase):
    """Test the object-split file class."""
    def setUp(self):
        # monkey-patch swiftclient to use out mock up
        import swiftnbd.swift as swift
        swift.client = MockConnection
        from swiftnbd.swift import SwiftStorage

        # create a disk doubling the actual size of the mock up so we
        # have some uninitialised space to run tests
        self.store = SwiftStorage('url', 'user', 'pass', 'container', 512, 16)
        self.store.flush()

    def tearDown(self):
        pass

    def test_read_full_object_content(self):
        self.store.seek(0)
        data = self.store.read(512)
        self.assertEqual(data, b'\xff' * 512)

    def test_read_full_object_no_content(self):
        self.store.seek(8 * 512)
        data = self.store.read(512)
        self.assertEqual(data, b'\0' * 512)

    def test_write_full_object(self):
        self.store.seek(0)
        self.store.write(b'X' * 512)
        self.assertEqual(MockConnection.object(0), b'X' * 512)

        self.store.seek(8 * 512)
        self.store.write(b'X' * 512)
        self.assertEqual(MockConnection.object(8), b'X' * 512)

    def test_read_partial_object_content(self):
        self.store.seek(0)
        data = self.store.read(256)
        self.assertEqual(data, b'\xff' * 256)

    def test_read_partial_object_no_content(self):
        self.store.seek(8 * 512)
        data = self.store.read(256)
        self.assertEqual(data, b'\0' * 256)

    def test_write_partial_object_content(self):
        self.store.seek(0)
        self.store.write(b'X' * 256)
        self.assertEqual(MockConnection.object(0), b'X' * 256 + b'\xff' * 256)

    def test_write_partial_object_no_content(self):
        self.store.seek(8 * 512)
        self.store.write(b'X' * 256)
        self.assertEqual(MockConnection.object(8), b'X' * 256 + b'\0' * 256)

    def test_read_inter_object_content(self):
        self.store.seek(256)
        data = self.store.read(512)
        self.assertEqual(data, b'\xff' * 512)

    def test_read_inter_object_no_content(self):
        self.store.seek(8 * 512 + 256)
        data = self.store.read(512)
        self.assertEqual(data, b'\0' * 512)

    def test_read_inter_object_content_and_no_content(self):
        self.store.seek(8 * 512 - 256)
        data = self.store.read(512)
        self.assertEqual(data, b'\xff' * 256 + b'\0' * 256)

    def test_write_inter_object_content(self):
        self.store.seek(256)
        self.store.write(b'X' * 512)
        self.assertEqual(MockConnection.object(0), b'\xff' * 256 + b'X' * 256)
        self.assertEqual(MockConnection.object(1), b'X' * 256 + b'\xff' * 256)

    def test_write_inter_object_no_content(self):
        self.store.seek(8 * 512 + 256)
        self.store.write(b'X' * 512)
        self.assertEqual(MockConnection.object(8), b'\0' * 256 + b'X' * 256)
        self.assertEqual(MockConnection.object(9), b'X' * 256 + b'\0' * 256)

    def test_write_inter_object_content_and_no_content(self):
        self.store.seek(8 * 512 - 256)
        self.store.write(b'X' * 512)
        self.assertEqual(MockConnection.object(7), b'\xff' * 256 + b'X' * 256)
        self.assertEqual(MockConnection.object(8), b'X' * 256 + b'\0' * 256)

    def test_seek_bad_offset(self):
        self.assertRaises(IOError, self.store.seek, -1)
        self.assertRaises(IOError, self.store.seek, 10000000000000)
        try:
            self.store.seek(-1)
        except IOError as ex:
            self.assertEqual(ex.errno, errno.ESPIPE)
        else:
            self.fail("didn't raise IOError")

    def test_tell(self):
        self.store.seek(0)
        self.assertEqual(self.store.tell(), 0)
        self.store.seek(1024)
        self.assertEqual(self.store.tell(), 1024)

    def test_read_end_of_disk(self):
        self.store.seek(15 * 512)
        data = self.store.read(1024)
        self.assertEqual(len(data), 512)

    def test_wite_end_of_disk(self):
        self.store.seek(15 * 512)
        self.assertRaises(IOError, self.store.write, b'X' * 1024)
class SwiftStorageTestCase(unittest.TestCase):
    """Test the object-split file class."""
    def setUp(self):
        # monkey-patch swiftclient to use out mock up
        import swiftnbd.swift as swift
        swift.client = MockConnection
        from swiftnbd.swift import SwiftStorage

        # create a disk doubling the actual size of the mock up so we
        # have some uninitialised space to run tests
        self.store = SwiftStorage('url', 'user', 'pass', 'container', 512, 16)
        self.store.flush()

    def tearDown(self):
        pass

    def test_read_full_object_content(self):
        self.store.seek(0)
        data = self.store.read(512)
        self.assertEqual(data, b'\xff'*512)

    def test_read_full_object_no_content(self):
        self.store.seek(8*512)
        data = self.store.read(512)
        self.assertEqual(data, b'\0'*512)

    def test_write_full_object(self):
        self.store.seek(0)
        self.store.write(b'X'*512)
        self.assertEqual(MockConnection.object(0), b'X'*512)

        self.store.seek(8*512)
        self.store.write(b'X'*512)
        self.assertEqual(MockConnection.object(8), b'X'*512)

    def test_read_partial_object_content(self):
        self.store.seek(0)
        data = self.store.read(256)
        self.assertEqual(data, b'\xff'*256)

    def test_read_partial_object_no_content(self):
        self.store.seek(8*512)
        data = self.store.read(256)
        self.assertEqual(data, b'\0'*256)

    def test_write_partial_object_content(self):
        self.store.seek(0)
        self.store.write(b'X'*256)
        self.assertEqual(MockConnection.object(0), b'X'*256 + b'\xff'*256)

    def test_write_partial_object_no_content(self):
        self.store.seek(8*512)
        self.store.write(b'X'*256)
        self.assertEqual(MockConnection.object(8), b'X'*256 + b'\0'*256)

    def test_read_inter_object_content(self):
        self.store.seek(256)
        data = self.store.read(512)
        self.assertEqual(data, b'\xff'*512)

    def test_read_inter_object_no_content(self):
        self.store.seek(8*512 + 256)
        data = self.store.read(512)
        self.assertEqual(data, b'\0'*512)

    def test_read_inter_object_content_and_no_content(self):
        self.store.seek(8*512 - 256)
        data = self.store.read(512)
        self.assertEqual(data, b'\xff'*256 +  b'\0'*256)

    def test_write_inter_object_content(self):
        self.store.seek(256)
        self.store.write(b'X'*512)
        self.assertEqual(MockConnection.object(0), b'\xff'*256 + b'X'*256)
        self.assertEqual(MockConnection.object(1), b'X'*256 + b'\xff'*256)

    def test_write_inter_object_no_content(self):
        self.store.seek(8*512 + 256)
        self.store.write(b'X'*512)
        self.assertEqual(MockConnection.object(8), b'\0'*256 + b'X'*256)
        self.assertEqual(MockConnection.object(9), b'X'*256 + b'\0'*256)

    def test_write_inter_object_content_and_no_content(self):
        self.store.seek(8*512 - 256)
        self.store.write(b'X'*512)
        self.assertEqual(MockConnection.object(7), b'\xff'*256 + b'X'*256)
        self.assertEqual(MockConnection.object(8), b'X'*256 + b'\0'*256)

    def test_seek_bad_offset(self):
        self.assertRaises(IOError, self.store.seek, -1)
        self.assertRaises(IOError, self.store.seek, 10000000000000)
        try:
            self.store.seek(-1)
        except IOError as ex:
            self.assertEqual(ex.errno, errno.ESPIPE)
        else:
            self.fail("didn't raise IOError")

    def test_tell(self):
        self.store.seek(0)
        self.assertEqual(self.store.tell(), 0)
        self.store.seek(1024)
        self.assertEqual(self.store.tell(), 1024)

    def test_read_end_of_disk(self):
        self.store.seek(15*512)
        data = self.store.read(1024)
        self.assertEqual(len(data), 512)

    def test_wite_end_of_disk(self):
        self.store.seek(15*512)
        self.assertRaises(IOError, self.store.write, b'X'*1024)