diff --git a/Makefile b/Makefile index 20f33d0394..e7b460b829 100644 --- a/Makefile +++ b/Makefile @@ -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 -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) diff --git a/src/pkg/lint/lint.go b/src/pkg/lint/lint.go index dfffccc2f4..40747f07c5 100644 --- a/src/pkg/lint/lint.go +++ b/src/pkg/lint/lint.go @@ -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{ @@ -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), + 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 } diff --git a/src/pkg/lint/lint_test.go b/src/pkg/lint/lint_test.go index e2e5493734..6974a4114f 100644 --- a/src/pkg/lint/lint_test.go +++ b/src/pkg/lint/lint_test.go @@ -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) { @@ -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, @@ -93,3 +95,67 @@ func TestFillObjTemplate(t *testing.T) { require.ElementsMatch(t, expectedFindings, findings) require.Equal(t, expectedComponent, component) } + +func TestLintPackageWithImports(t *testing.T) { + 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 + 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) +} diff --git a/src/test/packages/12-lint/linted-import/zarf.yaml b/src/pkg/lint/testdata/lint-with-imports/linted-import/zarf.yaml similarity index 89% rename from src/test/packages/12-lint/linted-import/zarf.yaml rename to src/pkg/lint/testdata/lint-with-imports/linted-import/zarf.yaml index d4a190f9e6..ed205ca622 100644 --- a/src/test/packages/12-lint/linted-import/zarf.yaml +++ b/src/pkg/lint/testdata/lint-with-imports/linted-import/zarf.yaml @@ -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 diff --git a/src/pkg/lint/testdata/lint-with-imports/zarf.yaml b/src/pkg/lint/testdata/lint-with-imports/zarf.yaml new file mode 100644 index 0000000000..1e646e652c --- /dev/null +++ b/src/pkg/lint/testdata/lint-with-imports/zarf.yaml @@ -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 diff --git a/src/pkg/lint/testdata/package-with-templates/zarf.yaml b/src/pkg/lint/testdata/package-with-templates/zarf.yaml index f32ec32bab..b0d6f333b6 100644 --- a/src/pkg/lint/testdata/package-with-templates/zarf.yaml +++ b/src/pkg/lint/testdata/package-with-templates/zarf.yaml @@ -1,4 +1,5 @@ kind: ZarfPackageConfig +# Tests that the schema regex validation happen after templating metadata: name: "###ZARF_PKG_VAR_PACKAGE_NAME###" components: diff --git a/src/test/e2e/12_lint_test.go b/src/test/e2e/12_lint_test.go deleted file mode 100644 index ffd63b6933..0000000000 --- a/src/test/e2e/12_lint_test.go +++ /dev/null @@ -1,64 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -// SPDX-FileCopyrightText: 2021-Present The Zarf Authors - -package test - -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") - - t.Run("zarf test lint success", func(t *testing.T) { - t.Log("E2E: Test lint on schema success") - - // This runs lint on the zarf.yaml in the base directory of the repo - _, _, err := e2e.Zarf(t, "dev", "lint") - require.NoError(t, err, "Expect no error here because the yaml file is following schema") - }) - - t.Run("zarf test lint fail", func(t *testing.T) { - t.Log("E2E: Test lint on schema fail") - - 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") - 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") - 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") - // 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") - // Testing import / compose + variables are working - 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") - - // Check flavors - 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, 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)) - }) -} diff --git a/src/test/packages/12-lint/zarf-config.toml b/src/test/packages/12-lint/zarf-config.toml deleted file mode 100644 index 1d1c57f33e..0000000000 --- a/src/test/packages/12-lint/zarf-config.toml +++ /dev/null @@ -1,3 +0,0 @@ -[package.create.set] -BUSYBOX_IMAGE = "latest" -PATH = "linted-import" diff --git a/src/test/packages/12-lint/zarf.yaml b/src/test/packages/12-lint/zarf.yaml deleted file mode 100644 index 8d4d9cd1d1..0000000000 --- a/src/test/packages/12-lint/zarf.yaml +++ /dev/null @@ -1,52 +0,0 @@ -kind: ZarfPackageConfig -metadata: - name: lint - description1: Testing bad yaml - -components: - - name: first-test-component - import: - not-path: packages/distros/k3s - - - name: import-test - import: - path: linted-import - - - name: full-repo - repos: - - https://github.com/zarf-dev/zarf-public-test.git - - https://dev.azure.com/defenseunicorns/zarf-public-test/_git/zarf-public-test@v0.0.1 - - https://gitlab.com/gitlab-org/build/omnibus-mirror/pcre2/-/tree/vreverse?ref_type=heads - images: - - registry.com:9001/whatever/image:1.0.0 - - busybox@sha256:3fbc632167424a6d997e74f52b878d7cc478225cffac6bc977eedfe51c7f4e79 - - busybox:###ZARF_PKG_VAR_WHATEVER_IMAGE### - - busybox:###ZARF_PKG_TMPL_BUSYBOX_IMAGE### - - ubuntu:###ZARF_PKG_TMPL_UBUNTU_IMAGE### - files: - - source: https://github.com/k3s-io/k3s/releases/download/v1.28.2+k3s1/k3s - shasum: 2f041d37a2c6d54d53e106e1c7713bc48f806f3919b0d9e092f5fcbdc55b41cf - target: src/ - - source: file-without-shasum.txt - target: src/ - - - name: oci-games-url - import: - url: oci://ghcr.io/zarf-dev/packages/dos-games:1.1.0 - name: baseline - - - 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