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: move lint e2e test to unit test #3334

Merged
merged 10 commits into from
Dec 16, 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
8 changes: 0 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,6 @@ init-package: ## Create the zarf init package (must `brew install coreutils` on
release-init-package:
$(ZARF_BIN) package create -o build -a $(ARCH) --set AGENT_IMAGE_TAG=$(AGENT_IMAGE_TAG) --confirm .

# INTERNAL: used to build an iron bank version of the init package with an ib version of the registry image
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shoving this into this PR. We don't do ironbank things anymore in Zarf, no reason to keep this around.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

ib-init-package:
@test -s $(ZARF_BIN) || $(MAKE)
$(ZARF_BIN) package create -o build -a $(ARCH) --confirm . \
--set REGISTRY_IMAGE_DOMAIN="registry1.dso.mil/" \
--set REGISTRY_IMAGE="ironbank/opensource/docker/registry-v2" \
--set REGISTRY_IMAGE_TAG="2.8.3"

# INTERNAL: used to publish the init package
publish-init-package:
$(ZARF_BIN) package publish build/zarf-init-$(ARCH)-$(CLI_VERSION).tar.zst oci://$(REPOSITORY_URL)
Expand Down
12 changes: 4 additions & 8 deletions src/pkg/lint/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ func templateZarfObj(zarfObj any, setVariables map[string]string) ([]PackageFind
return err
}

var unSetTemplates bool
for key := range yamlTemplates {
if deprecated {
findings = append(findings, PackageFinding{
Expand All @@ -128,15 +127,12 @@ func templateZarfObj(zarfObj any, setVariables map[string]string) ([]PackageFind
})
}
if _, present := setVariables[key]; !present {
unSetTemplates = true
findings = append(findings, PackageFinding{
Description: fmt.Sprintf("package template %s is not set and won't be evaluated during lint", key),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Severity: SevWarn,
})
}
}
if unSetTemplates {
findings = append(findings, PackageFinding{
Description: lang.UnsetVarLintWarning,
Severity: SevWarn,
})
}
for key, value := range setVariables {
templateMap[fmt.Sprintf("%s%s###", templatePrefix, key)] = value
}
Expand Down
68 changes: 67 additions & 1 deletion src/pkg/lint/lint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ package lint
import (
"context"
"fmt"
"os"
"testing"

"github.com/stretchr/testify/require"
"github.com/zarf-dev/zarf/src/api/v1alpha1"
"github.com/zarf-dev/zarf/src/config/lang"
"github.com/zarf-dev/zarf/src/test/testutil"
)

func TestLintError(t *testing.T) {
Expand Down Expand Up @@ -76,7 +78,7 @@ func TestFillObjTemplate(t *testing.T) {
expectedFindings := []PackageFinding{
{
Severity: SevWarn,
Description: "There are templates that are not set and won't be evaluated during lint",
Description: "package template KEY3 is not set and won't be evaluated during lint",
},
{
Severity: SevWarn,
Expand All @@ -93,3 +95,67 @@ func TestFillObjTemplate(t *testing.T) {
require.ElementsMatch(t, expectedFindings, findings)
require.Equal(t, expectedComponent, component)
}

func TestLintPackageWithImports(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In an ideal world we would testing more cases which are smaller. But this makes sense as an iterative step in the right direction.

ZarfSchema = testutil.LoadSchema(t, "../../../zarf.schema.json")
setVariables := map[string]string{
"BUSYBOX_IMAGE": "latest",
}
ctx := context.Background()
findings := []PackageFinding{
// unset exists in both the root and imported package
{
YqPath: "",
Description: "package template UNSET is not set and won't be evaluated during lint",
Item: "",
PackageNameOverride: "linted-import",
PackagePathOverride: "linted-import",
Severity: SevWarn,
},
{
YqPath: "",
Description: "package template UNSET is not set and won't be evaluated during lint",
Item: "",
PackageNameOverride: "lint",
PackagePathOverride: ".",
Severity: SevWarn,
},
// Test imported skeleton package lints properly
{
YqPath: ".components.[0].images.[0]",
Description: "Image not pinned with digest",
Item: "ghcr.io/zarf-dev/doom-game:0.0.1",
PackageNameOverride: "dos-games",
PackagePathOverride: "oci://ghcr.io/zarf-dev/packages/dos-games:1.1.0",
Severity: SevWarn,
},
// Test local import lints properly
{
YqPath: ".components.[1].images.[0]",
Description: "Image not pinned with digest",
Item: "busybox:latest",
PackageNameOverride: "linted-import",
PackagePathOverride: "linted-import",
Severity: SevWarn,
},
// Test flavors
{
YqPath: ".components.[4].images.[0]",
Description: "Image not pinned with digest",
Item: "image-in-good-flavor-component:unpinned",
PackageNameOverride: "lint",
PackagePathOverride: ".",
Severity: SevWarn,
},
}
cwd, err := os.Getwd()
require.NoError(t, err)
// TODO @austinabro321: remove this and parallelize the test once changing the working directory is no longer required
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense keeping the scope of changes manageable.

defer func() {
require.NoError(t, os.Chdir(cwd))
}()
err = Validate(ctx, "testdata/lint-with-imports", "good-flavor", setVariables)
var lintErr *LintError
require.ErrorAs(t, err, &lintErr)
require.ElementsMatch(t, findings, lintErr.Findings)
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ variables:

components:
- name: dont-care
images:
- image-that-should-not-show-up-in-lint:unpinned

- name: import-test
images:
- registry.com:9001/whatever/image:latest
- busybox@sha256:3fbc632167424a6d997e74f52b878d7cc478225cffac6bc977eedfe51c7f4e79
- busybox:###ZARF_PKG_TMPL_BUSYBOX_IMAGE###
- busybox@sha256:3fbc632167424a6d997e74f52b878d7cc478225cffac6bc977eedfe51c7f4e79
- busybox:###ZARF_PKG_TMPL_UNSET###

- name: oci-games-url
Expand Down
29 changes: 29 additions & 0 deletions src/pkg/lint/testdata/lint-with-imports/zarf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
kind: ZarfPackageConfig
metadata:
name: lint

components:
- name: import-test
import:
path: linted-import

- name: full-repo
images:
- busybox@sha256:3fbc632167424a6d997e74f52b878d7cc478225cffac6bc977eedfe51c7f4e79
- busybox:###ZARF_PKG_TMPL_UNSET###

- name: oci-games-url
import:
path: linted-import

- name: import-bad-flavor
only:
flavor: bad-flavor
images:
- image-in-bad-flavor-component:unpinned

- name: import-good-flavor
only:
flavor: good-flavor
images:
- image-in-good-flavor-component:unpinned
1 change: 1 addition & 0 deletions src/pkg/lint/testdata/package-with-templates/zarf.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
kind: ZarfPackageConfig
# Tests that the schema regex validation happen after templating
metadata:
name: "###ZARF_PKG_VAR_PACKAGE_NAME###"
components:
Expand Down
64 changes: 0 additions & 64 deletions src/test/e2e/12_lint_test.go

This file was deleted.

3 changes: 0 additions & 3 deletions src/test/packages/12-lint/zarf-config.toml

This file was deleted.

52 changes: 0 additions & 52 deletions src/test/packages/12-lint/zarf.yaml

This file was deleted.

Loading