Commits

Rodrigo Bistolfi committed 5a34545

Added has_partition_table(). Improved tests

Comments (0)

Files changed (1)

vinstall/backend/partitioning.py

 import utils
 import parted
 import unittest, tempfile, os
+from vinstall.backend import media
 
 __author__ = "Moises Henriquez"
 __email__ = "moc.liamg@xnl.e0m"[::-1]
                 disk - a media.Disk object
 
         """
-        #self.disk = disk._disk
         self.disk = disk
 
+    def has_partition_table(self):
+        """Find if there is a partition table. PTs start at position 0x1BE and
+        contains 4 entries of 16 bytes, one for each partition
+        
+        """
+        with open(self.disk._device.path) as f:
+            f.seek(0x1BE)
+            data = f.read(16)
+            if data == "\x00" * 16:
+                return False
+            else:
+                return True
+
     def create_partition_table(self, table_type="msdos"):
         """ Create the partition table on the disk.
         This is only necessary when using a fresh disk that has
         for i in self.disk._disk.getFreeSpaceRegions():
             if i > mypsize:  # partition fits here
                 start_marker = max(i.start, 2048)
-        print "Creating new partition @ block %s and extending to %s"% (start_marker, mypsize)
+
         mygeom = parted.Geometry(device = self.disk._device,
                                  start = start_marker,
                                  length = mypsize)
         self.disk._disk.addPartition(partition = mypartition,
                                      constraint = myconst)
 
-        
-
-        return
-        # OLD CODE ----
-        if self.disk.type_name is None:
-            self.create_partition_table()
-
-        psize = reparted.Size(size,  units)
-        partition = reparted.Partition(self.disk,  size=psize)
-        self.disk.add_partition(partition)
-
     def write_changes(self):
         """Finalize changes to the disk.  This needs to be called
         after creating partitions or deleting partitions to make sure
     def tearDown(self):
         os.unlink(self.path)
 
-    def test_automatic_partitioning(self):
-        from vinstall.backend import media
+    def test_add_partition(self):
         device = parted.Device(self.path)
         disk = media.Disk()
         disk._device = device
         p.create_partition_table()
         p.add_partition(size=0.1)
         p.write_changes()
+        self.assertEqual(len(disk._disk.partitions), 1)
 
+    def test_has_partition_table(self):
+        device = parted.Device(self.path)
+        disk = media.Disk()
+        disk._device = device
+        p = DiskPartitioner(disk)
+        self.assertFalse(p.has_partition_table())
+        p.create_partition_table()
+        p.add_partition(size=0.1)
+        p.write_changes()
+        self.assertTrue(p.has_partition_table())
+    
+    def test_delete_all_partitions(self):
+        device = parted.Device(self.path)
+        disk = media.Disk()
+        disk._device = device
+        p = DiskPartitioner(disk)
+        p.create_partition_table()
+        p.add_partition(size=0.05)
+        p.add_partition(size=0.05)
+        p.write_changes()
+        self.assertEqual(len(disk._disk.partitions), 2)
+        p.delete_all_partitions()
+        p.write_changes()
+        self.assertEqual(len(disk._disk.partitions), 0)
+ 
 
 if __name__ == "__main__":
     unittest.main()