-
Notifications
You must be signed in to change notification settings - Fork 1
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 { | ||
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"}}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 != "" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is another area where I think |
||
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) | ||
} | ||
} | ||
}) | ||
} | ||
} |
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) | ||
} | ||
}) | ||
} | ||
} |
There was a problem hiding this comment.
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 fromtestify
which simpliies this logic