Skip to content

Commit

Permalink
✨ add test runner to CLI
Browse files Browse the repository at this point in the history
Signed-off-by: Pranav Gaikwad <[email protected]>
  • Loading branch information
pranavgaikwad committed Feb 28, 2024
1 parent 59ee33a commit 9c845e9
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func init() {
logger := logrusr.New(logrusLog)
rootCmd.AddCommand(NewTransformCommand(logger))
rootCmd.AddCommand(NewAnalyzeCmd(logger))
rootCmd.AddCommand(NewTestCommand(logger))
rootCmd.AddCommand(NewVersionCommand())
}

Expand Down
1 change: 1 addition & 0 deletions cmd/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Config struct {
PodmanBinary string `env:"PODMAN_BIN" default:"/usr/bin/podman"`
RunnerImage string `env:"RUNNER_IMG" default:"quay.io/konveyor/kantra"`
JvmMaxMem string `env:"JVM_MAX_MEM" default:""`
RunLocal bool `env:"RUN_LOCAL"`
}

func (c *Config) Load() error {
Expand Down
52 changes: 52 additions & 0 deletions cmd/test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package cmd

import (
"os"

"github.com/go-logr/logr"
"github.com/konveyor-ecosystem/kantra/pkg/testing"
"github.com/spf13/cobra"
)

type testCommand struct {
testFilterString string
baseProviderSettings string
}

func NewTestCommand(log logr.Logger) *cobra.Command {
testCmd := &testCommand{}

testCobraCommand := &cobra.Command{
Use: "test",
Short: "Test YAML rules",
RunE: func(cmd *cobra.Command, args []string) error {
var testFilter testing.TestsFilter
if testCmd.testFilterString != "" {
testFilter = testing.NewInlineNameBasedFilter(testCmd.testFilterString)
}
tests, err := testing.Parse(args, testFilter)
if err != nil {
log.Error(err, "failed parsing rulesets")
return err
}
if len(tests) == 0 {
log.Info("no tests found")
return nil
}
results, err := testing.NewRunner().Run(tests, testing.TestOptions{
RunLocal: Settings.RunLocal,
ContainerImage: Settings.RunnerImage,
ProgressPrinter: testing.PrintProgress,
})
testing.PrintSummary(os.Stdout, results)
// TODO (pgaikwad): in future, return an error so parent can return non-zero exit code
if err != nil {
log.Error(err, "failed running tests")
}
return nil
},
}
testCobraCommand.Flags().StringVarP(&testCmd.testFilterString, "test-filter", "t", "", "filter tests / testcases by their names")
testCobraCommand.Flags().StringVarP(&testCmd.baseProviderSettings, "base-provider-settings", "b", "", "path to a provider settings file the runner will use as base")
return testCobraCommand
}
2 changes: 1 addition & 1 deletion pkg/testing/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ func runLocal(logFile io.Writer, dir string, analysisParams AnalysisParams) (str
cmd := exec.Command("konveyor-analyzer", args...)
cmd.Stdout = logFile
cmd.Stderr = logFile
return fmt.Sprintf("konveyor-analyzer", strings.Join(args, " ")), cmd.Run()
return fmt.Sprintf("konveyor-analyzer %s", strings.Join(args, " ")), cmd.Run()
}

func runInContainer(consoleLogger logr.Logger, image string, logFile io.Writer, volumes map[string]string, analysisParams AnalysisParams) (string, error) {
Expand Down

0 comments on commit 9c845e9

Please sign in to comment.