def test_find(self): environment = storage.Environment(True, storage.ProbeMode_NONE, storage.TargetMode_DIRECT) s = storage.Storage(environment) devicegraph = storage.Devicegraph(s) sda = storage.Disk.create(devicegraph, "/dev/sda") sda.set_region(storage.Region(0, 1000000, 512)) gpt = sda.create_partition_table(storage.PtType_GPT) sda1 = gpt.create_partition("/dev/sda1", storage.Region(0, 100, 512), storage.PartitionType_PRIMARY) self.assertEqual(sda.get_sid(), 42) self.assertEqual(gpt.get_sid(), 43) self.assertEqual(sda1.get_sid(), 44) self.assertTrue(storage.Disk.find_by_name(devicegraph, "/dev/sda")) self.assertRaises( storage.DeviceNotFound, lambda: storage.Disk.find_by_name(devicegraph, "/dev/not-here")) self.assertRaises( storage.DeviceHasWrongType, lambda: storage.Disk.find_by_name(devicegraph, "/dev/sda1"))
def test_polymorphism(self): environment = storage.Environment(True, storage.ProbeMode_NONE, storage.TargetMode_DIRECT) s = storage.Storage(environment) devicegraph = storage.Devicegraph(s) sda = storage.Disk.create(devicegraph, "/dev/sda") gpt = sda.create_partition_table(storage.PtType_GPT) self.assertEqual(sda.get_sid(), 42) self.assertEqual(gpt.get_sid(), 43) tmp1 = devicegraph.find_device(42) self.assertTrue(storage.is_disk(tmp1)) self.assertTrue(storage.to_disk(tmp1)) self.assertFalse(storage.is_partition_table(tmp1)) self.assertRaises(storage.DeviceHasWrongType, lambda: storage.to_partition_table(tmp1)) self.assertRaises(storage.Exception, lambda: storage.to_partition_table(tmp1)) self.assertEqual(storage.downcast(tmp1).__class__, storage.Disk) tmp2 = devicegraph.find_device(43) self.assertTrue(storage.is_partition_table(tmp2)) self.assertTrue(storage.to_partition_table(tmp2)) self.assertFalse(storage.is_disk(tmp2)) self.assertRaises(storage.DeviceHasWrongType, lambda: storage.to_disk(tmp2)) self.assertRaises(storage.Exception, lambda: storage.to_disk(tmp2)) self.assertEqual(storage.downcast(tmp2).__class__, storage.Gpt) tmp3 = devicegraph.find_holder(42, 43) self.assertTrue(storage.is_user(tmp3)) self.assertTrue(storage.to_user(tmp3)) self.assertFalse(storage.is_filesystem_user(tmp3)) self.assertRaises(storage.HolderHasWrongType, lambda: storage.to_filesystem_user(tmp3)) self.assertRaises(storage.Exception, lambda: storage.to_filesystem_user(tmp3)) self.assertEqual(storage.downcast(tmp3).__class__, storage.User)
#!/usr/bin/python import storage devicegraph = storage.Devicegraph() sda = storage.Disk.create(devicegraph, "/dev/sda") gpt = sda.create_partition_table(storage.PtType_GPT) sda1 = gpt.create_partition("/dev/sda1", storage.Region(0, 100, 262144), storage.PRIMARY) sda2 = gpt.create_partition("/dev/sda2", storage.Region(100, 100, 262144), storage.PRIMARY) print devicegraph print "partitions on gpt:" for partition in gpt.get_partitions(): print " %s %s" % (partition, partition.get_number()) print print "descendants of sda:" for device in sda.get_descendants(False): try: partition_table = storage.to_partition_table(device) print " %s is partition table" % partition_table except storage.DeviceHasWrongType: pass