From 4283fafe8b8a543149ee0fda757bde86391433ad Mon Sep 17 00:00:00 2001 From: Kit Patella Date: Wed, 25 Sep 2024 12:01:13 -0700 Subject: [PATCH] fix: check for or intentionally ignore some dangling errors in tests Signed-off-by: Kit Patella --- src/test/common.go | 19 +++++++++++--- src/test/e2e/00_use_cli_test.go | 3 ++- src/test/e2e/14_oci_compose_test.go | 8 ++++-- src/test/e2e/20_zarf_init_test.go | 4 +-- src/test/e2e/21_connect_creds_test.go | 4 ++- src/test/e2e/24_variables_test.go | 6 +++-- src/test/e2e/25_helm_test.go | 8 +++--- src/test/e2e/28_wait_test.go | 2 +- src/test/e2e/29_config_file_test.go | 29 ++++++++++++++-------- src/test/external/common.go | 3 ++- src/test/external/ext_in_cluster_test.go | 5 +++- src/test/external/ext_out_cluster_test.go | 16 +++++++----- src/test/upgrade/previously_built_test.go | 30 +++++++++++++++-------- 13 files changed, 93 insertions(+), 44 deletions(-) diff --git a/src/test/common.go b/src/test/common.go index 746600feb1..dbeae330ff 100644 --- a/src/test/common.go +++ b/src/test/common.go @@ -6,7 +6,9 @@ package test import ( "bufio" + "errors" "fmt" + "log/slog" "os" "regexp" "runtime" @@ -53,13 +55,16 @@ func GetCLIName() string { } // Zarf executes a Zarf command. -func (e2e *ZarfE2ETest) Zarf(t *testing.T, args ...string) (string, string, error) { +func (e2e *ZarfE2ETest) Zarf(t *testing.T, args ...string) (_ string, _ string, err error) { if !slices.Contains(args, "--tmpdir") && !slices.Contains(args, "tools") { tmpdir, err := os.MkdirTemp("", "zarf-") if err != nil { return "", "", err } - defer os.RemoveAll(tmpdir) + defer func(path string) { + errRemove := os.RemoveAll(path) + err = errors.Join(err, errRemove) + }(tmpdir) args = append(args, "--tmpdir", tmpdir) } if !slices.Contains(args, "--zarf-cache") && !slices.Contains(args, "tools") && os.Getenv("CI") == "true" { @@ -74,7 +79,10 @@ func (e2e *ZarfE2ETest) Zarf(t *testing.T, args ...string) (string, string, erro return "", "", err } args = append(args, "--zarf-cache", cacheDir) - defer os.RemoveAll(cacheDir) + defer func(path string) { + errRemove := os.RemoveAll(path) + err = errors.Join(err, errRemove) + }(cacheDir) } return exec.CmdWithTesting(t, exec.PrintCfg(), e2e.ZarfBinPath, args...) } @@ -89,7 +97,10 @@ func (e2e *ZarfE2ETest) Kubectl(t *testing.T, args ...string) (string, string, e // CleanFiles removes files and directories that have been created during the test. func (e2e *ZarfE2ETest) CleanFiles(files ...string) { for _, file := range files { - _ = os.RemoveAll(file) + err := os.RemoveAll(file) + if err != nil { + slog.Debug("Unable to cleanup files", "files", files, "error", err) + } } } diff --git a/src/test/e2e/00_use_cli_test.go b/src/test/e2e/00_use_cli_test.go index 366bacb7f9..d834445367 100644 --- a/src/test/e2e/00_use_cli_test.go +++ b/src/test/e2e/00_use_cli_test.go @@ -110,7 +110,8 @@ func TestUseCLI(t *testing.T) { t.Run("changing log level", func(t *testing.T) { t.Parallel() // Test that changing the log level actually applies the requested level - _, stdErr, _ := e2e.Zarf(t, "internal", "crc32", "zarf", "--log-level=debug") + _, stdErr, err := e2e.Zarf(t, "internal", "crc32", "zarf", "--log-level=debug") + require.NoError(t, err) expectedOutString := "Log level set to debug" require.Contains(t, stdErr, expectedOutString, "The log level should be changed to 'debug'") }) diff --git a/src/test/e2e/14_oci_compose_test.go b/src/test/e2e/14_oci_compose_test.go index 7159394107..95d1436f74 100644 --- a/src/test/e2e/14_oci_compose_test.go +++ b/src/test/e2e/14_oci_compose_test.go @@ -140,8 +140,6 @@ func (suite *PublishCopySkeletonSuite) Test_2_FilePaths() { var pkg v1alpha1.ZarfPackage unpacked := strings.TrimSuffix(pkgTar, ".tar.zst") - defer os.RemoveAll(unpacked) - defer os.RemoveAll(pkgTar) _, _, err := e2e.Zarf(suite.T(), "tools", "archiver", "decompress", pkgTar, unpacked, "--unarchive-all") suite.NoError(err) suite.DirExists(unpacked) @@ -176,6 +174,12 @@ func (suite *PublishCopySkeletonSuite) Test_2_FilePaths() { isSkeleton = true } suite.verifyComponentPaths(unpacked, components, isSkeleton) + + // Cleanup resources + err = os.RemoveAll(unpacked) + suite.NoError(err) + err = os.RemoveAll(pkgTar) + suite.NoError(err) } } diff --git a/src/test/e2e/20_zarf_init_test.go b/src/test/e2e/20_zarf_init_test.go index c94bfa4226..66106556bd 100644 --- a/src/test/e2e/20_zarf_init_test.go +++ b/src/test/e2e/20_zarf_init_test.go @@ -104,8 +104,8 @@ func TestZarfInit(t *testing.T) { verifyZarfServiceLabels(t) // Special sizing-hacking for reducing resources where Kind + CI eats a lot of free cycles (ignore errors) - _, _, _ = e2e.Kubectl(t, "scale", "deploy", "-n", "kube-system", "coredns", "--replicas=1") - _, _, _ = e2e.Kubectl(t, "scale", "deploy", "-n", "zarf", "agent-hook", "--replicas=1") + _, _, _ = e2e.Kubectl(t, "scale", "deploy", "-n", "kube-system", "coredns", "--replicas=1") //nolint:errcheck + _, _, _ = e2e.Kubectl(t, "scale", "deploy", "-n", "zarf", "agent-hook", "--replicas=1") //nolint:errcheck } func checkLogForSensitiveState(t *testing.T, logText string, zarfState types.ZarfState) { diff --git a/src/test/e2e/21_connect_creds_test.go b/src/test/e2e/21_connect_creds_test.go index c58244a736..93a28c1690 100644 --- a/src/test/e2e/21_connect_creds_test.go +++ b/src/test/e2e/21_connect_creds_test.go @@ -74,7 +74,6 @@ func TestMetrics(t *testing.T) { if err != nil { t.Fatal(err) } - defer resp.Body.Close() // Read the response body body, err := io.ReadAll(resp.Body) @@ -86,6 +85,9 @@ func TestMetrics(t *testing.T) { require.Contains(t, string(body), desiredString) require.NoError(t, err, resp) require.Equal(t, 200, resp.StatusCode) + + err = resp.Body.Close() + require.NoError(t, err) } func connectToZarfServices(ctx context.Context, t *testing.T) { diff --git a/src/test/e2e/24_variables_test.go b/src/test/e2e/24_variables_test.go index f5b06116d0..72490c3ebb 100644 --- a/src/test/e2e/24_variables_test.go +++ b/src/test/e2e/24_variables_test.go @@ -43,7 +43,8 @@ func TestVariables(t *testing.T) { require.Contains(t, stdErr, "", expectedOutString) // Test that not specifying a prompted variable results in an error - _, stdErr, _ = e2e.Zarf(t, "package", "deploy", path, "--confirm") + _, stdErr, err = e2e.Zarf(t, "package", "deploy", path, "--confirm") + require.NoError(t, err) expectedOutString = "variable 'SITE_NAME' must be '--set' when using the '--confirm' flag" require.Contains(t, stdErr, "", expectedOutString) @@ -73,7 +74,8 @@ func TestVariables(t *testing.T) { require.Contains(t, string(outputTF), "unicorn-land") // Verify the configmap was properly templated - kubectlOut, _, _ := e2e.Kubectl(t, "-n", "nginx", "get", "configmap", "nginx-configmap", "-o", "jsonpath='{.data.index\\.html}' ") + kubectlOut, _, err := e2e.Kubectl(t, "-n", "nginx", "get", "configmap", "nginx-configmap", "-o", "jsonpath='{.data.index\\.html}' ") + require.NoError(t, err, "unable to get nginx configmap") // OPTIONAL_FOOTER should remain unset because it was not set during deploy require.Contains(t, string(kubectlOut), "\n \n ") // STYLE should take the default value diff --git a/src/test/e2e/25_helm_test.go b/src/test/e2e/25_helm_test.go index 3e51900cdd..8589f8156f 100644 --- a/src/test/e2e/25_helm_test.go +++ b/src/test/e2e/25_helm_test.go @@ -99,7 +99,8 @@ func testHelmEscaping(t *testing.T) { require.NoError(t, err, stdOut, stdErr) // Verify the configmap was deployed, escaped, and contains all of its data - kubectlOut, _ := exec.Command("kubectl", "-n", "default", "describe", "cm", "dont-template-me").Output() + kubectlOut, err := exec.Command("kubectl", "-n", "default", "describe", "cm", "dont-template-me").Output() + require.NoError(t, err, "unable to describe configmap") require.Contains(t, string(kubectlOut), `alert: OOMKilled {{ "{{ \"random.Values\" }}" }}`) require.Contains(t, string(kubectlOut), "backtick1: \"content with backticks `some random things`\"") require.Contains(t, string(kubectlOut), "backtick2: \"nested templating with backticks {{` random.Values `}}\"") @@ -175,8 +176,9 @@ func testHelmAdoption(t *testing.T) { deploymentManifest := "src/test/packages/25-manifest-adoption/deployment.yaml" // Deploy dos-games manually into the cluster without Zarf - kubectlOut, _, _ := e2e.Kubectl(t, "apply", "-f", deploymentManifest) - require.Contains(t, string(kubectlOut), "deployment.apps/game created") + kubectlOut, _, err := e2e.Kubectl(t, "apply", "-f", deploymentManifest) + require.NoError(t, err, "unable to apply", "deploymentManifest", deploymentManifest) + require.Contains(t, kubectlOut, "deployment.apps/game created") // Deploy dos-games into the cluster with Zarf stdOut, stdErr, err := e2e.Zarf(t, "package", "deploy", packagePath, "--confirm", "--adopt-existing-resources") diff --git a/src/test/e2e/28_wait_test.go b/src/test/e2e/28_wait_test.go index e67b60d178..2881966863 100644 --- a/src/test/e2e/28_wait_test.go +++ b/src/test/e2e/28_wait_test.go @@ -51,7 +51,7 @@ func TestNoWait(t *testing.T) { case <-time.After(30 * time.Second): t.Error("Timeout waiting for zarf deploy (it tried to wait)") t.Log("Removing hanging namespace...") - _, _, _ = e2e.Kubectl(t, "delete", "namespace", "no-wait", "--force=true", "--wait=false", "--grace-period=0") + _, _, _ = e2e.Kubectl(t, "delete", "namespace", "no-wait", "--force=true", "--wait=false", "--grace-period=0") //nolint:errcheck } require.NoError(t, err, stdOut, stdErr) diff --git a/src/test/e2e/29_config_file_test.go b/src/test/e2e/29_config_file_test.go index 0cea0b4dd9..043cc9a606 100644 --- a/src/test/e2e/29_config_file_test.go +++ b/src/test/e2e/29_config_file_test.go @@ -26,7 +26,6 @@ func TestConfigFile(t *testing.T) { // Test the config file environment variable t.Setenv("ZARF_CONFIG", filepath.Join(dir, config)) - defer os.Unsetenv("ZARF_CONFIG") configFileTests(t, dir, path) configFileDefaultTests(t) @@ -34,7 +33,10 @@ func TestConfigFile(t *testing.T) { stdOut, stdErr, err := e2e.Zarf(t, "package", "remove", path, "--confirm") require.NoError(t, err, stdOut, stdErr) + // Cleanup e2e.CleanFiles(path) + err = os.Unsetenv("ZARF_CONFIG") + require.NoError(t, err) } func configFileTests(t *testing.T, dir, path string) { @@ -140,29 +142,36 @@ func configFileDefaultTests(t *testing.T) { // Test remaining default initializers t.Setenv("ZARF_CONFIG", filepath.Join("src", "test", "zarf-config-test.toml")) - defer os.Unsetenv("ZARF_CONFIG") // Test global flags - stdOut, _, _ := e2e.Zarf(t, "--help") + stdOut, _, err := e2e.Zarf(t, "--help") + require.NoError(t, err) for _, test := range globalFlags { - require.Contains(t, string(stdOut), test) + require.Contains(t, stdOut, test) } // Test init flags - stdOut, _, _ = e2e.Zarf(t, "init", "--help") + stdOut, _, err = e2e.Zarf(t, "init", "--help") + require.NoError(t, err) for _, test := range initFlags { - require.Contains(t, string(stdOut), test) + require.Contains(t, stdOut, test) } // Test package create flags - stdOut, _, _ = e2e.Zarf(t, "package", "create", "--help") + stdOut, _, err = e2e.Zarf(t, "package", "create", "--help") + require.NoError(t, err) for _, test := range packageCreateFlags { - require.Contains(t, string(stdOut), test) + require.Contains(t, stdOut, test) } // Test package deploy flags - stdOut, _, _ = e2e.Zarf(t, "package", "deploy", "--help") + stdOut, _, err = e2e.Zarf(t, "package", "deploy", "--help") + require.NoError(t, err) for _, test := range packageDeployFlags { - require.Contains(t, string(stdOut), test) + require.Contains(t, stdOut, test) } + + // Cleanup + err = os.Unsetenv("ZARF_CONFIG") + require.NoError(t, err) } diff --git a/src/test/external/common.go b/src/test/external/common.go index 0209ddadb2..dd5446002d 100644 --- a/src/test/external/common.go +++ b/src/test/external/common.go @@ -28,7 +28,8 @@ func createPodInfoPackageWithInsecureSources(t *testing.T, temp string) { require.NoError(t, err, "unable to yq edit helm source") err = exec.CmdWithPrint(zarfBinPath, "tools", "yq", "eval", ".spec.insecure = true", "-i", filepath.Join(temp, "oci", "podinfo-source.yaml")) require.NoError(t, err, "unable to yq edit oci source") - exec.CmdWithPrint(zarfBinPath, "package", "create", temp, "--confirm", "--output", temp) + err = exec.CmdWithPrint(zarfBinPath, "package", "create", temp, "--confirm", "--output", temp) + require.NoError(t, err, "unable to create package") } func verifyWaitSuccess(t *testing.T, timeoutMinutes time.Duration, cmd string, args []string, condition string, onTimeout string) bool { diff --git a/src/test/external/ext_in_cluster_test.go b/src/test/external/ext_in_cluster_test.go index ad1e338935..826b947268 100644 --- a/src/test/external/ext_in_cluster_test.go +++ b/src/test/external/ext_in_cluster_test.go @@ -157,7 +157,10 @@ func (suite *ExtInClusterTestSuite) Test_1_Deploy() { err := exec.CmdWithPrint(zarfBinPath, initArgs...) suite.NoError(err, "unable to initialize the k8s server with zarf") temp := suite.T().TempDir() - defer os.Remove(temp) + defer func(name string) { + err := os.Remove(name) + suite.NoError(err) + }(temp) createPodInfoPackageWithInsecureSources(suite.T(), temp) // Deploy the flux example package diff --git a/src/test/external/ext_out_cluster_test.go b/src/test/external/ext_out_cluster_test.go index 2cf6807d6d..297894ff28 100644 --- a/src/test/external/ext_out_cluster_test.go +++ b/src/test/external/ext_out_cluster_test.go @@ -58,13 +58,17 @@ func (suite *ExtOutClusterTestSuite) SetupSuite() { suite.Assertions = require.New(suite.T()) // Teardown any leftovers from previous tests - _ = exec.CmdWithPrint("k3d", "cluster", "delete", clusterName) - _ = exec.CmdWithPrint("docker", "rm", "-f", "k3d-"+registryHost) - _ = exec.CmdWithPrint("docker", "compose", "down") - _ = exec.CmdWithPrint("docker", "network", "remove", network) + err := exec.CmdWithPrint("k3d", "cluster", "delete", clusterName) + suite.NoError(err, "unable to teardown cluster") + err = exec.CmdWithPrint("docker", "rm", "-f", "k3d-"+registryHost) + suite.NoError(err, "unable to teardown k3d registry") + err = exec.CmdWithPrint("docker", "compose", "down") + suite.NoError(err, "unable to teardown docker compose resources") + err = exec.CmdWithPrint("docker", "network", "remove", network) + suite.NoError(err, "unable to teardown docker network resources") - // Setup a network for everything to live inside - err := exec.CmdWithPrint("docker", "network", "create", "--driver=bridge", "--subnet="+subnet, "--gateway="+gateway, network) + // Set up a network for everything to live inside + err = exec.CmdWithPrint("docker", "network", "create", "--driver=bridge", "--subnet="+subnet, "--gateway="+gateway, network) suite.NoError(err, "unable to create the k3d registry") // Install a k3d-managed registry server to act as the 'remote' container registry diff --git a/src/test/upgrade/previously_built_test.go b/src/test/upgrade/previously_built_test.go index 0b59761d6d..178a97ca5b 100644 --- a/src/test/upgrade/previously_built_test.go +++ b/src/test/upgrade/previously_built_test.go @@ -29,18 +29,24 @@ func TestPreviouslyBuiltZarfPackage(t *testing.T) { t.Log("Upgrade: Previously Built Zarf Package") // For the upgrade test, podinfo-upgrade should already be in the cluster (version 6.3.3) (see .github/workflows/test-upgrade.yml) - kubectlOut, _, _ := kubectl(t, "-n=podinfo-upgrade", "rollout", "status", "deployment/podinfo-upgrade") + kubectlOut, _, err := kubectl(t, "-n=podinfo-upgrade", "rollout", "status", "deployment/podinfo-upgrade") + require.NoError(t, err) require.Contains(t, kubectlOut, "successfully rolled out") - kubectlOut, _, _ = kubectl(t, "-n=podinfo-upgrade", "get", "deployment", "podinfo-upgrade", "-o=jsonpath={.metadata.labels}}") + kubectlOut, _, err = kubectl(t, "-n=podinfo-upgrade", "get", "deployment", "podinfo-upgrade", "-o=jsonpath={.metadata.labels}}") + require.NoError(t, err) require.Contains(t, kubectlOut, "6.3.3") // Verify that the private-registry secret and private-git-server secret in the podinfo-upgrade namespace are the same after re-init // This tests that `zarf tools update-creds` successfully updated the other namespace - zarfRegistrySecret, _, _ := kubectl(t, "-n=zarf", "get", "secret", "private-registry", "-o", "jsonpath={.data}") - podinfoRegistrySecret, _, _ := kubectl(t, "-n=podinfo-upgrade", "get", "secret", "private-registry", "-o", "jsonpath={.data}") + zarfRegistrySecret, _, err := kubectl(t, "-n=zarf", "get", "secret", "private-registry", "-o", "jsonpath={.data}") + require.NoError(t, err) + podinfoRegistrySecret, _, err := kubectl(t, "-n=podinfo-upgrade", "get", "secret", "private-registry", "-o", "jsonpath={.data}") + require.NoError(t, err) require.Equal(t, zarfRegistrySecret, podinfoRegistrySecret, "the zarf registry secret and podinfo-upgrade registry secret did not match") - zarfGitServerSecret, _, _ := kubectl(t, "-n=zarf", "get", "secret", "private-git-server", "-o", "jsonpath={.data}") - podinfoGitServerSecret, _, _ := kubectl(t, "-n=podinfo-upgrade", "get", "secret", "private-git-server", "-o", "jsonpath={.data}") + zarfGitServerSecret, _, err := kubectl(t, "-n=zarf", "get", "secret", "private-git-server", "-o", "jsonpath={.data}") + require.NoError(t, err) + podinfoGitServerSecret, _, err := kubectl(t, "-n=podinfo-upgrade", "get", "secret", "private-git-server", "-o", "jsonpath={.data}") + require.NoError(t, err) require.Equal(t, zarfGitServerSecret, podinfoGitServerSecret, "the zarf git server secret and podinfo-upgrade git server secret did not match") // We also expect a 6.3.4 package to have been previously built @@ -56,9 +62,11 @@ func TestPreviouslyBuiltZarfPackage(t *testing.T) { require.Contains(t, stdErr, "-----BEGIN PUBLIC KEY-----") // Verify that podinfo-upgrade successfully deploys in the cluster (version 6.3.4) - kubectlOut, _, _ = kubectl(t, "-n=podinfo-upgrade", "rollout", "status", "deployment/podinfo-upgrade") + kubectlOut, _, err = kubectl(t, "-n=podinfo-upgrade", "rollout", "status", "deployment/podinfo-upgrade") + require.NoError(t, err) require.Contains(t, kubectlOut, "successfully rolled out") - kubectlOut, _, _ = kubectl(t, "-n=podinfo-upgrade", "get", "deployment", "podinfo-upgrade", "-o=jsonpath={.metadata.labels}}") + kubectlOut, _, err = kubectl(t, "-n=podinfo-upgrade", "get", "deployment", "podinfo-upgrade", "-o=jsonpath={.metadata.labels}}") + require.NoError(t, err) require.Contains(t, kubectlOut, "6.3.4") // We also want to build a new package. @@ -75,9 +83,11 @@ func TestPreviouslyBuiltZarfPackage(t *testing.T) { require.Contains(t, stdErr, "-----BEGIN PUBLIC KEY-----") // Verify that podinfo-upgrade successfully deploys in the cluster (version 6.3.5) - kubectlOut, _, _ = kubectl(t, "-n=podinfo-upgrade", "rollout", "status", "deployment/podinfo-upgrade") + kubectlOut, _, err = kubectl(t, "-n=podinfo-upgrade", "rollout", "status", "deployment/podinfo-upgrade") + require.NoError(t, err) require.Contains(t, kubectlOut, "successfully rolled out") - kubectlOut, _, _ = kubectl(t, "-n=podinfo-upgrade", "get", "deployment", "podinfo-upgrade", "-o=jsonpath={.metadata.labels}}") + kubectlOut, _, err = kubectl(t, "-n=podinfo-upgrade", "get", "deployment", "podinfo-upgrade", "-o=jsonpath={.metadata.labels}}") + require.NoError(t, err) require.Contains(t, kubectlOut, "6.3.5") // Remove the package.