| 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 packagelib |
| 9 |
import storagelib |
| 10 |
import testlib |
| 11 |
|
| 12 |
|
| 13 |
@testlib.nondestructive |
| 14 |
class TestStorageNfs(storagelib.StorageCase): |
| 15 |
|
| 16 |
def testNfsClient(self): |
| 17 |
m = self.machine |
| 18 |
b = self.browser |
| 19 |
|
| 20 |
self.login_and_go("/storage") |
| 21 |
|
| 22 |
m.execute(f"mkdir {self.mnt_dir}/foo {self.mnt_dir}/bar {self.mnt_dir}/test") |
| 23 |
self.write_file("/etc/exports", f"{self.mnt_dir}/foo 127.0.0.0/24(rw)\n{self.mnt_dir}/bar 127.0.0.0/24(rw)\n", |
| 24 |
post_restore_action="systemctl restart nfs-server") |
| 25 |
m.execute("systemctl restart nfs-server") |
| 26 |
# Removing the nfs mount removes the target dir, if the test fails it |
| 27 |
# won't. It's important to umount before restoring /etc/exports as |
| 28 |
# otherwise nfs is confused and we can't umount the share. |
| 29 |
self.addCleanup(m.execute, f"if [ -e {self.mnt_dir}/test ]; then rm -r {self.mnt_dir}/test; fi") |
| 30 |
self.addCleanupMount(f"{self.mnt_dir}/test") |
| 31 |
|
| 32 |
orig_fstab = m.execute("cat /etc/fstab") |
| 33 |
|
| 34 |
# Add /foo |
| 35 |
self.click_dropdown(self.card_header("Storage"), "New NFS mount") |
| 36 |
self.dialog_wait_open() |
| 37 |
self.dialog_set_val("server", "127.0.0.1") |
| 38 |
self.dialog_set_val("remote", f"{self.mnt_dir}/foo") |
| 39 |
self.dialog_set_val("dir", f"{self.mnt_dir}/test") |
| 40 |
self.dialog_apply() |
| 41 |
self.dialog_wait_close() |
| 42 |
|
| 43 |
b.wait_visible(self.card_row("Storage", name=f"127.0.0.1:{self.mnt_dir}/foo")) |
| 44 |
b.wait_visible(self.card_row("Storage", location=f"{self.mnt_dir}/test")) |
| 45 |
b.wait_visible(self.card_row("Storage", location=f"{self.mnt_dir}/test") + " .usage-bar[role=progressbar]") |
| 46 |
|
| 47 |
# Should be saved to fstab |
| 48 |
self.assertEqual(m.execute("cat /etc/fstab"), orig_fstab + |
| 49 |
f"127.0.0.1:{self.mnt_dir}/foo {self.mnt_dir}/test nfs defaults\n") |
| 50 |
|
| 51 |
# Try to add some non-exported directory |
| 52 |
self.click_dropdown(self.card_header("Storage"), "New NFS mount") |
| 53 |
self.dialog_wait_open() |
| 54 |
self.dialog_set_val("server", "127.0.0.1") |
| 55 |
b.set_input_text(self.dialog_field("remote") + " input", "/usr/share") |
| 56 |
b.click(".pf-v6-c-menu ul > li > button") |
| 57 |
self.dialog_set_val("dir", f"{self.mnt_dir}/share") |
| 58 |
self.dialog_apply() |
| 59 |
b.wait_visible("#dialog .pf-v6-c-alert.pf-m-danger") |
| 60 |
self.dialog_cancel() |
| 61 |
self.dialog_wait_close() |
| 62 |
|
| 63 |
# Add /bar |
| 64 |
self.click_dropdown(self.card_header("Storage"), "New NFS mount") |
| 65 |
self.dialog_wait_open() |
| 66 |
self.dialog_set_val("server", "127.0.0.1") |
| 67 |
self.dialog_set_val("remote", f"{self.mnt_dir}/bar") |
| 68 |
self.dialog_set_val("dir", f"{self.mnt_dir}s/bar") |
| 69 |
self.dialog_apply() |
| 70 |
self.dialog_wait_close() |
| 71 |
|
| 72 |
b.wait_visible(self.card_row("Storage", name=f"127.0.0.1:{self.mnt_dir}/bar")) |
| 73 |
b.wait_visible(self.card_row("Storage", location=f"{self.mnt_dir}s/bar")) |
| 74 |
b.wait_visible(self.card_row("Storage", location=f"{self.mnt_dir}s/bar") + " .usage-bar[role=progressbar]") |
| 75 |
m.execute(f"test -d {self.mnt_dir}s/bar") |
| 76 |
|
| 77 |
# Go to details of /bar |
| 78 |
self.click_card_row("Storage", name=f"127.0.0.1:{self.mnt_dir}/bar") |
| 79 |
b.wait_text(self.card_desc("NFS mount", "Server"), f"127.0.0.1:{self.mnt_dir}/bar") |
| 80 |
b.wait_text(self.card_desc("NFS mount", "Mount point"), f"{self.mnt_dir}s/bar") |
| 81 |
|
| 82 |
# Change mount point of /bar |
| 83 |
b.click(self.card_button("NFS mount", "Edit")) |
| 84 |
self.dialog_wait_open() |
| 85 |
self.dialog_set_val("dir", f"{self.mnt_dir}s/barbar") |
| 86 |
self.dialog_apply() |
| 87 |
self.dialog_wait_close() |
| 88 |
self.addCleanup(m.execute, f"umount {self.mnt_dir}s/barbar; rmdir {self.mnt_dir}s/barbar") |
| 89 |
|
| 90 |
b.wait_text(self.card_desc("NFS mount", "Mount point"), f"{self.mnt_dir}s/barbar") |
| 91 |
m.execute(f"! test -e {self.mnt_dir}s/bar") |
| 92 |
m.execute(f"test -d {self.mnt_dir}s/barbar") |
| 93 |
self.assertEqual(m.execute(f"findmnt -s -n -o OPTIONS {self.mnt_dir}s/barbar").strip(), "defaults") |
| 94 |
|
| 95 |
# Set options for /barbar |
| 96 |
b.click(self.card_button("NFS mount", "Edit")) |
| 97 |
|
| 98 |
def wait_checked(field: str) -> None: |
| 99 |
b.wait_visible(self.dialog_field(field) + ":checked") |
| 100 |
|
| 101 |
def wait_not_checked(field: str) -> None: |
| 102 |
b.wait_visible(self.dialog_field(field) + ":not(:checked)") |
| 103 |
|
| 104 |
self.dialog_wait_open() |
| 105 |
wait_checked("mount_options.auto") |
| 106 |
wait_not_checked("mount_options.ro") |
| 107 |
self.dialog_set_val("mount_options.auto", val=False) |
| 108 |
self.dialog_set_val("mount_options.ro", val=True) |
| 109 |
self.dialog_set_val("mount_options.extra", "ac") |
| 110 |
self.dialog_apply() |
| 111 |
self.dialog_wait_close() |
| 112 |
|
| 113 |
self.assertEqual(m.execute(f"findmnt -s -n -o OPTIONS {self.mnt_dir}s/barbar").strip(), "noauto,ro,ac") |
| 114 |
|
| 115 |
# Should be saved to fstab |
| 116 |
self.assertEqual(m.execute("cat /etc/fstab"), orig_fstab + |
| 117 |
f"127.0.0.1:{self.mnt_dir}/foo {self.mnt_dir}/test nfs defaults\n" + |
| 118 |
f"127.0.0.1:{self.mnt_dir}/bar {self.mnt_dir}s/barbar nfs noauto,ro,ac\n") |
| 119 |
|
| 120 |
# Go to details of /foo |
| 121 |
b.go("#/") |
| 122 |
self.click_card_row("Storage", name=f"127.0.0.1:{self.mnt_dir}/foo") |
| 123 |
b.wait_text(self.card_desc("NFS mount", "Server"), f"127.0.0.1:{self.mnt_dir}/foo") |
| 124 |
b.wait_text(self.card_desc("NFS mount", "Mount point"), f"{self.mnt_dir}/test") |
| 125 |
|
| 126 |
# Unmount and remount /foo |
| 127 |
b.click(self.card_button("NFS mount", "Unmount")) |
| 128 |
b.click(self.card_button("NFS mount", "Mount")) |
| 129 |
b.wait_visible(self.card_button("NFS mount", "Unmount")) |
| 130 |
|
| 131 |
# Remove /foo |
| 132 |
self.click_card_dropdown("NFS mount", "Remove") |
| 133 |
b.wait_visible(self.card("Storage")) |
| 134 |
b.wait_not_present(self.card_row("Storage", name=f"127.0.0.1:{self.mnt_dir}/foo")) |
| 135 |
m.execute(f"! test -e {self.mnt_dir}/test") |
| 136 |
# Should be removed from fstab, too |
| 137 |
self.assertEqual(m.execute("cat /etc/fstab"), orig_fstab + |
| 138 |
f"127.0.0.1:{self.mnt_dir}/bar {self.mnt_dir}s/barbar nfs noauto,ro,ac\n") |
| 139 |
|
| 140 |
# Picks up mounts in fstab |
| 141 |
m.execute(f"echo '1.2.3.4:/something {self.mnt_dir}/somewhere nfs defaults 0 0' >> /etc/fstab") |
| 142 |
b.wait_visible(self.card_row("Storage", name="1.2.3.4:/something")) |
| 143 |
b.wait_visible(self.card_row("Storage", location=f"{self.mnt_dir}/somewhere (not mounted)")) |
| 144 |
|
| 145 |
# Ignores non-FS mounts which look similar |
| 146 |
m.execute(f"echo '2.3.4.5:/marmalade {self.mnt_dir}/dunno rfs defaults 0 0' >> /etc/fstab") |
| 147 |
# But recognizes variants like "nfs4" |
| 148 |
m.execute(f"echo '5.6.7.8:/stuff {self.mnt_dir}/four nfs4 defaults 0 0' >> /etc/fstab") |
| 149 |
b.wait_visible(self.card_row("Storage", name="5.6.7.8:/stuff")) |
| 150 |
b.wait_visible(self.card_row("Storage", location=f"{self.mnt_dir}/four (not mounted)")) |
| 151 |
b.wait_not_present(self.card_row("Storage", name="2.3.4.5:/marmalade")) |
| 152 |
|
| 153 |
def testNfsListExports(self): |
| 154 |
m = self.machine |
| 155 |
b = self.browser |
| 156 |
|
| 157 |
self.login_and_go("/storage") |
| 158 |
|
| 159 |
m.execute(f"mkdir {self.mnt_dir}/foo {self.mnt_dir}/bar") |
| 160 |
self.write_file("/etc/exports", f"{self.mnt_dir}/foo 127.0.0.0/24(rw)\n{self.mnt_dir}/bar 127.0.0.0/24(rw)\n", |
| 161 |
post_restore_action="systemctl restart nfs-server") |
| 162 |
m.execute("systemctl restart nfs-server") |
| 163 |
|
| 164 |
self.click_dropdown(self.card_header("Storage"), "New NFS mount") |
| 165 |
self.dialog_wait_open() |
| 166 |
self.dialog_set_val("server", "127.0.0.1") |
| 167 |
|
| 168 |
def wait_for_exports(): |
| 169 |
def check(): |
| 170 |
choices = self.dialog_combobox_choices("remote") |
| 171 |
return len(choices) == 2 and f"{self.mnt_dir}/foo" in choices and f"{self.mnt_dir}/bar" in choices |
| 172 |
self.retry(check) |
| 173 |
|
| 174 |
b.click('#dialog [data-field="remote"] button.pf-v6-c-menu-toggle__button') |
| 175 |
wait_for_exports() |
| 176 |
|
| 177 |
def testNfsMountWithoutDiscovery(self): |
| 178 |
m = self.machine |
| 179 |
b = self.browser |
| 180 |
|
| 181 |
self.login_and_go("/storage") |
| 182 |
|
| 183 |
m.execute(f"mkdir {self.mnt_dir}/srv-foo {self.mnt_dir}/srv-bar") |
| 184 |
self.addCleanup(m.execute, "systemctl restart nfs-server") |
| 185 |
self.write_file("/etc/exports", |
| 186 |
f"{self.mnt_dir}/srv-foo 127.0.0.0/24(rw)\n{self.mnt_dir}/srv-bar 127.0.0.0/24(rw)\n", |
| 187 |
post_restore_action="systemctl restart nfs-server") |
| 188 |
m.execute("systemctl restart nfs-server") |
| 189 |
|
| 190 |
# Break showmount. Cockpit uses showmount to list exported |
| 191 |
# directories, but that relies on a properly configured |
| 192 |
# firewall etc. Even if showmount doesn't work, Cockpit |
| 193 |
# should allow people to mount arbitrary directories. |
| 194 |
m.execute("mount -o bind /dev/null /usr/sbin/showmount") |
| 195 |
self.addCleanup(m.execute, "umount /usr/sbin/showmount") |
| 196 |
|
| 197 |
self.click_dropdown(self.card_header("Storage"), "New NFS mount") |
| 198 |
self.dialog_wait_open() |
| 199 |
self.dialog_set_val("server", "127.0.0.1") |
| 200 |
# Manually add a remote location to the select |
| 201 |
b.set_input_text("[data-field=remote] input", f"{self.mnt_dir}/srv-foo") |
| 202 |
b.click(".pf-v6-c-menu button.pf-v6-c-menu__item") |
| 203 |
self.dialog_set_val("dir", f"{self.mnt_dir}/foo") |
| 204 |
self.dialog_apply() |
| 205 |
self.dialog_wait_close() |
| 206 |
self.addCleanup(m.execute, f"umount {self.mnt_dir}/foo") |
| 207 |
|
| 208 |
b.wait_visible(self.card_row("Storage", name=f"127.0.0.1:{self.mnt_dir}/srv-foo")) |
| 209 |
b.wait_visible(self.card_row("Storage", location=f"{self.mnt_dir}/foo")) |
| 210 |
b.wait_visible(self.card_row("Storage", location=f"{self.mnt_dir}/foo") + " .usage-bar[role=progressbar]") |
| 211 |
|
| 212 |
def testNfsBusy(self): |
| 213 |
m = self.machine |
| 214 |
b = self.browser |
| 215 |
|
| 216 |
self.login_and_go("/storage") |
| 217 |
|
| 218 |
m.execute(f"mkdir {self.mnt_dir}/foo {self.mnt_dir}/bar") |
| 219 |
self.addCleanup(m.execute, "systemctl restart nfs-server") |
| 220 |
self.write_file("/etc/exports", f"{self.mnt_dir}/foo 127.0.0.0/24(rw)\n{self.mnt_dir}/bar 127.0.0.0/24(rw)\n", |
| 221 |
post_restore_action="systemctl restart nfs-server") |
| 222 |
m.execute("systemctl restart nfs-server") |
| 223 |
|
| 224 |
# Add /foo |
| 225 |
self.click_dropdown(self.card_header("Storage"), "New NFS mount") |
| 226 |
self.dialog_wait_open() |
| 227 |
self.dialog_set_val("server", "127.0.0.1") |
| 228 |
self.dialog_set_val("remote", f"{self.mnt_dir}/foo") |
| 229 |
self.dialog_set_val("dir", f"{self.mnt_dir}s/foo") |
| 230 |
self.dialog_apply() |
| 231 |
self.dialog_wait_close() |
| 232 |
|
| 233 |
b.wait_visible(self.card_row("Storage", name=f"127.0.0.1:{self.mnt_dir}/foo")) |
| 234 |
b.wait_visible(self.card_row("Storage", location=f"{self.mnt_dir}s/foo")) |
| 235 |
b.wait_visible(self.card_row("Storage", location=f"{self.mnt_dir}s/foo") + " .usage-bar[role=progressbar]") |
| 236 |
|
| 237 |
# Go to details of /foo |
| 238 |
self.click_card_row("Storage", name=f"127.0.0.1:{self.mnt_dir}/foo") |
| 239 |
b.wait_text(self.card_desc("NFS mount", "Server"), f"127.0.0.1:{self.mnt_dir}/foo") |
| 240 |
b.wait_text(self.card_desc("NFS mount", "Mount point"), f"{self.mnt_dir}s/foo") |
| 241 |
|
| 242 |
sleep_pid = m.spawn(f"cd {self.mnt_dir}s/foo; sleep infinity", "busy") |
| 243 |
b.click(self.card_button("NFS mount", "Edit")) |
| 244 |
|
| 245 |
self.dialog_wait_open() |
| 246 |
self.dialog_wait_alert("This NFS mount is in use") |
| 247 |
self.dialog_cancel() |
| 248 |
self.dialog_wait_close() |
| 249 |
|
| 250 |
b.click(self.card_button("NFS mount", "Unmount")) |
| 251 |
self.dialog_wait_open() |
| 252 |
b.wait_in_text("#dialog", str(sleep_pid)) |
| 253 |
b.wait_in_text("#dialog", "sleep infinity") |
| 254 |
b.wait_in_text("#dialog", "The listed processes will be forcefully stopped.") |
| 255 |
b.wait_in_text("#dialog", "Stop and unmount") |
| 256 |
b.assert_pixels("#dialog", "unmount", mock={"td[data-label='PID']": "1234", |
| 257 |
"td[data-label='Started']": "a little while ago"}) |
| 258 |
self.dialog_apply() |
| 259 |
self.dialog_wait_close() |
| 260 |
|
| 261 |
b.click(self.card_button("NFS mount", "Mount")) |
| 262 |
b.wait_visible(self.card_button("NFS mount", "Unmount")) |
| 263 |
|
| 264 |
sleep_pid = m.spawn(f"cd {self.mnt_dir}s/foo; sleep infinity", "busy") |
| 265 |
self.click_card_dropdown("NFS mount", "Remove") |
| 266 |
b.wait_in_text("#dialog", str(sleep_pid)) |
| 267 |
b.wait_in_text("#dialog", "sleep infinity") |
| 268 |
b.wait_in_text("#dialog", "The listed processes will be forcefully stopped.") |
| 269 |
b.wait_in_text("#dialog", "Stop and remove") |
| 270 |
self.dialog_apply() |
| 271 |
self.dialog_wait_close() |
| 272 |
|
| 273 |
# We are back on the Overview with nothing there |
| 274 |
b.wait_visible(self.card("Storage")) |
| 275 |
b.wait_not_present(self.card_row("Storage", name=f"127.0.0.1:{self.mnt_dir}/foo")) |
| 276 |
b.wait_not_present(self.card_row("Storage", location=f"{self.mnt_dir}s/foo")) |
| 277 |
|
| 278 |
|
| 279 |
# Re-use allowed journal messages from StorageCase |
| 280 |
@testlib.skipOstree("No udisks/cockpit-storaged on OSTree images") |
| 281 |
@testlib.nondestructive |
| 282 |
class TestStoragePackagesNFS(packagelib.PackageCase, storagelib.StorageCase): |
| 283 |
|
| 284 |
def testNfsMissingPackages(self): |
| 285 |
m = self.machine |
| 286 |
b = self.browser |
| 287 |
|
| 288 |
m.execute("systemctl restart nfs-server") |
| 289 |
|
| 290 |
# Override configuration so that we don't have to remove the |
| 291 |
# real package. |
| 292 |
|
| 293 |
self.machine.write("/etc/cockpit/storaged.override.json", |
| 294 |
"""{ "config": { "nfs_client_package": "fake-nfs-utils" } }""") |
| 295 |
|
| 296 |
if m.execute("if test -e /sbin/mount.nfs; then echo yes; fi").strip(): |
| 297 |
m.execute("mv /sbin/mount.nfs /sbin/mount.nfs.off") |
| 298 |
self.addCleanup(m.execute, "mv /sbin/mount.nfs.off /sbin/mount.nfs") |
| 299 |
|
| 300 |
self.login_and_go("/storage") |
| 301 |
|
| 302 |
# The fake-nfs-utils package is not available yet |
| 303 |
|
| 304 |
self.click_dropdown(self.card_header("Storage"), "New NFS mount") |
| 305 |
self.dialog_wait_open() |
| 306 |
b.wait_in_text("#dialog", "fake-nfs-utils is not available from any repository.") |
| 307 |
self.dialog_cancel() |
| 308 |
self.dialog_wait_close() |
| 309 |
|
| 310 |
# Now make the package available |
| 311 |
|
| 312 |
self.createPackage("dummy", "1.0", "1") |
| 313 |
self.createPackage("fake-nfs-utils", "1.0", "1", depends="fake-libnfs") |
| 314 |
self.createPackage("fake-libnfs", "1.0", "1") |
| 315 |
self.enableRepo() |
| 316 |
|
| 317 |
# HACK |
| 318 |
# |
| 319 |
# The first simulated install seems to silently not report |
| 320 |
# anything on the Debian test images, for unknown reasons. So |
| 321 |
# we install a dummy package to warm up all parts of the |
| 322 |
# machinery and distribute the fluids evenly. |
| 323 |
# https://github.com/PackageKit/PackageKit/issues/893 |
| 324 |
# |
| 325 |
if "debian" in m.image or "ubuntu" in m.image: |
| 326 |
m.execute(f"{self.packagekit_cmd} refresh; {self.packagekit_cmd} install -y dummy") |
| 327 |
|
| 328 |
# HACK |
| 329 |
# Packagekit on Arch Linux does not deal well with detecting new repositories |
| 330 |
if m.image == "arch": |
| 331 |
self.refreshMetadata(force=True) |
| 332 |
|
| 333 |
self.click_dropdown(self.card_header("Storage"), "New NFS mount") |
| 334 |
self.dialog_wait_open() |
| 335 |
b.wait_in_text("#dialog", "fake-nfs-utils will be installed") |
| 336 |
b.wait_in_text("#dialog", "fake-libnfs") |
| 337 |
self.dialog_apply() |
| 338 |
self.dialog_set_val("server", "127.0.0.1") |
| 339 |
self.dialog_cancel() |
| 340 |
self.dialog_wait_close() |
| 341 |
|
| 342 |
# Now we should go straight to the main dialog |
| 343 |
self.click_dropdown(self.card_header("Storage"), "New NFS mount") |
| 344 |
self.dialog_wait_open() |
| 345 |
self.dialog_set_val("server", "127.0.0.1") |
| 346 |
self.dialog_cancel() |
| 347 |
self.dialog_wait_close() |
| 348 |
|
| 349 |
|
| 350 |
if __name__ == '__main__': |
| 351 |
testlib.test_main() |