Skip to content
Draft
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
67 changes: 67 additions & 0 deletions cmd/dmt/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import (
"github.com/deckhouse/dmt/internal/flags"
"github.com/deckhouse/dmt/internal/fsutils"
"github.com/deckhouse/dmt/internal/logger"
"github.com/deckhouse/dmt/internal/test"
"github.com/deckhouse/dmt/internal/test/conversions"
"github.com/deckhouse/dmt/internal/version"
)

Expand Down Expand Up @@ -137,11 +139,31 @@ func execute() {
},
}

testCmd := &cobra.Command{
Use: "test <module-path>",
Short: "run tests for Deckhouse modules",
Long: `Run various tests for Deckhouse modules.

Currently supported test types:
- conversions: tests for config version conversions

To run conversion tests, create openapi/conversions/conversions_test.yaml in your module.

Example:
dmt test ./my-module
dmt test /path/to/module --type conversions`,
Args: cobra.ExactArgs(1),
SilenceUsage: true,
RunE: testCmdFunc,
}

lintCmd.Flags().AddFlagSet(flags.InitLintFlagSet())
bootstrapCmd.Flags().AddFlagSet(flags.InitBootstrapFlagSet())
testCmd.Flags().AddFlagSet(flags.InitTestFlagSet())

rootCmd.AddCommand(lintCmd)
rootCmd.AddCommand(bootstrapCmd)
rootCmd.AddCommand(testCmd)
rootCmd.Flags().AddFlagSet(flags.InitDefaultFlagSet())

err := rootCmd.Execute()
Expand Down Expand Up @@ -190,3 +212,48 @@ func runLintMultiple(dirs []string) error {

return nil
}

func testCmdFunc(_ *cobra.Command, args []string) error {
modulePath, err := fsutils.ExpandDir(args[0])
if err != nil {
return fmt.Errorf("error expanding path %s: %w", args[0], err)
}

return runTests(modulePath)
}

func runTests(dir string) error {
logger.InfoF("Running tests for: %s", dir)

// Create test runner and register testers
runner := test.NewRunner()
runner.Register(conversions.NewTester())

// Build test options
opts := test.RunOptions{
ModulePath: dir,
Verbose: flags.TestVerbose,
}

// Filter by test type if specified
if flags.TestType != "" {
opts.TestTypes = []test.TestType{test.TestType(flags.TestType)}
}

// Run tests
summary, err := runner.Run(opts)
if err != nil {
logger.ErrorF("Test execution failed: %v", err)
return err
}

// Print results
test.PrintSummary(summary)

// Return error if any tests failed
if summary.FailedTests > 0 {
return errors.New("some tests failed")
}

return nil
}
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require (
github.com/golang/snappy v0.0.4
github.com/google/go-containerregistry v0.20.2
github.com/iancoleman/strcase v0.3.0
github.com/itchyny/gojq v0.12.18
github.com/kyokomi/emoji v2.2.4+incompatible
github.com/mitchellh/go-homedir v1.1.0
github.com/mitchellh/go-wordwrap v1.0.1
Expand Down Expand Up @@ -99,6 +100,7 @@ require (
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/huandu/xstrings v1.5.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/itchyny/timefmt-go v0.1.7 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/jpillora/backoff v1.0.0 // indirect
Expand Down Expand Up @@ -152,7 +154,7 @@ require (
golang.org/x/net v0.40.0 // indirect
golang.org/x/oauth2 v0.28.0 // indirect
golang.org/x/sync v0.16.0 // indirect
golang.org/x/sys v0.33.0 // indirect
golang.org/x/sys v0.38.0 // indirect
golang.org/x/term v0.32.0 // indirect
golang.org/x/text v0.26.0 // indirect
golang.org/x/time v0.9.0 // indirect
Expand Down
8 changes: 6 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,10 @@ github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/ionos-cloud/sdk-go/v6 v6.3.2 h1:2mUmrZZz6cPyT9IRX0T8fBLc/7XU/eTxP2Y5tS7/09k=
github.com/ionos-cloud/sdk-go/v6 v6.3.2/go.mod h1:SXrO9OGyWjd2rZhAhEpdYN6VUAODzzqRdqA9BCviQtI=
github.com/itchyny/gojq v0.12.18 h1:gFGHyt/MLbG9n6dqnvlliiya2TaMMh6FFaR2b1H6Drc=
github.com/itchyny/gojq v0.12.18/go.mod h1:4hPoZ/3lN9fDL1D+aK7DY1f39XZpY9+1Xpjz8atrEkg=
github.com/itchyny/timefmt-go v0.1.7 h1:xyftit9Tbw+Dc/huSSPJaEmX1TVL8lw5vxjJLK4GMMA=
github.com/itchyny/timefmt-go v0.1.7/go.mod h1:5E46Q+zj7vbTgWY8o5YkMeYb4I6GeWLFnetPy5oBrAI=
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
Expand Down Expand Up @@ -438,8 +442,8 @@ golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=
golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
golang.org/x/sys v0.38.0 h1:3yZWxaJjBmCWXqhN1qh02AkOnCQ1poK6oF+a7xWL6Gc=
golang.org/x/sys v0.38.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
golang.org/x/term v0.32.0 h1:DR4lr0TjUs3epypdhTOkMmuF5CDFJ/8pOnbzMZPQ7bg=
golang.org/x/term v0.32.0/go.mod h1:uZG1FhGx848Sqfsq4/DlJr3xGGsYMu/L5GW4abiaEPQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down
15 changes: 15 additions & 0 deletions internal/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ var (
BootstrapModule string
)

var (
TestVerbose bool
TestType string
)

func InitDefaultFlagSet() *pflag.FlagSet {
defaults := pflag.NewFlagSet("defaults for all commands", pflag.ExitOnError)

Expand Down Expand Up @@ -108,3 +113,13 @@ func InitBootstrapFlagSet() *pflag.FlagSet {

return bootstrap
}

func InitTestFlagSet() *pflag.FlagSet {
testFlags := pflag.NewFlagSet("test", pflag.ContinueOnError)

testFlags.BoolVarP(&TestVerbose, "verbose", "v", false, "verbose output")
testFlags.StringVarP(&TestType, "type", "t", "", "test type to run (e.g., conversions)")
testFlags.StringVarP(&LogLevel, "log-level", "l", "INFO", "log-level [DEBUG | INFO | WARN | ERROR]")

return testFlags
}
Loading