Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: refactors tests and fixes bugs #464

Merged
merged 11 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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}'

Expand Down
6 changes: 4 additions & 2 deletions src/pkg/bundle/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ func Test_validateBundleVars(t *testing.T) {
},
},
wantErr: false,
}, {
},
{
name: "ImportDoesntMatchExport",
description: "error when import doesn't match export",
args: args{
Expand All @@ -37,7 +38,8 @@ func Test_validateBundleVars(t *testing.T) {
},
},
wantErr: true,
}, {
},
{
name: "FirstPkgHasImport",
description: "error when first pkg has an import",
args: args{
Expand Down
12 changes: 2 additions & 10 deletions src/pkg/bundle/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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 {
Expand Down
241 changes: 241 additions & 0 deletions src/pkg/bundle/deploy_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
2 changes: 1 addition & 1 deletion src/pkg/bundle/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
8 changes: 6 additions & 2 deletions src/pkg/bundle/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 10 additions & 0 deletions src/pkg/bundler/fetcher/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions src/test/bundles/01-uds-bundle/uds-bundle.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
17 changes: 0 additions & 17 deletions src/test/bundles/02-simple-vars/import-all/uds-bundle.yaml

This file was deleted.

Loading
Loading