Commits

Rodrigo Bistolfi committed 398b7b0

Added tests for partitioning.py

  • Participants
  • Parent commits 3acd6d9

Comments (0)

Files changed (1)

vinstall/backend/partitioning.py

 
 import utils
 import parted
+import unittest, tempfile, os
 
 __author__ = "Moises Henriquez"
 __email__ = "moc.liamg@xnl.e0m"[::-1]
         never been partitioned before.
 
         """
-        assert self.disk._disk is None, "Partition table already exists?"
-#        assert self.disk._disk.has_partition_table is False, "Partition table already exists?"
-#        assert not hasattr(self.disk, "_disk"), ".disk attribute already here.  Do we have a partition table already?"
-#        assert self.disk._disk is not None, "We already have .disk attribute.  Do we have a partition table already?"
-#        assert self.disk.type is None, "Disk already has a partition table"
         self.disk._disk = parted.freshDisk(self.disk._device, "msdos")
-        self.disk.has_partition_table = True
+        self.disk._disk.has_partition_table = True
 
     def delete_all_partitions(self):
         """ Delete all partitions from drive
 
         """
-        assert self.disk.has_partition_table is True, "No partition table on disk."
         self.disk._disk.deleteAllPartitions()
 
     def add_partition(self,  size=0, units='MB'):
                units = partition units (MB, GB, MiB, GiB), (defaults to MiB)
 
         """
-        assert self.disk.has_partition_table is False, "No partition table on disk.  Cannot add partitions"
         mypsize = parted.sizeToSectors(size, units, self.disk._device.sectorSize)
         # first partition starts at sector 1
         # find out where the free space is
         self.disk._disk.commit()
 
 
+class DiskPartitionerTestCase(unittest.TestCase):
+
+    def setUp(self):
+        (fd, self.path) = tempfile.mkstemp(prefix="fake-device-")
+        f = os.fdopen(fd)
+        f.seek(140000)
+        os.write(fd, "0")
+
+    def tearDown(self):
+        os.unlink(self.path)
+
+    def test_automatic_partitioning(self):
+        from vinstall.backend import media
+        device = parted.Device(self.path)
+        disk = media.Disk()
+        disk._device = device
+        p = DiskPartitioner(disk)
+        p.create_partition_table()
+        p.add_partition(size=0.1)
+        p.write_changes()
+
+
+if __name__ == "__main__":
+    unittest.main()