Skip to content

Commit

Permalink
chore: move context.TODO to context.Background() (2) (zarf-dev#2746)
Browse files Browse the repository at this point in the history
Signed-off-by: schristoff-du <[email protected]>
Co-authored-by: schristoff-du <[email protected]>
Signed-off-by: Tim Seagren <[email protected]>
  • Loading branch information
2 people authored and chaospuppy committed Aug 5, 2024
1 parent a294bb1 commit e3c48df
Show file tree
Hide file tree
Showing 42 changed files with 295 additions and 291 deletions.
8 changes: 8 additions & 0 deletions src/pkg/utils/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"runtime"
"strings"
"sync"
"testing"
)

// Config is a struct for configuring the Cmd function.
Expand Down Expand Up @@ -50,6 +51,13 @@ func CmdWithPrint(command string, args ...string) error {
return err
}

// CmdWithTesting takes a *testing.T and generates a context the cancels on cleanup
func CmdWithTesting(t *testing.T, config Config, command string, args ...string) (string, string, error) {
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
return CmdWithContext(ctx, config, command, args...)
}

// CmdWithContext executes a given command with given config.
func CmdWithContext(ctx context.Context, config Config, command string, args ...string) (string, string, error) {
if command == "" {
Expand Down
11 changes: 5 additions & 6 deletions src/test/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package test

import (
"bufio"
"context"
"fmt"
"os"
"regexp"
Expand Down Expand Up @@ -53,7 +52,7 @@ func GetCLIName() string {
}

// Zarf executes a Zarf command.
func (e2e *ZarfE2ETest) Zarf(args ...string) (string, string, error) {
func (e2e *ZarfE2ETest) Zarf(t *testing.T, args ...string) (string, string, error) {
if !slices.Contains(args, "--tmpdir") && !slices.Contains(args, "tools") {
tmpdir, err := os.MkdirTemp("", "zarf-")
if err != nil {
Expand All @@ -76,14 +75,14 @@ func (e2e *ZarfE2ETest) Zarf(args ...string) (string, string, error) {
args = append(args, "--zarf-cache", cacheDir)
defer os.RemoveAll(cacheDir)
}
return exec.CmdWithContext(context.TODO(), exec.PrintCfg(), e2e.ZarfBinPath, args...)
return exec.CmdWithTesting(t, exec.PrintCfg(), e2e.ZarfBinPath, args...)
}

// Kubectl executes `zarf tools kubectl ...`
func (e2e *ZarfE2ETest) Kubectl(args ...string) (string, string, error) {
func (e2e *ZarfE2ETest) Kubectl(t *testing.T, args ...string) (string, string, error) {
tk := []string{"tools", "kubectl"}
args = append(tk, args...)
return e2e.Zarf(args...)
return e2e.Zarf(t, args...)
}

// CleanFiles removes files and directories that have been created during the test.
Expand Down Expand Up @@ -132,7 +131,7 @@ func (e2e *ZarfE2ETest) TeardownRegistry(t *testing.T, port int) {
// GetZarfVersion returns the current build/zarf version
func (e2e *ZarfE2ETest) GetZarfVersion(t *testing.T) string {
// Get the version of the CLI
stdOut, stdErr, err := e2e.Zarf("version")
stdOut, stdErr, err := e2e.Zarf(t, "version")
require.NoError(t, err, stdOut, stdErr)
return strings.Trim(stdOut, "\n")
}
Expand Down
44 changes: 22 additions & 22 deletions src/test/e2e/00_use_cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestUseCLI(t *testing.T) {
err := os.WriteFile(shasumTestFilePath, []byte("random test data 🦄\n"), helpers.ReadWriteUser)
require.NoError(t, err)

stdOut, stdErr, err := e2e.Zarf("prepare", "sha256sum", shasumTestFilePath)
stdOut, stdErr, err := e2e.Zarf(t, "prepare", "sha256sum", shasumTestFilePath)
require.NoError(t, err, stdOut, stdErr)
require.Equal(t, expectedShasum, stdOut, "The expected SHASUM should equal the actual SHASUM")
})
Expand All @@ -45,27 +45,27 @@ func TestUseCLI(t *testing.T) {
// Test `zarf prepare sha256sum` for a remote asset
expectedShasum := "c3cdea0573ba5a058ec090b5d2683bf398e8b1614c37ec81136ed03b78167617\n"

stdOut, stdErr, err := e2e.Zarf("prepare", "sha256sum", "https://zarf-public.s3-us-gov-west-1.amazonaws.com/pipelines/zarf-prepare-shasum-remote-test-file.txt")
stdOut, stdErr, err := e2e.Zarf(t, "prepare", "sha256sum", "https://zarf-public.s3-us-gov-west-1.amazonaws.com/pipelines/zarf-prepare-shasum-remote-test-file.txt")
require.NoError(t, err, stdOut, stdErr)
require.Contains(t, stdOut, expectedShasum, "The expected SHASUM should equal the actual SHASUM")
})

t.Run("zarf version", func(t *testing.T) {
t.Parallel()
// Test `zarf version`
version, _, err := e2e.Zarf("version")
version, _, err := e2e.Zarf(t, "version")
require.NoError(t, err)
require.NotEmpty(t, version, "Zarf version should not be an empty string")
version = strings.Trim(version, "\n")

// test `zarf version --output=json`
stdOut, _, err := e2e.Zarf("version", "--output=json")
stdOut, _, err := e2e.Zarf(t, "version", "--output=json")
require.NoError(t, err)
jsonVersion := fmt.Sprintf(",\"version\":\"%s\"}", version)
require.Contains(t, stdOut, jsonVersion, "Zarf version should be the same in all formats")

// test `zarf version --output=yaml`
stdOut, _, err = e2e.Zarf("version", "--output=yaml")
stdOut, _, err = e2e.Zarf(t, "version", "--output=yaml")
require.NoError(t, err)
yamlVersion := fmt.Sprintf("version: %s", version)
require.Contains(t, stdOut, yamlVersion, "Zarf version should be the same in all formats")
Expand All @@ -75,37 +75,37 @@ func TestUseCLI(t *testing.T) {
t.Parallel()
// Test for expected failure when given a bad component input
path := fmt.Sprintf("build/zarf-package-component-actions-%s.tar.zst", e2e.Arch)
_, _, err := e2e.Zarf("package", "deploy", path, "--components=on-create,foo,git-server", "--confirm")
_, _, err := e2e.Zarf(t, "package", "deploy", path, "--components=on-create,foo,git-server", "--confirm")
require.Error(t, err)
})

t.Run("zarf deploy should return a warning when no components are deployed", func(t *testing.T) {
t.Parallel()
_, _, err := e2e.Zarf("package", "create", "src/test/packages/00-no-components", "-o=build", "--confirm")
_, _, err := e2e.Zarf(t, "package", "create", "src/test/packages/00-no-components", "-o=build", "--confirm")
require.NoError(t, err)
path := fmt.Sprintf("build/zarf-package-no-components-%s.tar.zst", e2e.Arch)

// Test that excluding all components with a leading dash results in a warning
_, stdErr, err := e2e.Zarf("package", "deploy", path, "--components=-deselect-me", "--confirm")
_, stdErr, err := e2e.Zarf(t, "package", "deploy", path, "--components=-deselect-me", "--confirm")
require.NoError(t, err)
require.Contains(t, stdErr, "No components were selected for deployment")

// Test that excluding still works even if a wildcard is given
_, stdErr, err = e2e.Zarf("package", "deploy", path, "--components=*,-deselect-me", "--confirm")
_, stdErr, err = e2e.Zarf(t, "package", "deploy", path, "--components=*,-deselect-me", "--confirm")
require.NoError(t, err)
require.NotContains(t, stdErr, "DESELECT-ME COMPONENT")
})

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("internal", "crc32", "zarf", "--log-level=debug")
_, stdErr, _ := e2e.Zarf(t, "internal", "crc32", "zarf", "--log-level=debug")
expectedOutString := "Log level set to debug"
require.Contains(t, stdErr, expectedOutString, "The log level should be changed to 'debug'")
})

t.Run("zarf package to test bad remote images", func(t *testing.T) {
_, stdErr, err := e2e.Zarf("package", "create", "src/test/packages/00-remote-pull-fail", "--confirm")
_, stdErr, err := e2e.Zarf(t, "package", "create", "src/test/packages/00-remote-pull-fail", "--confirm")
// expecting zarf to have an error and output to stderr
require.Error(t, err)
// Make sure we print the get request error (only look for GET since the actual error changes based on login status)
Expand All @@ -116,11 +116,11 @@ func TestUseCLI(t *testing.T) {

t.Run("zarf package to test archive path", func(t *testing.T) {
t.Parallel()
stdOut, stdErr, err := e2e.Zarf("package", "create", "packages/distros/eks", "--confirm")
stdOut, stdErr, err := e2e.Zarf(t, "package", "create", "packages/distros/eks", "--confirm")
require.NoError(t, err, stdOut, stdErr)

path := fmt.Sprintf("zarf-package-distro-eks-%s-0.0.3.tar.zst", e2e.Arch)
stdOut, stdErr, err = e2e.Zarf("package", "deploy", path, "--confirm")
stdOut, stdErr, err = e2e.Zarf(t, "package", "deploy", path, "--confirm")
require.NoError(t, err, stdOut, stdErr)

require.FileExists(t, "binaries/eksctl_Darwin_x86_64")
Expand All @@ -134,7 +134,7 @@ func TestUseCLI(t *testing.T) {
t.Parallel()
tmpdir := t.TempDir()
cacheDir := filepath.Join(t.TempDir(), ".cache-location")
stdOut, stdErr, err := e2e.Zarf("package", "create", "examples/dos-games", "--zarf-cache", cacheDir, "--tmpdir", tmpdir, "--log-level=debug", "-o=build", "--confirm")
stdOut, stdErr, err := e2e.Zarf(t, "package", "create", "examples/dos-games", "--zarf-cache", cacheDir, "--tmpdir", tmpdir, "--log-level=debug", "-o=build", "--confirm")
require.Contains(t, stdErr, tmpdir, "The other tmp path should show as being created")
require.NoError(t, err, stdOut, stdErr)

Expand All @@ -147,7 +147,7 @@ func TestUseCLI(t *testing.T) {
t.Parallel()
path := fmt.Sprintf("build/zarf-package-component-actions-%s.tar.zst", e2e.Arch)
tmpdir := t.TempDir()
stdOut, stdErr, err := e2e.Zarf("package", "inspect", path, "--tmpdir", tmpdir, "--log-level=debug")
stdOut, stdErr, err := e2e.Zarf(t, "package", "inspect", path, "--tmpdir", tmpdir, "--log-level=debug")
require.Contains(t, stdErr, tmpdir, "The other tmp path should show as being created")
require.NoError(t, err, stdOut, stdErr)
})
Expand All @@ -164,7 +164,7 @@ func TestUseCLI(t *testing.T) {
e2e.CleanFiles(firstFile, secondFile)
})
path := fmt.Sprintf("build/zarf-package-component-choice-%s.tar.zst", e2e.Arch)
stdOut, stdErr, err := e2e.Zarf("package", "deploy", path, "--tmpdir", tmpdir, "--log-level=debug", "--confirm")
stdOut, stdErr, err := e2e.Zarf(t, "package", "deploy", path, "--tmpdir", tmpdir, "--log-level=debug", "--confirm")
require.Contains(t, stdErr, tmpdir, "The other tmp path should show as being created")
require.NoError(t, err, stdOut, stdErr)
})
Expand All @@ -174,7 +174,7 @@ func TestUseCLI(t *testing.T) {
tmpdir := t.TempDir()
// Test removal of cache
cachePath := filepath.Join(tmpdir, ".cache-location")
stdOut, stdErr, err := e2e.Zarf("tools", "clear-cache", "--zarf-cache", cachePath)
stdOut, stdErr, err := e2e.Zarf(t, "tools", "clear-cache", "--zarf-cache", cachePath)
require.NoError(t, err, stdOut, stdErr)
// Check that ReadDir returns no such file or directory for the cachePath
_, err = os.ReadDir(cachePath)
Expand All @@ -196,7 +196,7 @@ func TestUseCLI(t *testing.T) {
t.Cleanup(func() {
e2e.CleanFiles(tlsCA, tlsCert, tlsKey)
})
stdOut, stdErr, err := e2e.Zarf("tools", "gen-pki", "github.com", "--sub-alt-name", "google.com")
stdOut, stdErr, err := e2e.Zarf(t, "tools", "gen-pki", "github.com", "--sub-alt-name", "google.com")
require.NoError(t, err, stdOut, stdErr)
require.Contains(t, stdErr, "Successfully created a chain of trust for github.com")

Expand Down Expand Up @@ -225,16 +225,16 @@ func TestUseCLI(t *testing.T) {
require.NoError(t, err)

// Test that yq can eval properly
_, stdErr, err := e2e.Zarf("tools", "yq", "eval", "-i", `.items[1].name = "renamed-item"`, file)
_, stdErr, err := e2e.Zarf(t, "tools", "yq", "eval", "-i", `.items[1].name = "renamed-item"`, file)
require.NoError(t, err, stdErr)
stdOut, _, err := e2e.Zarf("tools", "yq", ".items[1].name", file)
stdOut, _, err := e2e.Zarf(t, "tools", "yq", ".items[1].name", file)
require.NoError(t, err)
require.Contains(t, stdOut, "renamed-item")

// Test that yq ea can be used properly
_, _, err = e2e.Zarf("tools", "yq", "eval-all", "-i", `. as $doc ireduce ({}; .items += $doc.items)`, file, otherFile)
_, _, err = e2e.Zarf(t, "tools", "yq", "eval-all", "-i", `. as $doc ireduce ({}; .items += $doc.items)`, file, otherFile)
require.NoError(t, err)
stdOut, _, err = e2e.Zarf("tools", "yq", "e", ".items | length", file)
stdOut, _, err = e2e.Zarf(t, "tools", "yq", "e", ".items | length", file)
require.NoError(t, err)
require.Equal(t, "4\n", stdOut)
})
Expand Down
6 changes: 3 additions & 3 deletions src/test/e2e/01_component_choice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ func TestComponentChoice(t *testing.T) {

// Try to deploy both and expect failure due to only one component allowed at a time
// We currently don't have a pattern to actually test the interactive prompt, so just testing automation for now
stdOut, stdErr, err := e2e.Zarf("package", "deploy", path, "--components=first-choice,second-choice", "--confirm")
stdOut, stdErr, err := e2e.Zarf(t, "package", "deploy", path, "--components=first-choice,second-choice", "--confirm")
require.Error(t, err, stdOut, stdErr)

// Deploy a single choice and expect success
stdOut, stdErr, err = e2e.Zarf("package", "deploy", path, "--components=first-choice", "--confirm")
stdOut, stdErr, err = e2e.Zarf(t, "package", "deploy", path, "--components=first-choice", "--confirm")
require.NoError(t, err, stdOut, stdErr)
require.Contains(t, stdErr, "Component first-choice is using group which has been deprecated", "output should show a warning for group being deprecated.")

Expand All @@ -40,7 +40,7 @@ func TestComponentChoice(t *testing.T) {
require.NoFileExists(t, secondFile)

// Deploy using default choice
stdOut, stdErr, err = e2e.Zarf("package", "deploy", path, "--confirm")
stdOut, stdErr, err = e2e.Zarf(t, "package", "deploy", path, "--confirm")
require.NoError(t, err, stdOut, stdErr)

// Verify the file was created
Expand Down
20 changes: 10 additions & 10 deletions src/test/e2e/02_component_actions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestComponentActions(t *testing.T) {

/* Create */
// Try creating the package to test the onCreate actions.
stdOut, stdErr, err := e2e.Zarf("package", "create", "examples/component-actions", "--confirm")
stdOut, stdErr, err := e2e.Zarf(t, "package", "create", "examples/component-actions", "--confirm")
require.NoError(t, err, stdOut, stdErr)
require.Contains(t, stdErr, "Completed \"Create a test file\"")
require.Contains(t, stdErr, "Completed \"touch test-create-after.txt\"")
Expand All @@ -54,7 +54,7 @@ func TestComponentActions(t *testing.T) {
t.Parallel()

// Deploy the simple script that should pass.
stdOut, stdErr, err = e2e.Zarf("package", "deploy", path, "--components=on-deploy-and-remove", "--confirm")
stdOut, stdErr, err = e2e.Zarf(t, "package", "deploy", path, "--components=on-deploy-and-remove", "--confirm")
require.NoError(t, err, stdOut, stdErr)

// Check that the deploy artifacts were created.
Expand All @@ -63,7 +63,7 @@ func TestComponentActions(t *testing.T) {
}

// Remove the simple script that should pass.
stdOut, stdErr, err = e2e.Zarf("package", "remove", path, "--components=on-deploy-and-remove", "--confirm")
stdOut, stdErr, err = e2e.Zarf(t, "package", "remove", path, "--components=on-deploy-and-remove", "--confirm")
require.NoError(t, err, stdOut, stdErr)

// Check that the deploy artifacts were removed.
Expand All @@ -75,7 +75,7 @@ func TestComponentActions(t *testing.T) {
t.Run("action on-deploy-with-timeout", func(t *testing.T) {
t.Parallel()
// Deploy the simple action that should fail the timeout.
stdOut, stdErr, err = e2e.Zarf("package", "deploy", path, "--components=on-deploy-with-timeout", "--confirm")
stdOut, stdErr, err = e2e.Zarf(t, "package", "deploy", path, "--components=on-deploy-with-timeout", "--confirm")
require.Error(t, err, stdOut, stdErr)
require.Contains(t, stdErr, "after 1 second")
require.Contains(t, stdErr, "😭😭😭 this action failed because it took too long to run 😭😭😭")
Expand All @@ -85,7 +85,7 @@ func TestComponentActions(t *testing.T) {
t.Parallel()

// Test using a Zarf Variable within the action
stdOut, stdErr, err = e2e.Zarf("package", "deploy", path, "--components=on-deploy-with-variable", "--confirm")
stdOut, stdErr, err = e2e.Zarf(t, "package", "deploy", path, "--components=on-deploy-with-variable", "--confirm")
require.NoError(t, err, stdOut, stdErr)
require.Contains(t, stdErr, "the dog says ruff")

Expand All @@ -94,7 +94,7 @@ func TestComponentActions(t *testing.T) {
t.Run("action on-deploy-with-dynamic-variable", func(t *testing.T) {
t.Parallel()
// Test using dynamic and multiple-variables
stdOut, stdErr, err = e2e.Zarf("package", "deploy", path, "--components=on-deploy-with-dynamic-variable,on-deploy-with-multiple-variables", "--confirm")
stdOut, stdErr, err = e2e.Zarf(t, "package", "deploy", path, "--components=on-deploy-with-dynamic-variable,on-deploy-with-multiple-variables", "--confirm")
require.NoError(t, err, stdOut, stdErr)
require.Contains(t, stdErr, "the cat says meow")
require.Contains(t, stdErr, "the dog says ruff")
Expand All @@ -108,7 +108,7 @@ func TestComponentActions(t *testing.T) {
deployWithEnvVarArtifact := "test-filename-from-env.txt"

// Test using environment variables
stdOut, stdErr, err = e2e.Zarf("package", "deploy", path, "--components=on-deploy-with-env-var", "--confirm")
stdOut, stdErr, err = e2e.Zarf(t, "package", "deploy", path, "--components=on-deploy-with-env-var", "--confirm")
require.NoError(t, err, stdOut, stdErr)
require.FileExists(t, deployWithEnvVarArtifact)

Expand All @@ -121,7 +121,7 @@ func TestComponentActions(t *testing.T) {
deployTemplatedArtifact := "test-templated.txt"

// Test using a templated file but without dynamic variables
stdOut, stdErr, err = e2e.Zarf("package", "deploy", path, "--components=on-deploy-with-template-use-of-variable", "--confirm")
stdOut, stdErr, err = e2e.Zarf(t, "package", "deploy", path, "--components=on-deploy-with-template-use-of-variable", "--confirm")
require.NoError(t, err, stdOut, stdErr)
outTemplated, err := os.ReadFile(deployTemplatedArtifact)
require.NoError(t, err)
Expand All @@ -133,7 +133,7 @@ func TestComponentActions(t *testing.T) {
e2e.CleanFiles(deployTemplatedArtifact)

// Test using a templated file with dynamic variables
stdOut, stdErr, err = e2e.Zarf("package", "deploy", path, "--components=on-deploy-with-template-use-of-variable,on-deploy-with-dynamic-variable,on-deploy-with-multiple-variables", "--confirm")
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")
require.NoError(t, err, stdOut, stdErr)
outTemplated, err = os.ReadFile(deployTemplatedArtifact)
require.NoError(t, err)
Expand All @@ -147,7 +147,7 @@ func TestComponentActions(t *testing.T) {

t.Run("action on-deploy-immediate-failure", func(t *testing.T) {
t.Parallel()
stdOut, stdErr, err = e2e.Zarf("package", "deploy", path, "--components=on-deploy-immediate-failure", "--confirm")
stdOut, stdErr, err = e2e.Zarf(t, "package", "deploy", path, "--components=on-deploy-immediate-failure", "--confirm")
require.Error(t, err, stdOut, stdErr)
require.Contains(t, stdErr, "failed to deploy package")
// regression test to ensure that failed commands are not erroneously flagged as a timeout
Expand Down
Loading

0 comments on commit e3c48df

Please sign in to comment.