Skip to content

Commit

Permalink
fix: check for or intentionally ignore some dangling errors in tests
Browse files Browse the repository at this point in the history
Signed-off-by: Kit Patella <[email protected]>
  • Loading branch information
mkcp committed Sep 25, 2024
1 parent 6f53a29 commit 4283faf
Show file tree
Hide file tree
Showing 13 changed files with 93 additions and 44 deletions.
19 changes: 15 additions & 4 deletions src/test/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ package test

import (
"bufio"
"errors"
"fmt"
"log/slog"
"os"
"regexp"
"runtime"
Expand Down Expand Up @@ -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" {
Expand All @@ -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...)
}
Expand All @@ -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)
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/test/e2e/00_use_cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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'")
})
Expand Down
8 changes: 6 additions & 2 deletions src/test/e2e/14_oci_compose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/test/e2e/20_zarf_init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion src/test/e2e/21_connect_creds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {
Expand Down
6 changes: 4 additions & 2 deletions src/test/e2e/24_variables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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), "</pre>\n \n </body>")
// STYLE should take the default value
Expand Down
8 changes: 5 additions & 3 deletions src/test/e2e/25_helm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 `}}\"")
Expand Down Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion src/test/e2e/28_wait_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
29 changes: 19 additions & 10 deletions src/test/e2e/29_config_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,17 @@ 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)

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) {
Expand Down Expand Up @@ -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)
}
3 changes: 2 additions & 1 deletion src/test/external/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 4 additions & 1 deletion src/test/external/ext_in_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 10 additions & 6 deletions src/test/external/ext_out_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 20 additions & 10 deletions src/test/upgrade/previously_built_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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.
Expand Down

0 comments on commit 4283faf

Please sign in to comment.