Skip to content

Commit

Permalink
chore: extract test changes to another branch
Browse files Browse the repository at this point in the history
Signed-off-by: Kit Patella <[email protected]>
  • Loading branch information
mkcp committed Sep 26, 2024
1 parent 391296f commit 2c61d4c
Show file tree
Hide file tree
Showing 14 changed files with 53 additions and 116 deletions.
19 changes: 4 additions & 15 deletions src/test/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ package test

import (
"bufio"
"errors"
"fmt"
"log/slog"
"os"
"regexp"
"runtime"
Expand Down Expand Up @@ -55,16 +53,13 @@ func GetCLIName() string {
}

// Zarf executes a Zarf command.
func (e2e *ZarfE2ETest) Zarf(t *testing.T, args ...string) (_ string, _ string, err 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 {
return "", "", err
}
defer func(path string) {
errRemove := os.RemoveAll(path)
err = errors.Join(err, errRemove)
}(tmpdir)
defer os.RemoveAll(tmpdir)
args = append(args, "--tmpdir", tmpdir)
}
if !slices.Contains(args, "--zarf-cache") && !slices.Contains(args, "tools") && os.Getenv("CI") == "true" {
Expand All @@ -79,10 +74,7 @@ func (e2e *ZarfE2ETest) Zarf(t *testing.T, args ...string) (_ string, _ string,
return "", "", err
}
args = append(args, "--zarf-cache", cacheDir)
defer func(path string) {
errRemove := os.RemoveAll(path)
err = errors.Join(err, errRemove)
}(cacheDir)
defer os.RemoveAll(cacheDir)
}
return exec.CmdWithTesting(t, exec.PrintCfg(), e2e.ZarfBinPath, args...)
}
Expand All @@ -97,10 +89,7 @@ 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 {
err := os.RemoveAll(file)
if err != nil {
slog.Debug("Unable to cleanup files", "files", files, "error", err)
}
_ = os.RemoveAll(file)
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/test/e2e/00_use_cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,7 @@ 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, err := e2e.Zarf(t, "internal", "crc32", "zarf", "--log-level=debug")
require.NoError(t, err)
_, 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'")
})
Expand Down
6 changes: 2 additions & 4 deletions src/test/e2e/12_lint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,9 @@ func TestLint(t *testing.T) {

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

Expand Down
8 changes: 2 additions & 6 deletions src/test/e2e/14_oci_compose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ 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 @@ -174,12 +176,6 @@ 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") //nolint:errcheck
_, _, _ = e2e.Kubectl(t, "scale", "deploy", "-n", "zarf", "agent-hook", "--replicas=1") //nolint:errcheck
_, _, _ = e2e.Kubectl(t, "scale", "deploy", "-n", "kube-system", "coredns", "--replicas=1")
_, _, _ = e2e.Kubectl(t, "scale", "deploy", "-n", "zarf", "agent-hook", "--replicas=1")
}

func checkLogForSensitiveState(t *testing.T, logText string, zarfState types.ZarfState) {
Expand Down
4 changes: 1 addition & 3 deletions src/test/e2e/21_connect_creds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ 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 @@ -85,9 +86,6 @@ 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: 2 additions & 4 deletions src/test/e2e/24_variables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,8 @@ func TestVariables(t *testing.T) {
require.Contains(t, stdErr, "", expectedOutString)

// Test that not specifying a prompted variable results in an error
_, stdErr, err = e2e.Zarf(t, "package", "deploy", path, "--confirm")
_, stdErr, _ = e2e.Zarf(t, "package", "deploy", path, "--confirm")
expectedOutString = "variable 'SITE_NAME' must be '--set' when using the '--confirm' flag"
require.Error(t, err)
require.Contains(t, stdErr, "", expectedOutString)

// Test that specifying an invalid variable value results in an error
Expand Down Expand Up @@ -74,8 +73,7 @@ func TestVariables(t *testing.T) {
require.Contains(t, string(outputTF), "unicorn-land")

// Verify the configmap was properly templated
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")
kubectlOut, _, _ := e2e.Kubectl(t, "-n", "nginx", "get", "configmap", "nginx-configmap", "-o", "jsonpath='{.data.index\\.html}' ")
// 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: 3 additions & 5 deletions src/test/e2e/25_helm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,7 @@ func testHelmEscaping(t *testing.T) {
require.NoError(t, err, stdOut, stdErr)

// Verify the configmap was deployed, escaped, and contains all of its data
kubectlOut, err := exec.Command("kubectl", "-n", "default", "describe", "cm", "dont-template-me").Output()
require.NoError(t, err, "unable to describe configmap")
kubectlOut, _ := exec.Command("kubectl", "-n", "default", "describe", "cm", "dont-template-me").Output()
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 @@ -176,9 +175,8 @@ func testHelmAdoption(t *testing.T) {
deploymentManifest := "src/test/packages/25-manifest-adoption/deployment.yaml"

// Deploy dos-games manually into the cluster without Zarf
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")
kubectlOut, _, _ := e2e.Kubectl(t, "apply", "-f", deploymentManifest)
require.Contains(t, string(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") //nolint:errcheck
_, _, _ = e2e.Kubectl(t, "delete", "namespace", "no-wait", "--force=true", "--wait=false", "--grace-period=0")
}
require.NoError(t, err, stdOut, stdErr)

Expand Down
29 changes: 10 additions & 19 deletions src/test/e2e/29_config_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,15 @@ 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 @@ -142,36 +140,29 @@ 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, _, err := e2e.Zarf(t, "--help")
require.NoError(t, err)
stdOut, _, _ := e2e.Zarf(t, "--help")
for _, test := range globalFlags {
require.Contains(t, stdOut, test)
require.Contains(t, string(stdOut), test)
}

// Test init flags
stdOut, _, err = e2e.Zarf(t, "init", "--help")
require.NoError(t, err)
stdOut, _, _ = e2e.Zarf(t, "init", "--help")
for _, test := range initFlags {
require.Contains(t, stdOut, test)
require.Contains(t, string(stdOut), test)
}

// Test package create flags
stdOut, _, err = e2e.Zarf(t, "package", "create", "--help")
require.NoError(t, err)
stdOut, _, _ = e2e.Zarf(t, "package", "create", "--help")
for _, test := range packageCreateFlags {
require.Contains(t, stdOut, test)
require.Contains(t, string(stdOut), test)
}

// Test package deploy flags
stdOut, _, err = e2e.Zarf(t, "package", "deploy", "--help")
require.NoError(t, err)
stdOut, _, _ = e2e.Zarf(t, "package", "deploy", "--help")
for _, test := range packageDeployFlags {
require.Contains(t, stdOut, test)
require.Contains(t, string(stdOut), test)
}

// Cleanup
err = os.Unsetenv("ZARF_CONFIG")
require.NoError(t, err)
}
3 changes: 1 addition & 2 deletions src/test/external/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ 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")
err = exec.CmdWithPrint(zarfBinPath, "package", "create", temp, "--confirm", "--output", temp)
require.NoError(t, err, "unable to create package")
exec.CmdWithPrint(zarfBinPath, "package", "create", temp, "--confirm", "--output", temp)
}

func verifyWaitSuccess(t *testing.T, timeoutMinutes time.Duration, cmd string, args []string, condition string, onTimeout string) bool {
Expand Down
5 changes: 1 addition & 4 deletions src/test/external/ext_in_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ 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)
createPodInfoPackageWithInsecureSources(suite.T(), temp)

// Deploy the flux example package
Expand Down Expand Up @@ -199,10 +200,6 @@ func (suite *ExtInClusterTestSuite) Test_1_Deploy() {

_, _, err = exec.CmdWithTesting(suite.T(), exec.PrintCfg(), zarfBinPath, "destroy", "--confirm")
suite.NoError(err, "unable to teardown zarf")

// Cleanup tmpdir
err = os.RemoveAll(temp)
suite.NoError(err, "failed to clean up tempdir")
}

func TestExtInClusterTestSuite(t *testing.T) {
Expand Down
42 changes: 13 additions & 29 deletions src/test/external/ext_out_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,10 @@ func (suite *ExtOutClusterTestSuite) SetupSuite() {
suite.Assertions = require.New(suite.T())

// Teardown any leftovers from previous tests
// NOTE(mkcp): We dogsled these errors because some of these commands will error if they don't cleanup a resource,
// which is ok. A better solution would be checking for none or unexpected kinds of errors.
_ = exec.CmdWithPrint("k3d", "cluster", "delete", clusterName) //nolint:errcheck
_ = exec.CmdWithPrint("docker", "rm", "-f", "k3d-"+registryHost) //nolint:errcheck
_ = exec.CmdWithPrint("docker", "compose", "down") //nolint:errcheck
_ = exec.CmdWithPrint("docker", "network", "remove", network) //nolint:errcheck
_ = exec.CmdWithPrint("k3d", "cluster", "delete", clusterName)
_ = exec.CmdWithPrint("docker", "rm", "-f", "k3d-"+registryHost)
_ = exec.CmdWithPrint("docker", "compose", "down")
_ = exec.CmdWithPrint("docker", "network", "remove", network)

// Setup a network for everything to live inside
err := exec.CmdWithPrint("docker", "network", "create", "--driver=bridge", "--subnet="+subnet, "--gateway="+gateway, network)
Expand Down Expand Up @@ -149,6 +147,7 @@ func (suite *ExtOutClusterTestSuite) Test_1_Deploy() {
func (suite *ExtOutClusterTestSuite) Test_2_DeployGitOps() {
// Deploy the flux example package
temp := suite.T().TempDir()
defer os.Remove(temp)
createPodInfoPackageWithInsecureSources(suite.T(), temp)

deployArgs := []string{"package", "deploy", filepath.Join(temp, "zarf-package-podinfo-flux-amd64.tar.zst"), "--confirm"}
Expand All @@ -159,10 +158,6 @@ func (suite *ExtOutClusterTestSuite) Test_2_DeployGitOps() {
deployArgs = []string{"package", "deploy", path, "--confirm"}
err = exec.CmdWithPrint(zarfBinPath, deployArgs...)
suite.NoError(err)

// Clean up tmpdir
err = os.RemoveAll(temp)
suite.NoError(err, "unable to remove temporary directory")
}

func (suite *ExtOutClusterTestSuite) Test_3_AuthToPrivateHelmChart() {
Expand All @@ -173,12 +168,12 @@ func (suite *ExtOutClusterTestSuite) Test_3_AuthToPrivateHelmChart() {

tempDir := suite.T().TempDir()
repoPath := filepath.Join(tempDir, "repositories.yaml")
err := os.Setenv("HELM_REPOSITORY_CONFIG", repoPath)
suite.NoError(err, "unable to set HELM_REPOSITORY_CONFIG")
os.Setenv("HELM_REPOSITORY_CONFIG", repoPath)
defer os.Unsetenv("HELM_REPOSITORY_CONFIG")

packagePath := filepath.Join("..", "packages", "external-helm-auth")
findImageArgs := []string{"dev", "find-images", packagePath}
err = exec.CmdWithPrint(zarfBinPath, findImageArgs...)
err := exec.CmdWithPrint(zarfBinPath, findImageArgs...)
suite.Error(err, "Since auth has not been setup, this should fail")

repoFile := repo.NewFile()
Expand All @@ -200,10 +195,6 @@ func (suite *ExtOutClusterTestSuite) Test_3_AuthToPrivateHelmChart() {
packageCreateArgs := []string{"package", "create", packagePath, fmt.Sprintf("--output=%s", tempDir), "--confirm"}
err = exec.CmdWithPrint(zarfBinPath, packageCreateArgs...)
suite.NoError(err, "Unable to create package, helm auth likely failed")

// Cleanup env var
err = os.Unsetenv("HELM_REPOSITORY_CONFIG")
suite.NoError(err, "unable to unset HELM_REPOSITORY_CONFIG")
}

func (suite *ExtOutClusterTestSuite) createHelmChartInGitea(baseURL string, username string, password string) {
Expand All @@ -222,19 +213,15 @@ func (suite *ExtOutClusterTestSuite) createHelmChartInGitea(baseURL string, user

file, err := os.Open(podinfoTarballPath)
suite.NoError(err)
defer file.Close()

body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile("file", podinfoTarballPath)
suite.NoError(err)
_, err = io.Copy(part, file)
suite.NoError(err)

// Cleanup file and writer
err = file.Close()
suite.NoError(err, "unable to close file")
err = writer.Close()
suite.NoError(err, "unable to close writer")
writer.Close()

req, err := http.NewRequest("POST", url, body)
suite.NoError(err)
Expand All @@ -246,8 +233,7 @@ func (suite *ExtOutClusterTestSuite) createHelmChartInGitea(baseURL string, user

resp, err := client.Do(req)
suite.NoError(err)
err = resp.Body.Close()
suite.NoError(err, "unable to close response body")
resp.Body.Close()
}

func (suite *ExtOutClusterTestSuite) makeGiteaUserPrivate(baseURL string, username string, password string) {
Expand All @@ -272,12 +258,10 @@ func (suite *ExtOutClusterTestSuite) makeGiteaUserPrivate(baseURL string, userna
client := &http.Client{}
resp, err := client.Do(req)
suite.NoError(err)
defer resp.Body.Close()

_, err = io.ReadAll(resp.Body)
suite.NoError(err)

// Cleanup
err = resp.Body.Close()
suite.NoError(err, "unable to close response body")
}

func TestExtOurClusterTestSuite(t *testing.T) {
Expand Down
Loading

0 comments on commit 2c61d4c

Please sign in to comment.