2023-09-04 13:36:34 +02:00
|
|
|
package test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/go-git/go-billy/v5"
|
2023-09-17 22:50:17 +02:00
|
|
|
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/apis/v1alpha1"
|
2023-09-04 13:36:34 +02:00
|
|
|
"k8s.io/apimachinery/pkg/util/yaml"
|
|
|
|
)
|
|
|
|
|
|
|
|
func LoadTests(dirPath string, fileName string) (TestCases, error) {
|
|
|
|
return loadLocalTest(filepath.Clean(dirPath), fileName)
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadLocalTest(path string, fileName string) (TestCases, error) {
|
|
|
|
var tests []TestCase
|
|
|
|
files, err := os.ReadDir(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, file := range files {
|
|
|
|
if file.IsDir() {
|
|
|
|
ps, err := loadLocalTest(filepath.Join(path, file.Name()), fileName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
tests = append(tests, ps...)
|
|
|
|
} else if file.Name() == fileName {
|
|
|
|
tests = append(tests, LoadTest(nil, filepath.Join(path, fileName)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return tests, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func LoadTest(fs billy.Filesystem, path string) TestCase {
|
|
|
|
var yamlBytes []byte
|
|
|
|
if fs != nil {
|
|
|
|
file, err := fs.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return TestCase{
|
|
|
|
Path: path,
|
2023-09-04 22:27:20 +02:00
|
|
|
Fs: fs,
|
2023-09-04 13:36:34 +02:00
|
|
|
Err: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
data, err := io.ReadAll(file)
|
|
|
|
if err != nil {
|
|
|
|
return TestCase{
|
|
|
|
Path: path,
|
2023-09-04 22:27:20 +02:00
|
|
|
Fs: fs,
|
2023-09-04 13:36:34 +02:00
|
|
|
Err: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
yamlBytes = data
|
|
|
|
} else {
|
|
|
|
data, err := os.ReadFile(path) // #nosec G304
|
|
|
|
if err != nil {
|
|
|
|
return TestCase{
|
|
|
|
Path: path,
|
2023-09-04 22:27:20 +02:00
|
|
|
Fs: fs,
|
2023-09-04 13:36:34 +02:00
|
|
|
Err: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
yamlBytes = data
|
|
|
|
}
|
2023-09-17 22:50:17 +02:00
|
|
|
var test v1alpha1.Test
|
2023-09-04 13:36:34 +02:00
|
|
|
if err := yaml.UnmarshalStrict(yamlBytes, &test); err != nil {
|
|
|
|
return TestCase{
|
|
|
|
Path: path,
|
2023-09-04 22:27:20 +02:00
|
|
|
Fs: fs,
|
2023-09-04 13:36:34 +02:00
|
|
|
Err: err,
|
|
|
|
}
|
|
|
|
}
|
2024-12-12 14:39:24 +05:45
|
|
|
cleanTest(&test)
|
2023-09-04 13:36:34 +02:00
|
|
|
return TestCase{
|
|
|
|
Path: path,
|
2023-09-04 22:27:20 +02:00
|
|
|
Fs: fs,
|
2023-09-04 13:36:34 +02:00
|
|
|
Test: &test,
|
|
|
|
}
|
|
|
|
}
|
2024-12-12 14:39:24 +05:45
|
|
|
|
|
|
|
func cleanTest(test *v1alpha1.Test) {
|
|
|
|
test.Policies = removeDuplicateStrings(test.Policies)
|
|
|
|
test.Resources = removeDuplicateStrings(test.Resources)
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeDuplicateStrings(strings []string) []string {
|
|
|
|
seen := make(map[string]struct{})
|
|
|
|
var result []string
|
|
|
|
|
|
|
|
for _, str := range strings {
|
|
|
|
if _, exists := seen[str]; !exists {
|
|
|
|
seen[str] = struct{}{}
|
|
|
|
result = append(result, str)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|