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:
parent
aeb8e52fec
commit
2d8c74eb12
5 changed files with 199 additions and 1 deletions
|
@ -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) {
|
||||
|
|
|
@ -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"
|
||||
|
|
79
cmd/cli/kubectl-kyverno/variables/utils_test.go
Normal file
79
cmd/cli/kubectl-kyverno/variables/utils_test.go
Normal 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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
91
cmd/cli/kubectl-kyverno/variables/variables_test.go
Normal file
91
cmd/cli/kubectl-kyverno/variables/variables_test.go
Normal 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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue