From 7ee508976e720179fafe1ce788ae88ff7078af12 Mon Sep 17 00:00:00 2001 From: unclegedd Date: Mon, 26 Feb 2024 09:02:29 -0600 Subject: [PATCH 01/11] WIP: prefer path over repository in tests --- Makefile | 2 +- .../export-name-collision/uds-bundle.yaml | 6 ++-- .../import-all-bad-name/uds-bundle.yaml | 4 +-- .../02-simple-vars/import-all/uds-bundle.yaml | 4 +-- .../02-simple-vars/remote/uds-bundle.yaml | 22 ++++++++++++++ .../bundles/02-simple-vars/uds-bundle.yaml | 4 +-- .../12-exported-pkg-vars/uds-bundle.yaml | 2 +- src/test/e2e/bundle_test.go | 6 ++-- src/test/e2e/variable_test.go | 30 ++----------------- 9 files changed, 40 insertions(+), 40 deletions(-) create mode 100644 src/test/bundles/02-simple-vars/remote/uds-bundle.yaml diff --git a/Makefile b/Makefile index d91a26c4..3f2b0978 100644 --- a/Makefile +++ b/Makefile @@ -8,7 +8,7 @@ BUILD_ARGS := -s -w -X 'github.com/defenseunicorns/uds-cli/src/config.CLIVersion .PHONY: help help: ## Display this help information - @grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) \ + @grep -E '^[0-9a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) \ | sort | awk 'BEGIN {FS = ":.*?## "}; \ {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' diff --git a/src/test/bundles/02-simple-vars/export-name-collision/uds-bundle.yaml b/src/test/bundles/02-simple-vars/export-name-collision/uds-bundle.yaml index ad15a689..97a87f3a 100644 --- a/src/test/bundles/02-simple-vars/export-name-collision/uds-bundle.yaml +++ b/src/test/bundles/02-simple-vars/export-name-collision/uds-bundle.yaml @@ -6,20 +6,20 @@ metadata: packages: - name: output-var - repository: localhost:888/output-var + path: ../../../packages/no-cluster/output-var ref: 0.0.1 exports: - name: OUTPUT - name: PRECEDENCE - name: output-var-collision - repository: localhost:888/output-var-collision + path: ../../../packages/no-cluster/output-var-collision ref: 0.0.1 exports: - name: OUTPUT - name: receive-var - repository: localhost:888/receive-var + path: ../../../packages/no-cluster/receive-var ref: 0.0.1 imports: - package: output-var-collision diff --git a/src/test/bundles/02-simple-vars/import-all-bad-name/uds-bundle.yaml b/src/test/bundles/02-simple-vars/import-all-bad-name/uds-bundle.yaml index 34671511..b611cda9 100644 --- a/src/test/bundles/02-simple-vars/import-all-bad-name/uds-bundle.yaml +++ b/src/test/bundles/02-simple-vars/import-all-bad-name/uds-bundle.yaml @@ -6,14 +6,14 @@ metadata: packages: - name: output-var - repository: localhost:888/output-var + path: ../../../packages/no-cluster/output-var ref: 0.0.1 exports: - name: OUTPUT - name: PRECEDENCE - name: receive-var - repository: localhost:888/receive-var + path: ../../../packages/no-cluster/receive-var ref: 0.0.1 imports: - package: output-varz diff --git a/src/test/bundles/02-simple-vars/import-all/uds-bundle.yaml b/src/test/bundles/02-simple-vars/import-all/uds-bundle.yaml index 7c2e28fa..30021abe 100644 --- a/src/test/bundles/02-simple-vars/import-all/uds-bundle.yaml +++ b/src/test/bundles/02-simple-vars/import-all/uds-bundle.yaml @@ -6,12 +6,12 @@ metadata: packages: - name: output-var - repository: localhost:888/output-var + path: ../../../packages/no-cluster/output-var ref: 0.0.1 exports: - name: OUTPUT - name: PRECEDENCE - name: receive-var - repository: localhost:888/receive-var + path: ../../../packages/no-cluster/receive-var ref: 0.0.1 diff --git a/src/test/bundles/02-simple-vars/remote/uds-bundle.yaml b/src/test/bundles/02-simple-vars/remote/uds-bundle.yaml new file mode 100644 index 00000000..55bf4747 --- /dev/null +++ b/src/test/bundles/02-simple-vars/remote/uds-bundle.yaml @@ -0,0 +1,22 @@ +kind: UDSBundle +metadata: + name: simple-vars + description: show how vars work + version: 0.0.1 + +packages: + - name: output-var + repository: localhost:888/output-var + ref: 0.0.1 + exports: + - name: OUTPUT + - name: PRECEDENCE + + - name: receive-var + repository: localhost:888/receive-var + ref: 0.0.1 + imports: + - name: OUTPUT + package: output-var + - name: PRECEDENCE + package: output-var diff --git a/src/test/bundles/02-simple-vars/uds-bundle.yaml b/src/test/bundles/02-simple-vars/uds-bundle.yaml index 55bf4747..205d3325 100644 --- a/src/test/bundles/02-simple-vars/uds-bundle.yaml +++ b/src/test/bundles/02-simple-vars/uds-bundle.yaml @@ -6,14 +6,14 @@ metadata: packages: - name: output-var - repository: localhost:888/output-var + path: ../../packages/no-cluster/output-var ref: 0.0.1 exports: - name: OUTPUT - name: PRECEDENCE - name: receive-var - repository: localhost:888/receive-var + path: ../../packages/no-cluster/receive-var ref: 0.0.1 imports: - name: OUTPUT diff --git a/src/test/bundles/12-exported-pkg-vars/uds-bundle.yaml b/src/test/bundles/12-exported-pkg-vars/uds-bundle.yaml index e2bcc606..b10a10e2 100644 --- a/src/test/bundles/12-exported-pkg-vars/uds-bundle.yaml +++ b/src/test/bundles/12-exported-pkg-vars/uds-bundle.yaml @@ -6,7 +6,7 @@ metadata: packages: - name: output-var - repository: localhost:888/output-var + path: ../../packages/no-cluster/output-var ref: 0.0.1 exports: - name: COLOR diff --git a/src/test/e2e/bundle_test.go b/src/test/e2e/bundle_test.go index 766484ca..0bb33f05 100644 --- a/src/test/e2e/bundle_test.go +++ b/src/test/e2e/bundle_test.go @@ -41,6 +41,7 @@ func TestSimpleBundleWithZarfAction(t *testing.T) { } func TestCreateWithNoPath(t *testing.T) { + // need to use remote pkgs because we move the uds-bundle.yaml to the current directory zarfPkgPath1 := "src/test/packages/no-cluster/output-var" zarfPkgPath2 := "src/test/packages/no-cluster/receive-var" e2e.CreateZarfPkg(t, zarfPkgPath1, false) @@ -55,7 +56,8 @@ func TestCreateWithNoPath(t *testing.T) { pkg = filepath.Join(zarfPkgPath2, fmt.Sprintf("zarf-package-receive-var-%s-0.0.1.tar.zst", e2e.Arch)) zarfPublish(t, pkg, "localhost:888") - err := os.Link(fmt.Sprintf("src/test/bundles/02-simple-vars/%s", config.BundleYAML), config.BundleYAML) + // move the bundle to the current directory so we can test the create command with no path + err := os.Link(fmt.Sprintf("src/test/bundles/02-simple-vars/remote/%s", config.BundleYAML), config.BundleYAML) require.NoError(t, err) defer os.Remove(config.BundleYAML) defer os.Remove(fmt.Sprintf("uds-bundle-simple-vars-%s-0.0.1.tar.zst", e2e.Arch)) @@ -110,7 +112,7 @@ func TestBundle(t *testing.T) { bundleDir := "src/test/bundles/01-uds-bundle" bundlePath := filepath.Join(bundleDir, fmt.Sprintf("uds-bundle-example-%s-0.0.1.tar.zst", e2e.Arch)) - createLocal(t, bundleDir, e2e.Arch) // todo: allow creating from both the folder containing and direct reference to uds-bundle.yaml + createLocal(t, bundleDir, e2e.Arch) inspectLocal(t, bundlePath) inspectLocalAndSBOMExtract(t, bundlePath) // Test with an "options only" config file diff --git a/src/test/e2e/variable_test.go b/src/test/e2e/variable_test.go index 038bb2ed..51cf0d52 100644 --- a/src/test/e2e/variable_test.go +++ b/src/test/e2e/variable_test.go @@ -15,25 +15,13 @@ import ( ) func TestBundleVariables(t *testing.T) { - zarfPkgPath1 := "src/test/packages/no-cluster/output-var" - zarfPkgPath2 := "src/test/packages/no-cluster/receive-var" - e2e.CreateZarfPkg(t, zarfPkgPath1, false) - e2e.CreateZarfPkg(t, zarfPkgPath2, false) - - e2e.SetupDockerRegistry(t, 888) - defer e2e.TeardownRegistry(t, 888) - - pkg := filepath.Join(zarfPkgPath1, fmt.Sprintf("zarf-package-output-var-%s-0.0.1.tar.zst", e2e.Arch)) - zarfPublish(t, pkg, "localhost:888") - - pkg = filepath.Join(zarfPkgPath2, fmt.Sprintf("zarf-package-receive-var-%s-0.0.1.tar.zst", e2e.Arch)) - zarfPublish(t, pkg, "localhost:888") + e2e.CreateZarfPkg(t, "src/test/packages/no-cluster/output-var", false) + e2e.CreateZarfPkg(t, "src/test/packages/no-cluster/receive-var", false) bundleDir := "src/test/bundles/02-simple-vars" bundleTarballPath := filepath.Join(bundleDir, fmt.Sprintf("uds-bundle-simple-vars-%s-0.0.1.tar.zst", e2e.Arch)) createLocal(t, bundleDir, e2e.Arch) - createRemoteInsecure(t, bundleDir, "localhost:888", e2e.Arch) os.Setenv("UDS_ANIMAL", "Unicorns") os.Setenv("UDS_CONFIG", filepath.Join("src/test/bundles/02-simple-vars", "uds-config.yaml")) @@ -58,13 +46,9 @@ func TestBundleVariables(t *testing.T) { zarfPkgPath3 := "src/test/packages/no-cluster/output-var-collision" e2e.CreateZarfPkg(t, zarfPkgPath3, false) - pkg = filepath.Join(zarfPkgPath3, fmt.Sprintf("zarf-package-output-var-collision-%s-0.0.1.tar.zst", e2e.Arch)) - zarfPublish(t, pkg, "localhost:888") - bundleDir = "src/test/bundles/02-simple-vars/export-name-collision" bundleTarballPath = filepath.Join(bundleDir, fmt.Sprintf("uds-bundle-export-name-collision-%s-0.0.1.tar.zst", e2e.Arch)) createLocal(t, bundleDir, e2e.Arch) - createRemoteInsecure(t, bundleDir, "localhost:888", e2e.Arch) _, stderr = deploy(t, bundleTarballPath) require.Contains(t, stderr, "This fun-fact was imported: Daffodils are the national flower of Wales") require.NotContains(t, stderr, "This fun-fact was imported: Unicorns are the national animal of Scotland") @@ -237,15 +221,7 @@ func TestVariablePrecedence(t *testing.T) { func TestZarfPackageExportVarsAsGlobalBundleVars(t *testing.T) { deployZarfInit(t) - zarfPkgPath1 := "src/test/packages/no-cluster/output-var" - e2e.CreateZarfPkg(t, zarfPkgPath1, false) - - e2e.SetupDockerRegistry(t, 888) - defer e2e.TeardownRegistry(t, 888) - - pkg := filepath.Join(zarfPkgPath1, fmt.Sprintf("zarf-package-output-var-%s-0.0.1.tar.zst", e2e.Arch)) - zarfPublish(t, pkg, "localhost:888") - + e2e.CreateZarfPkg(t, "src/test/packages/no-cluster/output-var", false) e2e.HelmDepUpdate(t, "src/test/packages/helm/unicorn-podinfo") e2e.CreateZarfPkg(t, "src/test/packages/helm", false) bundleDir := "src/test/bundles/12-exported-pkg-vars" From 8e595fb070fa562e14d6baa9f463a4d06775ae68 Mon Sep 17 00:00:00 2001 From: unclegedd Date: Mon, 26 Feb 2024 09:52:56 -0600 Subject: [PATCH 02/11] WIP: more test refactor --- .../02-simple-vars/import-all/uds-bundle.yaml | 17 ------ .../bad-var-name}/uds-bundle.yaml | 2 +- .../export-name-collision/uds-bundle.yaml | 0 .../remote/uds-bundle.yaml | 2 +- .../uds-bundle.yaml | 4 +- .../uds-config.yaml | 0 src/test/common.go | 2 +- src/test/e2e/bundle_test.go | 4 +- src/test/e2e/variable_test.go | 59 ++++++++----------- 9 files changed, 31 insertions(+), 59 deletions(-) delete mode 100644 src/test/bundles/02-simple-vars/import-all/uds-bundle.yaml rename src/test/bundles/{02-simple-vars/import-all-bad-name => 02-variables/bad-var-name}/uds-bundle.yaml (93%) rename src/test/bundles/{02-simple-vars => 02-variables}/export-name-collision/uds-bundle.yaml (100%) rename src/test/bundles/{02-simple-vars => 02-variables}/remote/uds-bundle.yaml (95%) rename src/test/bundles/{02-simple-vars => 02-variables}/uds-bundle.yaml (83%) rename src/test/bundles/{02-simple-vars => 02-variables}/uds-config.yaml (100%) diff --git a/src/test/bundles/02-simple-vars/import-all/uds-bundle.yaml b/src/test/bundles/02-simple-vars/import-all/uds-bundle.yaml deleted file mode 100644 index 30021abe..00000000 --- a/src/test/bundles/02-simple-vars/import-all/uds-bundle.yaml +++ /dev/null @@ -1,17 +0,0 @@ -kind: UDSBundle -metadata: - name: import-all - description: show how global exports work - version: 0.0.1 - -packages: - - name: output-var - path: ../../../packages/no-cluster/output-var - ref: 0.0.1 - exports: - - name: OUTPUT - - name: PRECEDENCE - - - name: receive-var - path: ../../../packages/no-cluster/receive-var - ref: 0.0.1 diff --git a/src/test/bundles/02-simple-vars/import-all-bad-name/uds-bundle.yaml b/src/test/bundles/02-variables/bad-var-name/uds-bundle.yaml similarity index 93% rename from src/test/bundles/02-simple-vars/import-all-bad-name/uds-bundle.yaml rename to src/test/bundles/02-variables/bad-var-name/uds-bundle.yaml index b611cda9..6e9cc7c1 100644 --- a/src/test/bundles/02-simple-vars/import-all-bad-name/uds-bundle.yaml +++ b/src/test/bundles/02-variables/bad-var-name/uds-bundle.yaml @@ -1,6 +1,6 @@ kind: UDSBundle metadata: - name: import-all-bad-name + name: bad-var-name description: show errors for bad imports version: 0.0.1 diff --git a/src/test/bundles/02-simple-vars/export-name-collision/uds-bundle.yaml b/src/test/bundles/02-variables/export-name-collision/uds-bundle.yaml similarity index 100% rename from src/test/bundles/02-simple-vars/export-name-collision/uds-bundle.yaml rename to src/test/bundles/02-variables/export-name-collision/uds-bundle.yaml diff --git a/src/test/bundles/02-simple-vars/remote/uds-bundle.yaml b/src/test/bundles/02-variables/remote/uds-bundle.yaml similarity index 95% rename from src/test/bundles/02-simple-vars/remote/uds-bundle.yaml rename to src/test/bundles/02-variables/remote/uds-bundle.yaml index 55bf4747..63662ff0 100644 --- a/src/test/bundles/02-simple-vars/remote/uds-bundle.yaml +++ b/src/test/bundles/02-variables/remote/uds-bundle.yaml @@ -1,6 +1,6 @@ kind: UDSBundle metadata: - name: simple-vars + name: variables description: show how vars work version: 0.0.1 diff --git a/src/test/bundles/02-simple-vars/uds-bundle.yaml b/src/test/bundles/02-variables/uds-bundle.yaml similarity index 83% rename from src/test/bundles/02-simple-vars/uds-bundle.yaml rename to src/test/bundles/02-variables/uds-bundle.yaml index 205d3325..016abf4e 100644 --- a/src/test/bundles/02-simple-vars/uds-bundle.yaml +++ b/src/test/bundles/02-variables/uds-bundle.yaml @@ -1,6 +1,6 @@ kind: UDSBundle metadata: - name: simple-vars + name: variables description: show how vars work version: 0.0.1 @@ -18,5 +18,3 @@ packages: imports: - name: OUTPUT package: output-var - - name: PRECEDENCE - package: output-var diff --git a/src/test/bundles/02-simple-vars/uds-config.yaml b/src/test/bundles/02-variables/uds-config.yaml similarity index 100% rename from src/test/bundles/02-simple-vars/uds-config.yaml rename to src/test/bundles/02-variables/uds-config.yaml diff --git a/src/test/common.go b/src/test/common.go index d3444e5c..05427f8f 100644 --- a/src/test/common.go +++ b/src/test/common.go @@ -91,7 +91,7 @@ func (e2e *UDSE2ETest) GetLogFileContents(t *testing.T, stdErr string) string { // SetupDockerRegistry uses the host machine's docker daemon to spin up a local registry for testing purposes. func (e2e *UDSE2ETest) SetupDockerRegistry(t *testing.T, port int) { // spin up a local registry - registryImage := "registry:2.8.2" + registryImage := "registry:2.8.3" err := exec.CmdWithPrint("docker", "run", "-d", "--restart=always", "-p", fmt.Sprintf("%d:5000", port), "--name", fmt.Sprintf("registry-%d", port), registryImage) require.NoError(t, err) } diff --git a/src/test/e2e/bundle_test.go b/src/test/e2e/bundle_test.go index 0bb33f05..04a4f606 100644 --- a/src/test/e2e/bundle_test.go +++ b/src/test/e2e/bundle_test.go @@ -57,10 +57,10 @@ func TestCreateWithNoPath(t *testing.T) { zarfPublish(t, pkg, "localhost:888") // move the bundle to the current directory so we can test the create command with no path - err := os.Link(fmt.Sprintf("src/test/bundles/02-simple-vars/remote/%s", config.BundleYAML), config.BundleYAML) + err := os.Link(fmt.Sprintf("src/test/bundles/02-variables/remote/%s", config.BundleYAML), config.BundleYAML) require.NoError(t, err) defer os.Remove(config.BundleYAML) - defer os.Remove(fmt.Sprintf("uds-bundle-simple-vars-%s-0.0.1.tar.zst", e2e.Arch)) + defer os.Remove(fmt.Sprintf("uds-bundle-variables-%s-0.0.1.tar.zst", e2e.Arch)) // create cmd := strings.Split("create --confirm --insecure", " ") diff --git a/src/test/e2e/variable_test.go b/src/test/e2e/variable_test.go index 51cf0d52..db2a2b19 100644 --- a/src/test/e2e/variable_test.go +++ b/src/test/e2e/variable_test.go @@ -17,41 +17,32 @@ import ( func TestBundleVariables(t *testing.T) { e2e.CreateZarfPkg(t, "src/test/packages/no-cluster/output-var", false) e2e.CreateZarfPkg(t, "src/test/packages/no-cluster/receive-var", false) - - bundleDir := "src/test/bundles/02-simple-vars" - bundleTarballPath := filepath.Join(bundleDir, fmt.Sprintf("uds-bundle-simple-vars-%s-0.0.1.tar.zst", e2e.Arch)) - - createLocal(t, bundleDir, e2e.Arch) - os.Setenv("UDS_ANIMAL", "Unicorns") - os.Setenv("UDS_CONFIG", filepath.Join("src/test/bundles/02-simple-vars", "uds-config.yaml")) - - _, stderr := deploy(t, bundleTarballPath) - bundleVariablesTestChecks(t, stderr, bundleTarballPath) - remove(t, bundleTarballPath) - - // Run same test checks but with package that isn't explicitly importing vars - bundleDir = "src/test/bundles/02-simple-vars/import-all" - bundleTarballPath = filepath.Join(bundleDir, fmt.Sprintf("uds-bundle-import-all-%s-0.0.1.tar.zst", e2e.Arch)) - createLocal(t, bundleDir, e2e.Arch) - _, stderr = deploy(t, bundleTarballPath) - bundleVariablesTestChecks(t, stderr, bundleTarballPath) - - // Test with bad variable name in import - bundleDir = "src/test/bundles/02-simple-vars/import-all-bad-name" - stderr = createLocalError(bundleDir, e2e.Arch) - require.Contains(t, stderr, "does not have a matching export") - - // Test name collisions with exported variables - zarfPkgPath3 := "src/test/packages/no-cluster/output-var-collision" - e2e.CreateZarfPkg(t, zarfPkgPath3, false) - - bundleDir = "src/test/bundles/02-simple-vars/export-name-collision" - bundleTarballPath = filepath.Join(bundleDir, fmt.Sprintf("uds-bundle-export-name-collision-%s-0.0.1.tar.zst", e2e.Arch)) - createLocal(t, bundleDir, e2e.Arch) - _, stderr = deploy(t, bundleTarballPath) - require.Contains(t, stderr, "This fun-fact was imported: Daffodils are the national flower of Wales") - require.NotContains(t, stderr, "This fun-fact was imported: Unicorns are the national animal of Scotland") + os.Setenv("UDS_CONFIG", filepath.Join("src/test/bundles/02-variables", "uds-config.yaml")) + + t.Run("simple vars and global export", func(t *testing.T) { + bundleDir := "src/test/bundles/02-variables" + bundleTarballPath := filepath.Join(bundleDir, fmt.Sprintf("uds-bundle-variables-%s-0.0.1.tar.zst", e2e.Arch)) + createLocal(t, bundleDir, e2e.Arch) + _, stderr := deploy(t, bundleTarballPath) + bundleVariablesTestChecks(t, stderr, bundleTarballPath) + }) + + t.Run("bad var name in import", func(t *testing.T) { + bundleDir := "src/test/bundles/02-variables/bad-var-name" + stderr := createLocalError(bundleDir, e2e.Arch) + require.Contains(t, stderr, "does not have a matching export") + }) + + t.Run("var name collision with exported vars", func(t *testing.T) { + e2e.CreateZarfPkg(t, "src/test/packages/no-cluster/output-var-collision", false) + bundleDir := "src/test/bundles/02-variables/export-name-collision" + bundleTarballPath := filepath.Join(bundleDir, fmt.Sprintf("uds-bundle-export-name-collision-%s-0.0.1.tar.zst", e2e.Arch)) + createLocal(t, bundleDir, e2e.Arch) + _, stderr := deploy(t, bundleTarballPath) + require.Contains(t, stderr, "This fun-fact was imported: Daffodils are the national flower of Wales") + require.NotContains(t, stderr, "This fun-fact was imported: Unicorns are the national animal of Scotland") + }) } func bundleVariablesTestChecks(t *testing.T, stderr string, bundleTarballPath string) { From 7de625e086706294298e78e02ef02e22e676f01e Mon Sep 17 00:00:00 2001 From: unclegedd Date: Mon, 26 Feb 2024 15:17:54 -0600 Subject: [PATCH 03/11] WIP: more test refactor --- src/pkg/bundle/common_test.go | 6 +- src/pkg/bundle/deploy.go | 2 +- src/pkg/bundle/deploy_test.go | 241 ++++++++++++++++++ src/pkg/bundle/pull.go | 2 +- src/pkg/bundle/remove.go | 8 +- src/pkg/cache/cache.go | 4 +- .../01-uds-bundle/local/uds-bundle.yaml | 13 + .../{ => remote}/uds-bundle.yaml | 4 +- src/test/bundles/02-variables/uds-bundle.yaml | 1 + src/test/common.go | 2 +- src/test/e2e/bundle_test.go | 90 ++----- 11 files changed, 292 insertions(+), 81 deletions(-) create mode 100644 src/pkg/bundle/deploy_test.go create mode 100644 src/test/bundles/01-uds-bundle/local/uds-bundle.yaml rename src/test/bundles/01-uds-bundle/{ => remote}/uds-bundle.yaml (70%) diff --git a/src/pkg/bundle/common_test.go b/src/pkg/bundle/common_test.go index 2ef1e5fe..3f17b88a 100644 --- a/src/pkg/bundle/common_test.go +++ b/src/pkg/bundle/common_test.go @@ -27,7 +27,8 @@ func Test_validateBundleVars(t *testing.T) { }, }, wantErr: false, - }, { + }, + { name: "ImportDoesntMatchExport", description: "error when import doesn't match export", args: args{ @@ -37,7 +38,8 @@ func Test_validateBundleVars(t *testing.T) { }, }, wantErr: true, - }, { + }, + { name: "FirstPkgHasImport", description: "error when first pkg has an import", args: args{ diff --git a/src/pkg/bundle/deploy.go b/src/pkg/bundle/deploy.go index 97274525..0a199d03 100644 --- a/src/pkg/bundle/deploy.go +++ b/src/pkg/bundle/deploy.go @@ -227,7 +227,7 @@ func (b *Bundle) loadVariables(pkg types.Package, bundleExportedVars map[string] } } - // Set variables in order or precendence (least specific to most specific) + // Set variables in order or precedence (least specific to most specific) // imported vars for _, imp := range pkg.Imports { pkgVars[strings.ToUpper(imp.Name)] = bundleExportedVars[imp.Package][imp.Name] diff --git a/src/pkg/bundle/deploy_test.go b/src/pkg/bundle/deploy_test.go new file mode 100644 index 00000000..f4d7b697 --- /dev/null +++ b/src/pkg/bundle/deploy_test.go @@ -0,0 +1,241 @@ +package bundle + +import ( + "os" + "reflect" + "testing" + + "github.com/defenseunicorns/uds-cli/src/types" +) + +func TestLoadVariablesPrecedence(t *testing.T) { + testCases := []struct { + name string + description string + pkg types.Package + bundle Bundle + bundleExportVars map[string]map[string]string + loadEnvVar bool + expectedPkgVars map[string]string + }{ + { + name: "--set flag precedence", + loadEnvVar: true, + pkg: types.Package{ + Name: "fooPkg", + Imports: []types.BundleVariableImport{ + { + Name: "foo", + Package: "bazPkg", + }, + }, + }, + bundle: Bundle{ + cfg: &types.BundleConfig{ + DeployOpts: types.BundleDeployOptions{ + Variables: map[string]map[string]interface{}{ + "fooPkg": { + "foo": "set from variables key in uds-config.yaml", + }, + }, + // set from uds-config.yaml + SharedVariables: map[string]interface{}{ + "foo": "set from shared key in uds-config.yaml", + }, + SetVariables: map[string]string{ + "foo": "set using --set flag", + }, + }, + }, + }, + bundleExportVars: map[string]map[string]string{ + "barPkg": { + "foo": "exported from another pkg", + }, + "bazPkg": { + "foo": "imported from a specific pkg", + }, + }, + expectedPkgVars: map[string]string{ + "FOO": "set using --set flag", + }, + }, + { + name: "env var precedence", + loadEnvVar: true, + pkg: types.Package{ + Name: "fooPkg", + Imports: []types.BundleVariableImport{ + { + Name: "foo", + Package: "bazPkg", + }, + }, + }, + bundle: Bundle{ + cfg: &types.BundleConfig{ + DeployOpts: types.BundleDeployOptions{ + Variables: map[string]map[string]interface{}{ + "fooPkg": { + "foo": "set from variables key in uds-config.yaml", + }, + }, + // set from uds-config.yaml + SharedVariables: map[string]interface{}{ + "foo": "set from shared key in uds-config.yaml", + }, + }, + }, + }, + bundleExportVars: map[string]map[string]string{ + "barPkg": { + "foo": "exported from another pkg", + }, + "bazPkg": { + "foo": "imported from a specific pkg", + }, + }, + expectedPkgVars: map[string]string{ + "FOO": "set using env var", + }, + }, + { + name: "uds-config variables key precedence", + pkg: types.Package{ + Name: "fooPkg", + Imports: []types.BundleVariableImport{ + { + Name: "foo", + Package: "bazPkg", + }, + }, + }, + bundle: Bundle{ + cfg: &types.BundleConfig{ + DeployOpts: types.BundleDeployOptions{ + Variables: map[string]map[string]interface{}{ + "fooPkg": { + "foo": "set from variables key in uds-config.yaml", + }, + }, + // set from uds-config.yaml + SharedVariables: map[string]interface{}{ + "foo": "set from shared key in uds-config.yaml", + }, + }, + }, + }, + bundleExportVars: map[string]map[string]string{ + "barPkg": { + "foo": "exported from another pkg", + }, + "bazPkg": { + "foo": "imported from a specific pkg", + }, + }, + expectedPkgVars: map[string]string{ + "FOO": "set from variables key in uds-config.yaml", + }, + }, + { + name: "uds-config shared key precedence", + pkg: types.Package{ + Name: "fooPkg", + Imports: []types.BundleVariableImport{ + { + Name: "foo", + Package: "bazPkg", + }, + }, + }, + bundle: Bundle{ + cfg: &types.BundleConfig{ + DeployOpts: types.BundleDeployOptions{ + // set from uds-config.yaml + SharedVariables: map[string]interface{}{ + "foo": "set from shared key in uds-config.yaml", + }, + }, + }, + }, + bundleExportVars: map[string]map[string]string{ + "barPkg": { + "foo": "exported from another pkg", + }, + "bazPkg": { + "foo": "imported from a specific pkg", + }, + }, + expectedPkgVars: map[string]string{ + "FOO": "set from shared key in uds-config.yaml", + }, + }, + { + name: "uds-config shared key precedence", + pkg: types.Package{ + Name: "fooPkg", + Imports: []types.BundleVariableImport{ + { + Name: "foo", + Package: "bazPkg", + }, + }, + }, + bundle: Bundle{ + cfg: &types.BundleConfig{ + DeployOpts: types.BundleDeployOptions{ + SharedVariables: nil, + }, + }, + }, + bundleExportVars: map[string]map[string]string{ + "barPkg": { + "foo": "exported from another pkg", + }, + "bazPkg": { + "foo": "imported from a specific pkg", + }, + }, + expectedPkgVars: map[string]string{ + "FOO": "imported from a specific pkg", + }, + }, + { + name: "uds-config global export precedence", + pkg: types.Package{ + Name: "fooPkg", + }, + bundle: Bundle{ + cfg: &types.BundleConfig{ + DeployOpts: types.BundleDeployOptions{ + SharedVariables: nil, + }, + }, + }, + bundleExportVars: map[string]map[string]string{ + "barPkg": { + "foo": "exported from another pkg", + }, + }, + expectedPkgVars: map[string]string{ + "FOO": "exported from another pkg", + }, + }, + } + + // Run test cases + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + // Set for select test cases to test precedence of env vars + os.Unsetenv("UDS_FOO") + if tc.loadEnvVar { + os.Setenv("UDS_FOO", "set using env var") + } + actualPkgVars := tc.bundle.loadVariables(tc.pkg, tc.bundleExportVars) + + if !reflect.DeepEqual(actualPkgVars, tc.expectedPkgVars) { + t.Errorf("Test case %s failed. Expected %v, got %v", tc.name, tc.expectedPkgVars, actualPkgVars) + } + }) + } +} diff --git a/src/pkg/bundle/pull.go b/src/pkg/bundle/pull.go index ed6bf690..445fe587 100644 --- a/src/pkg/bundle/pull.go +++ b/src/pkg/bundle/pull.go @@ -25,7 +25,7 @@ import ( func (b *Bundle) Pull() error { cacheDir := filepath.Join(zarfConfig.GetAbsCachePath(), "packages") // create the cache directory if it doesn't exist - if err := utils.CreateDirectory(cacheDir, 0755); err != nil { + if err := utils.CreateDirectory(cacheDir, 0o700); err != nil { return err } diff --git a/src/pkg/bundle/remove.go b/src/pkg/bundle/remove.go index f6c5876c..c0279c88 100644 --- a/src/pkg/bundle/remove.go +++ b/src/pkg/bundle/remove.go @@ -81,14 +81,18 @@ func (b *Bundle) Remove() error { } func removePackages(packagesToRemove []types.Package, b *Bundle, zarfPackageNameMap map[string]string) error { - // Get deployed packages deployedPackageNames := GetDeployedPackageNames() for i := len(packagesToRemove) - 1; i >= 0; i-- { pkg := packagesToRemove[i] - zarfPackageName := zarfPackageNameMap[pkg.Name] + zarfPackageName := pkg.Name + // use the name map if it has been set (remote pkgs where the pkg name isn't consistent) + if zarfPackageNameMap[pkg.Name] != "" { + zarfPackageName = zarfPackageNameMap[pkg.Name] + } + if slices.Contains(deployedPackageNames, zarfPackageName) { opts := zarfTypes.ZarfPackageOptions{ PackageSource: b.cfg.RemoveOpts.Source, diff --git a/src/pkg/cache/cache.go b/src/pkg/cache/cache.go index 8e0520ec..6d008dfc 100644 --- a/src/pkg/cache/cache.go +++ b/src/pkg/cache/cache.go @@ -30,7 +30,7 @@ func expandTilde(cachePath string) string { func Add(filePathToAdd string) error { // ensure cache dir exists cacheDir := config.CommonOptions.CachePath - if err := os.MkdirAll(filepath.Join(cacheDir, "images"), 0755); err != nil { + if err := os.MkdirAll(filepath.Join(cacheDir, "images"), 0o700); err != nil { return err } @@ -74,7 +74,7 @@ func Use(layerDigest, dstDir string) error { defer srcFile.Close() // ensure blobs/sha256 dir has been created - if err := os.MkdirAll(dstDir, 0755); err != nil { + if err := os.MkdirAll(dstDir, 0o700); err != nil { return err } diff --git a/src/test/bundles/01-uds-bundle/local/uds-bundle.yaml b/src/test/bundles/01-uds-bundle/local/uds-bundle.yaml new file mode 100644 index 00000000..781423b9 --- /dev/null +++ b/src/test/bundles/01-uds-bundle/local/uds-bundle.yaml @@ -0,0 +1,13 @@ +kind: UDSBundle +metadata: + name: example-local + description: an example UDS bundle with local packages + version: 0.0.1 + +packages: + - name: nginx + path: ../../../packages/nginx + ref: 0.0.1 + - name: podinfo + path: ../../../packages/podinfo + ref: 0.0.1 diff --git a/src/test/bundles/01-uds-bundle/uds-bundle.yaml b/src/test/bundles/01-uds-bundle/remote/uds-bundle.yaml similarity index 70% rename from src/test/bundles/01-uds-bundle/uds-bundle.yaml rename to src/test/bundles/01-uds-bundle/remote/uds-bundle.yaml index 94666242..afa0d034 100644 --- a/src/test/bundles/01-uds-bundle/uds-bundle.yaml +++ b/src/test/bundles/01-uds-bundle/remote/uds-bundle.yaml @@ -1,7 +1,7 @@ kind: UDSBundle metadata: - name: example - description: an example UDS bundle + name: example-remote + description: an example UDS bundle with remote packages version: 0.0.1 packages: diff --git a/src/test/bundles/02-variables/uds-bundle.yaml b/src/test/bundles/02-variables/uds-bundle.yaml index 016abf4e..db5d1980 100644 --- a/src/test/bundles/02-variables/uds-bundle.yaml +++ b/src/test/bundles/02-variables/uds-bundle.yaml @@ -16,5 +16,6 @@ packages: path: ../../packages/no-cluster/receive-var ref: 0.0.1 imports: + # note that PRECEDENCE is not imported because exports are global! - name: OUTPUT package: output-var diff --git a/src/test/common.go b/src/test/common.go index 05427f8f..c4e500c1 100644 --- a/src/test/common.go +++ b/src/test/common.go @@ -154,7 +154,7 @@ func downloadFile(url string, outputDir string) error { return fmt.Errorf("unexpected status code: %d", response.StatusCode) } - if err := os.MkdirAll(outputDir, 0755); err != nil { + if err := os.MkdirAll(outputDir, 0o700); err != nil { return err } diff --git a/src/test/e2e/bundle_test.go b/src/test/e2e/bundle_test.go index 04a4f606..75669b1b 100644 --- a/src/test/e2e/bundle_test.go +++ b/src/test/e2e/bundle_test.go @@ -92,7 +92,7 @@ func TestBundleWithLocalAndRemotePkgs(t *testing.T) { remove(t, tarballPath) } -func TestBundle(t *testing.T) { +func TestLocalBundleWithRemotePkgs(t *testing.T) { deployZarfInit(t) e2e.CreateZarfPkg(t, "src/test/packages/nginx", false) @@ -109,9 +109,10 @@ func TestBundle(t *testing.T) { pkg = fmt.Sprintf("src/test/packages/podinfo/zarf-package-podinfo-%s-0.0.1.tar.zst", e2e.Arch) zarfPublish(t, pkg, "localhost:889") - bundleDir := "src/test/bundles/01-uds-bundle" - bundlePath := filepath.Join(bundleDir, fmt.Sprintf("uds-bundle-example-%s-0.0.1.tar.zst", e2e.Arch)) + bundleDir := "src/test/bundles/01-uds-bundle/remote" + bundlePath := filepath.Join(bundleDir, fmt.Sprintf("uds-bundle-example-remote-%s-0.0.1.tar.zst", e2e.Arch)) + // todo: not getting uds-config options on create (or probably anything other than deploy) createLocal(t, bundleDir, e2e.Arch) inspectLocal(t, bundlePath) inspectLocalAndSBOMExtract(t, bundlePath) @@ -126,32 +127,17 @@ func TestBundle(t *testing.T) { // remove customtmp folder if it exists err := os.RemoveAll("./customtmp") require.NoError(t, err) - } func TestPackagesFlag(t *testing.T) { deployZarfInit(t) - e2e.CreateZarfPkg(t, "src/test/packages/nginx", false) e2e.CreateZarfPkg(t, "src/test/packages/podinfo", false) - e2e.SetupDockerRegistry(t, 888) - defer e2e.TeardownRegistry(t, 888) - e2e.SetupDockerRegistry(t, 889) - defer e2e.TeardownRegistry(t, 889) - - pkg := fmt.Sprintf("src/test/packages/nginx/zarf-package-nginx-%s-0.0.1.tar.zst", e2e.Arch) - zarfPublish(t, pkg, "localhost:888") - - pkg = fmt.Sprintf("src/test/packages/podinfo/zarf-package-podinfo-%s-0.0.1.tar.zst", e2e.Arch) - zarfPublish(t, pkg, "localhost:889") - - bundleDir := "src/test/bundles/01-uds-bundle" - bundlePath := filepath.Join(bundleDir, fmt.Sprintf("uds-bundle-example-%s-0.0.1.tar.zst", e2e.Arch)) + bundleDir := "src/test/bundles/03-local-and-remote" + bundlePath := filepath.Join(bundleDir, fmt.Sprintf("uds-bundle-test-local-and-remote-%s-0.0.1.tar.zst", e2e.Arch)) createLocal(t, bundleDir, e2e.Arch) - inspectLocal(t, bundlePath) - inspectLocalAndSBOMExtract(t, bundlePath) // Test only podinfo deploy deployPackagesFlag(bundlePath, "podinfo") @@ -168,13 +154,13 @@ func TestPackagesFlag(t *testing.T) { require.Contains(t, deployments, "podinfo") require.Contains(t, deployments, "nginx") - // Remove only podinfo + // Remove only podinfo (local pkg) removePackagesFlag(bundlePath, "podinfo") deployments, _, _ = e2e.UDS(cmd...) require.NotContains(t, deployments, "podinfo") require.Contains(t, deployments, "nginx") - // Remove nginx + // Remove only nginx (remote pkg) removePackagesFlag(bundlePath, "nginx") deployments, _, _ = e2e.UDS(cmd...) require.NotContains(t, deployments, "podinfo") @@ -195,19 +181,8 @@ func TestResumeFlag(t *testing.T) { e2e.CreateZarfPkg(t, "src/test/packages/nginx", false) e2e.CreateZarfPkg(t, "src/test/packages/podinfo", false) - e2e.SetupDockerRegistry(t, 888) - defer e2e.TeardownRegistry(t, 888) - e2e.SetupDockerRegistry(t, 889) - defer e2e.TeardownRegistry(t, 889) - - pkg := fmt.Sprintf("src/test/packages/nginx/zarf-package-nginx-%s-0.0.1.tar.zst", e2e.Arch) - zarfPublish(t, pkg, "localhost:888") - - pkg = fmt.Sprintf("src/test/packages/podinfo/zarf-package-podinfo-%s-0.0.1.tar.zst", e2e.Arch) - zarfPublish(t, pkg, "localhost:889") - - bundleDir := "src/test/bundles/01-uds-bundle" - bundlePath := filepath.Join(bundleDir, fmt.Sprintf("uds-bundle-example-%s-0.0.1.tar.zst", e2e.Arch)) + bundleDir := "src/test/bundles/01-uds-bundle/local" + bundlePath := filepath.Join(bundleDir, fmt.Sprintf("uds-bundle-example-local-%s-0.0.1.tar.zst", e2e.Arch)) createLocal(t, bundleDir, e2e.Arch) inspectLocal(t, bundlePath) @@ -241,7 +216,7 @@ func TestResumeFlag(t *testing.T) { require.NotContains(t, deployments, "nginx") } -func TestRemoteBundle(t *testing.T) { +func TestRemoteBundleWithRemotePkgs(t *testing.T) { deployZarfInit(t) e2e.CreateZarfPkg(t, "src/test/packages/nginx", false) e2e.CreateZarfPkg(t, "src/test/packages/podinfo", false) @@ -260,12 +235,12 @@ func TestRemoteBundle(t *testing.T) { bundleRef := registry.Reference{ Registry: "oci://localhost:888", // this info is derived from the bundle's metadata - Repository: "example", + Repository: "example-remote", Reference: "0.0.1", } - tarballPath := filepath.Join("build", fmt.Sprintf("uds-bundle-example-%s-0.0.1.tar.zst", e2e.Arch)) - bundlePath := "src/test/bundles/01-uds-bundle" + tarballPath := filepath.Join("build", fmt.Sprintf("uds-bundle-example-remote-%s-0.0.1.tar.zst", e2e.Arch)) + bundlePath := "src/test/bundles/01-uds-bundle/remote" createRemoteInsecure(t, bundlePath, bundleRef.Registry, e2e.Arch) // Test without oci prefix @@ -276,11 +251,9 @@ func TestRemoteBundle(t *testing.T) { inspectRemoteAndSBOMExtract(t, bundleRef.String()) deployAndRemoveRemote(t, bundleRef.String(), tarballPath) - // Test without architecture specified bundleRef = registry.Reference{ - Registry: "oci://localhost:888", - // this info is derived from the bundle's metadata - Repository: "example", + Registry: "oci://localhost:888", + Repository: "example-remote", Reference: "0.0.1", } deployAndRemoveRemote(t, bundleRef.String(), tarballPath) @@ -303,24 +276,12 @@ func TestBundleWithYmlFile(t *testing.T) { e2e.CreateZarfPkg(t, "src/test/packages/nginx", true) e2e.CreateZarfPkg(t, "src/test/packages/podinfo", true) - e2e.SetupDockerRegistry(t, 888) - defer e2e.TeardownRegistry(t, 888) - e2e.SetupDockerRegistry(t, 889) - defer e2e.TeardownRegistry(t, 889) - - pkg := fmt.Sprintf("src/test/packages/nginx/zarf-package-nginx-%s-0.0.1.tar.zst", e2e.Arch) - zarfPublish(t, pkg, "localhost:888") - - pkg = fmt.Sprintf("src/test/packages/podinfo/zarf-package-podinfo-%s-0.0.1.tar.zst", e2e.Arch) - zarfPublish(t, pkg, "localhost:889") - bundleDir := "src/test/bundles/09-uds-bundle-yml" bundlePath := filepath.Join(bundleDir, fmt.Sprintf("uds-bundle-yml-example-%s-0.0.1.tar.zst", e2e.Arch)) createLocal(t, bundleDir, e2e.Arch) inspectLocal(t, bundlePath) inspectLocalAndSBOMExtract(t, bundlePath) - // Test with an "options only" config file os.Setenv("UDS_CONFIG", filepath.Join("src/test/bundles/09-uds-bundle-yml", "uds-config.yml")) deploy(t, bundlePath) remove(t, bundlePath) @@ -493,25 +454,14 @@ func TestBundleTmpDir(t *testing.T) { e2e.CreateZarfPkg(t, "src/test/packages/nginx", false) e2e.CreateZarfPkg(t, "src/test/packages/podinfo", false) - e2e.SetupDockerRegistry(t, 888) - defer e2e.TeardownRegistry(t, 888) - e2e.SetupDockerRegistry(t, 889) - defer e2e.TeardownRegistry(t, 889) + bundleDir := "src/test/bundles/01-uds-bundle/local" + bundlePath := filepath.Join(bundleDir, fmt.Sprintf("uds-bundle-example-local-%s-0.0.1.tar.zst", e2e.Arch)) - pkg := fmt.Sprintf("src/test/packages/nginx/zarf-package-nginx-%s-0.0.1.tar.zst", e2e.Arch) - zarfPublish(t, pkg, "localhost:888") - - pkg = fmt.Sprintf("src/test/packages/podinfo/zarf-package-podinfo-%s-0.0.1.tar.zst", e2e.Arch) - zarfPublish(t, pkg, "localhost:889") - - bundleDir := "src/test/bundles/01-uds-bundle" - bundlePath := filepath.Join(bundleDir, fmt.Sprintf("uds-bundle-example-%s-0.0.1.tar.zst", e2e.Arch)) - - //Test create using custom tmpDir + // Test create using custom tmpDir tmpDirName := "customtmp" tmpDir := fmt.Sprintf("%s/%s", bundleDir, tmpDirName) - err := os.Mkdir(tmpDir, 0755) + err := os.Mkdir(tmpDir, 0o700) if err != nil { t.Fatalf("error creating directory: %v", err) } From 3dc23ae5d08bab6f64fc477908ecad730ac1c898 Mon Sep 17 00:00:00 2001 From: unclegedd Date: Mon, 26 Feb 2024 15:48:43 -0600 Subject: [PATCH 04/11] WIP: more test refactor --- src/pkg/bundle/pull.go | 2 +- src/pkg/cache/cache.go | 4 ++-- src/test/common.go | 2 +- src/test/e2e/bundle_test.go | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/pkg/bundle/pull.go b/src/pkg/bundle/pull.go index 445fe587..695539b6 100644 --- a/src/pkg/bundle/pull.go +++ b/src/pkg/bundle/pull.go @@ -25,7 +25,7 @@ import ( func (b *Bundle) Pull() error { cacheDir := filepath.Join(zarfConfig.GetAbsCachePath(), "packages") // create the cache directory if it doesn't exist - if err := utils.CreateDirectory(cacheDir, 0o700); err != nil { + if err := utils.CreateDirectory(cacheDir, 0o755); err != nil { return err } diff --git a/src/pkg/cache/cache.go b/src/pkg/cache/cache.go index 6d008dfc..78080ae9 100644 --- a/src/pkg/cache/cache.go +++ b/src/pkg/cache/cache.go @@ -30,7 +30,7 @@ func expandTilde(cachePath string) string { func Add(filePathToAdd string) error { // ensure cache dir exists cacheDir := config.CommonOptions.CachePath - if err := os.MkdirAll(filepath.Join(cacheDir, "images"), 0o700); err != nil { + if err := os.MkdirAll(filepath.Join(cacheDir, "images"), 0o755); err != nil { return err } @@ -74,7 +74,7 @@ func Use(layerDigest, dstDir string) error { defer srcFile.Close() // ensure blobs/sha256 dir has been created - if err := os.MkdirAll(dstDir, 0o700); err != nil { + if err := os.MkdirAll(dstDir, 0o755); err != nil { return err } diff --git a/src/test/common.go b/src/test/common.go index c4e500c1..373f81f2 100644 --- a/src/test/common.go +++ b/src/test/common.go @@ -154,7 +154,7 @@ func downloadFile(url string, outputDir string) error { return fmt.Errorf("unexpected status code: %d", response.StatusCode) } - if err := os.MkdirAll(outputDir, 0o700); err != nil { + if err := os.MkdirAll(outputDir, 0o755); err != nil { return err } diff --git a/src/test/e2e/bundle_test.go b/src/test/e2e/bundle_test.go index 75669b1b..ff1238e7 100644 --- a/src/test/e2e/bundle_test.go +++ b/src/test/e2e/bundle_test.go @@ -461,7 +461,7 @@ func TestBundleTmpDir(t *testing.T) { tmpDirName := "customtmp" tmpDir := fmt.Sprintf("%s/%s", bundleDir, tmpDirName) - err := os.Mkdir(tmpDir, 0o700) + err := os.Mkdir(tmpDir, 0o755) if err != nil { t.Fatalf("error creating directory: %v", err) } From 9fca2cb64f0fd1ac72691c523d0923dad382a602 Mon Sep 17 00:00:00 2001 From: unclegedd Date: Mon, 26 Feb 2024 16:16:05 -0600 Subject: [PATCH 05/11] WIP: more test refactor --- src/pkg/bundler/fetcher/local.go | 8 +++++++- src/test/e2e/bundle_test.go | 4 ++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/pkg/bundler/fetcher/local.go b/src/pkg/bundler/fetcher/local.go index aa19a9d0..50671702 100644 --- a/src/pkg/bundler/fetcher/local.go +++ b/src/pkg/bundler/fetcher/local.go @@ -173,7 +173,13 @@ func (f *localFetcher) toBundle(pkg zarfTypes.ZarfPackage, pkgTmp string) ([]oci // adds title annotations to descs and creates layer to put in the store // title annotations need to be added to the pkg root manifest // Zarf image manifests already contain those title annotations in remote OCI repos, but they need to be added manually here - desc, err := src.Add(ctx, name, mediaType, path) + + // get current directory prepend to path to make it absolute + wd, err := os.Getwd() + if err != nil { + return nil, err + } + desc, err := src.Add(ctx, name, mediaType, filepath.Join(wd, path)) if err != nil { return nil, err } diff --git a/src/test/e2e/bundle_test.go b/src/test/e2e/bundle_test.go index ff1238e7..64c741d6 100644 --- a/src/test/e2e/bundle_test.go +++ b/src/test/e2e/bundle_test.go @@ -454,8 +454,8 @@ func TestBundleTmpDir(t *testing.T) { e2e.CreateZarfPkg(t, "src/test/packages/nginx", false) e2e.CreateZarfPkg(t, "src/test/packages/podinfo", false) - bundleDir := "src/test/bundles/01-uds-bundle/local" - bundlePath := filepath.Join(bundleDir, fmt.Sprintf("uds-bundle-example-local-%s-0.0.1.tar.zst", e2e.Arch)) + bundleDir := "src/test/bundles/03-local-and-remote" + bundlePath := filepath.Join(bundleDir, fmt.Sprintf("uds-bundle-test-local-and-remote-%s-0.0.1.tar.zst", e2e.Arch)) // Test create using custom tmpDir tmpDirName := "customtmp" From 1d763c0eceaa0b6c2e93e3cbe13f9db34878fe86 Mon Sep 17 00:00:00 2001 From: unclegedd Date: Mon, 26 Feb 2024 17:49:28 -0600 Subject: [PATCH 06/11] WIP: more test refactor --- src/pkg/bundler/fetcher/local.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/pkg/bundler/fetcher/local.go b/src/pkg/bundler/fetcher/local.go index 50671702..580cb9b2 100644 --- a/src/pkg/bundler/fetcher/local.go +++ b/src/pkg/bundler/fetcher/local.go @@ -174,12 +174,16 @@ func (f *localFetcher) toBundle(pkg zarfTypes.ZarfPackage, pkgTmp string) ([]oci // title annotations need to be added to the pkg root manifest // Zarf image manifests already contain those title annotations in remote OCI repos, but they need to be added manually here - // get current directory prepend to path to make it absolute - wd, err := os.Getwd() - if err != nil { - return nil, err + // if using a custom tmp dir that is not an absolute path, get working dir and prepend to path to make it absolute + if !filepath.IsAbs(path) { + wd, err := os.Getwd() + if err != nil { + return nil, err + } + path = filepath.Join(wd, path) } - desc, err := src.Add(ctx, name, mediaType, filepath.Join(wd, path)) + + desc, err := src.Add(ctx, name, mediaType, path) if err != nil { return nil, err } From d9b67738c0aab73d8defa06efd031fe4ce8d9451 Mon Sep 17 00:00:00 2001 From: unclegedd Date: Tue, 27 Feb 2024 09:31:20 -0600 Subject: [PATCH 07/11] WIP: more test refactor --- src/pkg/bundle/deploy.go | 12 +- .../03-local-and-remote/uds-config.yaml | 2 + .../bundles/07-helm-overrides/uds-bundle.yaml | 4 + .../12-exported-pkg-vars/uds-bundle.yaml | 2 - src/test/e2e/bundle_test.go | 5 +- src/test/e2e/variable_test.go | 204 ++++++++++-------- 6 files changed, 126 insertions(+), 103 deletions(-) create mode 100644 src/test/bundles/03-local-and-remote/uds-config.yaml diff --git a/src/pkg/bundle/deploy.go b/src/pkg/bundle/deploy.go index 0a199d03..b6b3cb21 100644 --- a/src/pkg/bundle/deploy.go +++ b/src/pkg/bundle/deploy.go @@ -35,15 +35,6 @@ type ZarfOverrideMap map[string]map[string]map[string]interface{} var templatedVarRegex = regexp.MustCompile(`\${([^}]+)}`) // Deploy deploys a bundle -// -// : create a new provider -// : pull the bundle's metadata + sig -// : read the metadata into memory -// : validate the sig (if present) -// : loop through each package -// : : load the package into a fresh temp dir -// : : validate the sig (if present) -// : : deploy the package func (b *Bundle) Deploy() error { ctx := context.TODO() @@ -356,6 +347,7 @@ func (b *Bundle) processOverrideVariables(overrideMap *map[string]map[string]*va var overrideVal interface{} // Ensuring variable name is upper case since comparisons are being done against upper case env and config variables v.Name = strings.ToUpper(v.Name) + // check for override in env vars if envVarOverride, exists := os.LookupEnv(strings.ToUpper(config.EnvVarPrefix + v.Name)); exists { if err := addOverrideValue(*overrideMap, componentName, chartName, v.Path, envVarOverride, nil); err != nil { @@ -363,6 +355,7 @@ func (b *Bundle) processOverrideVariables(overrideMap *map[string]map[string]*va } continue } + // todo: untested?? // check for override in config configFileOverride, existsInConfig := b.cfg.DeployOpts.Variables[pkgName][v.Name] sharedConfigOverride, existsInSharedConfig := b.cfg.DeployOpts.SharedVariables[v.Name] @@ -377,6 +370,7 @@ func (b *Bundle) processOverrideVariables(overrideMap *map[string]map[string]*va overrideVal = sharedConfigOverride } else { // use default v if no config v is set + //overrideVal = "sharedConfigOverride" overrideVal = v.Default } diff --git a/src/test/bundles/03-local-and-remote/uds-config.yaml b/src/test/bundles/03-local-and-remote/uds-config.yaml new file mode 100644 index 00000000..e6078069 --- /dev/null +++ b/src/test/bundles/03-local-and-remote/uds-config.yaml @@ -0,0 +1,2 @@ +options: + log_level: debug diff --git a/src/test/bundles/07-helm-overrides/uds-bundle.yaml b/src/test/bundles/07-helm-overrides/uds-bundle.yaml index a0019dc3..dd385516 100644 --- a/src/test/bundles/07-helm-overrides/uds-bundle.yaml +++ b/src/test/bundles/07-helm-overrides/uds-bundle.yaml @@ -29,6 +29,10 @@ packages: value: customAnnotation: "customValue" variables: + - name: log_level + path: "podinfo.logLevel" + description: "Set the log level for podinfo" + default: "debug" # not overwritten! - name: ui_color path: "podinfo.ui.color" description: "Set the color for podinfo's UI" diff --git a/src/test/bundles/12-exported-pkg-vars/uds-bundle.yaml b/src/test/bundles/12-exported-pkg-vars/uds-bundle.yaml index b10a10e2..8745785c 100644 --- a/src/test/bundles/12-exported-pkg-vars/uds-bundle.yaml +++ b/src/test/bundles/12-exported-pkg-vars/uds-bundle.yaml @@ -27,8 +27,6 @@ packages: podinfo-component: unicorn-podinfo: values: - - path: "podinfo.replicaCount" - value: 1 - path: "podinfo.ui.color" value: ${COLOR} - path: podinfo.podAnnotations diff --git a/src/test/e2e/bundle_test.go b/src/test/e2e/bundle_test.go index 64c741d6..10f114ee 100644 --- a/src/test/e2e/bundle_test.go +++ b/src/test/e2e/bundle_test.go @@ -112,16 +112,13 @@ func TestLocalBundleWithRemotePkgs(t *testing.T) { bundleDir := "src/test/bundles/01-uds-bundle/remote" bundlePath := filepath.Join(bundleDir, fmt.Sprintf("uds-bundle-example-remote-%s-0.0.1.tar.zst", e2e.Arch)) - // todo: not getting uds-config options on create (or probably anything other than deploy) createLocal(t, bundleDir, e2e.Arch) inspectLocal(t, bundlePath) inspectLocalAndSBOMExtract(t, bundlePath) - // Test with an "options only" config file - os.Setenv("UDS_CONFIG", filepath.Join("src/test/bundles/01-uds-bundle", "uds-config.yaml")) deploy(t, bundlePath) remove(t, bundlePath) - //Test create using custom tmpDir + // Test create using custom tmpDir runCmd(t, "create "+bundleDir+" --tmpdir ./customtmp --confirm --insecure") // remove customtmp folder if it exists diff --git a/src/test/e2e/variable_test.go b/src/test/e2e/variable_test.go index db2a2b19..407acecd 100644 --- a/src/test/e2e/variable_test.go +++ b/src/test/e2e/variable_test.go @@ -81,59 +81,78 @@ func TestBundleWithHelmOverrides(t *testing.T) { createLocal(t, bundleDir, e2e.Arch) deploy(t, bundlePath) - // check values overrides - cmd := strings.Split("zarf tools kubectl get deploy -n podinfo unicorn-podinfo -o=jsonpath='{.spec.replicas}'", " ") - outputNumReplicas, _, err := e2e.UDS(cmd...) - require.Equal(t, "'2'", outputNumReplicas) - require.NoError(t, err) + // test values overrides + t.Run("check values overrides", func(t *testing.T) { + cmd := strings.Split("zarf tools kubectl get deploy -n podinfo unicorn-podinfo -o=jsonpath='{.spec.replicas}'", " ") + outputNumReplicas, _, err := e2e.UDS(cmd...) + require.Equal(t, "'2'", outputNumReplicas) + require.NoError(t, err) + }) - // check object-type override in values - cmd = strings.Split("zarf tools kubectl get deployment -n podinfo unicorn-podinfo -o=jsonpath='{.spec.template.metadata.annotations}'", " ") - annotations, _, err := e2e.UDS(cmd...) - require.Contains(t, annotations, "\"customAnnotation\":\"customValue\"") - require.NoError(t, err) + t.Run("check object-type override in values", func(t *testing.T) { + cmd := strings.Split("zarf tools kubectl get deployment -n podinfo unicorn-podinfo -o=jsonpath='{.spec.template.metadata.annotations}'", " ") + annotations, _, err := e2e.UDS(cmd...) + require.Contains(t, annotations, "\"customAnnotation\":\"customValue\"") + require.NoError(t, err) - // check list-type override in values - cmd = strings.Split("zarf tools kubectl get deployment -n podinfo unicorn-podinfo -o=jsonpath='{.spec.template.spec.tolerations}'", " ") - tolerations, _, err := e2e.UDS(cmd...) - require.Contains(t, tolerations, "\"key\":\"uds\"") - require.Contains(t, tolerations, "\"value\":\"defense\"") - require.Contains(t, tolerations, "\"key\":\"unicorn\"") - require.Contains(t, tolerations, "\"effect\":\"NoSchedule\"") - require.NoError(t, err) + }) - // check variables overrides - cmd = strings.Split("zarf tools kubectl get deploy -n podinfo unicorn-podinfo -o=jsonpath='{.spec.template.spec.containers[0].env[?(@.name==\"PODINFO_UI_COLOR\")].value}'", " ") - outputUIColor, _, err := e2e.UDS(cmd...) - require.Equal(t, "'green, yellow'", outputUIColor) - require.NoError(t, err) + t.Run("check list-type override in values", func(t *testing.T) { + cmd := strings.Split("zarf tools kubectl get deployment -n podinfo unicorn-podinfo -o=jsonpath='{.spec.template.spec.tolerations}'", " ") + tolerations, _, err := e2e.UDS(cmd...) + require.Contains(t, tolerations, "\"key\":\"uds\"") + require.Contains(t, tolerations, "\"value\":\"defense\"") + require.Contains(t, tolerations, "\"key\":\"unicorn\"") + require.Contains(t, tolerations, "\"effect\":\"NoSchedule\"") + require.NoError(t, err) - // check variables overrides, no default but set in config - cmd = strings.Split("zarf tools kubectl get deploy -n podinfo unicorn-podinfo -o=jsonpath='{.spec.template.spec.containers[0].env[?(@.name==\"PODINFO_UI_MESSAGE\")].value}'", " ") - outputMsg, _, err := e2e.UDS(cmd...) - require.Equal(t, "'Hello Unicorn'", outputMsg) - require.NoError(t, err) + }) - // check variables overrides, no default and not set in config - cmd = strings.Split("zarf tools kubectl get secret test-secret -n podinfo -o jsonpath=\"{.data.test}\"", " ") - secretValue, _, err := e2e.UDS(cmd...) - // expect the value to be from the underlying chart's values.yaml, no overrides - require.Equal(t, "\"dGVzdC1zZWNyZXQ=\"", secretValue) - require.NoError(t, err) + // test variables overrides + t.Run("check variables overrides, use default", func(t *testing.T) { + cmd := strings.Split("zarf tools kubectl get deploy unicorn-podinfo -n podinfo -o=jsonpath='{.spec.template.spec.containers[*].command[*]}'", " ") + podCmd, _, err := e2e.UDS(cmd...) + require.NoError(t, err) + require.Contains(t, podCmd, "--level=debug") + }) - // check variables overrides with an object-type value - cmd = strings.Split("zarf tools kubectl get deployment -n podinfo unicorn-podinfo -o=jsonpath='{.spec.template.spec.containers[0].securityContext}'", " ") - securityContext, _, err := e2e.UDS(cmd...) - require.NoError(t, err) - require.Contains(t, securityContext, "NET_ADMIN") - require.Contains(t, securityContext, "\"runAsGroup\":4000") + t.Run("check variables overrides, default overwritten by config", func(t *testing.T) { + cmd := strings.Split("zarf tools kubectl get deploy -n podinfo unicorn-podinfo -o=jsonpath='{.spec.template.spec.containers[0].env[?(@.name==\"PODINFO_UI_COLOR\")].value}'", " ") + outputUIColor, _, err := e2e.UDS(cmd...) + require.Equal(t, "'green, yellow'", outputUIColor) + require.NoError(t, err) + }) - // check variables overrides with a list-type value - cmd = strings.Split("zarf tools kubectl get ingress -n podinfo unicorn-podinfo -o=jsonpath='{.spec.rules[*].host}''", " ") - hosts, _, err := e2e.UDS(cmd...) - require.NoError(t, err) - require.Contains(t, hosts, "podinfo.burning.boats") - require.Contains(t, hosts, "podinfo.unicorns") + t.Run("check variables overrides, no default but set in config", func(t *testing.T) { + cmd := strings.Split("zarf tools kubectl get deploy -n podinfo unicorn-podinfo -o=jsonpath='{.spec.template.spec.containers[0].env[?(@.name==\"PODINFO_UI_MESSAGE\")].value}'", " ") + outputMsg, _, err := e2e.UDS(cmd...) + require.Equal(t, "'Hello Unicorn'", outputMsg) + require.NoError(t, err) + }) + + t.Run("check variables overrides, no default and not set in config", func(t *testing.T) { + cmd := strings.Split("zarf tools kubectl get secret test-secret -n podinfo -o jsonpath=\"{.data.test}\"", " ") + secretValue, _, err := e2e.UDS(cmd...) + // expect the value to be from the underlying chart's values.yaml, no overrides + require.Equal(t, "\"dGVzdC1zZWNyZXQ=\"", secretValue) + require.NoError(t, err) + }) + + t.Run("check variables overrides with an object-type value", func(t *testing.T) { + cmd := strings.Split("zarf tools kubectl get deployment -n podinfo unicorn-podinfo -o=jsonpath='{.spec.template.spec.containers[0].securityContext}'", " ") + securityContext, _, err := e2e.UDS(cmd...) + require.NoError(t, err) + require.Contains(t, securityContext, "NET_ADMIN") + require.Contains(t, securityContext, "\"runAsGroup\":4000") + }) + + t.Run("check variables overrides with a list-type value", func(t *testing.T) { + cmd := strings.Split("zarf tools kubectl get ingress -n podinfo unicorn-podinfo -o=jsonpath='{.spec.rules[*].host}''", " ") + hosts, _, err := e2e.UDS(cmd...) + require.NoError(t, err) + require.Contains(t, hosts, "podinfo.burning.boats") + require.Contains(t, hosts, "podinfo.unicorns") + }) remove(t, bundlePath) } @@ -158,16 +177,19 @@ func TestBundleWithEnvVarHelmOverrides(t *testing.T) { createLocal(t, bundleDir, e2e.Arch) deploy(t, bundlePath) - // check override variables, ensure they are coming from env vars and take highest precedence - cmd := strings.Split("z tools kubectl get deploy -n podinfo unicorn-podinfo -o=jsonpath='{.spec.template.spec.containers[0].env[?(@.name==\"PODINFO_UI_COLOR\")].value}'", " ") - outputUIColor, _, err := e2e.UDS(cmd...) - require.Equal(t, fmt.Sprintf("'%s'", color), outputUIColor) - require.NoError(t, err) + t.Run("check override variables, ensure they are coming from env vars and take highest precedence", func(t *testing.T) { + cmd := strings.Split("z tools kubectl get deploy -n podinfo unicorn-podinfo -o=jsonpath='{.spec.template.spec.containers[0].env[?(@.name==\"PODINFO_UI_COLOR\")].value}'", " ") + outputUIColor, _, err := e2e.UDS(cmd...) + require.Equal(t, fmt.Sprintf("'%s'", color), outputUIColor) + require.NoError(t, err) + }) - cmd = strings.Split("z tools kubectl get secret test-secret -n podinfo -o jsonpath=\"{.data.test}\"", " ") - secretValue, _, err := e2e.UDS(cmd...) - require.Equal(t, fmt.Sprintf("\"%s\"", b64Secret), secretValue) - require.NoError(t, err) + t.Run("check override secret val", func(t *testing.T) { + cmd := strings.Split("z tools kubectl get secret test-secret -n podinfo -o jsonpath=\"{.data.test}\"", " ") + secretValue, _, err := e2e.UDS(cmd...) + require.Equal(t, fmt.Sprintf("\"%s\"", b64Secret), secretValue) + require.NoError(t, err) + }) remove(t, bundlePath) } @@ -189,28 +211,31 @@ func TestVariablePrecedence(t *testing.T) { require.NoError(t, err) _, stderr := deploy(t, bundlePath) - // test env var taking highest precedence - cmd := strings.Split("zarf tools kubectl get deploy -n podinfo unicorn-podinfo -o=jsonpath='{.spec.template.spec.containers[0].env[?(@.name==\"PODINFO_UI_COLOR\")].value}'", " ") - outputUIColor, _, err := e2e.UDS(cmd...) - require.Equal(t, fmt.Sprintf("'%s'", color), outputUIColor) - require.NoError(t, err) + t.Run("test precedence, env var > uds-config.variables > uds-config.shared", func(t *testing.T) { + cmd := strings.Split("zarf tools kubectl get deploy -n podinfo unicorn-podinfo -o=jsonpath='{.spec.template.spec.containers[0].env[?(@.name==\"PODINFO_UI_COLOR\")].value}'", " ") + // test env har taking highest precedence + outputUIColor, _, err := e2e.UDS(cmd...) + require.Equal(t, fmt.Sprintf("'%s'", color), outputUIColor) + require.NoError(t, err) - // test uds-config.variables overriding a shared var - require.Contains(t, stderr, "shared var in output-var pkg: unicorns.uds.dev") + // test uds-config.variables overriding a shared var + require.Contains(t, stderr, "shared var in output-var pkg: unicorns.uds.dev") - // test uds-config.shared overriding a Zarf var - require.Contains(t, stderr, "shared var in helm-overrides pkg: burning.boats") + // test uds-config.shared overriding a Zarf var + require.Contains(t, stderr, "shared var in helm-overrides pkg: burning.boats") + }) - // test uds-config.shared overriding values in a Helm chart (ie. bundle overrides) - cmd = strings.Split("zarf tools kubectl get deploy unicorn-podinfo -n podinfo -o=jsonpath='{.spec.template.spec.containers[0].env[?(@.name==\"PODINFO_BACKEND_URL\")].value}'", " ") - backend, _, err := e2e.UDS(cmd...) - require.Equal(t, fmt.Sprintf("'%s'", "burning.boats"), backend) - require.NoError(t, err) + t.Run("test uds-config.shared overriding values in a Helm chart (ie. bundle overrides)", func(t *testing.T) { + cmd := strings.Split("zarf tools kubectl get deploy unicorn-podinfo -n podinfo -o=jsonpath='{.spec.template.spec.containers[0].env[?(@.name==\"PODINFO_BACKEND_URL\")].value}'", " ") + backend, _, err := e2e.UDS(cmd...) + require.Equal(t, fmt.Sprintf("'%s'", "burning.boats"), backend) + require.NoError(t, err) + }) remove(t, bundlePath) } -func TestZarfPackageExportVarsAsGlobalBundleVars(t *testing.T) { +func TestExportVarsAsGlobalVars(t *testing.T) { deployZarfInit(t) e2e.CreateZarfPkg(t, "src/test/packages/no-cluster/output-var", false) e2e.HelmDepUpdate(t, "src/test/packages/helm/unicorn-podinfo") @@ -221,26 +246,29 @@ func TestZarfPackageExportVarsAsGlobalBundleVars(t *testing.T) { createLocal(t, bundleDir, e2e.Arch) deploy(t, bundlePath) - // check templated variables overrides in values - cmd := strings.Split("zarf tools kubectl get deploy -n podinfo unicorn-podinfo -o=jsonpath='{.spec.template.spec.containers[0].env[?(@.name==\"PODINFO_UI_COLOR\")].value}'", " ") - outputUIColor, _, err := e2e.UDS(cmd...) - require.Equal(t, "'orange'", outputUIColor) - require.NoError(t, err) + t.Run("check templated variables overrides in values", func(t *testing.T) { + cmd := strings.Split("zarf tools kubectl get deploy -n podinfo unicorn-podinfo -o=jsonpath='{.spec.template.spec.containers[0].env[?(@.name==\"PODINFO_UI_COLOR\")].value}'", " ") + outputUIColor, _, err := e2e.UDS(cmd...) + require.Equal(t, "'orange'", outputUIColor) + require.NoError(t, err) + }) - // check multiple templated variables as object overrides in values - cmd = strings.Split("zarf tools kubectl get deployment -n podinfo unicorn-podinfo -o=jsonpath='{.spec.template.metadata.annotations}'", " ") - annotations, _, err := e2e.UDS(cmd...) - require.Contains(t, annotations, "\"customAnnotation\":\"orangeAnnotation\"") - require.NoError(t, err) + t.Run("check multiple templated variables as object overrides in values", func(t *testing.T) { + cmd := strings.Split("zarf tools kubectl get deployment -n podinfo unicorn-podinfo -o=jsonpath='{.spec.template.metadata.annotations}'", " ") + annotations, _, err := e2e.UDS(cmd...) + require.Contains(t, annotations, "\"customAnnotation\":\"orangeAnnotation\"") + require.NoError(t, err) + }) - // check templated variable list-type overrides in values - cmd = strings.Split("zarf tools kubectl get deployment -n podinfo unicorn-podinfo -o=jsonpath='{.spec.template.spec.tolerations}'", " ") - tolerations, _, err := e2e.UDS(cmd...) - require.Contains(t, tolerations, "\"key\":\"uds\"") - require.Contains(t, tolerations, "\"value\":\"true\"") - require.Contains(t, tolerations, "\"key\":\"unicorn\"") - require.Contains(t, tolerations, "\"value\":\"defense\"") - require.NoError(t, err) + t.Run("check templated variable list-type overrides in values", func(t *testing.T) { + cmd := strings.Split("zarf tools kubectl get deployment -n podinfo unicorn-podinfo -o=jsonpath='{.spec.template.spec.tolerations}'", " ") + tolerations, _, err := e2e.UDS(cmd...) + require.Contains(t, tolerations, "\"key\":\"uds\"") + require.Contains(t, tolerations, "\"value\":\"true\"") + require.Contains(t, tolerations, "\"key\":\"unicorn\"") + require.Contains(t, tolerations, "\"value\":\"defense\"") + require.NoError(t, err) + }) remove(t, bundlePath) } From 70b2c68da9bc26a1905d96ad5dad9ed7fdc548d7 Mon Sep 17 00:00:00 2001 From: unclegedd Date: Tue, 27 Feb 2024 09:49:21 -0600 Subject: [PATCH 08/11] fix: refactors tests and fixes bugs --- src/pkg/bundle/deploy.go | 1 - 1 file changed, 1 deletion(-) diff --git a/src/pkg/bundle/deploy.go b/src/pkg/bundle/deploy.go index b6b3cb21..900589b6 100644 --- a/src/pkg/bundle/deploy.go +++ b/src/pkg/bundle/deploy.go @@ -355,7 +355,6 @@ func (b *Bundle) processOverrideVariables(overrideMap *map[string]map[string]*va } continue } - // todo: untested?? // check for override in config configFileOverride, existsInConfig := b.cfg.DeployOpts.Variables[pkgName][v.Name] sharedConfigOverride, existsInSharedConfig := b.cfg.DeployOpts.SharedVariables[v.Name] From 332e9dc22be22ddb617abe163740cfcc1a09cd82 Mon Sep 17 00:00:00 2001 From: unclegedd Date: Tue, 27 Feb 2024 10:08:10 -0600 Subject: [PATCH 09/11] fix: refactors tests and fixes bugs --- src/pkg/bundle/deploy.go | 1 - src/pkg/bundle/remove.go | 2 +- .../01-uds-bundle/local/uds-bundle.yaml | 13 -- .../{remote => }/uds-bundle.yaml | 0 src/test/e2e/bundle_test.go | 168 +++++++++++------- 5 files changed, 103 insertions(+), 81 deletions(-) delete mode 100644 src/test/bundles/01-uds-bundle/local/uds-bundle.yaml rename src/test/bundles/01-uds-bundle/{remote => }/uds-bundle.yaml (100%) diff --git a/src/pkg/bundle/deploy.go b/src/pkg/bundle/deploy.go index 900589b6..cdb834fd 100644 --- a/src/pkg/bundle/deploy.go +++ b/src/pkg/bundle/deploy.go @@ -369,7 +369,6 @@ func (b *Bundle) processOverrideVariables(overrideMap *map[string]map[string]*va overrideVal = sharedConfigOverride } else { // use default v if no config v is set - //overrideVal = "sharedConfigOverride" overrideVal = v.Default } diff --git a/src/pkg/bundle/remove.go b/src/pkg/bundle/remove.go index c0279c88..6896bfcc 100644 --- a/src/pkg/bundle/remove.go +++ b/src/pkg/bundle/remove.go @@ -89,7 +89,7 @@ func removePackages(packagesToRemove []types.Package, b *Bundle, zarfPackageName pkg := packagesToRemove[i] zarfPackageName := pkg.Name // use the name map if it has been set (remote pkgs where the pkg name isn't consistent) - if zarfPackageNameMap[pkg.Name] != "" { + if _, ok := zarfPackageNameMap[pkg.Name]; ok { zarfPackageName = zarfPackageNameMap[pkg.Name] } diff --git a/src/test/bundles/01-uds-bundle/local/uds-bundle.yaml b/src/test/bundles/01-uds-bundle/local/uds-bundle.yaml deleted file mode 100644 index 781423b9..00000000 --- a/src/test/bundles/01-uds-bundle/local/uds-bundle.yaml +++ /dev/null @@ -1,13 +0,0 @@ -kind: UDSBundle -metadata: - name: example-local - description: an example UDS bundle with local packages - version: 0.0.1 - -packages: - - name: nginx - path: ../../../packages/nginx - ref: 0.0.1 - - name: podinfo - path: ../../../packages/podinfo - ref: 0.0.1 diff --git a/src/test/bundles/01-uds-bundle/remote/uds-bundle.yaml b/src/test/bundles/01-uds-bundle/uds-bundle.yaml similarity index 100% rename from src/test/bundles/01-uds-bundle/remote/uds-bundle.yaml rename to src/test/bundles/01-uds-bundle/uds-bundle.yaml diff --git a/src/test/e2e/bundle_test.go b/src/test/e2e/bundle_test.go index 10f114ee..6b714978 100644 --- a/src/test/e2e/bundle_test.go +++ b/src/test/e2e/bundle_test.go @@ -109,7 +109,7 @@ func TestLocalBundleWithRemotePkgs(t *testing.T) { pkg = fmt.Sprintf("src/test/packages/podinfo/zarf-package-podinfo-%s-0.0.1.tar.zst", e2e.Arch) zarfPublish(t, pkg, "localhost:889") - bundleDir := "src/test/bundles/01-uds-bundle/remote" + bundleDir := "src/test/bundles/01-uds-bundle" bundlePath := filepath.Join(bundleDir, fmt.Sprintf("uds-bundle-example-remote-%s-0.0.1.tar.zst", e2e.Arch)) createLocal(t, bundleDir, e2e.Arch) @@ -117,98 +117,131 @@ func TestLocalBundleWithRemotePkgs(t *testing.T) { inspectLocalAndSBOMExtract(t, bundlePath) deploy(t, bundlePath) remove(t, bundlePath) - - // Test create using custom tmpDir - runCmd(t, "create "+bundleDir+" --tmpdir ./customtmp --confirm --insecure") - - // remove customtmp folder if it exists - err := os.RemoveAll("./customtmp") - require.NoError(t, err) } func TestPackagesFlag(t *testing.T) { deployZarfInit(t) - e2e.CreateZarfPkg(t, "src/test/packages/podinfo", false) - bundleDir := "src/test/bundles/03-local-and-remote" bundlePath := filepath.Join(bundleDir, fmt.Sprintf("uds-bundle-test-local-and-remote-%s-0.0.1.tar.zst", e2e.Arch)) createLocal(t, bundleDir, e2e.Arch) - // Test only podinfo deploy - deployPackagesFlag(bundlePath, "podinfo") - cmd := strings.Split("zarf tools kubectl get deployments -A -o=jsonpath='{.items[*].metadata.name}'", " ") - deployments, _, _ := e2e.UDS(cmd...) - require.Contains(t, deployments, "podinfo") - require.NotContains(t, deployments, "nginx") - - remove(t, bundlePath) - - // Test both podinfo and nginx deploy - deployPackagesFlag(bundlePath, "podinfo,nginx") - deployments, _, _ = e2e.UDS(cmd...) - require.Contains(t, deployments, "podinfo") - require.Contains(t, deployments, "nginx") + t.Run("Test only podinfo deploy (local pkg)", func(t *testing.T) { + deployPackagesFlag(bundlePath, "podinfo") + cmd := strings.Split("zarf tools kubectl get deployments -A -o=jsonpath='{.items[*].metadata.name}'", " ") + deployments, _, _ := e2e.UDS(cmd...) + require.Contains(t, deployments, "podinfo") + require.NotContains(t, deployments, "nginx") + remove(t, bundlePath) + }) - // Remove only podinfo (local pkg) - removePackagesFlag(bundlePath, "podinfo") - deployments, _, _ = e2e.UDS(cmd...) - require.NotContains(t, deployments, "podinfo") - require.Contains(t, deployments, "nginx") - - // Remove only nginx (remote pkg) - removePackagesFlag(bundlePath, "nginx") - deployments, _, _ = e2e.UDS(cmd...) - require.NotContains(t, deployments, "podinfo") - require.NotContains(t, deployments, "nginx") - - // Test invalid package deploy - _, stderr := deployPackagesFlag(bundlePath, "podinfo,nginx,peanuts") - require.Contains(t, stderr, "invalid zarf packages specified by --packages") - - // Test invalid package remove - _, stderr = removePackagesFlag(bundlePath, "podinfo,nginx,peanuts") - require.Contains(t, stderr, "invalid zarf packages specified by --packages") + cmd := strings.Split("zarf tools kubectl get deployments -A -o=jsonpath='{.items[*].metadata.name}'", " ") + t.Run("Test only nginx deploy (remote pkg)", func(t *testing.T) { + deployPackagesFlag(bundlePath, "nginx") + deployments, _, _ := e2e.UDS(cmd...) + require.Contains(t, deployments, "nginx") + require.NotContains(t, deployments, "podinfo") + remove(t, bundlePath) + }) + + t.Run("Test both podinfo and nginx deploy", func(t *testing.T) { + deployPackagesFlag(bundlePath, "podinfo,nginx") + deployments, _, _ := e2e.UDS(cmd...) + require.Contains(t, deployments, "podinfo") + require.Contains(t, deployments, "nginx") + + }) + t.Run("Remove only podinfo (local pkg)", func(t *testing.T) { + removePackagesFlag(bundlePath, "podinfo") + deployments, _, _ := e2e.UDS(cmd...) + require.NotContains(t, deployments, "podinfo") + require.Contains(t, deployments, "nginx") + + }) + t.Run("Remove only nginx (remote pkg)", func(t *testing.T) { + removePackagesFlag(bundlePath, "nginx") + deployments, _, _ := e2e.UDS(cmd...) + require.NotContains(t, deployments, "podinfo") + require.NotContains(t, deployments, "nginx") + + }) + t.Run("Test invalid package deploy", func(t *testing.T) { + _, stderr := deployPackagesFlag(bundlePath, "podinfo,nginx,peanuts") + require.Contains(t, stderr, "invalid zarf packages specified by --packages") + + }) + t.Run("Test invalid package remove", func(t *testing.T) { + _, stderr := removePackagesFlag(bundlePath, "podinfo,nginx,peanuts") + require.Contains(t, stderr, "invalid zarf packages specified by --packages") + + }) } func TestResumeFlag(t *testing.T) { deployZarfInit(t) - e2e.CreateZarfPkg(t, "src/test/packages/nginx", false) e2e.CreateZarfPkg(t, "src/test/packages/podinfo", false) - - bundleDir := "src/test/bundles/01-uds-bundle/local" - bundlePath := filepath.Join(bundleDir, fmt.Sprintf("uds-bundle-example-local-%s-0.0.1.tar.zst", e2e.Arch)) + bundleDir := "src/test/bundles/03-local-and-remote" + bundlePath := filepath.Join(bundleDir, fmt.Sprintf("uds-bundle-test-local-and-remote-%s-0.0.1.tar.zst", e2e.Arch)) createLocal(t, bundleDir, e2e.Arch) inspectLocal(t, bundlePath) inspectLocalAndSBOMExtract(t, bundlePath) - // Deploy only podinfo from bundle - deployPackagesFlag(bundlePath, "podinfo") - cmd := strings.Split("zarf tools kubectl get deployments -A -o=jsonpath='{.items[*].metadata.name}'", " ") - deployments, _, _ := e2e.UDS(cmd...) - require.Contains(t, deployments, "podinfo") - require.NotContains(t, deployments, "nginx") + getDeploymentsCmd := strings.Split("zarf tools kubectl get deployments -A -o=jsonpath='{.items[*].metadata.name}'", " ") + + // Deploy only podinfo (local pkg) + t.Run("Deploy only podinfo (local pkg)", func(t *testing.T) { + deployPackagesFlag(bundlePath, "podinfo") + deployments, _, _ := e2e.UDS(getDeploymentsCmd...) + require.Contains(t, deployments, "podinfo") + require.NotContains(t, deployments, "nginx") + }) - // Deploy bundle --resume - deployResumeFlag(t, bundlePath) - cmd = strings.Split("zarf tools kubectl get deployments -A -o=jsonpath='{.items[*].metadata.name}'", " ") - deployments, _, _ = e2e.UDS(cmd...) - require.Contains(t, deployments, "podinfo") - require.Contains(t, deployments, "nginx") + // Deploy bundle --resume (resumes remote pkg) + t.Run("Deploy bundle --resume", func(t *testing.T) { + deployResumeFlag(t, bundlePath) + deployments, _, _ := e2e.UDS(getDeploymentsCmd...) + require.Contains(t, deployments, "podinfo") + require.Contains(t, deployments, "nginx") + }) // Remove only podinfo - removePackagesFlag(bundlePath, "podinfo") - deployments, _, _ = e2e.UDS(cmd...) - require.NotContains(t, deployments, "podinfo") - require.Contains(t, deployments, "nginx") + t.Run("Remove only podinfo", func(t *testing.T) { + removePackagesFlag(bundlePath, "podinfo") + deployments, _, _ := e2e.UDS(getDeploymentsCmd...) + require.NotContains(t, deployments, "podinfo") + require.Contains(t, deployments, "nginx") + }) + + // Deploy only nginx (remote pkg) + t.Run("Deploy only nginx (remote pkg)", func(t *testing.T) { + deployPackagesFlag(bundlePath, "nginx") + deployments, _, _ := e2e.UDS(getDeploymentsCmd...) + require.Contains(t, deployments, "nginx") + require.NotContains(t, deployments, "podinfo") + }) + + // Deploy bundle --resume (resumes remote pkg) + t.Run("Deploy bundle --resume", func(t *testing.T) { + deployResumeFlag(t, bundlePath) + deployments, _, _ := e2e.UDS(getDeploymentsCmd...) + require.Contains(t, deployments, "podinfo") + require.Contains(t, deployments, "nginx") + }) + + // Remove only nginx + t.Run("Remove only podinfo", func(t *testing.T) { + removePackagesFlag(bundlePath, "nginx") + deployments, _, _ := e2e.UDS(getDeploymentsCmd...) + require.NotContains(t, deployments, "nginx") + require.Contains(t, deployments, "podinfo") + }) // Remove bundle remove(t, bundlePath) - cmd = strings.Split("zarf tools kubectl get deployments -A -o=jsonpath='{.items[*].metadata.name}'", " ") - deployments, _, _ = e2e.UDS(cmd...) + deployments, _, _ := e2e.UDS(getDeploymentsCmd...) require.NotContains(t, deployments, "podinfo") require.NotContains(t, deployments, "nginx") } @@ -237,7 +270,7 @@ func TestRemoteBundleWithRemotePkgs(t *testing.T) { } tarballPath := filepath.Join("build", fmt.Sprintf("uds-bundle-example-remote-%s-0.0.1.tar.zst", e2e.Arch)) - bundlePath := "src/test/bundles/01-uds-bundle/remote" + bundlePath := "src/test/bundles/01-uds-bundle" createRemoteInsecure(t, bundlePath, bundleRef.Registry, e2e.Arch) // Test without oci prefix @@ -531,4 +564,7 @@ func TestBundleTmpDir(t *testing.T) { case <-time.After(10 * time.Second): // Timeout after 10 seconds t.Fatal("timeout waiting for directory to get populated") } + // remove customtmp folder if it exists + err = os.RemoveAll("./customtmp") + require.NoError(t, err) } From 07cba2c5d6ff8373dd68bd9f62d1f324c7b9604a Mon Sep 17 00:00:00 2001 From: unclegedd Date: Thu, 29 Feb 2024 13:52:34 -0600 Subject: [PATCH 10/11] WIP --- src/test/e2e/bundle_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/e2e/bundle_test.go b/src/test/e2e/bundle_test.go index 6b714978..56c233dc 100644 --- a/src/test/e2e/bundle_test.go +++ b/src/test/e2e/bundle_test.go @@ -121,6 +121,7 @@ func TestLocalBundleWithRemotePkgs(t *testing.T) { func TestPackagesFlag(t *testing.T) { deployZarfInit(t) + e2e.CreateZarfPkg(t, "src/test/packages/nginx", false) e2e.CreateZarfPkg(t, "src/test/packages/podinfo", false) bundleDir := "src/test/bundles/03-local-and-remote" bundlePath := filepath.Join(bundleDir, fmt.Sprintf("uds-bundle-test-local-and-remote-%s-0.0.1.tar.zst", e2e.Arch)) From a65cd685ae1ca06dc566c27c85eced5cebfbff63 Mon Sep 17 00:00:00 2001 From: unclegedd Date: Thu, 29 Feb 2024 16:46:29 -0600 Subject: [PATCH 11/11] fix: refactors tests and fixes bugs --- src/test/e2e/bundle_test.go | 97 +++++++++++++++---------------------- 1 file changed, 38 insertions(+), 59 deletions(-) diff --git a/src/test/e2e/bundle_test.go b/src/test/e2e/bundle_test.go index 56c233dc..cf289fbd 100644 --- a/src/test/e2e/bundle_test.go +++ b/src/test/e2e/bundle_test.go @@ -121,52 +121,48 @@ func TestLocalBundleWithRemotePkgs(t *testing.T) { func TestPackagesFlag(t *testing.T) { deployZarfInit(t) - e2e.CreateZarfPkg(t, "src/test/packages/nginx", false) e2e.CreateZarfPkg(t, "src/test/packages/podinfo", false) bundleDir := "src/test/bundles/03-local-and-remote" bundlePath := filepath.Join(bundleDir, fmt.Sprintf("uds-bundle-test-local-and-remote-%s-0.0.1.tar.zst", e2e.Arch)) createLocal(t, bundleDir, e2e.Arch) + cmd := strings.Split("zarf tools kubectl get deployments -A -o=jsonpath='{.items[*].metadata.name}'", " ") t.Run("Test only podinfo deploy (local pkg)", func(t *testing.T) { deployPackagesFlag(bundlePath, "podinfo") - cmd := strings.Split("zarf tools kubectl get deployments -A -o=jsonpath='{.items[*].metadata.name}'", " ") deployments, _, _ := e2e.UDS(cmd...) require.Contains(t, deployments, "podinfo") require.NotContains(t, deployments, "nginx") + remove(t, bundlePath) + deployments, _, _ = e2e.UDS(cmd...) + require.NotContains(t, deployments, "podinfo") }) - cmd := strings.Split("zarf tools kubectl get deployments -A -o=jsonpath='{.items[*].metadata.name}'", " ") - t.Run("Test only nginx deploy (remote pkg)", func(t *testing.T) { + t.Run("Test only nginx deploy and remove (remote pkg)", func(t *testing.T) { deployPackagesFlag(bundlePath, "nginx") deployments, _, _ := e2e.UDS(cmd...) require.Contains(t, deployments, "nginx") require.NotContains(t, deployments, "podinfo") remove(t, bundlePath) + + removePackagesFlag(bundlePath, "nginx") + deployments, _, _ = e2e.UDS(cmd...) + require.NotContains(t, deployments, "nginx") }) - t.Run("Test both podinfo and nginx deploy", func(t *testing.T) { + t.Run("Test both podinfo and nginx deploy and remove", func(t *testing.T) { deployPackagesFlag(bundlePath, "podinfo,nginx") deployments, _, _ := e2e.UDS(cmd...) require.Contains(t, deployments, "podinfo") require.Contains(t, deployments, "nginx") - }) - t.Run("Remove only podinfo (local pkg)", func(t *testing.T) { - removePackagesFlag(bundlePath, "podinfo") - deployments, _, _ := e2e.UDS(cmd...) - require.NotContains(t, deployments, "podinfo") - require.Contains(t, deployments, "nginx") - - }) - t.Run("Remove only nginx (remote pkg)", func(t *testing.T) { - removePackagesFlag(bundlePath, "nginx") - deployments, _, _ := e2e.UDS(cmd...) + removePackagesFlag(bundlePath, "podinfo,nginx") + deployments, _, _ = e2e.UDS(cmd...) require.NotContains(t, deployments, "podinfo") require.NotContains(t, deployments, "nginx") - }) + t.Run("Test invalid package deploy", func(t *testing.T) { _, stderr := deployPackagesFlag(bundlePath, "podinfo,nginx,peanuts") require.Contains(t, stderr, "invalid zarf packages specified by --packages") @@ -181,7 +177,6 @@ func TestPackagesFlag(t *testing.T) { func TestResumeFlag(t *testing.T) { deployZarfInit(t) - e2e.CreateZarfPkg(t, "src/test/packages/nginx", false) e2e.CreateZarfPkg(t, "src/test/packages/podinfo", false) bundleDir := "src/test/bundles/03-local-and-remote" bundlePath := filepath.Join(bundleDir, fmt.Sprintf("uds-bundle-test-local-and-remote-%s-0.0.1.tar.zst", e2e.Arch)) @@ -193,56 +188,44 @@ func TestResumeFlag(t *testing.T) { getDeploymentsCmd := strings.Split("zarf tools kubectl get deployments -A -o=jsonpath='{.items[*].metadata.name}'", " ") // Deploy only podinfo (local pkg) - t.Run("Deploy only podinfo (local pkg)", func(t *testing.T) { - deployPackagesFlag(bundlePath, "podinfo") - deployments, _, _ := e2e.UDS(getDeploymentsCmd...) - require.Contains(t, deployments, "podinfo") - require.NotContains(t, deployments, "nginx") - }) + deployPackagesFlag(bundlePath, "podinfo") + deployments, _, _ := e2e.UDS(getDeploymentsCmd...) + require.Contains(t, deployments, "podinfo") + require.NotContains(t, deployments, "nginx") // Deploy bundle --resume (resumes remote pkg) - t.Run("Deploy bundle --resume", func(t *testing.T) { - deployResumeFlag(t, bundlePath) - deployments, _, _ := e2e.UDS(getDeploymentsCmd...) - require.Contains(t, deployments, "podinfo") - require.Contains(t, deployments, "nginx") - }) + deployResumeFlag(t, bundlePath) + deployments, _, _ = e2e.UDS(getDeploymentsCmd...) + require.Contains(t, deployments, "podinfo") + require.Contains(t, deployments, "nginx") // Remove only podinfo - t.Run("Remove only podinfo", func(t *testing.T) { - removePackagesFlag(bundlePath, "podinfo") - deployments, _, _ := e2e.UDS(getDeploymentsCmd...) - require.NotContains(t, deployments, "podinfo") - require.Contains(t, deployments, "nginx") - }) + removePackagesFlag(bundlePath, "podinfo") + deployments, _, _ = e2e.UDS(getDeploymentsCmd...) + require.NotContains(t, deployments, "podinfo") + require.Contains(t, deployments, "nginx") // Deploy only nginx (remote pkg) - t.Run("Deploy only nginx (remote pkg)", func(t *testing.T) { - deployPackagesFlag(bundlePath, "nginx") - deployments, _, _ := e2e.UDS(getDeploymentsCmd...) - require.Contains(t, deployments, "nginx") - require.NotContains(t, deployments, "podinfo") - }) + deployPackagesFlag(bundlePath, "nginx") + deployments, _, _ = e2e.UDS(getDeploymentsCmd...) + require.Contains(t, deployments, "nginx") + require.NotContains(t, deployments, "podinfo") // Deploy bundle --resume (resumes remote pkg) - t.Run("Deploy bundle --resume", func(t *testing.T) { - deployResumeFlag(t, bundlePath) - deployments, _, _ := e2e.UDS(getDeploymentsCmd...) - require.Contains(t, deployments, "podinfo") - require.Contains(t, deployments, "nginx") - }) + deployResumeFlag(t, bundlePath) + deployments, _, _ = e2e.UDS(getDeploymentsCmd...) + require.Contains(t, deployments, "podinfo") + require.Contains(t, deployments, "nginx") // Remove only nginx - t.Run("Remove only podinfo", func(t *testing.T) { - removePackagesFlag(bundlePath, "nginx") - deployments, _, _ := e2e.UDS(getDeploymentsCmd...) - require.NotContains(t, deployments, "nginx") - require.Contains(t, deployments, "podinfo") - }) + removePackagesFlag(bundlePath, "nginx") + deployments, _, _ = e2e.UDS(getDeploymentsCmd...) + require.NotContains(t, deployments, "nginx") + require.Contains(t, deployments, "podinfo") // Remove bundle remove(t, bundlePath) - deployments, _, _ := e2e.UDS(getDeploymentsCmd...) + deployments, _, _ = e2e.UDS(getDeploymentsCmd...) require.NotContains(t, deployments, "podinfo") require.NotContains(t, deployments, "nginx") } @@ -303,9 +286,7 @@ func TestBundleWithGitRepo(t *testing.T) { func TestBundleWithYmlFile(t *testing.T) { deployZarfInit(t) - e2e.CreateZarfPkg(t, "src/test/packages/nginx", true) - e2e.CreateZarfPkg(t, "src/test/packages/podinfo", true) bundleDir := "src/test/bundles/09-uds-bundle-yml" bundlePath := filepath.Join(bundleDir, fmt.Sprintf("uds-bundle-yml-example-%s-0.0.1.tar.zst", e2e.Arch)) @@ -481,8 +462,6 @@ func validateMultiArchIndex(t *testing.T, index ocispec.Index) { func TestBundleTmpDir(t *testing.T) { deployZarfInit(t) - - e2e.CreateZarfPkg(t, "src/test/packages/nginx", false) e2e.CreateZarfPkg(t, "src/test/packages/podinfo", false) bundleDir := "src/test/bundles/03-local-and-remote"