Skip to content

Commit

Permalink
lint testing
Browse files Browse the repository at this point in the history
Signed-off-by: Austin Abro <[email protected]>
  • Loading branch information
AustinAbro321 committed Dec 13, 2024
1 parent b9355a4 commit 14ee3e9
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 30 deletions.
10 changes: 0 additions & 10 deletions src/test/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,6 @@ func (e2e *ZarfE2ETest) GetZarfVersion(t *testing.T) string {
return strings.Trim(stdOut, "\n")
}

// StripMessageFormatting strips any ANSI color codes and extra spaces from a given string
func (e2e *ZarfE2ETest) StripMessageFormatting(input string) string {
// Regex to strip any color codes from the output - https://regex101.com/r/YFyIwC/2
ansiRegex := regexp.MustCompile(`\x1b\[(.*?)m`)
unAnsiInput := ansiRegex.ReplaceAllString(input, "")
// Regex to strip any more than two spaces or newline - https://regex101.com/r/wqQmys/1
multiSpaceRegex := regexp.MustCompile(`\s{2,}|\n`)
return multiSpaceRegex.ReplaceAllString(unAnsiInput, " ")
}

// NormalizeYAMLFilenames normalizes YAML filenames / paths across Operating Systems (i.e Windows vs Linux)
func (e2e *ZarfE2ETest) NormalizeYAMLFilenames(input string) string {
if runtime.GOOS != "windows" {
Expand Down
2 changes: 1 addition & 1 deletion src/test/e2e/08_create_differential_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestCreateDifferential(t *testing.T) {
// Build the differential package without changing the version
_, stdErr, err = e2e.Zarf(t, "package", "create", packagePath, "--set=PACKAGE_VERSION=v0.25.0", differentialFlag, "--confirm")
require.Error(t, err, "zarf package create should have errored when a differential package was being created without updating the package version number")
require.Contains(t, e2e.StripMessageFormatting(stdErr), lang.PkgCreateErrDifferentialSameVersion)
require.Contains(t, stdErr, lang.PkgCreateErrDifferentialSameVersion)

// Build the differential package
stdOut, stdErr, err = e2e.Zarf(t, "package", "create", packagePath, "--set=PACKAGE_VERSION=v0.26.0", differentialFlag, "--confirm")
Expand Down
2 changes: 1 addition & 1 deletion src/test/e2e/09_component_compose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,5 +181,5 @@ func TestComposabilityBadLocalOS(t *testing.T) {
composeTestBadLocalOS := filepath.Join("src", "test", "packages", "09-composable-packages", "bad-local-os")
_, stdErr, err := e2e.Zarf(t, "package", "create", composeTestBadLocalOS, "-o", "build", "--no-color", "--confirm")
require.Error(t, err)
require.Contains(t, e2e.StripMessageFormatting(stdErr), "\"only.localOS\" \"linux\" cannot be redefined as \"windows\" during compose")
require.Contains(t, stdErr, "\"only.localOS\" \"linux\" cannot be redefined as \"windows\" during compose")
}
32 changes: 17 additions & 15 deletions src/test/e2e/12_lint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import (
"fmt"
"os"
"path/filepath"
"regexp"
"testing"

"github.com/stretchr/testify/require"
"github.com/zarf-dev/zarf/src/config/lang"
)

// TODO (@AustinAbro321) - remove this test in favor of unit testing lint.Validate
func TestLint(t *testing.T) {
t.Log("E2E: Lint")

Expand All @@ -31,32 +33,32 @@ func TestLint(t *testing.T) {
configPath := filepath.Join(testPackagePath, "zarf-config.toml")
osSetErr := os.Setenv("ZARF_CONFIG", configPath)
require.NoError(t, osSetErr, "Unable to set ZARF_CONFIG")
stdOut, stdErr, err := e2e.Zarf(t, "dev", "lint", testPackagePath, "-f", "good-flavor")
stdout, 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")
require.Error(t, err, "Require an exit code since there was warnings / errors")
strippedStdOut := e2e.StripMessageFormatting(stdOut)
strippedStdErr := e2e.StripMessageFormatting(stdErr)
multiSpaceRegex := regexp.MustCompile(`\s{2,}|\n`)
strippedStdout := multiSpaceRegex.ReplaceAllString(stdout, " ")

key := "WHATEVER_IMAGE"
require.Contains(t, strippedStdOut, lang.UnsetVarLintWarning)
require.Contains(t, strippedStdOut, fmt.Sprintf(lang.PkgValidateTemplateDeprecation, key, key, key))
require.Contains(t, strippedStdOut, ".components.[2].repos.[0] | Unpinned repository")
require.Contains(t, strippedStdOut, ".metadata | Additional property description1 is not allowed")
require.Contains(t, strippedStdOut, ".components.[0].import | Additional property not-path is not allowed")
require.Contains(t, strippedStdout, lang.UnsetVarLintWarning)
require.Contains(t, strippedStdout, fmt.Sprintf(lang.PkgValidateTemplateDeprecation, key, key, key))
require.Contains(t, strippedStdout, ".components.[2].repos.[0] | Unpinned repository")
require.Contains(t, strippedStdout, ".metadata | Additional property description1 is not allowed")
require.Contains(t, strippedStdout, ".components.[0].import | Additional property not-path is not allowed")
// Testing the import / compose on lint is working
require.Contains(t, strippedStdOut, ".components.[1].images.[0] | Image not pinned with digest - registry.com:9001/whatever/image:latest")
require.Contains(t, strippedStdout, ".components.[1].images.[0] | Image not pinned with digest - registry.com:9001/whatever/image:latest")
// Testing import / compose + variables are working
require.Contains(t, strippedStdOut, ".components.[2].images.[3] | Image not pinned with digest - busybox:latest")
require.Contains(t, strippedStdout, ".components.[2].images.[3] | Image not pinned with digest - busybox:latest")
// Testing OCI imports get linted
require.Contains(t, strippedStdOut, ".components.[0].images.[0] | Image not pinned with digest - ghcr.io/zarf-dev/doom-game:0.0.1")
require.Contains(t, strippedStdout, ".components.[0].images.[0] | Image not pinned with digest - ghcr.io/zarf-dev/doom-game:0.0.1")

// Check flavors
require.NotContains(t, strippedStdOut, "image-in-bad-flavor-component:unpinned")
require.Contains(t, strippedStdOut, "image-in-good-flavor-component:unpinned")
require.NotContains(t, stdout, "image-in-bad-flavor-component:unpinned")
require.Contains(t, stdout, "image-in-good-flavor-component:unpinned")

// Check reported filepaths
require.Contains(t, strippedStdErr, "linting package name=dos-games path=oci://ghcr.io/zarf-dev/packages/dos-games:1.1.0")
require.Contains(t, strippedStdErr, fmt.Sprintf("linting package name=lint path=%s", testPackagePath))
require.Contains(t, stderr, "linting package name=dos-games path=oci://ghcr.io/zarf-dev/packages/dos-games:1.1.0")
require.Contains(t, stderr, fmt.Sprintf("linting package name=lint path=%s", testPackagePath))
})
}
4 changes: 2 additions & 2 deletions src/test/e2e/25_helm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ func testHelmChartsExample(t *testing.T) {
evilChartDepsPath := filepath.Join("src", "test", "packages", "25-evil-chart-deps")
stdOut, stdErr, err = e2e.Zarf(t, "package", "create", evilChartDepsPath, "--tmpdir", tmpdir, "--confirm")
require.Error(t, err, stdOut, stdErr)
require.Contains(t, e2e.StripMessageFormatting(stdErr), "could not download https://charts.jetstack.io/charts/cert-manager-v1.11.1.tgz")
require.Contains(t, stdErr, "could not download https://charts.jetstack.io/charts/cert-manager-v1.11.1.tgz")
require.FileExists(t, filepath.Join(evilChartDepsPath, "good-chart", "charts", "gitlab-runner-0.55.0.tgz"))

// Create a package with a chart name that doesn't exist in a repo
evilChartLookupPath := filepath.Join("src", "test", "packages", "25-evil-chart-lookup")
stdOut, stdErr, err = e2e.Zarf(t, "package", "create", evilChartLookupPath, "--tmpdir", tmpdir, "--confirm")
require.Error(t, err, stdOut, stdErr)
require.Contains(t, e2e.StripMessageFormatting(stdErr), "chart \"asdf\" version \"6.4.0\" not found")
require.Contains(t, stdErr, "chart \"asdf\" version \"6.4.0\" not found")

// Create a test package (with a registry override (host+subpath to host+subpath) to test that as well)
stdOut, stdErr, err = e2e.Zarf(t, "package", "create", "examples/helm-charts", "-o", "build", "--registry-override", "ghcr.io/stefanprodan=docker.io/stefanprodan", "--tmpdir", tmpdir, "--confirm")
Expand Down
2 changes: 1 addition & 1 deletion src/test/e2e/34_custom_init_package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestCustomInit(t *testing.T) {
// Test that we get an error when trying to deploy a package without providing the public key
stdOut, stdErr, err = e2e.Zarf(t, "init", "--confirm")
require.Error(t, err, stdOut, stdErr)
require.Contains(t, e2e.StripMessageFormatting(stdErr), "unable to load the package: package is signed but no key was provided - add a key with the --key flag or use the --skip-signature-validation flag and run the command again")
require.Contains(t, stdErr, "unable to load the package: package is signed but no key was provided - add a key with the --key flag or use the --skip-signature-validation flag and run the command again")

/* Test operations during package deploy */
// Test that we can deploy the package with the public key
Expand Down

0 comments on commit 14ee3e9

Please sign in to comment.