From 7392806b7fc1db80dcb83531ff9b4bc6f686e048 Mon Sep 17 00:00:00 2001 From: Derek Nola Date: Fri, 17 Jan 2025 10:52:00 -0800 Subject: [PATCH] Make (Docker) RunOnNode a method Signed-off-by: Derek Nola --- tests/docker/basics/basics_test.go | 6 +-- .../bootstraptoken/bootstraptoken_test.go | 2 +- tests/docker/lazypull/lazypull_test.go | 2 +- tests/docker/skew/skew_test.go | 8 +-- tests/docker/test-helpers.go | 53 ++++++++++--------- tests/docker/upgrade/upgrade_test.go | 12 ++--- 6 files changed, 43 insertions(+), 40 deletions(-) diff --git a/tests/docker/basics/basics_test.go b/tests/docker/basics/basics_test.go index 54a0092a23..ea2765e5e9 100644 --- a/tests/docker/basics/basics_test.go +++ b/tests/docker/basics/basics_test.go @@ -59,9 +59,9 @@ var _ = Describe("Basic Tests", Ordered, func() { Context("Verify Binaries and Images", func() { It("has valid bundled binaries", func() { for _, server := range config.Servers { - Expect(tester.VerifyValidVersion(server.Name, "kubectl")).To(Succeed()) - Expect(tester.VerifyValidVersion(server.Name, "ctr")).To(Succeed()) - Expect(tester.VerifyValidVersion(server.Name, "crictl")).To(Succeed()) + Expect(tester.VerifyValidVersion(server, "kubectl")).To(Succeed()) + Expect(tester.VerifyValidVersion(server, "ctr")).To(Succeed()) + Expect(tester.VerifyValidVersion(server, "crictl")).To(Succeed()) } }) It("has valid airgap images", func() { diff --git a/tests/docker/bootstraptoken/bootstraptoken_test.go b/tests/docker/bootstraptoken/bootstraptoken_test.go index e61f08baca..87e1fb1401 100644 --- a/tests/docker/bootstraptoken/bootstraptoken_test.go +++ b/tests/docker/bootstraptoken/bootstraptoken_test.go @@ -37,7 +37,7 @@ var _ = Describe("Boostrap Token Tests", Ordered, func() { var newSecret string It("creates a bootstrap token", func() { var err error - newSecret, err = tester.RunCmdOnDocker(config.Servers[0].Name, "k3s token create --ttl=5m --description=Test") + newSecret, err = config.Servers[0].RunCmdOnNode("k3s token create --ttl=5m --description=Test") Expect(err).NotTo(HaveOccurred()) Expect(newSecret).NotTo(BeEmpty()) }) diff --git a/tests/docker/lazypull/lazypull_test.go b/tests/docker/lazypull/lazypull_test.go index 47f3c470be..c9c4870518 100644 --- a/tests/docker/lazypull/lazypull_test.go +++ b/tests/docker/lazypull/lazypull_test.go @@ -84,7 +84,7 @@ func lookLayers(node, layer string) error { layersNum := 0 var err error for layersNum = 0; layersNum < 100; layersNum++ { - // We use RunCommand instead of RunCmdOnDocker because we pipe the output to jq + // We use RunCommand instead of RunCmdOnNode because we pipe the output to jq cmd := fmt.Sprintf("docker exec -i %s ctr --namespace=k8s.io snapshot --snapshotter=stargz info %s | jq -r '.Parent'", node, layer) layer, err = tester.RunCommand(cmd) if err != nil { diff --git a/tests/docker/skew/skew_test.go b/tests/docker/skew/skew_test.go index 73bd71b696..e551be3ac6 100644 --- a/tests/docker/skew/skew_test.go +++ b/tests/docker/skew/skew_test.go @@ -72,7 +72,7 @@ var _ = Describe("Skew Tests", Ordered, func() { }) It("should match respective versions", func() { for _, server := range config.Servers { - out, err := tester.RunCmdOnDocker(server.Name, "k3s --version") + out, err := server.RunCmdOnNode("k3s --version") Expect(err).NotTo(HaveOccurred()) // The k3s image is in the format rancher/k3s:v1.20.0-k3s1 cVersion := strings.Split(*k3sImage, ":")[1] @@ -81,7 +81,7 @@ var _ = Describe("Skew Tests", Ordered, func() { Expect(out).To(ContainSubstring(cVersion)) } for _, agent := range config.Agents { - Expect(tester.RunCmdOnDocker(agent.Name, "k3s --version")). + Expect(agent.RunCmdOnNode("k3s --version")). To(ContainSubstring(strings.Replace(lastMinorVersion, "-", "+", 1))) } }) @@ -120,11 +120,11 @@ var _ = Describe("Skew Tests", Ordered, func() { }, "60s", "5s").Should(Succeed()) }) It("should match respective versions", func() { - out, err := tester.RunCmdOnDocker(config.Servers[0].Name, "k3s --version") + out, err := config.Servers[0].RunCmdOnNode("k3s --version") Expect(err).NotTo(HaveOccurred()) Expect(out).To(ContainSubstring(strings.Replace(lastMinorVersion, "-", "+", 1))) for _, server := range config.Servers[1:] { - out, err := tester.RunCmdOnDocker(server.Name, "k3s --version") + out, err := server.RunCmdOnNode("k3s --version") Expect(err).NotTo(HaveOccurred()) // The k3s image is in the format rancher/k3s:v1.20.0-k3s1-amd64 cVersion := strings.Split(*k3sImage, ":")[1] diff --git a/tests/docker/test-helpers.go b/tests/docker/test-helpers.go index 8d7bf2aeac..acc13182df 100644 --- a/tests/docker/test-helpers.go +++ b/tests/docker/test-helpers.go @@ -32,21 +32,20 @@ type TestConfig struct { NumAgents int NeedRestart bool Servers []Server - Agents []Agent + Agents []DockerNode ServerYaml string AgentYaml string } -type Server struct { +type DockerNode struct { Name string - Port int IP string - URL string } -type Agent struct { - Name string - IP string +type Server struct { + DockerNode + Port int + URL string } // NewTestConfig initializes the test environment and returns the configuration @@ -140,6 +139,12 @@ func (config *TestConfig) ProvisionServers(numOfServers int) error { joinOrStart = fmt.Sprintf("--server %s", config.Servers[0].URL) } } + newServer := Server{ + DockerNode: DockerNode{ + Name: name, + }, + Port: port, + } // If we need restarts, we use the systemd-node container, volume mount the k3s binary // and start the server using the install script @@ -167,7 +172,7 @@ func (config *TestConfig) ProvisionServers(numOfServers int) error { // The pipe requires that we use sh -c with "" to run the command sCmd := fmt.Sprintf("/bin/sh -c \"curl -sfL https://get.k3s.io | INSTALL_K3S_EXEC='%s' INSTALL_K3S_SKIP_DOWNLOAD=true sh -\"", joinOrStart+" "+os.Getenv(fmt.Sprintf("SERVER_%d_ARGS", i))) - if out, err := RunCmdOnDocker(name, sCmd); err != nil { + if out, err := newServer.RunCmdOnNode(sCmd); err != nil { return fmt.Errorf("failed to start server: %s: %v", out, err) } } else { @@ -199,13 +204,9 @@ func (config *TestConfig) ProvisionServers(numOfServers int) error { ip := strings.TrimSpace(ipOutput) url := fmt.Sprintf("https://%s:6443", ip) - - config.Servers = append(config.Servers, Server{ - Name: name, - Port: port, - IP: ip, - URL: url, - }) + newServer.URL = url + newServer.IP = ip + config.Servers = append(config.Servers, newServer) fmt.Printf("Started %s @ %s\n", name, url) @@ -235,6 +236,9 @@ func (config *TestConfig) ProvisionAgents(numOfAgents int) error { name := fmt.Sprintf("k3s-agent-%d-%s", i, strings.ToLower(testID)) agentInstanceArgs := fmt.Sprintf("AGENT_%d_ARGS", i) + newAgent := DockerNode{ + Name: name, + } if config.NeedRestart { dRun := strings.Join([]string{"docker run -d", @@ -257,7 +261,8 @@ func (config *TestConfig) ProvisionAgents(numOfAgents int) error { // The pipe requires that we use sh -c with "" to run the command sCmd := fmt.Sprintf("/bin/sh -c \"curl -sfL https://get.k3s.io | INSTALL_K3S_EXEC='agent %s' INSTALL_K3S_SKIP_DOWNLOAD=true sh -\"", os.Getenv(agentInstanceArgs)) - if out, err := RunCmdOnDocker(name, sCmd); err != nil { + + if out, err := newAgent.RunCmdOnNode(sCmd); err != nil { return fmt.Errorf("failed to start server: %s: %v", out, err) } } else { @@ -285,11 +290,9 @@ func (config *TestConfig) ProvisionAgents(numOfAgents int) error { return err } ip := strings.TrimSpace(ipOutput) + newAgent.IP = ip + config.Agents = append(config.Agents, newAgent) - config.Agents = append(config.Agents, Agent{ - Name: name, - IP: ip, - }) fmt.Printf("Started %s\n", name) return nil }) @@ -385,9 +388,9 @@ func copyAndModifyKubeconfig(config *TestConfig) error { return nil } -// RunCmdOnDocker runs a command on a docker container -func RunCmdOnDocker(container, cmd string) (string, error) { - dCmd := fmt.Sprintf("docker exec %s %s", container, cmd) +// RunCmdOnNode runs a command on a docker container +func (node DockerNode) RunCmdOnNode(cmd string) (string, error) { + dCmd := fmt.Sprintf("docker exec %s %s", node.Name, cmd) return RunCommand(dCmd) } @@ -425,8 +428,8 @@ func getEnvOrDefault(key, defaultValue string) string { } // VerifyValidVersion checks for invalid version strings -func VerifyValidVersion(container string, binary string) error { - output, err := RunCmdOnDocker(container, binary+" version") +func VerifyValidVersion(node Server, binary string) error { + output, err := node.RunCmdOnNode(binary + " version") if err != nil { return err } diff --git a/tests/docker/upgrade/upgrade_test.go b/tests/docker/upgrade/upgrade_test.go index 9bec74438b..639fd058de 100644 --- a/tests/docker/upgrade/upgrade_test.go +++ b/tests/docker/upgrade/upgrade_test.go @@ -1,4 +1,4 @@ -package main +package upgrade import ( "flag" @@ -81,7 +81,7 @@ var _ = Describe("Upgrade Tests", Ordered, func() { }) It("should confirm latest version", func() { for _, server := range config.Servers { - out, err := tester.RunCmdOnDocker(server.Name, "k3s --version") + out, err := server.RunCmdOnNode("k3s --version") Expect(err).NotTo(HaveOccurred()) Expect(out).To(ContainSubstring(strings.Replace(latestVersion, "-", "+", 1))) } @@ -127,11 +127,11 @@ var _ = Describe("Upgrade Tests", Ordered, func() { }) It("should confirm commit version", func() { for _, server := range config.Servers { - Expect(tester.VerifyValidVersion(server.Name, "kubectl")).To(Succeed()) - Expect(tester.VerifyValidVersion(server.Name, "ctr")).To(Succeed()) - Expect(tester.VerifyValidVersion(server.Name, "crictl")).To(Succeed()) + Expect(tester.VerifyValidVersion(server, "kubectl")).To(Succeed()) + Expect(tester.VerifyValidVersion(server, "ctr")).To(Succeed()) + Expect(tester.VerifyValidVersion(server, "crictl")).To(Succeed()) - out, err := tester.RunCmdOnDocker(server.Name, "k3s --version") + out, err := server.RunCmdOnNode("k3s --version") Expect(err).NotTo(HaveOccurred()) cVersion := strings.Split(*k3sImage, ":")[1] cVersion = strings.Replace(cVersion, "-amd64", "", 1)