def test_partition_remaining(self):
     model = make_model()
     make_disk(model, serial='aaaa', size=dehumanize_size("100M"))
     fake_up_blockdata(model)
     model.apply_autoinstall_config([
         {
             'type': 'disk',
             'id': 'disk0',
         },
         {
             'type': 'partition',
             'id': 'part0',
             'device': 'disk0',
             'size': dehumanize_size('50M'),
         },
         {
             'type': 'partition',
             'id': 'part1',
             'device': 'disk0',
             'size': -1,
         },
     ])
     disk = model._one(type="disk")
     part1 = model._one(type="partition", id="part1")
     self.assertEqual(
         part1.size, disk.available_for_partitions - dehumanize_size('50M'))
Example #2
0
 def guided_lvm(self, disk, lvm_options):
     self.reformat(disk)
     if DeviceAction.MAKE_BOOT in disk.supported_actions:
         self.make_boot_disk(disk)
     self.create_partition(
         device=disk, spec=dict(
             size=dehumanize_size('1G'),
             fstype="ext4",
             mount='/boot'
             ))
     part = self.create_partition(
         device=disk, spec=dict(
             size=disk.free_for_partitions,
             fstype=None,
             ))
     spec = dict(name="ubuntu-vg", devices=set([part]))
     if lvm_options['encrypt']:
         spec['password'] = lvm_options['luks_options']['password']
     # create volume group on partition
     vg = self.create_volgroup(spec)
     self.create_logical_volume(
         vg=vg, spec=dict(
             size=dehumanize_size("4G"),
             name="ubuntu-lv",
             fstype="ext4",
             mount="/",
             ))
Example #3
0
 def test_lv_remaining(self):
     model = make_model()
     make_disk(model, serial='aaaa', size=dehumanize_size("100M"))
     fake_up_blockdata(model)
     model.apply_autoinstall_config([
         {
             'type': 'disk',
             'id': 'disk0',
         },
         {
             'type': 'lvm_volgroup',
             'id': 'vg0',
             'name': 'vg0',
             'devices': ['disk0'],
         },
         {
             'type': 'lvm_partition',
             'id': 'lv1',
             'name': 'lv1',
             'volgroup': 'vg0',
             'size': dehumanize_size("50M"),
         },
         {
             'type': 'lvm_partition',
             'id': 'lv2',
             'name': 'lv2',
             'volgroup': 'vg0',
             'size': -1,
         },
     ])
     vg = model._one(type="lvm_volgroup")
     lv2 = model._one(type="lvm_partition", id='lv2')
     self.assertEqual(lv2.size,
                      vg.available_for_partitions - dehumanize_size("50M"))
Example #4
0
 def test_errors(self):
     for input, expected_error in self.errors:
         with self.subTest(input=input):
             try:
                 dehumanize_size(input)
             except ValueError as e:
                 actual_error = str(e)
             else:
                 self.fail(
                     "dehumanize_size({!r}) did not error".format(input))
             self.assertEqual(expected_error, actual_error)
Example #5
0
 def clean_size(self, val):
     if not val:
         return self.max_size
     suffixes = ''.join(HUMAN_UNITS) + ''.join(HUMAN_UNITS).lower()
     if val[-1] not in suffixes:
         val += self.size_str[-1]
     return dehumanize_size(val)
Example #6
0
    def test_edit_boot_partition(self):
        form_data = {
            'size': "256M",
        }
        model, disk = make_model_and_disk()
        partition = model.add_partition(disk, 512 * (2**20), "boot")
        fs = model.add_filesystem(partition, "fat32")
        model.add_mount(fs, '/boot/efi')
        view, stretchy = make_partition_view(model, disk, partition)

        self.assertFalse(stretchy.form.fstype.enabled)
        self.assertEqual(stretchy.form.fstype.value, "fat32")
        self.assertFalse(stretchy.form.mount.enabled)
        self.assertEqual(stretchy.form.mount.value, "/boot/efi")

        view_helpers.enter_data(stretchy.form, form_data)
        view_helpers.click(stretchy.form.done_btn.base_widget)
        expected_data = {
            'size': dehumanize_size(form_data['size']),
            'fstype': "fat32",
            'mount': '/boot/efi',
            'use_swap': False,
        }
        view.controller.partition_disk_handler.assert_called_once_with(
            stretchy.disk, stretchy.partition, expected_data)
Example #7
0
 def __init__(self, model, controller, method):
     self.model = model
     self.controller = controller
     self.method = method
     cancel = cancel_btn(_("Cancel"), on_press=self.cancel)
     rows = []
     for disk in self.model.all_disks():
         if disk.size >= dehumanize_size("6G"):
             disk_btn = ClickableIcon(disk.label)
             connect_signal(
                 disk_btn, 'click', self.choose_disk, disk)
             attr = Color.done_button
         else:
             disk_btn = Text("  "+disk.label)
             attr = Color.info_minor
         rows.append(attr(TableRow([
             Text('['),
             disk_btn,
             Text(humanize_size(disk.size), align='right'),
             Text('\N{BLACK RIGHT-POINTING SMALL TRIANGLE}'),
             Text(']'),
             ])))
     super().__init__(screen(
         TableListBox(rows, spacing=1, colspecs={
             1: ColSpec(can_shrink=True, min_width=20, rpad=2),
             2: ColSpec(min_width=9),
             }),
         button_pile([cancel]),
         focus_buttons=False,
         excerpt=(
             excerpts[method]
             + "\n\n"
             + _("Choose the disk to install to:"))))
Example #8
0
 def test_create_partition(self):
     valid_data = {
         'size': "1M",
         'fstype': FilesystemModel.fs_by_name["ext4"],
     }
     view, stretchy = self.make_view()
     view_helpers.enter_data(stretchy.form, valid_data)
     view_helpers.click(stretchy.form.done_btn.base_widget)
     valid_data['mount'] = '/'
     valid_data['size'] = dehumanize_size(valid_data['size'])
     view.controller.partition_disk_handler.assert_called_once_with(
         stretchy.disk, None, valid_data)
Example #9
0
 def test_create_partition(self):
     valid_data = {
         'size': "1M",
         'fstype': "ext4",
     }
     model, disk = make_model_and_disk()
     view, stretchy = make_view(model, disk)
     view_helpers.enter_data(stretchy.form, valid_data)
     view_helpers.click(stretchy.form.done_btn.base_widget)
     valid_data['mount'] = '/'
     valid_data['size'] = dehumanize_size(valid_data['size'])
     view.controller.partition_disk_handler.assert_called_once_with(
         stretchy.disk, None, valid_data)
Example #10
0
 def choose_disk(self, btn, disk_path):
     self.model.reset()
     disk = self.model.disk_by_path(disk_path)
     if self.method == "direct":
         result = {
             "size": disk.free_for_partitions,
             "fstype": "ext4",
             "mount": "/",
         }
         self.controller.partition_disk_handler(disk, None, result)
     elif self.method == 'lvm':
         if DeviceAction.MAKE_BOOT in disk.supported_actions:
             self.controller.make_boot_disk(disk)
         self.controller.create_partition(device=disk,
                                          spec=dict(
                                              size=dehumanize_size('1G'),
                                              fstype="ext4",
                                              mount='/boot'))
         part = self.controller.create_partition(
             device=disk,
             spec=dict(
                 size=disk.free_for_partitions,
                 fstype=None,
             ))
         vg = self.controller.create_volgroup(spec=dict(
             name="ubuntu-vg",
             devices=set([part]),
         ))
         self.controller.create_logical_volume(
             vg=vg,
             spec=dict(
                 size=dehumanize_size("4G"),
                 name="ubuntu-lv",
                 fstype="ext4",
                 mount="/",
             ))
     else:
         raise Exception("unknown guided method '{}'".format(self.method))
     self.controller.manual()
 def test_partition_percent(self):
     model = make_model()
     make_disk(model, serial='aaaa', size=dehumanize_size("100M"))
     fake_up_blockdata(model)
     model.apply_autoinstall_config([{
         'type': 'disk',
         'id': 'disk0',
     }, {
         'type': 'partition',
         'id': 'part0',
         'device': 'disk0',
         'size': '50%',
     }])
     disk = model._one(type="disk")
     part = model._one(type="partition")
     self.assertEqual(part.size, disk.available_for_partitions // 2)
Example #12
0
 def guided_lvm(self, disk, lvm_options=None):
     self.reformat(disk)
     if DeviceAction.TOGGLE_BOOT in DeviceAction.supported(disk):
         self.add_boot_disk(disk)
     self.create_partition(
         device=disk, spec=dict(
             size=dehumanize_size('1G'),
             fstype="ext4",
             mount='/boot'
             ))
     part = self.create_partition(
         device=disk, spec=dict(
             size=disk.free_for_partitions,
             fstype=None,
             ))
     vg_name = 'ubuntu-vg'
     i = 0
     while self.model._one(type='lvm_volgroup', name=vg_name) is not None:
         i += 1
         vg_name = 'ubuntu-vg-{}'.format(i)
     spec = dict(name=vg_name, devices=set([part]))
     if lvm_options and lvm_options['encrypt']:
         spec['password'] = lvm_options['luks_options']['password']
     vg = self.create_volgroup(spec)
     # There's no point using LVM and unconditionally filling the
     # VG with a single LV, but we should use more of a smaller
     # disk to avoid the user running into out of space errors
     # earlier than they probably expect to.
     if vg.size < 10 * (2 << 30):
         # Use all of a small (<10G) disk.
         lv_size = vg.size
     elif vg.size < 20 * (2 << 30):
         # Use 10G of a smallish (<20G) disk.
         lv_size = 10 * (2 << 30)
     elif vg.size < 200 * (2 << 30):
         # Use half of a larger (<200G) disk.
         lv_size = vg.size // 2
     else:
         # Use at most 100G of a large disk.
         lv_size = 100 * (2 << 30)
     self.create_logical_volume(
         vg=vg, spec=dict(
             size=lv_size,
             name="ubuntu-lv",
             fstype="ext4",
             mount="/",
             ))
Example #13
0
 def test_edit_boot_partition(self):
     form_data = {
         'size': "256M",
     }
     model, disk = make_model_and_disk()
     partition = model.add_partition(disk, 512 * (2**20), "boot")
     fs = model.add_filesystem(partition, "fat32")
     model.add_mount(fs, '/boot/efi')
     view, stretchy = make_view(model, disk, partition)
     view_helpers.enter_data(stretchy.form, form_data)
     view_helpers.click(stretchy.form.done_btn.base_widget)
     expected_data = {
         'size': dehumanize_size(form_data['size']),
         'fstype': FilesystemModel.fs_by_name["fat32"],
         'mount': '/boot/efi',
     }
     view.controller.partition_disk_handler.assert_called_once_with(
         stretchy.disk, stretchy.partition, expected_data)
Example #14
0
 def test_edit_partition(self):
     form_data = {
         'size': "256M",
         'fstype': FilesystemModel.fs_by_name['xfs'],
     }
     model, disk = make_model_and_disk()
     partition = model.add_partition(disk, 512 * (2**20))
     model.add_filesystem(partition, "ext4")
     view, stretchy = make_view(model, disk, partition)
     self.assertTrue(stretchy.form.done_btn.enabled)
     view_helpers.enter_data(stretchy.form, form_data)
     view_helpers.click(stretchy.form.done_btn.base_widget)
     expected_data = {
         'size': dehumanize_size(form_data['size']),
         'fstype': FilesystemModel.fs_by_name['xfs'],
         'mount': None,
     }
     view.controller.partition_disk_handler.assert_called_once_with(
         stretchy.disk, stretchy.partition, expected_data)
Example #15
0
 def __init__(self, model, controller, method):
     self.model = model
     self.controller = controller
     self.method = method
     cancel = cancel_btn(_("Cancel"), on_press=self.cancel)
     rows = []
     for disk in self.model.all_disks():
         for obj, cells in summarize_device(disk):
             wrap = Color.info_minor
             if obj is disk:
                 start, end = '[', ']'
                 arrow = '\N{BLACK RIGHT-POINTING SMALL TRIANGLE}'
                 if disk.size >= dehumanize_size("6G"):
                     arrow = ClickableIcon(arrow)
                     connect_signal(arrow, 'click', self.choose_disk, disk)
                     wrap = _wrap_button_row
             else:
                 start, arrow, end = '', '', ''
             if isinstance(arrow, str):
                 arrow = Text(arrow)
             rows.append(
                 wrap(TableRow([Text(start)] + cells +
                               [arrow, Text(end)])))
         rows.append(TableRow([Text("")]))
     super().__init__(
         screen(TableListBox(rows[:-1],
                             spacing=2,
                             colspecs={
                                 0: ColSpec(rpad=1),
                                 2: ColSpec(can_shrink=True),
                                 4: ColSpec(min_width=9),
                                 5: ColSpec(rpad=1),
                             },
                             align='center'),
                button_pile([cancel]),
                focus_buttons=False,
                excerpt=(excerpts[method] + "\n\n" +
                         _("Choose the disk to install to:"))))
Example #16
0
        elif calc_size == real_size:
            print("exactly right!")
            r = True
        else:
            print("subiquity wasted space", real_size - calc_size)
            r = True
    finally:
        cleanup()
    return r


fails = 0
run(['mount', '-t', 'tmpfs', 'tmpfs', tmpdir])
try:
    for size in '1G', '10G', '100G', '1T', '10T':
        size = dehumanize_size(size)
        for level in raidlevels:
            for count in range(2, 10):
                if count >= level.min_devices:
                    if not verify_size_ok(level.value, [size] * count):
                        fails += 1
                    if not verify_size_ok(
                            level.value,
                        [align_down(random.randrange(size, 10 * size))] *
                            count):
                        fails += 1
                    sizes = [
                        align_down(random.randrange(size, 10 * size))
                        for _ in range(count)
                    ]
                    if not verify_size_ok(level.value, sizes):
Example #17
0
 def test_basics(self):
     for input, expected_output in self.basics:
         with self.subTest(input=input):
             self.assertEqual(expected_output, dehumanize_size(input))