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/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..cdb834fd 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() @@ -227,7 +218,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] @@ -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 { 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..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, 0755); err != nil { + if err := utils.CreateDirectory(cacheDir, 0o755); err != nil { return err } diff --git a/src/pkg/bundle/remove.go b/src/pkg/bundle/remove.go index f6c5876c..6896bfcc 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 _, ok := zarfPackageNameMap[pkg.Name]; ok { + zarfPackageName = zarfPackageNameMap[pkg.Name] + } + if slices.Contains(deployedPackageNames, zarfPackageName) { opts := zarfTypes.ZarfPackageOptions{ PackageSource: b.cfg.RemoveOpts.Source, diff --git a/src/pkg/bundler/fetcher/local.go b/src/pkg/bundler/fetcher/local.go index aa19a9d0..580cb9b2 100644 --- a/src/pkg/bundler/fetcher/local.go +++ b/src/pkg/bundler/fetcher/local.go @@ -173,6 +173,16 @@ 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 + + // 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, path) if err != nil { return nil, err diff --git a/src/pkg/cache/cache.go b/src/pkg/cache/cache.go index 8e0520ec..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"), 0755); 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, 0755); err != nil { + if err := os.MkdirAll(dstDir, 0o755); err != nil { return err } diff --git a/src/test/bundles/01-uds-bundle/uds-bundle.yaml b/src/test/bundles/01-uds-bundle/uds-bundle.yaml index 94666242..afa0d034 100644 --- a/src/test/bundles/01-uds-bundle/uds-bundle.yaml +++ b/src/test/bundles/01-uds-bundle/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-simple-vars/import-all/uds-bundle.yaml b/src/test/bundles/02-simple-vars/import-all/uds-bundle.yaml deleted file mode 100644 index 7c2e28fa..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 - 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 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 70% 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 34671511..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,19 +1,19 @@ kind: UDSBundle metadata: - name: import-all-bad-name + name: bad-var-name description: show errors for bad imports version: 0.0.1 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/export-name-collision/uds-bundle.yaml b/src/test/bundles/02-variables/export-name-collision/uds-bundle.yaml similarity index 73% 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 index ad15a689..97a87f3a 100644 --- 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 @@ -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/uds-bundle.yaml b/src/test/bundles/02-variables/remote/uds-bundle.yaml similarity index 95% rename from src/test/bundles/02-simple-vars/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/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-variables/uds-bundle.yaml b/src/test/bundles/02-variables/uds-bundle.yaml new file mode 100644 index 00000000..db5d1980 --- /dev/null +++ b/src/test/bundles/02-variables/uds-bundle.yaml @@ -0,0 +1,21 @@ +kind: UDSBundle +metadata: + name: variables + description: show how vars 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 + imports: + # note that PRECEDENCE is not imported because exports are global! + - name: OUTPUT + 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/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 e2bcc606..8745785c 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 @@ -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/common.go b/src/test/common.go index d3444e5c..373f81f2 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) } @@ -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, 0o755); err != nil { return err } diff --git a/src/test/e2e/bundle_test.go b/src/test/e2e/bundle_test.go index 766484ca..cf289fbd 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,10 +56,11 @@ 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-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", " ") @@ -90,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) @@ -108,138 +110,127 @@ func TestBundle(t *testing.T) { 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)) + bundlePath := filepath.Join(bundleDir, fmt.Sprintf("uds-bundle-example-remote-%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 - 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 - 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/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") 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") - - // Remove only podinfo - removePackagesFlag(bundlePath, "podinfo") - deployments, _, _ = e2e.UDS(cmd...) - require.NotContains(t, deployments, "podinfo") - require.Contains(t, deployments, "nginx") - - // Remove nginx - 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") + t.Run("Test only podinfo deploy (local pkg)", func(t *testing.T) { + deployPackagesFlag(bundlePath, "podinfo") + 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") + }) + + 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 and remove", func(t *testing.T) { + deployPackagesFlag(bundlePath, "podinfo,nginx") + deployments, _, _ := e2e.UDS(cmd...) + require.Contains(t, deployments, "podinfo") + require.Contains(t, deployments, "nginx") + + 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") + + }) + 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) - - 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) - // Deploy only podinfo from bundle + getDeploymentsCmd := strings.Split("zarf tools kubectl get deployments -A -o=jsonpath='{.items[*].metadata.name}'", " ") + + // Deploy only podinfo (local pkg) deployPackagesFlag(bundlePath, "podinfo") - cmd := strings.Split("zarf tools kubectl get deployments -A -o=jsonpath='{.items[*].metadata.name}'", " ") - deployments, _, _ := e2e.UDS(cmd...) + deployments, _, _ := e2e.UDS(getDeploymentsCmd...) require.Contains(t, deployments, "podinfo") require.NotContains(t, deployments, "nginx") - // Deploy bundle --resume + // Deploy bundle --resume (resumes remote pkg) deployResumeFlag(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.Contains(t, deployments, "podinfo") require.Contains(t, deployments, "nginx") // Remove only podinfo removePackagesFlag(bundlePath, "podinfo") - deployments, _, _ = e2e.UDS(cmd...) + deployments, _, _ = e2e.UDS(getDeploymentsCmd...) require.NotContains(t, deployments, "podinfo") require.Contains(t, deployments, "nginx") + // Deploy only nginx (remote pkg) + deployPackagesFlag(bundlePath, "nginx") + deployments, _, _ = e2e.UDS(getDeploymentsCmd...) + require.Contains(t, deployments, "nginx") + require.NotContains(t, deployments, "podinfo") + + // Deploy bundle --resume (resumes remote pkg) + deployResumeFlag(t, bundlePath) + deployments, _, _ = e2e.UDS(getDeploymentsCmd...) + require.Contains(t, deployments, "podinfo") + require.Contains(t, deployments, "nginx") + + // Remove only nginx + 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") } -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) @@ -258,11 +249,11 @@ 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)) + 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" createRemoteInsecure(t, bundlePath, bundleRef.Registry, e2e.Arch) @@ -274,11 +265,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) @@ -297,20 +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) - - 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)) @@ -318,7 +294,6 @@ func TestBundleWithYmlFile(t *testing.T) { 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) @@ -487,29 +462,16 @@ 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) - 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)) - //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, 0o755) if err != nil { t.Fatalf("error creating directory: %v", err) } @@ -582,4 +544,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) } diff --git a/src/test/e2e/variable_test.go b/src/test/e2e/variable_test.go index 038bb2ed..407acecd 100644 --- a/src/test/e2e/variable_test.go +++ b/src/test/e2e/variable_test.go @@ -15,59 +15,34 @@ 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") - - 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) - + e2e.CreateZarfPkg(t, "src/test/packages/no-cluster/output-var", false) + e2e.CreateZarfPkg(t, "src/test/packages/no-cluster/receive-var", false) 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) - - 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") + 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) { @@ -106,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) - - // 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) - - // 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) - - // 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) - - // 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") - - // 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") + // 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) + }) + + 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) + + }) + + 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) + + }) + + // 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") + }) + + 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) + }) + + 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) } @@ -183,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) } @@ -214,38 +211,33 @@ 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) - 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" @@ -254,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) - - // 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) - - // 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 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) + }) + + 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) + }) + + 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) }