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

test: cleanup e2e tests #2601

Merged
merged 1 commit into from
Jun 10, 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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ publish-init-package:
$(ZARF_BIN) package publish . oci://$(REPOSITORY_URL)

build-examples: ## Build all of the example packages
@test -s $(ZARF_BIN) || $(MAKE) build-cli
@test -s $(ZARF_BIN) || $(MAKE)

@test -s ./build/zarf-package-dos-games-$(ARCH)-1.0.0.tar.zst || $(ZARF_BIN) package create examples/dos-games -o build -a $(ARCH) --confirm

Expand Down
52 changes: 12 additions & 40 deletions src/test/e2e/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
package test

import (
"fmt"
"log"
"os"
"path"
"path/filepath"
"testing"

"github.com/defenseunicorns/zarf/src/config"
Expand All @@ -25,53 +25,25 @@ const (
skipK8sEnvVar = "SKIP_K8S"
)

// TestMain lets us customize the test run. See https://medium.com/goingogo/why-use-testmain-for-testing-in-go-dafb52b406bc.
func TestMain(m *testing.M) {
// Work from the root directory of the project
err := os.Chdir("../../../")
rootDir, err := filepath.Abs("../../../")
if err != nil {
fmt.Println(err) //nolint:forbidigo
log.Fatal(err)
}

// K3d use the intern package, which requires this to be set in go 1.19
os.Setenv("ASSUME_NO_MOVING_GC_UNSAFE_RISK_IT_WITH", "go1.19")

// Set the log level to trace for when we call Zarf functions internally
message.SetLogLevel(message.TraceLevel)

retCode, err := doAllTheThings(m)
if err != nil {
fmt.Println(err) //nolint:forbidigo
if err := os.Chdir(rootDir); err != nil {
log.Fatal(err)
}

os.Exit(retCode)
}

// doAllTheThings just wraps what should go in TestMain. It's in its own function so it can
// [a] Not have a bunch of `os.Exit()` calls in it
// [b] Do defers properly
// [c] Handle errors cleanly
//
// It returns the return code passed from `m.Run()` and any error thrown.
func doAllTheThings(m *testing.M) (int, error) {
var err error

// Set up constants in the global variable that all the tests are able to access
e2e.Arch = config.GetArch()
e2e.ZarfBinPath = path.Join("build", test.GetCLIName())
e2e.ZarfBinPath = filepath.Join("build", test.GetCLIName())
e2e.ApplianceMode = os.Getenv(applianceModeEnvVar) == "true"
e2e.ApplianceModeKeep = os.Getenv(applianceModeKeepEnvVar) == "true"
e2e.RunClusterTests = os.Getenv(skipK8sEnvVar) != "true"

// Validate that the Zarf binary exists. If it doesn't that means the dev hasn't built it, usually by running
// `make build-cli`
_, err = os.Stat(e2e.ZarfBinPath)
if err != nil {
return 1, fmt.Errorf("zarf binary %s not found", e2e.ZarfBinPath)
}

// Run the tests, with the cluster cleanup being deferred to the end of the function call
returnCode := m.Run()
message.SetLogLevel(message.TraceLevel)

return returnCode, nil
if _, err := os.Stat(e2e.ZarfBinPath); err != nil {
log.Fatalf("zarf binary %s not found: %v", e2e.ZarfBinPath, err)
}
os.Exit(m.Run())
}
Loading