-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathfeatures_test.go
More file actions
168 lines (144 loc) · 4.59 KB
/
features_test.go
File metadata and controls
168 lines (144 loc) · 4.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package e2e
import (
"bytes"
"fmt"
"io"
"log"
"os"
"strings"
"testing"
"github.com/cucumber/godog"
"github.com/cucumber/godog/colors"
"github.com/spf13/pflag"
testutil "github.com/operator-framework/operator-controller/internal/shared/util/test"
"github.com/operator-framework/operator-controller/test/e2e/steps"
)
var cliOpts = godog.Options{
Concurrency: 1,
Format: "pretty",
Paths: []string{"features"},
Output: colors.Colored(os.Stdout),
NoColors: true,
}
func init() {
godog.BindCommandLineFlags("godog.", &cliOpts)
}
func TestMain(m *testing.M) {
// parse CLI arguments
pflag.Parse()
cliOpts.Paths = pflag.Args()
if cliOpts.Tags != "" {
fmt.Println("Note: Custom feature tags provided - disabling automatic test parallelization")
// run tests explicitly as requested by CLI
sc := godog.TestSuite{
TestSuiteInitializer: InitializeSuite,
ScenarioInitializer: InitializeScenario,
Options: &cliOpts,
}.Run()
if sc != 0 {
// 1 - failed
// 2 - command line usage error
// 128 - or higher, os signal related error exit codes
log.Fatalf("non-zero status returned: (%d), failed to run feature tests", sc)
}
} else {
executeTestsParallel()
}
path := os.Getenv("E2E_SUMMARY_OUTPUT")
if path == "" {
fmt.Println("Note: E2E_SUMMARY_OUTPUT is unset; skipping summary generation")
} else {
if err := testutil.PrintSummary(path); err != nil {
// Fail the run if alerts are found
fmt.Printf("%v", err)
os.Exit(1)
}
}
}
func executeTestsParallel() {
// Create buffers to capture output for final summary
var parallelBuf, serialBuf bytes.Buffer
parallelOpts := cliOpts
if !pflag.Lookup("godog.concurrency").Changed {
// Override default concurrency value with 100; otherwise use whatever was provided by CLI
parallelOpts.Concurrency = 100
}
parallelOpts.Tags = "~@Serial"
// Write to both specified output (live to stdout, by default) and buffer (for summary)
parallelOpts.Output = io.MultiWriter(parallelOpts.Output, ¶llelBuf)
// run tests concurrently
scParallel := godog.TestSuite{
TestSuiteInitializer: InitializeSuite,
ScenarioInitializer: InitializeScenario,
Options: ¶llelOpts,
}.Run()
fmt.Println("End of parallel run - beginning serial tests")
serialOpts := cliOpts
serialOpts.Concurrency = 1
serialOpts.Tags = "@Serial"
// Write to both specified output (live to stdout, by default) and buffer (for summary)
serialOpts.Output = io.MultiWriter(serialOpts.Output, &serialBuf)
// run tests serially
scSerial := godog.TestSuite{
TestSuiteInitializer: InitializeSuite,
ScenarioInitializer: InitializeScenario,
Options: &serialOpts,
}.Run()
// TODO We re-print the output of any failed steps here for easier debugging. However, it would be
// better to combine this with the E2E_SUMMARY_OUTPUT and show pass/fail + performance in one
// markdown output then preserve the console output for local testing.
// Print aggregated summary
fmt.Println("\n" + strings.Repeat("=", 80))
fmt.Println("TEST EXECUTION SUMMARY")
fmt.Println(strings.Repeat("=", 80))
fmt.Printf("\nParallel Tests Exit Code: %d\n", scParallel)
if scParallel != 0 {
failedSteps := extractFailedSteps(parallelBuf.String())
if failedSteps != "" {
fmt.Println("\nParallel Test Failures:")
fmt.Println(strings.Repeat("-", 80))
fmt.Println(failedSteps)
}
}
fmt.Printf("\nSerial Tests Exit Code: %d\n", scSerial)
if scSerial != 0 {
failedSteps := extractFailedSteps(serialBuf.String())
if failedSteps != "" {
fmt.Println("\nSerial Test Failures:")
fmt.Println(strings.Repeat("-", 80))
fmt.Println(failedSteps)
}
}
fmt.Println(strings.Repeat("=", 80))
if scParallel != 0 || scSerial != 0 {
// 1 - failed
// 2 - command line usage error
// 128 - or higher, os signal related error exit codes
log.Fatalf("non-zero status returned; parallel: (%d), serial: (%d), failed to run feature tests", scParallel, scSerial)
}
}
// extractFailedSteps extracts the "--- Failed steps:" section from godog output
func extractFailedSteps(output string) string {
lines := strings.Split(output, "\n")
var failedSection []string
capturing := false
for _, line := range lines {
if strings.Contains(line, "--- Failed steps:") {
capturing = true
}
if capturing {
failedSection = append(failedSection, line)
}
}
if len(failedSection) == 0 {
return ""
}
return strings.Join(failedSection, "\n")
}
func InitializeSuite(tc *godog.TestSuiteContext) {
tc.BeforeSuite(steps.BeforeSuite)
}
func InitializeScenario(sc *godog.ScenarioContext) {
steps.RegisterSteps(sc)
steps.RegisterHooks(sc)
}