From 10e65677ea69031b7c13cca0143d7ca3f2399b90 Mon Sep 17 00:00:00 2001 From: Ian Cardoso Date: Fri, 17 Nov 2023 17:05:54 -0300 Subject: [PATCH] test Signed-off-by: Ian Cardoso --- contrib/util/k3s-killall.sh | 35 +++++++++++-------- tests/e2e/killall/killall_test.go | 6 ++++ tests/integration/startup/startup_int_test.go | 26 ++++++++++++++ 3 files changed, 52 insertions(+), 15 deletions(-) diff --git a/contrib/util/k3s-killall.sh b/contrib/util/k3s-killall.sh index 9ad6a6278f27..314c7d31f601 100644 --- a/contrib/util/k3s-killall.sh +++ b/contrib/util/k3s-killall.sh @@ -1,18 +1,18 @@ #!/bin/sh -[ $(id -u) -eq 0 ] || exec sudo $0 $@ + for bin in /var/lib/rancher/k3s/data/**/bin/; do - [ -d $bin ] && export PATH=$PATH:$bin:$bin/aux + [ -d "$bin" ] && export PATH=$PATH:$bin:$bin/aux done -set -x + for service in /etc/systemd/system/k3s*.service; do - [ -s $service ] && systemctl stop $(basename $service) + [ -s "$service" ] && systemctl stop "$(basename "$service")" done for service in /etc/init.d/k3s*; do - [ -x $service ] && $service stop + [ -x "$service" ] && $service stop done pschildren() { @@ -23,27 +23,30 @@ pschildren() { } pstree() { - for pid in $@; do - echo $pid - for child in $(pschildren $pid); do - pstree $child + + for pid in "$@"; do + if [ -n "$$" ]; then + echo "$pid" + for child in $(pschildren "$pid"); do + pstree "$child" done + fi done } killtree() { - kill -9 $( + kill -9 "$( { set +x; } 2>/dev/null; - pstree $@; + pstree "$@"; set -x; - ) 2>/dev/null + )" 2>/dev/null } remove_interfaces() { # Delete network interface(s) that match 'master cni0' ip link show 2>/dev/null | grep 'master cni0' | while read ignore iface ignore; do iface=${iface%%@*} - [ -z "$iface" ] || ip link delete $iface + [ -z "$iface" ] || ip link delete "$iface" done # Delete cni related interfaces @@ -64,7 +67,7 @@ getshims() { ps -e -o pid= -o args= | sed -e 's/^ *//; s/\s\s*/\t/;' | grep -w 'k3s/data/[^/]*/bin/containerd-shim' | cut -f1 } -killtree $({ set +x; } 2>/dev/null; getshims; set -x) + do_unmount_and_remove() { set +x @@ -74,6 +77,8 @@ do_unmount_and_remove() { set -x } +killtree "$({ set +x; } 2>/dev/null; getshims; set -x)" + do_unmount_and_remove '/run/k3s' do_unmount_and_remove '/var/lib/rancher/k3s' do_unmount_and_remove '/var/lib/kubelet/pods' @@ -87,4 +92,4 @@ remove_interfaces rm -rf /var/lib/cni/ iptables-save | grep -v KUBE- | grep -v CNI- | grep -iv flannel | iptables-restore -ip6tables-save | grep -v KUBE- | grep -v CNI- | grep -iv flannel | ip6tables-restore \ No newline at end of file +ip6tables-save | grep -v KUBE- | grep -v CNI- | grep -iv flannel | ip6tables-restore diff --git a/tests/e2e/killall/killall_test.go b/tests/e2e/killall/killall_test.go index 19d2a99ce3c7..5a2a67a4dd32 100644 --- a/tests/e2e/killall/killall_test.go +++ b/tests/e2e/killall/killall_test.go @@ -88,6 +88,12 @@ var _ = Describe("Verify Create", Ordered, func() { Expect(err).ToNot(HaveOccurred()) } fmt.Println(out) + process, err := e2e.RunCmdOnNode("ps aux | grep k3s", serverNodeNames[0]) + if err != nil { + fmt.Println(err.Error()) + Expect(err).ToNot(HaveOccurred()) + } + fmt.Println(process) }) }) diff --git a/tests/integration/startup/startup_int_test.go b/tests/integration/startup/startup_int_test.go index 13f9c36a3498..0cf7357483b8 100644 --- a/tests/integration/startup/startup_int_test.go +++ b/tests/integration/startup/startup_int_test.go @@ -1,7 +1,9 @@ package integration import ( + "fmt" "os" + "os/exec" "path/filepath" "testing" @@ -124,6 +126,7 @@ var _ = Describe("startup tests", Ordered, func() { Expect(err).ToNot(HaveOccurred()) Expect(apiInfo).To(ContainSubstring("11.11.22.22")) }) + It("dies cleanly", func() { Expect(testutil.K3sKillServer(startupServer)).To(Succeed()) Expect(testutil.K3sCleanup(-1, "")).To(Succeed()) @@ -263,6 +266,29 @@ var _ = Describe("startup tests", Ordered, func() { Expect(testutil.K3sCleanup(-1, "")).To(Succeed()) }) }) + FWhen("a server with a dummy pod", func() { + It("is created with no arguments", func() { + var err error + startupServer, err = testutil.K3sStartServer(startupServerArgs...) + Expect(err).ToNot(HaveOccurred()) + }) + It("has the default pods deployed", func() { + Eventually(func() error { + return testutil.K3sDefaultDeployments() + }, "120s", "5s").Should(Succeed()) + }) + It("dies cleanly", func() { + res, err := testutil.K3sCmd("k3s-killall") + Expect(err).ToNot(HaveOccurred()) + fmt.Print(res) + cmd := exec.Command("ps aux", "|", "grep k3s") + out, err := cmd.Output() + Expect(err).ToNot(HaveOccurred()) + fmt.Print(string(out)) + //Expect(testutil.K3sKillServer(startupServer)).To(Succeed()) + //Expect(testutil.K3sCleanup(-1, "")).To(Succeed()) + }) + }) })