Exemplo n.º 1
0
  def __init__(self, path, destination=None, part_ids=None, mount_opts=None,
               dev=LOOP_DEV, part_count=0):
    """Initialize.

    Args:
      (shared with LoopbackPartitions)
      path: Path to the image file.
      destination: destination directory.
      part_ids: Mount these partitions at context manager entry.
      mount_opts: Use these mount_opts for mounting |part_ids|.
      (unique to LoopbackPartitionsMock)
      dev: Path for the base loopback device.
      part_count: How many partition device files to make up.  Default: normal
          partition table.
    """
    self.path = path
    self.dev = dev
    self.part_ids = part_ids
    self.mount_opts = mount_opts
    if destination:
      self.destination = destination
    else:
      self.destination = osutils.TempDir()
    if part_count:
      self._gpt_table = [
          image_lib.PartitionInfo(num, 0, 0, 0, '', 'my-%d' % num, '')
          for num in range(1, part_count + 1)]
    else:
      self._gpt_table = LOOP_PARTITION_INFO
    self.parts = {p.number: '%sp%s' % (dev, p.number)
                  for p in self._gpt_table}
    self.enable_rw_called = set()
    self.disable_rw_called = set()
    def testGenerateStatefulPayload(self):
        """Test correct arguments propagated to tar call."""

        self.PatchObject(osutils.TempDir,
                         '__enter__',
                         return_value=self.tempdir)
        fake_partitions = (image_lib.PartitionInfo(3, 0, 4, 4, 'fs', 'STATE',
                                                   ''), )
        self.PatchObject(image_lib,
                         'GetImageDiskPartitionInfo',
                         return_value=fake_partitions)

        paygen_stateful_payload_lib.GenerateStatefulPayload(
            'dev/null', self.tempdir)

        self.assertCommandContains([
            'tar',
            '-czf',
            os.path.join(self.tempdir, 'stateful.tgz'),
            '--directory=%s/dir-1' % self.tempdir,
            '--transform=s,^dev_image,dev_image_new,',
            '--transform=s,^var_overlay,var_new,',
            'dev_image',
            'var_overlay',
        ])
Exemplo n.º 3
0
 def setUp(self):
     # create dummy image file
     self.image_file = os.path.join(self.tempdir,
                                    constants.BASE_IMAGE_NAME + '.bin')
     osutils.WriteFile(self.image_file, '')
     fake_partitions = (
         image_lib.PartitionInfo(1, 0, 0, 0, 'fs', 'STATE', 'flag'),
         image_lib.PartitionInfo(2, 0, 0, 0, 'fs', 'KERN-A', 'flag'),
         image_lib.PartitionInfo(3, 0, 0, 0, 'fs', 'ROOT-A', 'flag'),
     )
     self.PatchObject(image_lib,
                      'GetImageDiskPartitionInfo',
                      autospec=True,
                      return_value=fake_partitions)
     self.PatchObject(image_lib.LoopbackPartitions, '_Mount', autospec=True)
     self.PatchObject(image_lib.LoopbackPartitions,
                      '_Unmount',
                      autospec=True)
    def testExtractPartition(self):
        """Tests extraction on a simple image."""
        part_a_bin = '0123'
        part_b_bin = '4567'
        image_bin = part_a_bin + part_b_bin

        image = os.path.join(self.tempdir, 'image.bin')
        osutils.WriteFile(image, image_bin)
        part_a = os.path.join(self.tempdir, 'a.bin')

        fake_partitions = (
            image_lib.PartitionInfo(1, 0, 4, 4, 'fs', 'PART-A', ''),
            image_lib.PartitionInfo(2, 4, 8, 4, 'fs', 'PART-B', ''),
        )
        self.PatchObject(image_lib,
                         'GetImageDiskPartitionInfo',
                         return_value=fake_partitions)

        partition_lib.ExtractPartition(image, 'PART-A', part_a)
        self.assertEqual(osutils.ReadFile(part_a), part_a_bin)
from chromite.lib import osutils
from chromite.lib import retry_util
from chromite.lib import partial_mock

# pylint: disable=protected-access


class FakeException(Exception):
    """Fake exception used for testing exception handling."""


FAKE_PATH = '/imaginary/file'
LOOP_DEV = '/dev/loop9999'
LOOP_PART_COUNT = 12
LOOP_PARTITION_INFO = [
    image_lib.PartitionInfo(1, 2928640, 2957311, 28672, 14680064, 'STATE', ''),
    image_lib.PartitionInfo(2, 20480, 53247, 32768, 16777216, 'KERN-A', ''),
    image_lib.PartitionInfo(3, 286720, 2928639, 2641920, 1352663040, 'ROOT-A',
                            ''),
    image_lib.PartitionInfo(4, 53248, 86015, 32768, 16777216, 'KERN-B', ''),
    image_lib.PartitionInfo(5, 282624, 286719, 4096, 2097152, 'ROOT-B', ''),
    image_lib.PartitionInfo(6, 16448, 16448, 1, 512, 'KERN-C', ''),
    image_lib.PartitionInfo(7, 16449, 16449, 1, 512, 'ROOT-C', ''),
    image_lib.PartitionInfo(8, 86016, 118783, 32768, 16777216, 'OEM', ''),
    image_lib.PartitionInfo(9, 16450, 16450, 1, 512, 'reserved', ''),
    image_lib.PartitionInfo(10, 16451, 16451, 1, 512, 'reserved', ''),
    image_lib.PartitionInfo(11, 64, 16447, 16384, 8388608, 'RWFW', ''),
    image_lib.PartitionInfo(12, 249856, 282623, 32768, 16777216, 'EFI-SYSTEM',
                            ''),
]
LOOP_PARTS_DICT = {