1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2024-12-14 11:57:48 +00:00

chore: improve unit tests in cli (#8296)

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
This commit is contained in:
Charles-Edouard Brétéché 2023-09-06 19:01:23 +02:00 committed by GitHub
parent aeb8e52fec
commit 2d8c74eb12
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 199 additions and 1 deletions

View file

@ -6,6 +6,7 @@ import (
"testing" "testing"
"github.com/go-git/go-billy/v5" "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" 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", filepath: "../_testdata/values/valid.yaml",
want: mustReadFile("../_testdata/values/valid.yaml"), want: mustReadFile("../_testdata/values/valid.yaml"),
wantErr: false, 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 { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {

View file

@ -5,7 +5,8 @@ import (
) )
func NeedsVariable(variable string) bool { 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, "request.operation") &&
!strings.Contains(variable, "element") && !strings.Contains(variable, "element") &&
variable != "elementIndex" variable != "elementIndex"

View file

@ -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)
}
})
}
}

View file

@ -33,6 +33,9 @@ func (v Variables) Subresources() []valuesapi.Subresource {
if v.values == nil { if v.values == nil {
return nil return nil
} }
if len(v.values.Subresources) == 0 {
return nil
}
return v.values.Subresources return v.values.Subresources
} }

View file

@ -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)
}
})
}
}