k8s.io/perf-tests/clusterloader2@v0.0.0-20240304094227-64bdb12da87e/pkg/test/simple_reporter.go (about)

     1  /*
     2  Copyright 2020 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package test
    18  
    19  import (
    20  	"fmt"
    21  	"time"
    22  
    23  	ginkgoconfig "github.com/onsi/ginkgo/config"
    24  	ginkgoreporters "github.com/onsi/ginkgo/reporters"
    25  	ginkgotypes "github.com/onsi/ginkgo/types"
    26  	"k8s.io/perf-tests/clusterloader2/pkg/errors"
    27  )
    28  
    29  type simpleReporter struct {
    30  	testName       string
    31  	suiteStart     time.Time
    32  	junitReporter  *ginkgoreporters.JUnitReporter
    33  	suiteSummary   *ginkgotypes.SuiteSummary
    34  	stepsSummaries []*ginkgotypes.SpecSummary
    35  }
    36  
    37  func CreateSimpleReporter(reportFilename, testSuiteDescription string) Reporter {
    38  	return &simpleReporter{
    39  		junitReporter: ginkgoreporters.NewJUnitReporter(reportFilename),
    40  		suiteSummary: &ginkgotypes.SuiteSummary{
    41  			SuiteDescription: testSuiteDescription,
    42  		},
    43  	}
    44  }
    45  
    46  func (str *simpleReporter) SetTestName(name string) {
    47  	str.testName = name
    48  }
    49  
    50  func (str *simpleReporter) GetNumberOfFailedTestItems() int {
    51  	return str.suiteSummary.NumberOfFailedSpecs
    52  }
    53  
    54  func (str *simpleReporter) BeginTestSuite() {
    55  	str.junitReporter.SpecSuiteWillBegin(ginkgoconfig.GinkgoConfig, str.suiteSummary)
    56  	str.suiteStart = time.Now()
    57  }
    58  
    59  func (str *simpleReporter) EndTestSuite() {
    60  	str.suiteSummary.RunTime = time.Since(str.suiteStart)
    61  	str.junitReporter.SpecSuiteDidEnd(str.suiteSummary)
    62  }
    63  
    64  func (str *simpleReporter) ReportTestStepFinish(duration time.Duration, stepName string, errList *errors.ErrorList) {
    65  	stepSummary := &ginkgotypes.SpecSummary{
    66  		ComponentTexts: []string{str.suiteSummary.SuiteDescription, fmt.Sprintf("%s: %s", str.testName, stepName)},
    67  		RunTime:        duration,
    68  	}
    69  	str.handleSummary(stepSummary, errList)
    70  	str.stepsSummaries = append(str.stepsSummaries, stepSummary)
    71  }
    72  
    73  func (str *simpleReporter) ReportTestStep(result *StepResult) {
    74  	for _, subtestResult := range result.getAllResults() {
    75  		str.ReportTestStepFinish(subtestResult.duration, subtestResult.name, subtestResult.err)
    76  	}
    77  }
    78  
    79  func (str *simpleReporter) ReportTestFinish(duration time.Duration, testConfigPath string, errList *errors.ErrorList) {
    80  	testSummary := &ginkgotypes.SpecSummary{
    81  		ComponentTexts: []string{str.suiteSummary.SuiteDescription, fmt.Sprintf("%s overall (%s)", str.testName, testConfigPath)},
    82  		RunTime:        duration,
    83  	}
    84  	str.handleSummary(testSummary, errList)
    85  
    86  	str.junitReporter.SpecDidComplete(testSummary)
    87  	for _, stepSummary := range str.stepsSummaries {
    88  		str.junitReporter.SpecDidComplete(stepSummary)
    89  	}
    90  	str.stepsSummaries = nil
    91  }
    92  
    93  func (str *simpleReporter) handleSummary(summary *ginkgotypes.SpecSummary, errList *errors.ErrorList) {
    94  	if errList.IsEmpty() {
    95  		summary.State = ginkgotypes.SpecStatePassed
    96  	} else {
    97  		summary.State = ginkgotypes.SpecStateFailed
    98  		summary.Failure = ginkgotypes.SpecFailure{
    99  			Message: errList.String(),
   100  		}
   101  		str.suiteSummary.NumberOfFailedSpecs++
   102  	}
   103  }