Skip to content

Commit

Permalink
chore: cleanup errchecking in tests (#3040)
Browse files Browse the repository at this point in the history
Signed-off-by: Kit Patella <kit@defenseunicorns.com>
  • Loading branch information
mkcp authored Sep 30, 2024
1 parent aab365b commit 59b373a
Show file tree
Hide file tree
Showing 31 changed files with 198 additions and 134 deletions.
18 changes: 13 additions & 5 deletions src/test/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package test

import (
"bufio"
"errors"
"fmt"
"os"
"regexp"
Expand Down Expand Up @@ -53,13 +54,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 +78,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 @@ -87,9 +94,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) {
func (e2e *ZarfE2ETest) CleanFiles(t *testing.T, files ...string) {
for _, file := range files {
_ = os.RemoveAll(file)
err := os.RemoveAll(file)
require.NoError(t, err)
}
}

Expand Down
13 changes: 7 additions & 6 deletions src/test/e2e/00_use_cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ func TestUseCLI(t *testing.T) {
expectedShasum := "61b50898f982d015ed87093ba822de0fe011cec6dd67db39f99d8c56391a6109\n"
shasumTestFilePath := "shasum-test-file"

e2e.CleanFiles(shasumTestFilePath)
e2e.CleanFiles(t, shasumTestFilePath)
t.Cleanup(func() {
e2e.CleanFiles(shasumTestFilePath)
e2e.CleanFiles(t, shasumTestFilePath)
})

err := os.WriteFile(shasumTestFilePath, []byte("random test data 🦄\n"), helpers.ReadWriteUser)
Expand Down 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 Expand Up @@ -138,7 +139,7 @@ func TestUseCLI(t *testing.T) {
require.FileExists(t, "binaries/eksctl_Darwin_arm64")
require.FileExists(t, "binaries/eksctl_Linux_x86_64")

e2e.CleanFiles("binaries/eksctl_Darwin_x86_64", "binaries/eksctl_Darwin_arm64", "binaries/eksctl_Linux_x86_64", path, "eks.yaml")
e2e.CleanFiles(t, "binaries/eksctl_Darwin_x86_64", "binaries/eksctl_Darwin_arm64", "binaries/eksctl_Linux_x86_64", path, "eks.yaml")
})

t.Run("zarf package create with tmpdir and cache", func(t *testing.T) {
Expand All @@ -164,7 +165,7 @@ func TestUseCLI(t *testing.T) {
secondFile = "second-choice-file.txt"
)
t.Cleanup(func() {
e2e.CleanFiles(firstFile, secondFile)
e2e.CleanFiles(t, firstFile, secondFile)
})
path := fmt.Sprintf("build/zarf-package-component-choice-%s.tar.zst", e2e.Arch)
stdOut, stdErr, err := e2e.Zarf(t, "package", "deploy", path, "--tmpdir", tmpdir, "--log-level=debug", "--confirm")
Expand Down Expand Up @@ -197,7 +198,7 @@ func TestUseCLI(t *testing.T) {
tlsCert := "tls.crt"
tlsKey := "tls.key"
t.Cleanup(func() {
e2e.CleanFiles(tlsCA, tlsCert, tlsKey)
e2e.CleanFiles(t, tlsCA, tlsCert, tlsKey)
})
stdOut, stdErr, err := e2e.Zarf(t, "tools", "gen-pki", "github.com", "--sub-alt-name", "google.com")
require.NoError(t, err, stdOut, stdErr)
Expand Down
2 changes: 1 addition & 1 deletion src/test/e2e/01_component_choice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestComponentChoice(t *testing.T) {
secondFile = "second-choice-file.txt"
)
t.Cleanup(func() {
e2e.CleanFiles(firstFile, secondFile)
e2e.CleanFiles(t, firstFile, secondFile)
})

path := fmt.Sprintf("build/zarf-package-component-choice-%s.tar.zst", e2e.Arch)
Expand Down
10 changes: 5 additions & 5 deletions src/test/e2e/02_component_actions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ func TestComponentActions(t *testing.T) {
}

allArtifacts := append(deployArtifacts, createArtifacts...)
e2e.CleanFiles(allArtifacts...)
defer e2e.CleanFiles(allArtifacts...)
e2e.CleanFiles(t, allArtifacts...)
defer e2e.CleanFiles(t, allArtifacts...)

/* Create */
// Try creating the package to test the onCreate actions.
Expand Down Expand Up @@ -111,7 +111,7 @@ func TestComponentActions(t *testing.T) {
require.FileExists(t, deployWithEnvVarArtifact)

// Remove the env var file at the end of the test
e2e.CleanFiles(deployWithEnvVarArtifact)
e2e.CleanFiles(t, deployWithEnvVarArtifact)
})

t.Run("action on-deploy-with-template", func(t *testing.T) {
Expand All @@ -128,7 +128,7 @@ func TestComponentActions(t *testing.T) {
require.Contains(t, string(outTemplated), "The snake says ###ZARF_VAR_SNAKE_SOUND###")

// Remove the templated file so we can test with dynamic variables
e2e.CleanFiles(deployTemplatedArtifact)
e2e.CleanFiles(t, deployTemplatedArtifact)

// Test using a templated file with dynamic variables
stdOut, stdErr, err = e2e.Zarf(t, "package", "deploy", path, "--components=on-deploy-with-template-use-of-variable,on-deploy-with-dynamic-variable,on-deploy-with-multiple-variables", "--confirm")
Expand All @@ -140,7 +140,7 @@ func TestComponentActions(t *testing.T) {
require.Contains(t, string(outTemplated), "The snake says hiss")

// Remove the templated file at the end of the test
e2e.CleanFiles(deployTemplatedArtifact)
e2e.CleanFiles(t, deployTemplatedArtifact)
})

t.Run("action on-deploy-immediate-failure", func(t *testing.T) {
Expand Down
12 changes: 6 additions & 6 deletions src/test/e2e/03_deprecations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ func TestDeprecatedComponentScripts(t *testing.T) {
"test-deprecated-deploy-after-hook.txt",
}
allArtifacts := append(deployArtifacts, prepareArtifact)
e2e.CleanFiles(allArtifacts...)
defer e2e.CleanFiles(allArtifacts...)
e2e.CleanFiles(t, allArtifacts...)
defer e2e.CleanFiles(t, allArtifacts...)

// 1. Try creating the package to test the create scripts
testPackagePath := fmt.Sprintf("%s/zarf-package-deprecated-component-scripts-%s.tar.zst", testPackageDirPath, e2e.Arch)
outputFlag := fmt.Sprintf("-o=%s", testPackageDirPath)
stdOut, stdErr, err := e2e.Zarf(t, "package", "create", testPackageDirPath, outputFlag, "--confirm")
defer e2e.CleanFiles(testPackagePath)
defer e2e.CleanFiles(t, testPackagePath)
require.NoError(t, err, stdOut, stdErr)
require.Contains(t, stdErr, "Component '1-test-deprecated-prepare-scripts' is using scripts")
require.Contains(t, stdErr, "Component '2-test-deprecated-deploy-scripts' is using scripts")
Expand Down Expand Up @@ -71,8 +71,8 @@ func TestDeprecatedSetAndPackageVariables(t *testing.T) {
"test-deprecated-deploy-after-hook.txt",
}
allArtifacts := append(deployArtifacts, prepareArtifact)
e2e.CleanFiles(allArtifacts...)
defer e2e.CleanFiles(allArtifacts...)
e2e.CleanFiles(t, allArtifacts...)
defer e2e.CleanFiles(t, allArtifacts...)

// 2. Try creating the package to test the create scripts
testPackagePath := fmt.Sprintf("%s/zarf-package-deprecated-set-variable-%s.tar.zst", testPackageDirPath, e2e.Arch)
Expand All @@ -85,7 +85,7 @@ func TestDeprecatedSetAndPackageVariables(t *testing.T) {

// Check that the command displays a warning on create
stdOut, stdErr, err = e2e.Zarf(t, "package", "create", testPackageDirPath, outputFlag, "--confirm", "--set", "ECHO=Zarf-The-Axolotl")
defer e2e.CleanFiles(testPackagePath)
defer e2e.CleanFiles(t, testPackagePath)
require.NoError(t, err, stdOut, stdErr)
require.Contains(t, stdErr, "Component '1-test-deprecated-set-variable' is using setVariable")
require.Contains(t, stdErr, "deprecated syntax ###ZARF_PKG_VAR_ECHO###")
Expand Down
2 changes: 1 addition & 1 deletion src/test/e2e/04_create_templating_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,5 @@ func TestCreateTemplating(t *testing.T) {
require.NoError(t, err)
require.Contains(t, string(filesJSON), "pandas")

e2e.CleanFiles(pkgName, fileFoldersPkgName)
e2e.CleanFiles(t, pkgName, fileFoldersPkgName)
}
8 changes: 4 additions & 4 deletions src/test/e2e/05_tarball_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestMultiPartPackage(t *testing.T) {
outputFile = "multi-part-demo.dat"
)

e2e.CleanFiles(deployPath, outputFile)
e2e.CleanFiles(t, deployPath, outputFile)

// Create the package with a max size of 20MB
stdOut, stdErr, err := e2e.Zarf(t, "package", "create", createPath, "--max-package-size=20", "--confirm")
Expand Down Expand Up @@ -73,8 +73,8 @@ func TestMultiPartPackage(t *testing.T) {
err = helpers.SHAsMatch(parts[0], pkgData.Sha256Sum)
require.NoError(t, err)

e2e.CleanFiles(parts...)
e2e.CleanFiles(outputFile)
e2e.CleanFiles(t, parts...)
e2e.CleanFiles(t, outputFile)
}

func TestReproducibleTarballs(t *testing.T) {
Expand All @@ -98,7 +98,7 @@ func TestReproducibleTarballs(t *testing.T) {
err = utils.ReadYaml(filepath.Join(unpack1, layout.ZarfYAML), &pkg1)
require.NoError(t, err)

e2e.CleanFiles(unpack1, tb)
e2e.CleanFiles(t, unpack1, tb)

stdOut, stdErr, err = e2e.Zarf(t, "package", "create", createPath, "--confirm", "--output", tmp)
require.NoError(t, err, stdOut, stdErr)
Expand Down
4 changes: 2 additions & 2 deletions src/test/e2e/06_create_sbom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestCreateSBOM(t *testing.T) {
require.FileExists(t, filepath.Join(sbomPath, "dos-games", "ghcr.io_zarf-dev_doom-game_0.0.1.json"))

// Clean the SBOM path so it is force to be recreated
e2e.CleanFiles(sbomPath)
e2e.CleanFiles(t, sbomPath)

stdOut, stdErr, err = e2e.Zarf(t, "package", "inspect", pkgName, "--sbom-out", sbomPath)
require.NoError(t, err, stdOut, stdErr)
Expand Down Expand Up @@ -68,5 +68,5 @@ func TestCreateSBOM(t *testing.T) {
_, err = os.ReadFile(filepath.Join(sbomPath, "init", "compare.html"))
require.NoError(t, err)

e2e.CleanFiles(pkgName)
e2e.CleanFiles(t, pkgName)
}
2 changes: 1 addition & 1 deletion src/test/e2e/07_create_git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestCreateGit(t *testing.T) {
path := fmt.Sprintf("build/zarf-package-git-data-%s-0.0.1.tar.zst", e2e.Arch)
stdOut, stdErr, err := e2e.Zarf(t, "tools", "archiver", "decompress", path, extractDir, "--unarchive-all")
require.NoError(t, err, stdOut, stdErr)
defer e2e.CleanFiles(extractDir)
defer e2e.CleanFiles(t, extractDir)

// Verify the full-repo component
gitDir := fmt.Sprintf("%s/components/full-repo/repos/zarf-public-test-2395699829/.git", extractDir)
Expand Down
4 changes: 2 additions & 2 deletions src/test/e2e/08_create_differential_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestCreateDifferential(t *testing.T) {
// Build the package a first time
stdOut, stdErr, err := e2e.Zarf(t, "package", "create", packagePath, "--set=PACKAGE_VERSION=v0.25.0", "--confirm")
require.NoError(t, err, stdOut, stdErr)
defer e2e.CleanFiles(packageName)
defer e2e.CleanFiles(t, packageName)

// Build the differential package without changing the version
_, stdErr, err = e2e.Zarf(t, "package", "create", packagePath, "--set=PACKAGE_VERSION=v0.25.0", differentialFlag, "--confirm")
Expand All @@ -40,7 +40,7 @@ func TestCreateDifferential(t *testing.T) {
// Build the differential package
stdOut, stdErr, err = e2e.Zarf(t, "package", "create", packagePath, "--set=PACKAGE_VERSION=v0.26.0", differentialFlag, "--confirm")
require.NoError(t, err, stdOut, stdErr)
defer e2e.CleanFiles(differentialPackageName)
defer e2e.CleanFiles(t, differentialPackageName)

// Extract the yaml of the differential package
err = archiver.Extract(differentialPackageName, layout.ZarfYAML, tmpdir)
Expand Down
2 changes: 1 addition & 1 deletion src/test/e2e/11_oci_pull_inspect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (suite *PullInspectTestSuite) SetupSuite() {

func (suite *PullInspectTestSuite) TearDownSuite() {
local := fmt.Sprintf("zarf-package-dos-games-%s-1.0.0.tar.zst", e2e.Arch)
e2e.CleanFiles(local)
e2e.CleanFiles(suite.T(), local)
}

func (suite *PullInspectTestSuite) Test_0_Pull() {
Expand Down
6 changes: 4 additions & 2 deletions src/test/e2e/12_lint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ func TestLint(t *testing.T) {

testPackagePath := filepath.Join("src", "test", "packages", "12-lint")
configPath := filepath.Join(testPackagePath, "zarf-config.toml")
os.Setenv("ZARF_CONFIG", configPath)
osSetErr := os.Setenv("ZARF_CONFIG", configPath)
require.NoError(t, osSetErr, "Unable to set ZARF_CONFIG")
_, stderr, err := e2e.Zarf(t, "dev", "lint", testPackagePath, "-f", "good-flavor")
os.Unsetenv("ZARF_CONFIG")
osUnsetErr := os.Unsetenv("ZARF_CONFIG")
require.NoError(t, osUnsetErr, "Unable to cleanup ZARF_CONFIG")
require.Error(t, err, "Require an exit code since there was warnings / errors")
strippedStderr := e2e.StripMessageFormatting(stderr)

Expand Down
79 changes: 44 additions & 35 deletions src/test/e2e/14_oci_compose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,45 +137,54 @@ func (suite *PublishCopySkeletonSuite) Test_2_FilePaths() {
}

for _, pkgTar := range pkgTars {
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)

// Verify skeleton contains kustomize-generated manifests.
if strings.HasSuffix(pkgTar, "zarf-package-test-compose-package-skeleton-0.0.1.tar.zst") {
kustomizeGeneratedManifests := []string{
"kustomization-connect-service-0.yaml",
"kustomization-connect-service-1.yaml",
"kustomization-connect-service-two-0.yaml",
}
manifestDir := filepath.Join(unpacked, "components", "test-compose-package", "manifests")
for _, manifest := range kustomizeGeneratedManifests {
manifestPath := filepath.Join(manifestDir, manifest)
suite.FileExists(manifestPath, "expected to find kustomize-generated manifest: %q", manifestPath)
var configMap corev1.ConfigMap
err := utils.ReadYaml(manifestPath, &configMap)
suite.NoError(err)
suite.Equal("ConfigMap", configMap.Kind, "expected manifest %q to be of kind ConfigMap", manifestPath)
// Wrap in a fn to ensure our defers cleanup resources on each iteration
func() {
var pkg v1alpha1.ZarfPackage

unpacked := strings.TrimSuffix(pkgTar, ".tar.zst")
_, _, err := e2e.Zarf(suite.T(), "tools", "archiver", "decompress", pkgTar, unpacked, "--unarchive-all")
suite.NoError(err)
suite.DirExists(unpacked)

// Cleanup resources
defer func() {
suite.NoError(os.RemoveAll(unpacked))
}()
defer func() {
suite.NoError(os.RemoveAll(pkgTar))
}()

// Verify skeleton contains kustomize-generated manifests.
if strings.HasSuffix(pkgTar, "zarf-package-test-compose-package-skeleton-0.0.1.tar.zst") {
kustomizeGeneratedManifests := []string{
"kustomization-connect-service-0.yaml",
"kustomization-connect-service-1.yaml",
"kustomization-connect-service-two-0.yaml",
}
manifestDir := filepath.Join(unpacked, "components", "test-compose-package", "manifests")
for _, manifest := range kustomizeGeneratedManifests {
manifestPath := filepath.Join(manifestDir, manifest)
suite.FileExists(manifestPath, "expected to find kustomize-generated manifest: %q", manifestPath)
var configMap corev1.ConfigMap
err := utils.ReadYaml(manifestPath, &configMap)
suite.NoError(err)
suite.Equal("ConfigMap", configMap.Kind, "expected manifest %q to be of kind ConfigMap", manifestPath)
}
}
}

err = utils.ReadYaml(filepath.Join(unpacked, layout.ZarfYAML), &pkg)
suite.NoError(err)
suite.NotNil(pkg)
err = utils.ReadYaml(filepath.Join(unpacked, layout.ZarfYAML), &pkg)
suite.NoError(err)
suite.NotNil(pkg)

components := pkg.Components
suite.NotNil(components)
components := pkg.Components
suite.NotNil(components)

isSkeleton := false
if strings.Contains(pkgTar, "-skeleton-") {
isSkeleton = true
}
suite.verifyComponentPaths(unpacked, components, isSkeleton)
isSkeleton := false
if strings.Contains(pkgTar, "-skeleton-") {
isSkeleton = true
}
suite.verifyComponentPaths(unpacked, components, isSkeleton)
}()
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/test/e2e/20_zarf_init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestZarfInit(t *testing.T) {
expectedErrorMessage = "unable to run component before action: command \"Check that the host architecture matches the package architecture\""
)
t.Cleanup(func() {
e2e.CleanFiles(mismatchedInitPackage)
e2e.CleanFiles(t, mismatchedInitPackage)
})

if runtime.GOOS == "linux" {
Expand Down 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") // TODO(mkcp): intentionally ignored, mark nolint
_, _, _ = e2e.Kubectl(t, "scale", "deploy", "-n", "zarf", "agent-hook", "--replicas=1") // TODO(mkcp): intentionally ignored, mark nolint
}

func checkLogForSensitiveState(t *testing.T, logText string, zarfState types.ZarfState) {
Expand Down
Loading

0 comments on commit 59b373a

Please sign in to comment.