|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + _ "embed" |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + "github.com/carlmjohnson/versioninfo" |
| 8 | + junit "github.com/joshdk/go-junit" |
| 9 | + "github.com/pkg/errors" |
| 10 | + log "github.com/sirupsen/logrus" |
| 11 | + "github.com/stackrox/junit2jira/pkg/testcase" |
| 12 | + "os" |
| 13 | +) |
| 14 | + |
| 15 | +const totalRunsLimit = 30 |
| 16 | + |
| 17 | +const errDescAboveThreshold = "allowed flake ratio for test is above threshold" |
| 18 | +const errDescGetRatio = "get ratio for test failed" |
| 19 | +const errDescNoFailedTests = "no failed tests to process" |
| 20 | +const errDescNoMatch = "there is no match in allowed flake tests" |
| 21 | +const errDescShortHistory = "total runs for test is under history count threshold" |
| 22 | + |
| 23 | +type flakeCheckerParams struct { |
| 24 | + junitReportsDir string |
| 25 | + configFile string |
| 26 | + |
| 27 | + jobName string |
| 28 | + orchestrator string |
| 29 | + |
| 30 | + dryRun bool |
| 31 | +} |
| 32 | + |
| 33 | +func main() { |
| 34 | + var debug bool |
| 35 | + var err error |
| 36 | + |
| 37 | + p := flakeCheckerParams{} |
| 38 | + flag.StringVar(&p.junitReportsDir, "junit-reports-dir", os.Getenv("ARTIFACT_DIR"), "Dir that contains jUnit reports XML files") |
| 39 | + flag.StringVar(&p.configFile, "config-file", "", "Config file with defined failure ratios") |
| 40 | + |
| 41 | + flag.StringVar(&p.jobName, "job-name", "", "Name of CI job.") |
| 42 | + flag.StringVar(&p.orchestrator, "orchestrator", "", "orchestrator name (such as GKE or OpenShift), if any.") |
| 43 | + |
| 44 | + flag.BoolVar(&p.dryRun, "dry-run", false, "When set to true issues will NOT be created.") |
| 45 | + flag.BoolVar(&debug, "debug", false, "Enable debug log level") |
| 46 | + versioninfo.AddFlag(flag.CommandLine) |
| 47 | + flag.Parse() |
| 48 | + |
| 49 | + if debug { |
| 50 | + log.SetLevel(log.DebugLevel) |
| 51 | + } |
| 52 | + |
| 53 | + err = p.run() |
| 54 | + if err != nil { |
| 55 | + log.Fatal(err) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +type recentFlakyTestInfo struct { |
| 60 | + JobName string |
| 61 | + FilteredName string |
| 62 | + Classname string |
| 63 | + TotalAll int |
| 64 | + FailRatio int |
| 65 | +} |
| 66 | + |
| 67 | +func (p *flakeCheckerParams) checkFailedTests(bqClient biqQueryClient, failedTests []testcase.TestCase, flakeCheckerRecs []*flakeDetectionPolicy) error { |
| 68 | + if len(failedTests) == 0 { |
| 69 | + return errors.New(errDescNoFailedTests) |
| 70 | + } |
| 71 | + |
| 72 | + for _, failedTest := range failedTests { |
| 73 | + found := false |
| 74 | + log.Infof("Checking failed test: %q / %q / %q", p.jobName, failedTest.Name, failedTest.Classname) |
| 75 | + for _, flakeCheckerRec := range flakeCheckerRecs { |
| 76 | + match, err := flakeCheckerRec.matchJobName(p.jobName) |
| 77 | + if err != nil { |
| 78 | + return err |
| 79 | + } |
| 80 | + |
| 81 | + if !match { |
| 82 | + continue |
| 83 | + } |
| 84 | + |
| 85 | + match, err = flakeCheckerRec.matchTestName(failedTest.Name) |
| 86 | + if err != nil { |
| 87 | + return err |
| 88 | + } |
| 89 | + |
| 90 | + if !match { |
| 91 | + continue |
| 92 | + } |
| 93 | + |
| 94 | + match, err = flakeCheckerRec.matchClassname(failedTest.Classname) |
| 95 | + if err != nil { |
| 96 | + return err |
| 97 | + } |
| 98 | + |
| 99 | + if !match { |
| 100 | + continue |
| 101 | + } |
| 102 | + |
| 103 | + found = true |
| 104 | + log.Infof("Match found: %q / %q / %q", flakeCheckerRec.config.MatchJobName, flakeCheckerRec.config.TestNameRegex, flakeCheckerRec.config.Classname) |
| 105 | + totalRuns, failRatio, err := bqClient.GetRatioForTest(flakeCheckerRec, failedTest.Name) |
| 106 | + if err != nil { |
| 107 | + return errors.Wrap(err, errDescGetRatio) |
| 108 | + } |
| 109 | + |
| 110 | + if totalRuns < totalRunsLimit { |
| 111 | + return errors.Wrap(fmt.Errorf("%d", totalRuns), errDescShortHistory) |
| 112 | + } |
| 113 | + |
| 114 | + if failRatio > flakeCheckerRec.config.RatioThreshold { |
| 115 | + return errors.Wrap(fmt.Errorf("(%d > %d)", failRatio, flakeCheckerRec.config.RatioThreshold), errDescAboveThreshold) |
| 116 | + } |
| 117 | + |
| 118 | + log.Infof("Ratio is below threshold: (%d <= %d)", failRatio, flakeCheckerRec.config.RatioThreshold) |
| 119 | + } |
| 120 | + |
| 121 | + if !found { |
| 122 | + return errors.Wrap(errors.New(failedTest.Name), errDescNoMatch) |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + return nil |
| 127 | +} |
| 128 | + |
| 129 | +func (p *flakeCheckerParams) run() error { |
| 130 | + testSuites, err := junit.IngestDir(p.junitReportsDir) |
| 131 | + if err != nil { |
| 132 | + return errors.Wrap(err, "could not read files") |
| 133 | + } |
| 134 | + |
| 135 | + failedTests, err := testcase.GetFailedTests(testSuites) |
| 136 | + if err != nil { |
| 137 | + return errors.Wrap(err, "could not find failed tests") |
| 138 | + } |
| 139 | + log.Infof("Found %d failed tests", len(failedTests)) |
| 140 | + |
| 141 | + flakeConfigs, err := loadFlakeConfigFile(p.configFile) |
| 142 | + if err != nil { |
| 143 | + return errors.Wrapf(err, "unable to load config file (%s)", p.configFile) |
| 144 | + } |
| 145 | + |
| 146 | + bqClient, err := getNewBigQueryClient() |
| 147 | + if err != nil { |
| 148 | + return errors.Wrap(err, "unable to create BigQuery client") |
| 149 | + } |
| 150 | + |
| 151 | + if err = p.checkFailedTests(bqClient, failedTests, flakeConfigs); err != nil { |
| 152 | + return errors.Wrap(err, "check failed tests") |
| 153 | + } |
| 154 | + |
| 155 | + log.Info("All failed tests are within allowed flake thresholds") |
| 156 | + return nil |
| 157 | +} |
0 commit comments