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

cmd/critest: fix empty ginkgo flag's value issue #930

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 38 additions & 3 deletions cmd/critest/cri_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (

"github.com/onsi/ginkgo/v2"
"github.com/onsi/ginkgo/v2/reporters"
ginkgotypes "github.com/onsi/ginkgo/v2/types"
"github.com/onsi/gomega"

_ "github.com/kubernetes-sigs/cri-tools/pkg/benchmark"
Expand Down Expand Up @@ -135,14 +136,30 @@ func runParallelTestSuite(t *testing.T) {
}
defer os.Remove(tempFileName)

ginkgoArgs := []string{fmt.Sprintf("-nodes=%d", *parallel)}
ginkgoArgs, err := generateGinkgoRunFlags()
if err != nil {
t.Fatalf("Failed to generate ginkgo args: %v", err)
}
ginkgoArgs = append(ginkgoArgs, fmt.Sprintf("--nodes=%d", *parallel))

var testArgs []string
flag.Visit(func(f *flag.Flag) {
// NOTE(fuweid):
//
// The ginkgo has changed the flag var from string to string slice
// for some, like ginkgo.Skip/Focus.
//
// The --skip flag's config is https://github.com/onsi/ginkgo/blob/v2.0.0/types/config.go#L284.
// And the value will be appended to https://github.com/onsi/ginkgo/blob/v2.0.0/types/config.go#L22.
// The flag var is https://github.com/onsi/ginkgo/blob/v2.0.0/types/flags.go#L428,
// which means that we can't get value by interface String().
//
// So we need to skip the "ginkgo.*" flags and use ginkgo API
// to generate the flags.
if strings.HasPrefix(f.Name, "ginkgo.") {
flagName := strings.TrimPrefix(f.Name, "ginkgo.")
ginkgoArgs = append(ginkgoArgs, fmt.Sprintf("-%s=%s", flagName, f.Value.String()))
return
}

if f.Name == parallelFlag || f.Name == benchmarkFlag {
return
}
Expand Down Expand Up @@ -183,3 +200,21 @@ func TestCRISuite(t *testing.T) {
runTestSuite(t)
}
}

// generateGinkgoRunFlags is based on ginkgotypes.GenerateGinkgoTestRunArgs.
//
// Since the GenerateGinkgoTestRunArgs adds "ginkgo." as prefix for each
// flags and we use --nodes instead of ParallelConfigFlags, we need to call
// GenerateFlagArgs to get what we want.
func generateGinkgoRunFlags() ([]string, error) {
suiteConfig, reporterConfig := ginkgo.GinkgoConfiguration()

flags := ginkgotypes.SuiteConfigFlags
flags = flags.CopyAppend(ginkgotypes.ReporterConfigFlags...)

bindings := map[string]interface{}{
"S": &suiteConfig,
"R": &reporterConfig,
}
return ginkgotypes.GenerateFlagArgs(flags, bindings)
}