diff --git a/cmd/cli/kubectl-kyverno/values/load_test.go b/cmd/cli/kubectl-kyverno/values/load_test.go index 6e3915e134..78997b6de4 100644 --- a/cmd/cli/kubectl-kyverno/values/load_test.go +++ b/cmd/cli/kubectl-kyverno/values/load_test.go @@ -6,6 +6,7 @@ import ( "testing" "github.com/go-git/go-billy/v5" + "github.com/go-git/go-billy/v5/memfs" valuesapi "github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/apis/values" ) @@ -44,6 +45,29 @@ func Test_readFile(t *testing.T) { filepath: "../_testdata/values/valid.yaml", want: mustReadFile("../_testdata/values/valid.yaml"), wantErr: false, + }, { + name: "empty (billy)", + f: memfs.New(), + filepath: "", + want: nil, + wantErr: true, + }, { + name: "valid (billy)", + f: func() billy.Filesystem { + f := memfs.New() + file, err := f.Create("valid.yaml") + if err != nil { + t.Fatal(err) + } + if _, err := file.Write([]byte("foo: bar")); err != nil { + t.Fatal(err) + } + defer file.Close() + return f + }(), + filepath: "valid.yaml", + want: []byte("foo: bar"), + wantErr: false, }} for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/cmd/cli/kubectl-kyverno/variables/utils.go b/cmd/cli/kubectl-kyverno/variables/utils.go index d4b36e7911..ca737418b9 100644 --- a/cmd/cli/kubectl-kyverno/variables/utils.go +++ b/cmd/cli/kubectl-kyverno/variables/utils.go @@ -5,7 +5,8 @@ import ( ) func NeedsVariable(variable string) bool { - return !strings.Contains(variable, "request.object") && + return variable != "" && + !strings.Contains(variable, "request.object") && !strings.Contains(variable, "request.operation") && !strings.Contains(variable, "element") && variable != "elementIndex" diff --git a/cmd/cli/kubectl-kyverno/variables/utils_test.go b/cmd/cli/kubectl-kyverno/variables/utils_test.go new file mode 100644 index 0000000000..6323acdfca --- /dev/null +++ b/cmd/cli/kubectl-kyverno/variables/utils_test.go @@ -0,0 +1,79 @@ +package variables + +import ( + "testing" +) + +func TestNeedsVariable(t *testing.T) { + tests := []struct { + name string + want bool + }{{ + name: "", + want: false, + }, { + name: "request.object.spec", + want: false, + }, { + name: "request.operation", + want: false, + }, { + name: "element.spec.container", + want: false, + }, { + name: "elementIndex", + want: false, + }, { + name: "foo", + want: true, + }} + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := NeedsVariable(tt.name); got != tt.want { + t.Errorf("NeedsVariable() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestNeedsVariables(t *testing.T) { + tests := []struct { + name string + variables []string + want bool + }{{ + name: "nil", + variables: nil, + want: false, + }, { + name: "empty", + variables: []string{}, + want: false, + }, { + name: "false", + variables: []string{ + "request.object.spec", + "request.operation", + "element.spec.container", + "elementIndex", + }, + want: false, + }, { + name: "true", + variables: []string{ + "request.object.spec", + "request.operation", + "element.spec.container", + "elementIndex", + "foo", + }, + want: true, + }} + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := NeedsVariables(tt.variables...); got != tt.want { + t.Errorf("NeedsVariables() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/cmd/cli/kubectl-kyverno/variables/variables.go b/cmd/cli/kubectl-kyverno/variables/variables.go index db8cdc05b7..3cb45e6b12 100644 --- a/cmd/cli/kubectl-kyverno/variables/variables.go +++ b/cmd/cli/kubectl-kyverno/variables/variables.go @@ -33,6 +33,9 @@ func (v Variables) Subresources() []valuesapi.Subresource { if v.values == nil { return nil } + if len(v.values.Subresources) == 0 { + return nil + } return v.values.Subresources } diff --git a/cmd/cli/kubectl-kyverno/variables/variables_test.go b/cmd/cli/kubectl-kyverno/variables/variables_test.go new file mode 100644 index 0000000000..66ec1b4a3d --- /dev/null +++ b/cmd/cli/kubectl-kyverno/variables/variables_test.go @@ -0,0 +1,91 @@ +package variables + +import ( + "reflect" + "testing" + + valuesapi "github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/apis/values" +) + +func TestVariables_HasVariables(t *testing.T) { + tests := []struct { + name string + values *valuesapi.Values + variables map[string]string + want bool + }{{ + name: "nil", + values: nil, + variables: nil, + want: false, + }, { + name: "empty", + values: nil, + variables: map[string]string{}, + want: false, + }, { + name: "not empty", + values: nil, + variables: map[string]string{ + "foo": "bar", + }, + want: true, + }} + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + v := Variables{ + values: tt.values, + variables: tt.variables, + } + if got := v.HasVariables(); got != tt.want { + t.Errorf("Variables.HasVariables() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestVariables_Subresources(t *testing.T) { + tests := []struct { + name string + values *valuesapi.Values + variables map[string]string + want []valuesapi.Subresource + }{{ + name: "nil values", + values: nil, + variables: nil, + want: nil, + }, { + name: "nil subresources", + values: &valuesapi.Values{ + Subresources: nil, + }, + variables: nil, + want: nil, + }, { + name: "empty subresources", + values: &valuesapi.Values{ + Subresources: []valuesapi.Subresource{}, + }, + variables: nil, + want: nil, + }, { + name: "subresources", + values: &valuesapi.Values{ + Subresources: []valuesapi.Subresource{{}}, + }, + variables: nil, + want: []valuesapi.Subresource{{}}, + }} + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + v := Variables{ + values: tt.values, + variables: tt.variables, + } + if got := v.Subresources(); !reflect.DeepEqual(got, tt.want) { + t.Errorf("Variables.Subresources() = %v, want %v", got, tt.want) + } + }) + } +}