From 511a2ecbc5cd6c5ef4b2a40073e1d9483380c9bd Mon Sep 17 00:00:00 2001
From: Vasiliy Ulyanov <vulyanov@suse.de>
Date: Mon, 21 Aug 2023 11:32:56 +0200
Subject: [PATCH 1/3] tests: Delete VMI prior to NFS server pod

Kubelet fails to umount the NFS volume and gets stuck when performing
pod cleanup if the server has already gone. Ensure that the VMI is
deleted first and the corresponding volume is released (current global
tests cleanup hook does not guarantee deletion order).

Signed-off-by: Vasiliy Ulyanov <vulyanov@suse.de>
---
 tests/storage/storage.go | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/tests/storage/storage.go b/tests/storage/storage.go
index a5828ad27..a1d51a755 100644
--- a/tests/storage/storage.go
+++ b/tests/storage/storage.go
@@ -252,6 +252,11 @@ var _ = SIGDescribe("Storage", func() {
 				var pvName string
 				var nfsPod *k8sv1.Pod
 				AfterEach(func() {
+					// Ensure VMI is deleted before bringing down the NFS server
+					err = virtClient.VirtualMachineInstance(vmi.Namespace).Delete(vmi.Name, &metav1.DeleteOptions{})
+					Expect(err).ToNot(HaveOccurred(), failedDeleteVMI)
+					tests.WaitForVirtualMachineToDisappearWithTimeout(vmi, 120)
+
 					if targetImagePath != testsuite.HostPathAlpine {
 						tests.DeleteAlpineWithNonQEMUPermissions()
 					}
-- 
2.41.0


From 699a189418fd107f53ca361b4d4a68f078efc16d Mon Sep 17 00:00:00 2001
From: Vasiliy Ulyanov <vulyanov@suse.de>
Date: Wed, 23 Aug 2023 14:41:57 +0200
Subject: [PATCH 2/3] tests: Delete AlpineWithNonQEMUPermissions image

Previously the disk image was not deleted because targetImagePath was
not set properly. The condition in AfterEach was never positive:

    if targetImagePath != testsuite.HostPathAlpine {
        tests.DeleteAlpineWithNonQEMUPermissions()
    }

Signed-off-by: Vasiliy Ulyanov <vulyanov@suse.de>
---
 tests/storage/storage.go | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/tests/storage/storage.go b/tests/storage/storage.go
index a1d51a755..a8dc19c6d 100644
--- a/tests/storage/storage.go
+++ b/tests/storage/storage.go
@@ -267,11 +267,10 @@ var _ = SIGDescribe("Storage", func() {
 					var nodeName string
 					// Start the VirtualMachineInstance with the PVC attached
 					if storageEngine == "nfs" {
-						targetImage := targetImagePath
 						if !imageOwnedByQEMU {
-							targetImage, nodeName = tests.CopyAlpineWithNonQEMUPermissions()
+							targetImagePath, nodeName = tests.CopyAlpineWithNonQEMUPermissions()
 						}
-						nfsPod = storageframework.InitNFS(targetImage, nodeName)
+						nfsPod = storageframework.InitNFS(targetImagePath, nodeName)
 						pvName = createNFSPvAndPvc(family, nfsPod)
 					} else {
 						pvName = tests.DiskAlpineHostPath
-- 
2.41.0


From 7848346ee364dfa402a13ed36a56705f16b8f137 Mon Sep 17 00:00:00 2001
From: Vasiliy Ulyanov <vulyanov@suse.de>
Date: Thu, 24 Aug 2023 11:34:47 +0200
Subject: [PATCH 3/3] Hack nfs-server image to run it 'graceless'

The NFS grace period is set to 90 seconds and it stalls the clients
trying to access the share right after the server start. This may affect
the tests and lead to timeouts so disable the setting.

Signed-off-by: Vasiliy Ulyanov <vulyanov@suse.de>
---
 images/nfs-server/BUILD.bazel   | 12 ++++++++++++
 images/nfs-server/entrypoint.sh | 12 ++++++++++++
 2 files changed, 24 insertions(+)
 create mode 100644 images/nfs-server/entrypoint.sh

diff --git a/images/nfs-server/BUILD.bazel b/images/nfs-server/BUILD.bazel
index 343d72cf1..8494fcac5 100644
--- a/images/nfs-server/BUILD.bazel
+++ b/images/nfs-server/BUILD.bazel
@@ -2,6 +2,14 @@ load(
     "@io_bazel_rules_docker//container:container.bzl",
     "container_image",
 )
+load("@rules_pkg//:pkg.bzl", "pkg_tar")
+
+pkg_tar(
+    name = "entrypoint",
+    srcs = [":entrypoint.sh"],
+    mode = "0775",
+    package_dir = "/",
+)
 
 container_image(
     name = "nfs-server-image",
@@ -13,6 +21,7 @@ container_image(
         "@io_bazel_rules_go//go/platform:linux_arm64": "@nfs-server_aarch64//image",
         "//conditions:default": "@nfs-server//image",
     }),
+    cmd = ["/entrypoint.sh"],
     ports = [
         "111/udp",
         "2049/udp",
@@ -25,5 +34,8 @@ container_image(
         "32766/tcp",
         "32767/tcp",
     ],
+    tars = [
+        ":entrypoint",
+    ],
     visibility = ["//visibility:public"],
 )
diff --git a/images/nfs-server/entrypoint.sh b/images/nfs-server/entrypoint.sh
new file mode 100644
index 000000000..aa40154cd
--- /dev/null
+++ b/images/nfs-server/entrypoint.sh
@@ -0,0 +1,12 @@
+#!/bin/bash
+
+set -euxo pipefail
+
+# The NFS grace period is set to 90 seconds and it stalls the clients
+# trying to access the share right after the server start. This may affect
+# the tests and lead to timeouts so disable the setting.
+sed -i"" \
+    -e "s#Grace_Period = 90#Graceless = true#g" \
+    /opt/start_nfs.sh
+
+exec /opt/start_nfs.sh
-- 
2.41.0

