Skip to content
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
13 changes: 10 additions & 3 deletions cmd/gdt/cmd/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"github.com/gdt-dev/core/parse"
"github.com/gdt-dev/core/scenario"
_ "github.com/gdt-dev/gdt"
_ "github.com/gdt-dev/kube"
"github.com/samber/lo"
"github.com/spf13/cobra"

Expand Down Expand Up @@ -66,20 +66,27 @@ func doLint(cmd *cobra.Command, args []string) error {
return err
}
if fi.IsDir() {
cli.Vf("checking directory %q ...", path)
cli.Df("checking directory %q ...", path)
dirResults, err := lintDir(path)
if err != nil {
return err
}
results = append(results, dirResults...)
} else {
cli.Vf("checking file %q ...", path)
cli.Df("checking file %q ...", path)
res := lintResult{path: path}
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()

// Need to chdir here so that test scenario may reference files in
// relative directories
if err := os.Chdir(filepath.Dir(path)); err != nil {
return err
}

sc, err := scenario.FromReader(f, scenario.WithPath(path))
if err != nil {
if ep, ok := err.(*parse.Error); ok {
Expand Down
183 changes: 183 additions & 0 deletions cmd/gdt/cmd/run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
package cmd

import (
"fmt"
"os"
"strings"
"time"

gdtcontext "github.com/gdt-dev/core/context"
"github.com/gdt-dev/core/run"
"github.com/gdt-dev/core/scenario"
"github.com/gdt-dev/core/suite"
"github.com/spf13/cobra"

"github.com/gdt-dev/gdt/cmd/gdt/pkg/cli"
)

const (
debugPrefix = "[gdt]"
runUsage = `run <subject> [<subject> ...]`
runDescLong = `Check test scenarios or test suites for parse errors.

The command will run gdt test scenarios or test suites pointed to by <subject>.

<subject> should be a path to a YAML file or a directory containing YAML files.

Returns 0 on if all subject test scenarios complete without failure, 1
otherwise.
`
)

var RunCmd = &cobra.Command{
Use: runUsage,
Short: "run test scenario/suites.",
Long: runDescLong,
Aliases: []string{"exec"},
RunE: doRun,
}

func init() {
RunCmd.Flags().BoolVarP(
&optQuiet,
"quiet",
"q",
false,
optQuietUsage,
)
}

func doRun(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return fmt.Errorf("supply <subject> containing filepath to YAML file or directory.")
}
if cli.CommonOptions.Debug {
cli.CommonOptions.Verbose = true
}
ctx := gdtcontext.New(gdtcontext.WithDebugPrefix(debugPrefix))
run := run.New()
for _, path := range args {
fi, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
return fmt.Errorf("%q not found.", path)
}
return err
}
if fi.IsDir() {
cli.Df("loading suite from directory %q ...", path)
su, err := suite.FromDir(path)
if err != nil {
return err
}
err = su.Run(ctx, run)
if err != nil {
// Run() only returns RuntimeErrors. The `run` object will
// contain assertion failures, which are not considered
// RuntimeErrors.
return err
}
} else {
cli.Df("loading scenario from file %q ...", path)
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()

sc, err := scenario.FromReader(f, scenario.WithPath(path))
if err != nil {
return err
}
err = sc.Run(ctx, run)
if err != nil {
// Run() only returns RuntimeErrors. The `run` object will
// contain assertion failures, which are not considered
// RuntimeErrors.
return err
}
}
}

if !optQuiet {
paths := run.ScenarioPaths()
for _, path := range paths {
if cli.CommonOptions.Verbose {
fmt.Printf("=== RUN: %s\n", path)
}
var scenElapsed time.Duration

results := run.ScenarioResults(path)
scenOK := true
for _, res := range results {
scenElapsed += res.Elapsed()
scenOK = scenOK && res.OK()
printTestUnitResult(res)
}

if !optQuiet {
if scenOK {
if cli.CommonOptions.Verbose {
fmt.Printf("PASS (%s)\n", scenElapsed)
} else {
fmt.Printf("ok\t%s\t%s\n", path, scenElapsed)
}
} else {
fmt.Printf("FAIL\t%s\t%s\n", path, scenElapsed)
}
}
}
}
if !run.OK() {
if cli.CommonOptions.Verbose {
fmt.Println("FAIL")
}
os.Exit(1)
} else {
if cli.CommonOptions.Verbose {
fmt.Println("PASS")
}
}
return nil
}

func printTestUnitResult(r run.TestUnitResult) {
if r.Skipped() {
if cli.CommonOptions.Verbose {
fmt.Printf("--- SKIP: %s (%s)\n", r.Name(), r.Elapsed())
}
} else if r.OK() {
if cli.CommonOptions.Verbose {
fmt.Printf("--- PASS: %s (%s)\n", r.Name(), r.Elapsed())
}
} else {
for _, fail := range r.Failures() {
indentFail := indent(fail.Error(), 1)
if !optQuiet {
fmt.Printf(
"--- FAIL: %s (%s)\n%s\n",
r.Name(), r.Elapsed(), indentFail,
)
}
}
}

if cli.CommonOptions.Debug || !r.OK() {
detail := r.Detail()
if len(detail) > 0 {
cli.HorizontalSectionHeader("detail")
fmt.Printf("%s", r.Detail())
cli.HorizontalBar()
}
}
}

func indent(subject string, level int) string {
indentStr := strings.Repeat(" ", level*4)
b := strings.Builder{}
lines := strings.Split(subject, "\n")
for _, line := range lines {
b.WriteString(fmt.Sprintf("%s%s", indentStr, line))
}
return b.String()
}
49 changes: 43 additions & 6 deletions cmd/gdt/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,58 @@ module github.com/gdt-dev/gdt/cmd/gdt
go 1.24.3

require (
github.com/gdt-dev/core v1.9.11
github.com/gdt-dev/gdt v1.9.9
github.com/gdt-dev/core v1.10.0
github.com/gdt-dev/kube v1.10.1
github.com/samber/lo v1.51.0
github.com/spf13/cobra v1.10.1
github.com/spf13/pflag v1.0.10
golang.org/x/term v0.35.0
)

require (
github.com/PaesslerAG/gval v1.0.0 // indirect
github.com/PaesslerAG/jsonpath v0.1.1 // indirect
github.com/cenkalti/backoff v2.2.1+incompatible // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.12.2 // indirect
github.com/evanphx/json-patch/v5 v5.9.11 // indirect
github.com/fxamacker/cbor/v2 v2.9.0 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-openapi/jsonpointer v0.21.0 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.23.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/google/gnostic-models v0.7.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.3-0.20250322232337-35a7c28c31ee // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/theory/jsonpath v0.10.1 // indirect
github.com/x448/float16 v0.8.4 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
go.yaml.in/yaml/v2 v2.4.2 // indirect
go.yaml.in/yaml/v3 v3.0.4 // indirect
golang.org/x/net v0.38.0 // indirect
golang.org/x/oauth2 v0.27.0 // indirect
golang.org/x/sys v0.36.0 // indirect
golang.org/x/text v0.22.0 // indirect
golang.org/x/text v0.23.0 // indirect
golang.org/x/time v0.9.0 // indirect
google.golang.org/protobuf v1.36.5 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/api v0.34.1 // indirect
k8s.io/apimachinery v0.34.1 // indirect
k8s.io/client-go v0.34.1 // indirect
k8s.io/klog/v2 v2.130.1 // indirect
k8s.io/kube-openapi v0.0.0-20250710124328-f3f2b991d03b // indirect
k8s.io/utils v0.0.0-20250604170112-4c0f3b243397 // indirect
sigs.k8s.io/controller-runtime v0.22.1 // indirect
sigs.k8s.io/json v0.0.0-20241014173422-cfa47c3a1cc8 // indirect
sigs.k8s.io/randfill v1.0.0 // indirect
sigs.k8s.io/structured-merge-diff/v6 v6.3.0 // indirect
sigs.k8s.io/yaml v1.6.0 // indirect
)
Loading
Loading