Migrate ProgressReporter to Ginkgo V2

Signed-off-by: Dave Chen <dave.chen@arm.com>
This commit is contained in:
Dave Chen 2022-05-02 15:42:04 +08:00
parent fd4b5b629b
commit 46a3954ba5
3 changed files with 50 additions and 55 deletions

View File

@ -74,9 +74,12 @@ const (
namespaceCleanupTimeout = 15 * time.Minute namespaceCleanupTimeout = 15 * time.Minute
) )
var progressReporter = &e2ereporters.ProgressReporter{}
var _ = ginkgo.SynchronizedBeforeSuite(func() []byte { var _ = ginkgo.SynchronizedBeforeSuite(func() []byte {
// Reference common test to make the import valid. // Reference common test to make the import valid.
commontest.CurrentSuite = commontest.E2E commontest.CurrentSuite = commontest.E2E
progressReporter.SetStartMsg()
setupSuite() setupSuite()
return nil return nil
}, func(data []byte) { }, func(data []byte) {
@ -85,6 +88,7 @@ var _ = ginkgo.SynchronizedBeforeSuite(func() []byte {
}) })
var _ = ginkgo.SynchronizedAfterSuite(func() { var _ = ginkgo.SynchronizedAfterSuite(func() {
progressReporter.SetEndMsg()
CleanupSuite() CleanupSuite()
}, func() { }, func() {
AfterSuiteActions() AfterSuiteActions()
@ -98,7 +102,7 @@ var _ = ginkgo.SynchronizedAfterSuite(func() {
func RunE2ETests(t *testing.T) { func RunE2ETests(t *testing.T) {
logs.InitLogs() logs.InitLogs()
defer logs.FlushLogs() defer logs.FlushLogs()
progressReporter = e2ereporters.NewProgressReporter(framework.TestContext.ProgressReportURL)
gomega.RegisterFailHandler(framework.Fail) gomega.RegisterFailHandler(framework.Fail)
// Disable skipped tests unless they are explicitly requested. // Disable skipped tests unless they are explicitly requested.
if config.GinkgoConfig.FocusString == "" && config.GinkgoConfig.SkipString == "" { if config.GinkgoConfig.FocusString == "" && config.GinkgoConfig.SkipString == "" {
@ -117,9 +121,6 @@ func RunE2ETests(t *testing.T) {
} }
} }
// Stream the progress to stdout and optionally a URL accepting progress updates.
r = append(r, e2ereporters.NewProgressReporter(framework.TestContext.ProgressReportURL))
klog.Infof("Starting e2e run %q on Ginkgo node %d", framework.RunID, config.GinkgoConfig.ParallelNode) klog.Infof("Starting e2e run %q on Ginkgo node %d", framework.RunID, config.GinkgoConfig.ParallelNode)
ginkgo.RunSpecsWithDefaultAndCustomReporters(t, "Kubernetes e2e suite", r) ginkgo.RunSpecsWithDefaultAndCustomReporters(t, "Kubernetes e2e suite", r)
} }

View File

@ -139,6 +139,10 @@ func TestE2E(t *testing.T) {
RunE2ETests(t) RunE2ETests(t)
} }
var _ = ginkgo.ReportAfterEach(func(report ginkgo.SpecReport) {
progressReporter.ProcessSpecReport(report)
})
var _ = ginkgo.ReportAfterSuite("Kubernetes e2e suite report", func(report ginkgo.Report) { var _ = ginkgo.ReportAfterSuite("Kubernetes e2e suite report", func(report ginkgo.Report) {
var err error var err error
// The DetailsRepoerter will output details about every test (name, files, lines, etc) which helps // The DetailsRepoerter will output details about every test (name, files, lines, etc) which helps

View File

@ -25,19 +25,19 @@ import (
"strings" "strings"
"time" "time"
"k8s.io/klog/v2" "github.com/onsi/ginkgo/v2"
"github.com/onsi/ginkgo/v2/config"
"github.com/onsi/ginkgo/v2/types" "github.com/onsi/ginkgo/v2/types"
"k8s.io/klog/v2"
) )
// ProgressReporter is a ginkgo reporter which tracks the total number of tests to be run/passed/failed/skipped. // ProgressReporter is a ginkgo reporter which tracks the total number of tests to be run/passed/failed/skipped.
// As new tests are completed it updates the values and prints them to stdout and optionally, sends the updates // As new tests are completed it updates the values and prints them to stdout and optionally, sends the updates
// to the configured URL. // to the configured URL.
// TODO: Number of test specs is not available now, we can add it back when this is fixed in the Ginkgo V2.
// pls see: https://github.com/kubernetes/kubernetes/issues/109744
type ProgressReporter struct { type ProgressReporter struct {
LastMsg string `json:"msg"` LastMsg string `json:"msg"`
TestsTotal int `json:"total"`
TestsCompleted int `json:"completed"` TestsCompleted int `json:"completed"`
TestsSkipped int `json:"skipped"` TestsSkipped int `json:"skipped"`
TestsFailed int `json:"failed"` TestsFailed int `json:"failed"`
@ -62,47 +62,8 @@ func NewProgressReporter(progressReportURL string) *ProgressReporter {
return rep return rep
} }
// SpecSuiteWillBegin is invoked by ginkgo when the suite is about to start and is the first point in which we can // SendUpdates serializes the current progress and prints it to stdout and also posts it to the configured endpoint if set.
// antipate the number of tests which will be run. func (reporter *ProgressReporter) SendUpdates() {
func (reporter *ProgressReporter) SpecSuiteWillBegin(cfg config.GinkgoConfigType, summary *types.SuiteSummary) {
reporter.TestsTotal = summary.NumberOfSpecsThatWillBeRun
reporter.LastMsg = "Test Suite starting"
reporter.sendUpdates()
}
// SpecSuiteDidEnd is the last method invoked by Ginkgo after all the specs are run.
func (reporter *ProgressReporter) SpecSuiteDidEnd(summary *types.SuiteSummary) {
reporter.LastMsg = "Test Suite completed"
reporter.sendUpdates()
}
// SpecDidComplete is invoked by Ginkgo each time a spec is completed (including skipped specs).
func (reporter *ProgressReporter) SpecDidComplete(specSummary *types.SpecSummary) {
testname := strings.Join(specSummary.ComponentTexts[1:], " ")
switch specSummary.State {
case types.SpecStateFailed:
if len(specSummary.ComponentTexts) > 0 {
reporter.Failures = append(reporter.Failures, testname)
} else {
reporter.Failures = append(reporter.Failures, "Unknown test name")
}
reporter.TestsFailed++
reporter.LastMsg = fmt.Sprintf("FAILED %v", testname)
case types.SpecStatePassed:
reporter.TestsCompleted++
reporter.LastMsg = fmt.Sprintf("PASSED %v", testname)
case types.SpecStateSkipped:
reporter.TestsSkipped++
return
default:
return
}
reporter.sendUpdates()
}
// sendUpdates serializes the current progress and prints it to stdout and also posts it to the configured endpoint if set.
func (reporter *ProgressReporter) sendUpdates() {
b := reporter.serialize() b := reporter.serialize()
fmt.Println(string(b)) fmt.Println(string(b))
go reporter.postProgressToURL(b) go reporter.postProgressToURL(b)
@ -143,11 +104,40 @@ func (reporter *ProgressReporter) serialize() []byte {
return b return b
} }
// SpecWillRun is implemented as a noop to satisfy the reporter interface for ginkgo. func (reporter *ProgressReporter) SetStartMsg() {
func (reporter *ProgressReporter) SpecWillRun(specSummary *types.SpecSummary) {} reporter.LastMsg = "Test Suite starting"
reporter.SendUpdates()
}
// BeforeSuiteDidRun is implemented as a noop to satisfy the reporter interface for ginkgo. // ProcessSpecReport summarizes the report state and sends the state to the configured endpoint if set.
func (reporter *ProgressReporter) BeforeSuiteDidRun(setupSummary *types.SetupSummary) {} func (reporter *ProgressReporter) ProcessSpecReport(report ginkgo.SpecReport) {
testName := strings.Join(report.ContainerHierarchyTexts, " ")
if len(report.LeafNodeText) > 0 {
testName = testName + " " + report.LeafNodeText
}
switch report.State {
case types.SpecStateFailed:
if len(testName) > 0 {
reporter.Failures = append(reporter.Failures, testName)
} else {
reporter.Failures = append(reporter.Failures, "Unknown test name")
}
reporter.TestsFailed++
reporter.LastMsg = fmt.Sprintf("FAILED %v", testName)
case types.SpecStatePassed:
reporter.TestsCompleted++
reporter.LastMsg = fmt.Sprintf("PASSED %v", testName)
case types.SpecStateSkipped:
reporter.TestsSkipped++
return
default:
return
}
// AfterSuiteDidRun is implemented as a noop to satisfy the reporter interface for ginkgo. reporter.SendUpdates()
func (reporter *ProgressReporter) AfterSuiteDidRun(setupSummary *types.SetupSummary) {} }
func (reporter *ProgressReporter) SetEndMsg() {
reporter.LastMsg = "Test Suite completed"
reporter.SendUpdates()
}