Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor deprecation e2e test to not depend on CLI output #3130

Merged
merged 1 commit into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/test/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ func GetCLIName() string {

// Zarf executes a Zarf command.
func (e2e *ZarfE2ETest) Zarf(t *testing.T, args ...string) (_ string, _ string, err error) {
return e2e.ZarfInDir(t, "", args...)
}

// ZarfInDir executes a Zarf command in specific directory.
func (e2e *ZarfE2ETest) ZarfInDir(t *testing.T, dir string, args ...string) (_ string, _ string, err error) {
if !slices.Contains(args, "--tmpdir") && !slices.Contains(args, "tools") {
tmpdir, err := os.MkdirTemp("", "zarf-")
if err != nil {
Expand Down Expand Up @@ -83,7 +88,9 @@ func (e2e *ZarfE2ETest) Zarf(t *testing.T, args ...string) (_ string, _ string,
err = errors.Join(err, errRemove)
}(cacheDir)
}
return exec.CmdWithTesting(t, exec.PrintCfg(), e2e.ZarfBinPath, args...)
cfg := exec.PrintCfg()
cfg.Dir = dir
return exec.CmdWithTesting(t, cfg, e2e.ZarfBinPath, args...)
}

// Kubectl executes `zarf tools kubectl ...`
Expand Down
123 changes: 58 additions & 65 deletions src/test/e2e/03_deprecations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,98 +5,91 @@
package test

import (
"context"
"fmt"
"path/filepath"
"testing"

goyaml "github.com/goccy/go-yaml"
"github.com/otiai10/copy"
"github.com/stretchr/testify/require"

layout2 "github.com/zarf-dev/zarf/src/internal/packager2/layout"
)

// TestDeprecatedComponentScripts verifies that deprecated component scripts are still able to be executed (after being internally
// migrated into zarf actions).
// TestDeprecatedComponentScripts verifies that deprecated component scripts are still able to be executed after being internally migrated into zarf actions.
func TestDeprecatedComponentScripts(t *testing.T) {
t.Log("E2E: Testing deprecated component scripts")
t.Parallel()

// Note these files will be created in the package directory, not CWD
testPackageDirPath := "src/test/packages/03-deprecated-component-scripts"
prepareArtifact := fmt.Sprintf("%s/test-deprecated-prepare-hook.txt", testPackageDirPath)
deployArtifacts := []string{
"test-deprecated-deploy-before-hook.txt",
"test-deprecated-deploy-after-hook.txt",
}
allArtifacts := append(deployArtifacts, prepareArtifact)
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(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")
require.Contains(t, stdErr, "Component '3-test-deprecated-timeout-scripts' is using scripts")

// Test for package create prepare artifact
require.FileExists(t, prepareArtifact)

// Test to ensure the deploy scripts are not executed

packagePath := t.TempDir()
err := copy.Copy("src/test/packages/03-deprecated-component-scripts", packagePath)
require.NoError(t, err)

workingDirPath := t.TempDir()
tarName := fmt.Sprintf("zarf-package-deprecated-component-scripts-%s.tar.zst", e2e.Arch)

// Try creating the package to test the create scripts
_, _, err = e2e.ZarfInDir(t, workingDirPath, "package", "create", packagePath, "--confirm")
require.NoError(t, err)

require.FileExists(t, filepath.Join(packagePath, "test-deprecated-prepare-hook.txt"))
for _, artifact := range deployArtifacts {
require.NoFileExists(t, artifact)
require.NoFileExists(t, filepath.Join(workingDirPath, artifact))
}

// 2. Deploy the simple script that should pass
stdOut, stdErr, err = e2e.Zarf(t, "package", "deploy", testPackagePath, "--confirm", "--components=2-test-deprecated-deploy-scripts")
require.NoError(t, err, stdOut, stdErr)
// Deploy the simple script that should pass
_, _, err = e2e.ZarfInDir(t, workingDirPath, "package", "deploy", tarName, "--confirm", "--components=2-test-deprecated-deploy-scripts")
require.NoError(t, err)

// Check that the deploy artifacts were created
for _, artifact := range deployArtifacts {
require.FileExists(t, artifact)
require.FileExists(t, filepath.Join(workingDirPath, artifact))
}

// 3. Deploy the simple script that should fail the timeout
stdOut, stdErr, err = e2e.Zarf(t, "package", "deploy", testPackagePath, "--confirm", "--components=3-test-deprecated-timeout-scripts")
require.Error(t, err, stdOut, stdErr)
// Deploy the simple script that should fail the timeout
_, _, err = e2e.ZarfInDir(t, workingDirPath, "package", "deploy", tarName, "--confirm", "--components=3-test-deprecated-timeout-scripts")
require.Error(t, err)
}

// TestDeprecatedSetAndPackageVariables verifies that deprecated setVariables and PKG_VARs still able to be set.
func TestDeprecatedSetAndPackageVariables(t *testing.T) {
t.Log("E2E: Testing deprecated set variables")
t.Parallel()

// Note prepare script files will be created in the package directory, not CWD
testPackageDirPath := "src/test/packages/03-deprecated-set-variable"
prepareArtifact := fmt.Sprintf("%s/test-deprecated-prepare-hook.txt", testPackageDirPath)
deployArtifacts := []string{
"test-deprecated-deploy-before-hook.txt",
"test-deprecated-deploy-after-hook.txt",
}
allArtifacts := append(deployArtifacts, prepareArtifact)
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)
outputFlag := fmt.Sprintf("-o=%s", testPackageDirPath)
outPath := t.TempDir()
tarPath := filepath.Join(outPath, fmt.Sprintf("zarf-package-deprecated-set-variable-%s.tar.zst", e2e.Arch))

// Check that the command still errors out
stdOut, stdErr, err := e2e.Zarf(t, "package", "create", testPackageDirPath, outputFlag, "--confirm")
require.Error(t, err, stdOut, stdErr)
require.Contains(t, stdErr, "template \"ECHO\" must be '--set'")

// 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(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###")

// 1. Deploy the setVariable action that should pass and output the variable
stdOut, stdErr, err = e2e.Zarf(t, "package", "deploy", testPackagePath, "--confirm", "--components=1-test-deprecated-set-variable")
require.NoError(t, err, stdOut, stdErr)
require.Contains(t, stdErr, "Hello from Hello Kitteh")

// 2. Deploy the setVariable action that should pass and output the variable
stdOut, stdErr, err = e2e.Zarf(t, "package", "deploy", testPackagePath, "--confirm", "--components=2-test-deprecated-pkg-var")
require.NoError(t, err, stdOut, stdErr)
require.Contains(t, stdErr, "Zarf-The-Axolotl")
_, _, err := e2e.Zarf(t, "package", "create", testPackageDirPath, "-o", outPath, "--confirm")
require.Error(t, err)

// // Check that the command displays a warning on create
_, _, err = e2e.Zarf(t, "package", "create", testPackageDirPath, "-o", outPath, "--confirm", "--set", "ECHO=Zarf-The-Axolotl")
require.NoError(t, err)

pkgLayout, err := layout2.LoadFromTar(context.Background(), tarPath, layout2.PackageLayoutOptions{})
require.NoError(t, err)
b, err := goyaml.Marshal(pkgLayout.Pkg.Components)
require.NoError(t, err)
expectedYaml := `- name: 1-test-deprecated-set-variable
actions:
onDeploy:
before:
- cmd: echo "Hello Kitteh"
setVariables:
- name: HELLO_KITTEH
- cmd: echo "Hello from ${ZARF_VAR_HELLO_KITTEH}"
- name: 2-test-deprecated-pkg-var
actions:
onDeploy:
before:
- cmd: echo "Zarf-The-Axolotl"
`
require.Equal(t, expectedYaml, string(b))
}
6 changes: 5 additions & 1 deletion src/test/e2e/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ func TestMain(m *testing.M) {
}

e2e.Arch = config.GetArch()
e2e.ZarfBinPath = filepath.Join("build", test.GetCLIName())
zarfBinPath, err := filepath.Abs(filepath.Join("build", test.GetCLIName()))
if err != nil {
log.Fatal(err)
}
e2e.ZarfBinPath = zarfBinPath
e2e.ApplianceMode = os.Getenv(applianceModeEnvVar) == "true"
e2e.ApplianceModeKeep = os.Getenv(applianceModeKeepEnvVar) == "true"

Expand Down