mirror of
https://github.com/kyverno/kyverno.git
synced 2025-03-06 16:06:56 +00:00
* fix: validate the YAML test file syntactically Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com> * schema validation Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com> * unit tests Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com> * fix Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com> --------- Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
63 lines
1.1 KiB
Go
63 lines
1.1 KiB
Go
package test
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
|
|
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/test/api"
|
|
)
|
|
|
|
func Test_loadTest(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
data []byte
|
|
want *api.Test
|
|
wantErr bool
|
|
}{{
|
|
name: "invalid schema",
|
|
data: []byte(`
|
|
- name: mytest
|
|
policies:
|
|
- pol.yaml
|
|
resources:
|
|
- pod.yaml
|
|
results:
|
|
- policy: evil-policy-match-foreign-pods
|
|
rule: evil-validation
|
|
resource: nginx
|
|
status: pass
|
|
`),
|
|
want: nil,
|
|
wantErr: true,
|
|
}, {
|
|
name: "unknown field",
|
|
data: []byte(`
|
|
name: mytest
|
|
policies:
|
|
- pol.yaml
|
|
resources:
|
|
- pod.yaml
|
|
results:
|
|
- policy: evil-policy-match-foreign-pods
|
|
rule: evil-validation
|
|
resource: nginx
|
|
foo: bar
|
|
result: pass
|
|
`),
|
|
want: nil,
|
|
wantErr: true,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got, err := loadTest(tt.data)
|
|
if (err != nil) != tt.wantErr {
|
|
t.Errorf("loadTest() error = %v, wantErr %v", err, tt.wantErr)
|
|
return
|
|
}
|
|
if !reflect.DeepEqual(got, tt.want) {
|
|
t.Errorf("loadTest() = %v, want %v", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|