| 1 |
#!/usr/bin/python3 -cimport os, sys; os.execv(os.path.dirname(sys.argv[1]) + "/../common/pywrap", sys.argv) |
| 2 |
|
| 3 |
# |
| 4 |
# Copyright (C) 2015 Red Hat, Inc. |
| 5 |
# SPDX-License-Identifier: LGPL-2.1-or-later |
| 6 |
|
| 7 |
|
| 8 |
import storagelib |
| 9 |
import testlib |
| 10 |
|
| 11 |
|
| 12 |
@testlib.nondestructive |
| 13 |
class TestStorageUsed(storagelib.StorageCase): |
| 14 |
|
| 15 |
def testUsed(self): |
| 16 |
m = self.machine |
| 17 |
b = self.browser |
| 18 |
|
| 19 |
self.login_and_go("/storage") |
| 20 |
|
| 21 |
disk = self.add_ram_disk() |
| 22 |
b.wait_visible(self.card_row("Storage", name=disk)) |
| 23 |
m.execute(f"parted -s {disk} mktable msdos") |
| 24 |
m.execute(f"parted -s {disk} mkpart primary ext2 1M 25") |
| 25 |
m.execute("udevadm settle") |
| 26 |
m.execute(f"until udevadm info --query=all {disk} | grep -q ID_PART_TABLE_TYPE=dos; do sleep 0.1; done") |
| 27 |
m.execute(f"echo einszweidrei | cryptsetup luksFormat --pbkdf-memory 32768 {disk}1") |
| 28 |
m.execute(f"echo einszweidrei | cryptsetup luksOpen {disk}1 dm-test") |
| 29 |
m.execute("udevadm settle") |
| 30 |
m.execute("until [ -b /dev/mapper/dm-test ]; do sleep 0.1; done") |
| 31 |
m.execute("until udevadm info --query=all /dev/mapper/dm-test | grep -q DM_NAME=dm-test; do sleep 0.1; done") |
| 32 |
m.execute("mke2fs -q -L TEST /dev/mapper/dm-test") |
| 33 |
m.execute(f"mount /dev/mapper/dm-test {self.mnt_dir}") |
| 34 |
|
| 35 |
# Keep the mount point busy. The extra "true" is here to |
| 36 |
# prevent bash from applying tail call optimization to the |
| 37 |
# "sleep" invocation. |
| 38 |
sleep_pid = m.spawn(f"cd {self.mnt_dir}; sleep infinity; true", "sleep") |
| 39 |
self.write_file("/etc/systemd/system/keep-mnt-busy.service", |
| 40 |
f""" |
| 41 |
[Unit] |
| 42 |
Description=Test Service |
| 43 |
|
| 44 |
[Service] |
| 45 |
WorkingDirectory={self.mnt_dir} |
| 46 |
ExecStart=/usr/bin/sleep infinity |
| 47 |
""") |
| 48 |
m.execute("systemctl start keep-mnt-busy") |
| 49 |
|
| 50 |
# Now all of /dev/mapper/dm-test, /dev/sda1, and /dev/sda |
| 51 |
# should be 'in use' but Cockpit can clean them all up anyway. |
| 52 |
|
| 53 |
self.click_card_row("Storage", name=disk) |
| 54 |
b.wait_visible(self.card("Solid State Drive")) |
| 55 |
|
| 56 |
self.click_card_row("DOS partitions", 1) |
| 57 |
self.click_card_dropdown("ext2 filesystem", "Format") |
| 58 |
self.dialog_wait_open() |
| 59 |
self.dialog_set_val("type", "ext4") |
| 60 |
b.click("#dialog button:contains('Currently in use')") |
| 61 |
b.wait_in_text(".pf-v6-c-popover", str(sleep_pid)) |
| 62 |
b.wait_in_text(".pf-v6-c-popover", "keep-mnt-busy") |
| 63 |
b.assert_pixels(".pf-v6-c-popover", "popover", |
| 64 |
mock={".pf-v6-c-popover__body ul:nth-of-type(2) li": "process (user: root, pid: 1234)"}, |
| 65 |
scroll_into_view="#dialog button:contains('Currently in use')") |
| 66 |
b.click(".pf-v6-c-popover button") |
| 67 |
b.assert_pixels('#dialog', "format", wait_after_layout_change=True, chrome_hack_double_shots=True) |
| 68 |
self.dialog_cancel() |
| 69 |
self.dialog_wait_close() |
| 70 |
|
| 71 |
self.click_card_dropdown("Partition", "Delete") |
| 72 |
self.dialog_wait_open() |
| 73 |
b.wait_visible("#dialog button:contains('Currently in use')") |
| 74 |
b.assert_pixels('#dialog', "delete") |
| 75 |
self.dialog_cancel() |
| 76 |
self.dialog_wait_close() |
| 77 |
|
| 78 |
# Now go ahead and let the automatic teardown take care of the mount |
| 79 |
|
| 80 |
b.click(self.card_parent_link()) |
| 81 |
|
| 82 |
# Sometimes /dev/sda1 is still held open by something |
| 83 |
# immediately after locking it. This prevents the |
| 84 |
# kernel from reading the new partition table. Let's |
| 85 |
# just retry. |
| 86 |
|
| 87 |
def first_setup(): |
| 88 |
self.dialog_set_val("type", "empty") |
| 89 |
b.wait_visible("#dialog tbody:first-of-type button:contains('Currently in use')") |
| 90 |
b.assert_pixels('#dialog', "format-disk") |
| 91 |
|
| 92 |
def retry_setup(): |
| 93 |
self.dialog_set_val("type", "empty") |
| 94 |
|
| 95 |
self.dialog_with_error_retry(trigger=lambda: self.click_card_dropdown("Solid State Drive", |
| 96 |
"Create partition table"), |
| 97 |
first_setup=first_setup, |
| 98 |
retry_setup=retry_setup, |
| 99 |
errors=["Timed out waiting for object"]) |
| 100 |
|
| 101 |
m.execute("! systemctl --quiet is-active keep-mnt-busy") |
| 102 |
|
| 103 |
b.wait_visible(self.card("Unformatted data")) |
| 104 |
|
| 105 |
def testUsedAsPV(self): |
| 106 |
m = self.machine |
| 107 |
b = self.browser |
| 108 |
|
| 109 |
self.login_and_go("/storage") |
| 110 |
|
| 111 |
dev_1 = self.add_ram_disk() |
| 112 |
dev_2 = self.add_loopback_disk() |
| 113 |
b.wait_visible(self.card_row("Storage", name=dev_1)) |
| 114 |
b.wait_visible(self.card_row("Storage", name=dev_2)) |
| 115 |
|
| 116 |
# Create a volume group out of two disks |
| 117 |
m.execute(f"vgcreate TEST1 {dev_1} {dev_2}") |
| 118 |
self.addCleanupVG("TEST1") |
| 119 |
b.wait_visible(self.card_row("Storage", name="TEST1")) |
| 120 |
|
| 121 |
# Formatting dev_1 should cleanly remove it from the volume |
| 122 |
# group. |
| 123 |
|
| 124 |
self.click_card_row("Storage", name=dev_1) |
| 125 |
self.click_card_dropdown("Solid State Drive", "Create partition table") |
| 126 |
b.wait_in_text('#dialog', "remove from LVM2, initialize") |
| 127 |
self.dialog_apply() |
| 128 |
self.dialog_wait_close() |
| 129 |
self.assertEqual(int(m.execute("vgs TEST1 -o pv_count --noheadings")), 1) |
| 130 |
|
| 131 |
# Formatting dev_2 should now cleanly remove the whole volume |
| 132 |
# group. |
| 133 |
|
| 134 |
b.go("#/") |
| 135 |
self.click_card_row("Storage", name=dev_2) |
| 136 |
self.click_card_dropdown("Block device", "Create partition table") |
| 137 |
b.wait_in_text('#dialog', "remove from LVM2, initialize") |
| 138 |
self.dialog_apply() |
| 139 |
self.dialog_wait_close() |
| 140 |
|
| 141 |
self.assertEqual(m.execute("vgs TEST1 || echo GONE").strip(), "GONE") |
| 142 |
|
| 143 |
def testTeardownRetry(self): |
| 144 |
m = self.machine |
| 145 |
b = self.browser |
| 146 |
|
| 147 |
self.login_and_go("/storage") |
| 148 |
|
| 149 |
disk = self.add_ram_disk() |
| 150 |
b.wait_visible(self.card_row("Storage", name=disk)) |
| 151 |
m.execute(f"mke2fs -q -L TEST {disk}") |
| 152 |
m.execute(f"mount {disk} {self.mnt_dir}") |
| 153 |
|
| 154 |
self.click_card_row("Storage", name=disk) |
| 155 |
b.wait_in_text(self.card("ext2 filesystem"), f"The filesystem is currently mounted on {self.mnt_dir}") |
| 156 |
|
| 157 |
# Start formatting, and while the dialog is open, make the |
| 158 |
# filesystem unmountable. |
| 159 |
# |
| 160 |
# We have two processes that keep the filesystem busy: one |
| 161 |
# that is supposed to be picked up by the dialog, and one that |
| 162 |
# is not. The first is only used to figure out when the dialog |
| 163 |
# is done initializing. |
| 164 |
|
| 165 |
m.spawn(f"cd {self.mnt_dir}; sleep infinity; true", "sleep") |
| 166 |
|
| 167 |
self.click_card_dropdown("Solid State Drive", "Create partition table") |
| 168 |
self.dialog_wait_open() |
| 169 |
b.wait_visible("#dialog tbody:first-of-type button:contains('Currently in use')") |
| 170 |
self.dialog_wait_apply_enabled() |
| 171 |
m.spawn(f"cd {self.mnt_dir}; sleep infinity; true", "sleep") |
| 172 |
self.dialog_apply() |
| 173 |
b.wait_in_text("#dialog", f"umount: {self.mnt_dir}: target is busy") |
| 174 |
self.dialog_wait_apply_disabled() |
| 175 |
self.dialog_cancel() |
| 176 |
self.dialog_wait_close() |
| 177 |
|
| 178 |
self.click_card_dropdown("Solid State Drive", "Create partition table") |
| 179 |
self.dialog_wait_open() |
| 180 |
b.wait_visible("#dialog button:contains('Currently in use')") |
| 181 |
self.dialog_apply() |
| 182 |
self.dialog_wait_close() |
| 183 |
|
| 184 |
|
| 185 |
if __name__ == '__main__': |
| 186 |
testlib.test_main() |