mirror of
https://github.com/kyverno/kyverno.git
synced 2025-03-05 15:37:19 +00:00
test: add config package unit tests (#6581)
Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
This commit is contained in:
parent
a08d0b8749
commit
7386dc9b9d
3 changed files with 198 additions and 14 deletions
|
@ -110,7 +110,6 @@ func Test_CreateCustomClientConfig_WithContext(t *testing.T) {
|
|||
currentContext := "dev"
|
||||
createCustomKubeConfig(t, customKubeConfig, hosts, currentContext)
|
||||
defer os.Remove(customKubeConfig)
|
||||
|
||||
testCases := []struct {
|
||||
testName string
|
||||
kubeConfig string
|
||||
|
@ -135,7 +134,6 @@ func Test_CreateCustomClientConfig_WithContext(t *testing.T) {
|
|||
host: hosts["qa"],
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range testCases {
|
||||
restConfig, err := config.CreateClientConfigWithContext(test.kubeConfig, test.context)
|
||||
assert.NilError(t, err, fmt.Sprintf("test %s failed", test.testName))
|
||||
|
@ -143,7 +141,6 @@ func Test_CreateCustomClientConfig_WithContext(t *testing.T) {
|
|||
assert.Equal(t, restConfig.Host, test.host, fmt.Sprintf("test %s failed", test.testName))
|
||||
}
|
||||
}
|
||||
|
||||
t.Setenv("KUBECONFIG", customKubeConfig) // use custom kubeconfig instead of ~/.kube/config
|
||||
newCustomKubeConfig := pwd + "/newkubeConfig"
|
||||
newHosts := map[string]string{
|
||||
|
@ -177,7 +174,6 @@ func Test_CreateCustomClientConfig_WithContext(t *testing.T) {
|
|||
host: newHosts["qa"],
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range testCases {
|
||||
restConfig, err := config.CreateClientConfigWithContext(test.kubeConfig, test.context)
|
||||
assert.NilError(t, err, fmt.Sprintf("test %s failed", test.testName))
|
|
@ -13,14 +13,25 @@ type WebhookConfig struct {
|
|||
ObjectSelector *metav1.LabelSelector `json:"objectSelector,omitempty"`
|
||||
}
|
||||
|
||||
func parseWebhooks(webhooks string) ([]WebhookConfig, error) {
|
||||
func parseWebhooks(in string) ([]WebhookConfig, error) {
|
||||
webhookCfgs := make([]WebhookConfig, 0, 10)
|
||||
if err := json.Unmarshal([]byte(webhooks), &webhookCfgs); err != nil {
|
||||
if err := json.Unmarshal([]byte(in), &webhookCfgs); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return webhookCfgs, nil
|
||||
}
|
||||
|
||||
func parseRbac(in string) []string {
|
||||
var out []string
|
||||
for _, in := range strings.Split(in, ",") {
|
||||
in := strings.TrimSpace(in)
|
||||
if in != "" {
|
||||
out = append(out, in)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func parseWebhookAnnotations(in string) (map[string]string, error) {
|
||||
var out map[string]string
|
||||
if err := json.Unmarshal([]byte(in), &out); err != nil {
|
||||
|
@ -29,18 +40,14 @@ func parseWebhookAnnotations(in string) (map[string]string, error) {
|
|||
return out, nil
|
||||
}
|
||||
|
||||
func parseRbac(list string) []string {
|
||||
return strings.Split(list, ",")
|
||||
}
|
||||
|
||||
type namespacesConfig struct {
|
||||
IncludeNamespaces []string `json:"include,omitempty"`
|
||||
ExcludeNamespaces []string `json:"exclude,omitempty"`
|
||||
}
|
||||
|
||||
func parseIncludeExcludeNamespacesFromNamespacesConfig(jsonStr string) (namespacesConfig, error) {
|
||||
func parseIncludeExcludeNamespacesFromNamespacesConfig(in string) (namespacesConfig, error) {
|
||||
var namespacesConfigObject namespacesConfig
|
||||
err := json.Unmarshal([]byte(jsonStr), &namespacesConfigObject)
|
||||
err := json.Unmarshal([]byte(in), &namespacesConfigObject)
|
||||
return namespacesConfigObject, err
|
||||
}
|
||||
|
||||
|
@ -52,11 +59,11 @@ type filter struct {
|
|||
|
||||
// ParseKinds parses the kinds if a single string contains comma separated kinds
|
||||
// {"1,2,3","4","5"} => {"1","2","3","4","5"}
|
||||
func parseKinds(list string) []filter {
|
||||
func parseKinds(in string) []filter {
|
||||
resources := []filter{}
|
||||
var resource filter
|
||||
re := regexp.MustCompile(`\[([^\[\]]*)\]`)
|
||||
submatchall := re.FindAllString(list, -1)
|
||||
submatchall := re.FindAllString(in, -1)
|
||||
for _, element := range submatchall {
|
||||
element = strings.Trim(element, "[")
|
||||
element = strings.Trim(element, "]")
|
||||
|
|
181
pkg/config/types_test.go
Normal file
181
pkg/config/types_test.go
Normal file
|
@ -0,0 +1,181 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_parseRbac(t *testing.T) {
|
||||
type args struct {
|
||||
in string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want []string
|
||||
}{{
|
||||
args: args{""},
|
||||
want: nil,
|
||||
}, {
|
||||
args: args{"abc"},
|
||||
want: []string{"abc"},
|
||||
}, {
|
||||
args: args{" abc "},
|
||||
want: []string{"abc"},
|
||||
}, {
|
||||
args: args{"abc,def"},
|
||||
want: []string{"abc", "def"},
|
||||
}, {
|
||||
args: args{"abc,,,def,"},
|
||||
want: []string{"abc", "def"},
|
||||
}, {
|
||||
args: args{"abc, def"},
|
||||
want: []string{"abc", "def"},
|
||||
}, {
|
||||
args: args{"abc ,def "},
|
||||
want: []string{"abc", "def"},
|
||||
}}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := parseRbac(tt.args.in); !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("parseRbac() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_parseKinds(t *testing.T) {
|
||||
type args struct {
|
||||
in string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want []filter
|
||||
}{{
|
||||
args: args{""},
|
||||
want: []filter{},
|
||||
}, {
|
||||
args: args{"[]"},
|
||||
// TODO: this looks strange
|
||||
want: []filter{
|
||||
{},
|
||||
},
|
||||
}, {
|
||||
args: args{"[*]"},
|
||||
want: []filter{
|
||||
{"*", "", ""},
|
||||
},
|
||||
}, {
|
||||
args: args{"[Node]"},
|
||||
want: []filter{
|
||||
{"Node", "", ""},
|
||||
},
|
||||
}, {
|
||||
args: args{"[Node,*,*]"},
|
||||
want: []filter{
|
||||
{"Node", "*", "*"},
|
||||
},
|
||||
}, {
|
||||
args: args{"[Pod,default,nginx]"},
|
||||
want: []filter{
|
||||
{"Pod", "default", "nginx"},
|
||||
},
|
||||
}, {
|
||||
args: args{"[Pod,*,nginx]"},
|
||||
want: []filter{
|
||||
{"Pod", "*", "nginx"},
|
||||
},
|
||||
}, {
|
||||
args: args{"[Pod,*]"},
|
||||
want: []filter{
|
||||
{"Pod", "*", ""},
|
||||
},
|
||||
}, {
|
||||
args: args{"[Pod,default,nginx][Pod,kube-system,api-server]"},
|
||||
want: []filter{
|
||||
{"Pod", "default", "nginx"},
|
||||
{"Pod", "kube-system", "api-server"},
|
||||
},
|
||||
}, {
|
||||
args: args{"[Pod,default,nginx],[Pod,kube-system,api-server]"},
|
||||
want: []filter{
|
||||
{"Pod", "default", "nginx"},
|
||||
{"Pod", "kube-system", "api-server"},
|
||||
},
|
||||
}, {
|
||||
args: args{"[Pod,default,nginx] [Pod,kube-system,api-server]"},
|
||||
want: []filter{
|
||||
{"Pod", "default", "nginx"},
|
||||
{"Pod", "kube-system", "api-server"},
|
||||
},
|
||||
}, {
|
||||
args: args{"[Pod,default,nginx]Pod,kube-system,api-server[Pod,kube-system,api-server]"},
|
||||
want: []filter{
|
||||
{"Pod", "default", "nginx"},
|
||||
{"Pod", "kube-system", "api-server"},
|
||||
},
|
||||
}, {
|
||||
args: args{"[Pod,default,nginx,unexpected]"},
|
||||
want: []filter{
|
||||
{},
|
||||
},
|
||||
}}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := parseKinds(tt.args.in); !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("parseKinds() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_parseIncludeExcludeNamespacesFromNamespacesConfig(t *testing.T) {
|
||||
type args struct {
|
||||
in string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want namespacesConfig
|
||||
wantErr bool
|
||||
}{{
|
||||
args: args{""},
|
||||
wantErr: true,
|
||||
}, {
|
||||
args: args{"null"},
|
||||
}, {
|
||||
args: args{"{}"},
|
||||
}, {
|
||||
args: args{`{"include": "aaa"}`},
|
||||
wantErr: true,
|
||||
}, {
|
||||
args: args{`{"include": ["aaa", "bbb"]}`},
|
||||
want: namespacesConfig{
|
||||
IncludeNamespaces: []string{"aaa", "bbb"},
|
||||
},
|
||||
}, {
|
||||
args: args{`{"exclude": ["aaa", "bbb"]}`},
|
||||
want: namespacesConfig{
|
||||
ExcludeNamespaces: []string{"aaa", "bbb"},
|
||||
},
|
||||
}, {
|
||||
args: args{`{"include": ["aaa", "bbb"], "exclude": ["aaa", "bbb"]}`},
|
||||
want: namespacesConfig{
|
||||
IncludeNamespaces: []string{"aaa", "bbb"},
|
||||
ExcludeNamespaces: []string{"aaa", "bbb"},
|
||||
},
|
||||
}}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := parseIncludeExcludeNamespacesFromNamespacesConfig(tt.args.in)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("parseIncludeExcludeNamespacesFromNamespacesConfig() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("parseIncludeExcludeNamespacesFromNamespacesConfig() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue