Ejemplo n.º 1
0
def test_loopback_event(tmpdir):
    listener = udev.MultipathListener()
    received = threading.Event()
    devices = []

    def callback(device):
        pprint.pprint({k: device[k] for k in device})
        devices.append(device)
        received.set()

    listener._callback = callback
    with running(listener):
        # Create a backing file
        filename = str(tmpdir.join("file"))
        with open(filename, "wb") as f:
            f.truncate(1024**2 * 10)

        # Create and remove a loop device
        with loopback.Device(filename) as loop:
            print("Created a loop device at %r" % loop.path)
            if not received.wait(1):
                raise RuntimeError("Timeout receiving event")

            # We expect an event about our loop device
            assert devices[0].get("DEVNAME") == loop.path
Ejemplo n.º 2
0
 def test_many_devices(self):
     with namedTemporaryDir(dir="/tmp") as tmpdir:
         filename = os.path.join(tmpdir, "file")
         self.prepare_backing_file(filename)
         for i in range(300):
             with loopback.Device(filename) as device:
                 self.assertTrue(device.is_attached())
             self.assertFalse(device.is_attached())
Ejemplo n.º 3
0
 def test_with_device(self):
     with namedTemporaryDir(dir="/tmp") as tmpdir:
         filename = os.path.join(tmpdir, "file")
         self.prepare_backing_file(filename)
         with loopback.Device(filename) as device:
             self.assertTrue(device.is_attached())
             self.check_device(device)
         self.assertFalse(device.is_attached())
         self.check_backing_file(filename)
Ejemplo n.º 4
0
 def test_supported(self):
     with namedTemporaryDir() as tmpdir:
         # Prepare backing file poisoned with "x"
         backing_file = os.path.join(tmpdir, "backing_file")
         with io.open(backing_file, "wb") as f:
             f.write(b"x" * SIZE)
         # Create a loop device
         with loopback.Device(backing_file) as loop_device:
             # And discard it
             blockdev.discard(loop_device.path)
             stat = os.stat(backing_file)
             self.assertEqual(stat.st_blocks, 0)
Ejemplo n.º 5
0
 def test_attach_detach_manually(self):
     with namedTemporaryDir(dir="/tmp") as tmpdir:
         filename = os.path.join(tmpdir, "file")
         self.prepare_backing_file(filename)
         device = loopback.Device(filename)
         device.attach()
         try:
             self.assertTrue(device.is_attached())
             self.check_device(device)
         finally:
             device.detach()
         self.assertFalse(device.is_attached())
         self.check_backing_file(filename)
Ejemplo n.º 6
0
    def create_device(self, size):
        """
        Create loop device of size bytes.

        The block device is detached when the instance is closed.
        """
        name = "backing-file-%03d" % next(self._count)
        backing_file = os.path.join(self._tmpdir, name)
        with open(backing_file, "w") as f:
            f.truncate(size)

        device = loopback.Device(backing_file)
        device.attach()
        self._devices.append(device)

        return device.path
Ejemplo n.º 7
0
def loop_device(tmpdir):
    backing_file = str(tmpdir.join("backing_file"))
    with io.open(backing_file, "wb") as f:
        f.write(b"x" * FILE_SIZE)
    with loopback.Device(backing_file) as loop_device:
        yield loop_device