import os
import unittest

from utils import create_sparse_tempfile
from gi.repository import BlockDev
if not BlockDev.is_initialized():
    BlockDev.init(None, None)

class LoopTestCase(unittest.TestCase):
    def setUp(self):
        self.dev_file = create_sparse_tempfile("loop_test", 1024**3)

    def tearDown(self):
        os.unlink(self.dev_file)

    def testLoop_setup_teardown(self):
        """Verify that loop_setup and loop_teardown work as expected"""

        succ, loop = BlockDev.loop_setup(self.dev_file)
        self.assertTrue(succ)
        self.assertTrue(loop)

        succ = BlockDev.loop_teardown(loop)
        self.assertTrue(succ)

    def testLoop_get_loop_name(self):
        """Verify that loop_get_loop_name works as expected"""

        succ, loop = BlockDev.loop_setup(self.dev_file)
        ret_loop = BlockDev.loop_get_loop_name(self.dev_file)
        self.assertEqual(ret_loop, loop)
        BlockDev.loop_teardown(loop)

    def testLoop_get_backing_file(self):
        """Verify that loop_get_backing_file works as expected"""

        self.assertIs(BlockDev.loop_get_backing_file("/non/existing"), None)

        succ, loop = BlockDev.loop_setup(self.dev_file)
        f_name = BlockDev.loop_get_backing_file(loop)
        self.assertEqual(f_name, self.dev_file)
        BlockDev.loop_teardown(loop)


@@ -294,32 +294,149 @@
 *3)%0A
-%0A    def tearDown(self):
+        self.loop = None%0A%0A    def tearDown(self):%0A        try:%0A            BlockDev.loop_teardown(self.loop)%0A        except:%0A            pass
 %0A   
@@ -584,32 +584,37 @@
 %0A%0A        succ, 
+self.
 loop = BlockDev.
@@ -693,16 +693,21 @@
 ertTrue(
+self.
 loop)%0A%0A 
@@ -739,24 +739,29 @@
 op_teardown(
+self.
 loop)%0A      
@@ -893,32 +893,37 @@
 %0A%0A        succ, 
+self.
 loop = BlockDev.
@@ -1049,45 +1049,13 @@
 op, 
-loop)%0A        BlockDev.loop_teardown(
+self.
 loop
@@ -1257,16 +1257,21 @@
   succ, 
+self.
 loop = B
@@ -1352,16 +1352,21 @@
 ng_file(
+self.
 loop)%0A  
@@ -1414,43 +1414,6 @@
 ile)
-%0A        BlockDev.loop_teardown(loop)
 %0A%0A

Make sure that loop devices are deactivated after tests