Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): allow selecting resource type when running multiple files #4020

Merged
merged 1 commit into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 37 additions & 5 deletions cli/cloud/runner/multifile_orchestrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"os"
"strings"
"sync"

"github.com/kubeshop/tracetest/cli/formatters"
Expand Down Expand Up @@ -209,27 +210,58 @@ func (o orchestrator) Run(ctx context.Context, opts RunOptions, outputFormat str
return exitCode, nil
}

func (o orchestrator) getDefinitionFiles(file []string) ([]string, error) {
files := make([]string, 0)

func (o orchestrator) getDefinitionFiles(file []string, resourceName string) (files []string, err error) {
for _, f := range file {
files = append(files, fileutil.ReadDirFileNames(f)...)
}

if resourceName != "" {
o.logger.Debug("filtering files by resource name", zap.String("resourceName", resourceName))
files, err = o.filterFilesByResourceName(files, resourceName)
if err != nil {
return nil, fmt.Errorf("cannot filter files by resource name: %w", err)
}
}

o.logger.Debug("selected files", zap.Strings("files", files))

return files, nil
}

func (o orchestrator) filterFilesByResourceName(files []string, resourceName string) ([]string, error) {
filteredFiles := make([]string, 0)

for _, f := range files {
o.logger.Debug("reading file", zap.String("file", f))
file, err := fileutil.Read(f)
if err != nil {
return nil, fmt.Errorf("cannot read file: %w", err)
}

// EqualFold means compare case insensitive
if ft := file.Type(); !strings.EqualFold(ft, resourceName) {
o.logger.Debug("skipping file", zap.String("type", ft), zap.String("file", f))
continue
}

o.logger.Debug("file matches", zap.String("file", f))
filteredFiles = append(filteredFiles, f)
}

return filteredFiles, nil
}

func (o orchestrator) runByFiles(ctx context.Context, opts RunOptions, resourceFetcher runner.ResourceFetcher, vars *varset.VarSets, varsID string, runGroupID string) ([]any, []runner.RunResult, error) {
resources := make([]any, 0)
runsResults := make([]runner.RunResult, 0)
var mainErr error

hasDefinitionFilesDefined := opts.DefinitionFiles != nil && len(opts.DefinitionFiles) > 0
hasDefinitionFilesDefined := len(opts.DefinitionFiles) > 0
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not related, but it was causing an annoying warning

Screenshot 2024-09-17 at 13 34 15

if !hasDefinitionFilesDefined {
return resources, runsResults, fmt.Errorf("no definition files defined")
}

definitionFiles, err := o.getDefinitionFiles(opts.DefinitionFiles)
definitionFiles, err := o.getDefinitionFiles(opts.DefinitionFiles, opts.ResourceName)
if err != nil {
return resources, runsResults, fmt.Errorf("cannot get definition files: %w", err)
}
Expand Down
1 change: 0 additions & 1 deletion cli/pkg/fileutil/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ func ReadDirFileNames(path string) []string {

var result []string
for _, file := range files {
// TODO: add validation for file extensions, tracetest runnable definitions (?)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should not be handled here, and this PR addresses this todo

if file.IsDir() {
result = append(result, ReadDirFileNames(filepath.Join(path, file.Name()))...)
} else {
Expand Down
Loading