mirror of
https://github.com/kyverno/kyverno.git
synced 2025-03-31 03:45:17 +00:00
feat: add multiple paths support to cli test command (#8247)
* refactor: cli test loading Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com> * feat: add multiple paths support to cli test command Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com> --------- Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
This commit is contained in:
parent
e411eea188
commit
5615b883db
3 changed files with 29 additions and 14 deletions
|
@ -94,7 +94,7 @@ func testCommandExecute(
|
|||
return rc, fmt.Errorf("unable to create open api controller, %w", err)
|
||||
}
|
||||
// load tests
|
||||
fs, tests, err := loadTests(dirPath, fileName, gitBranch)
|
||||
tests, err := loadTests(dirPath, fileName, gitBranch)
|
||||
if err != nil {
|
||||
fmt.Println()
|
||||
fmt.Println("Error loading tests:")
|
||||
|
@ -124,9 +124,9 @@ func testCommandExecute(
|
|||
for _, p := range tests {
|
||||
resourcePath := filepath.Dir(p.Path)
|
||||
if tests, responses, err := applyPoliciesFromPath(
|
||||
fs,
|
||||
p.Fs,
|
||||
p.Test,
|
||||
fs != nil,
|
||||
p.Fs != nil,
|
||||
resourcePath,
|
||||
rc,
|
||||
openApiManager,
|
||||
|
@ -134,7 +134,7 @@ func testCommandExecute(
|
|||
false,
|
||||
); err != nil {
|
||||
return rc, sanitizederror.NewWithError("failed to apply test command", err)
|
||||
} else if t, err := printTestResult(tests, responses, rc, failOnly, detailedResults, fs, resourcePath); err != nil {
|
||||
} else if t, err := printTestResult(tests, responses, rc, failOnly, detailedResults, p.Fs, resourcePath); err != nil {
|
||||
return rc, sanitizederror.NewWithError("failed to print test result:", err)
|
||||
} else {
|
||||
table.AddFailed(t.RawRows...)
|
||||
|
|
|
@ -7,7 +7,6 @@ import (
|
|||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/go-git/go-billy/v5"
|
||||
"github.com/go-git/go-billy/v5/memfs"
|
||||
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/test"
|
||||
sanitizederror "github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/utils/sanitizedError"
|
||||
|
@ -15,19 +14,29 @@ import (
|
|||
gitutils "github.com/kyverno/kyverno/pkg/utils/git"
|
||||
)
|
||||
|
||||
func loadTests(dirPath []string, fileName string, gitBranch string) (billy.Filesystem, test.TestCases, error) {
|
||||
func loadTests(paths []string, fileName string, gitBranch string) (test.TestCases, error) {
|
||||
var tests []test.TestCase
|
||||
for _, path := range paths {
|
||||
t, err := loadTest(path, fileName, gitBranch)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tests = append(tests, t...)
|
||||
}
|
||||
return tests, nil
|
||||
}
|
||||
|
||||
func loadTest(path string, fileName string, gitBranch string) (test.TestCases, error) {
|
||||
var tests []test.TestCase
|
||||
// TODO support multiple paths
|
||||
path := dirPath[0]
|
||||
if source.IsGit(path) {
|
||||
fs := memfs.New()
|
||||
gitURL, err := url.Parse(path)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
return nil, err
|
||||
} else {
|
||||
pathElems := strings.Split(gitURL.Path[1:], "/")
|
||||
if len(pathElems) <= 1 {
|
||||
return nil, nil, fmt.Errorf("invalid URL path %s - expected https://github.com/:owner/:repository/:branch (without --git-branch flag) OR https://github.com/:owner/:repository/:directory (with --git-branch flag)", gitURL.Path)
|
||||
return nil, fmt.Errorf("invalid URL path %s - expected https://github.com/:owner/:repository/:branch (without --git-branch flag) OR https://github.com/:owner/:repository/:directory (with --git-branch flag)", gitURL.Path)
|
||||
}
|
||||
gitURL.Path = strings.Join([]string{pathElems[0], pathElems[1]}, "/")
|
||||
repoURL := gitURL.String()
|
||||
|
@ -52,11 +61,11 @@ func loadTests(dirPath []string, fileName string, gitBranch string) (billy.Files
|
|||
}
|
||||
}
|
||||
if _, err := gitutils.Clone(repoURL, fs, gitBranch); err != nil {
|
||||
return nil, nil, fmt.Errorf("Error: failed to clone repository \nCause: %s\n", err)
|
||||
return nil, fmt.Errorf("Error: failed to clone repository \nCause: %s\n", err)
|
||||
}
|
||||
yamlFiles, err := gitutils.ListYamls(fs, gitPathToYamls)
|
||||
if err != nil {
|
||||
return nil, nil, sanitizederror.NewWithError("failed to list YAMLs in repository", err)
|
||||
return nil, sanitizederror.NewWithError("failed to list YAMLs in repository", err)
|
||||
}
|
||||
sort.Strings(yamlFiles)
|
||||
for _, yamlFilePath := range yamlFiles {
|
||||
|
@ -66,9 +75,9 @@ func loadTests(dirPath []string, fileName string, gitBranch string) (billy.Files
|
|||
}
|
||||
}
|
||||
}
|
||||
return fs, tests, nil
|
||||
return tests, nil
|
||||
} else {
|
||||
tests, err := test.LoadTests(path, fileName)
|
||||
return nil, tests, err
|
||||
return tests, err
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,6 +12,7 @@ import (
|
|||
|
||||
type TestCase struct {
|
||||
Path string
|
||||
Fs billy.Filesystem
|
||||
Test *api.Test
|
||||
Err error
|
||||
}
|
||||
|
@ -59,6 +60,7 @@ func LoadTest(fs billy.Filesystem, path string) TestCase {
|
|||
if err != nil {
|
||||
return TestCase{
|
||||
Path: path,
|
||||
Fs: fs,
|
||||
Err: err,
|
||||
}
|
||||
}
|
||||
|
@ -66,6 +68,7 @@ func LoadTest(fs billy.Filesystem, path string) TestCase {
|
|||
if err != nil {
|
||||
return TestCase{
|
||||
Path: path,
|
||||
Fs: fs,
|
||||
Err: err,
|
||||
}
|
||||
}
|
||||
|
@ -75,6 +78,7 @@ func LoadTest(fs billy.Filesystem, path string) TestCase {
|
|||
if err != nil {
|
||||
return TestCase{
|
||||
Path: path,
|
||||
Fs: fs,
|
||||
Err: err,
|
||||
}
|
||||
}
|
||||
|
@ -84,11 +88,13 @@ func LoadTest(fs billy.Filesystem, path string) TestCase {
|
|||
if err := yaml.UnmarshalStrict(yamlBytes, &test); err != nil {
|
||||
return TestCase{
|
||||
Path: path,
|
||||
Fs: fs,
|
||||
Err: err,
|
||||
}
|
||||
}
|
||||
return TestCase{
|
||||
Path: path,
|
||||
Fs: fs,
|
||||
Test: &test,
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue