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

feat: included tests for utils package #40

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/alecthomas/jsonschema v0.0.0-20220216202328-9eeeec9d044b
github.com/defenseunicorns/zarf v0.32.5
github.com/goccy/go-yaml v1.11.3
github.com/google/go-cmp v0.6.0
github.com/mholt/archiver/v3 v3.5.1
github.com/pterm/pterm v0.12.79
github.com/spf13/cobra v1.8.0
Expand Down Expand Up @@ -196,7 +197,6 @@ require (
github.com/google/btree v1.1.2 // indirect
github.com/google/certificate-transparency-go v1.1.7 // indirect
github.com/google/gnostic-models v0.6.9-0.20230804172637-c7be7c783f49 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/go-containerregistry v0.19.0 // indirect
github.com/google/go-github/v55 v55.0.0 // indirect
github.com/google/go-querystring v1.1.0 // indirect
Expand Down
182 changes: 182 additions & 0 deletions src/pkg/utils/template_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2023-Present The UDS Authors

package utils

import (
"testing"

zarfTypes "github.com/defenseunicorns/zarf/src/types"

"github.com/defenseunicorns/maru-runner/src/types"
zarfUtils "github.com/defenseunicorns/zarf/src/pkg/utils"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)

func TestTemplateString(t *testing.T) {
type args struct {
templateMap map[string]*zarfUtils.TextTemplate
s string
}
tests := []struct {
name string
args args
want string
}{
{
name: "single replacement",
args: args{
templateMap: map[string]*zarfUtils.TextTemplate{
"${VAR1}": {Value: "replacement1"},
},
s: "This is a ${VAR1} string.",
},
want: "This is a replacement1 string.",
},
{
name: "multiple replacements",
args: args{
templateMap: map[string]*zarfUtils.TextTemplate{
"${VAR1}": {Value: "replacement1"},
"${VAR2}": {Value: "replacement2"},
},
s: "This is a ${VAR1} and ${VAR2} string.",
},
want: "This is a replacement1 and replacement2 string.",
},
{
name: "no replacements",
args: args{
templateMap: map[string]*zarfUtils.TextTemplate{},
s: "This string has no variables.",
},
want: "This string has no variables.",
},
{
name: "missing replacement",
args: args{
templateMap: map[string]*zarfUtils.TextTemplate{
"${VAR1}": {Value: "replacement1"},
},
s: "This is a ${VAR1} and ${MISSING_VAR} string.",
},
want: "This is a replacement1 and ${MISSING_VAR} string.",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := TemplateString(tt.args.templateMap, tt.args.s); got != tt.want {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Instead of manually rolling the checks, in other tests we use the require function from testify which simpliies this logic

t.Errorf("TemplateString() = %v, want %v", got, tt.want)
}
})
}
}

func TestTemplateTaskActionsWithInputs(t *testing.T) {
type args struct {
task types.Task
withs map[string]string
}
tests := []struct {
name string
args args
want []types.Action
wantErr bool
}{
{
name: "successful template with inputs",
args: args{
task: types.Task{
Name: "test-task",
Description: "test task",
Inputs: map[string]types.InputParameter{
"test-input": {Default: "default1", Description: "test input"},
},
Actions: []types.Action{
{TaskReference: "test-task", With: map[string]string{"test-input": "value1"}},
},
},
withs: map[string]string{"test-input": "value1"},
},
want: []types.Action{
{TaskReference: "test-task", With: map[string]string{"test-input": "value1"}},
Copy link
Collaborator

Choose a reason for hiding this comment

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

I may be missing it, but where does the templating occur in this test (docs for reference)

},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := TemplateTaskActionsWithInputs(tt.args.task, tt.args.withs)
if (err != nil) != tt.wantErr {
t.Errorf("TemplateTaskActionsWithInputs() error = %v, wantErr %v", err, tt.wantErr)
return
}
if diff := cmp.Diff(tt.want, got, cmpopts.IgnoreFields(types.Action{}, "ZarfComponentAction")); diff != "" {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is another area where I think require is a bit cleaner with less code; maybe you could compare just a single field in the Action instead of doing the deep inspection

t.Errorf("TemplateTaskActionsWithInputs() mismatch (-want +got):\n%s", diff)
}
})
}
}

func TestPopulateTemplateMap(t *testing.T) {
type args struct {
zarfVariables []zarfTypes.ZarfPackageVariable
setVariables map[string]string
}
tests := []struct {
name string
args args
want map[string]*zarfUtils.TextTemplate
}{
{
name: "Populate with no overrides",
args: args{
zarfVariables: []zarfTypes.ZarfPackageVariable{
{Name: "TEST_VAR", Default: "default_value", Sensitive: false, AutoIndent: false, Type: "string"},
},
setVariables: map[string]string{},
},
want: map[string]*zarfUtils.TextTemplate{
"${TEST_VAR}": {
Value: "default_value",
Sensitive: false,
AutoIndent: false,
Type: "string",
},
},
},
{
name: "Populate with overrides",
args: args{
zarfVariables: []zarfTypes.ZarfPackageVariable{
{Name: "TEST_VAR", Default: "default_value", Sensitive: false, AutoIndent: false, Type: "string"},
},
setVariables: map[string]string{
"TEST_VAR": "overridden_value",
},
},
want: map[string]*zarfUtils.TextTemplate{
"${TEST_VAR}": {
Value: "overridden_value",
Sensitive: false,
AutoIndent: false,
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := PopulateTemplateMap(tt.args.zarfVariables, tt.args.setVariables)
for key, wantVal := range tt.want {
if gotVal, ok := got[key]; ok {
if diff := cmp.Diff(wantVal, gotVal); diff != "" {
t.Errorf("PopulateTemplateMap() mismatch (-want +got):\n%s", diff)
}
} else {
t.Errorf("PopulateTemplateMap() missing key: %v", key)
}
}
})
}
}
115 changes: 115 additions & 0 deletions src/pkg/utils/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2023-Present The UDS Authors

package utils

import (
"testing"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)

func TestMergeEnv(t *testing.T) {
type args struct {
env1 []string
env2 []string
}
tests := []struct {
name string
args args
want []string
}{
{
name: "Merge with no duplicates",
args: args{
env1: []string{"PATH=/usr/bin", "HOME=/home/user"},
env2: []string{"GO111MODULE=on", "CGO_ENABLED=0"},
},
want: []string{"CGO_ENABLED=0", "PATH=/usr/bin", "HOME=/home/user", "GO111MODULE=on"},
},
{
name: "Merge with duplicates",
args: args{
env1: []string{"PATH=/usr/bin", "HOME=/home/user"},
env2: []string{"PATH=/usr/local/bin", "EDITOR=vim"},
},
want: []string{"PATH=/usr/bin", "HOME=/home/user", "EDITOR=vim"},
},
{
name: "Merge with empty first env",
args: args{
env1: []string{},
env2: []string{"PATH=/usr/local/bin", "EDITOR=vim"},
},
want: []string{"PATH=/usr/local/bin", "EDITOR=vim"},
},
{
name: "Merge with empty second env",
args: args{
env1: []string{"PATH=/usr/bin", "HOME=/home/user"},
env2: []string{},
},
want: []string{"PATH=/usr/bin", "HOME=/home/user"},
},
{
name: "Merge both envs empty",
args: args{
env1: []string{},
env2: []string{},
},
want: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := MergeEnv(tt.args.env1, tt.args.env2)
if diff := cmp.Diff(tt.want, got, cmpopts.SortSlices(func(a, b string) bool { return a < b })); diff != "" {
t.Errorf("MergeEnv() mismatch (-want +got):\n%s", diff)
}
})
}
}
func TestFormatEnvVar(t *testing.T) {
type args struct {
name string
value string
}
tests := []struct {
name string
args args
want string
}{
{
name: "Format standard variable",
args: args{
name: "PATH",
value: "/usr/bin:/bin",
},
want: "INPUT_PATH=/usr/bin:/bin",
},
{
name: "Format empty value",
args: args{
name: "EMPTY_VAR",
value: "",
},
want: "INPUT_EMPTY_VAR=",
},
{
name: "Format variable with spaces",
args: args{
name: "WITH_SPACES",
value: "value with spaces",
},
want: "INPUT_WITH_SPACES=value with spaces",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := FormatEnvVar(tt.args.name, tt.args.value); got != tt.want {
t.Errorf("FormatEnvVar() = %v, want %v", got, tt.want)
}
})
}
}
Loading