• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

redhat-openshift-ecosystem / openshift-preflight / 18021266486

25 Sep 2025 09:30PM UTC coverage: 83.795% (-0.02%) from 83.812%
18021266486

Pull #1316

github

web-flow
Merge 8c9a54772 into c2622b141
Pull Request #1316: Add new flag to use non-zero exit code when checks fail

4 of 6 new or added lines in 2 files covered. (66.67%)

18 existing lines in 3 files now uncovered.

5021 of 5992 relevant lines covered (83.8%)

165.55 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

80.82
/internal/cli/cli.go
1
package cli
2

3
import (
4
        "bytes"
5
        "context"
6
        "errors"
7
        "fmt"
8
        "io"
9
        "os"
10
        "strings"
11

12
        "github.com/redhat-openshift-ecosystem/openshift-preflight/artifacts"
13
        "github.com/redhat-openshift-ecosystem/openshift-preflight/certification"
14
        "github.com/redhat-openshift-ecosystem/openshift-preflight/internal/formatters"
15
        "github.com/redhat-openshift-ecosystem/openshift-preflight/internal/lib"
16
        "github.com/redhat-openshift-ecosystem/openshift-preflight/internal/log"
17
        "github.com/redhat-openshift-ecosystem/openshift-preflight/internal/viper"
18

19
        "github.com/go-logr/logr"
20
)
21

22
type CheckConfig struct {
23
        IncludeJUnitResults bool
24
        SubmitResults       bool
25
}
26

27
// RunPreflight executes checks, writes logs, results, and submits results if requested.
28
func RunPreflight(
29
        ctx context.Context,
30
        runChecks func(context.Context) (certification.Results, error),
31
        cfg CheckConfig,
32
        formatter formatters.ResponseFormatter,
33
        rw lib.ResultWriter,
34
        rs lib.ResultSubmitter,
35
) error {
6✔
36
        logger := logr.FromContextOrDiscard(ctx)
6✔
37

6✔
38
        // Configure artifact writing if not already configured. For CLI
6✔
39
        // executions, we default to writing to the filesystem.
6✔
40
        artifactsWriter := artifacts.WriterFromContext(ctx)
6✔
41
        var err error
6✔
42
        if artifactsWriter == nil {
7✔
43
                return errors.New("no artifact writer was configured")
1✔
44
        }
1✔
45
        // Fail early if we cannot write to the results path.
46
        resultsFilePath, err := artifactsWriter.WriteFile(ResultsFilenameWithExtension(formatter.FileExtension()), strings.NewReader(""))
5✔
47
        if err != nil {
5✔
UNCOV
48
                return err
×
UNCOV
49
        }
×
50

51
        resultsFile, err := rw.OpenFile(resultsFilePath)
5✔
52
        if err != nil {
5✔
UNCOV
53
                return err
×
UNCOV
54
        }
×
55

56
        defer resultsFile.Close()
5✔
57
        resultsOutputTarget := io.MultiWriter(os.Stdout, resultsFile)
5✔
58

5✔
59
        // Execute Checks.
5✔
60
        results, err := runChecks(ctx)
5✔
61
        if err != nil {
6✔
62
                return err
1✔
63
        }
1✔
64

65
        // Format and write the results.
66
        formattedResults, err := formatter.Format(ctx, results)
4✔
67
        if err != nil {
5✔
68
                return err
1✔
69
        }
1✔
70

71
        fmt.Fprintln(resultsOutputTarget, string(formattedResults))
3✔
72

3✔
73
        // Optionally write the JUnit results alongside the regular results.
3✔
74
        if cfg.IncludeJUnitResults {
4✔
75
                if err := writeJUnit(ctx, results); err != nil {
1✔
UNCOV
76
                        return err
×
UNCOV
77
                }
×
78
        }
79

80
        if cfg.SubmitResults {
5✔
81
                if err := rs.Submit(ctx); err != nil {
3✔
82
                        return err
1✔
83
                }
1✔
84
        }
85

86
        logger.Info(fmt.Sprintf("Preflight result: %s", convertPassedOverall(results.PassedOverall)))
2✔
87

2✔
88
        if !results.PassedOverall && viper.Instance().GetBool("exit_with_failure") {
2✔
NEW
UNCOV
89
                return fmt.Errorf("one or more checks did not pass")
×
NEW
UNCOV
90
        }
×
91

92
        return nil
2✔
93
}
94

95
// writeJUnit will write JUnit results as an artifact using the ArtifactWriter configured
96
// in ctx.
97
func writeJUnit(ctx context.Context, results certification.Results) error {
2✔
98
        logger := logr.FromContextOrDiscard(ctx)
2✔
99

2✔
100
        junitformatter, err := formatters.NewByName("junitxml")
2✔
101
        if err != nil {
2✔
102
                return err
×
103
        }
×
104

105
        junitResults, err := junitformatter.Format(ctx, results)
2✔
106
        if err != nil {
2✔
UNCOV
107
                return err
×
UNCOV
108
        }
×
109

110
        if aw := artifacts.WriterFromContext(ctx); aw != nil {
4✔
111
                junitFilename, err := aw.WriteFile("results-junit.xml", bytes.NewReader((junitResults)))
2✔
112
                if err != nil {
2✔
UNCOV
113
                        return err
×
UNCOV
114
                }
×
115
                logger.V(log.TRC).Info("JUnitXML filename", "filename", junitFilename)
2✔
116
        }
117

118
        return nil
2✔
119
}
120

121
func convertPassedOverall(passedOverall bool) string {
4✔
122
        if passedOverall {
7✔
123
                return "PASSED"
3✔
124
        }
3✔
125

126
        return "FAILED"
1✔
127
}
128

129
func ResultsFilenameWithExtension(ext string) string {
5✔
130
        return strings.Join([]string{"results", ext}, ".")
5✔
131
}
5✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc