-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.go
More file actions
133 lines (110 loc) · 3.67 KB
/
main.go
File metadata and controls
133 lines (110 loc) · 3.67 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
package main
import (
"context"
"fmt"
"io/fs"
"os"
"sort"
"strings"
"time"
"github.com/spf13/pflag"
"github.com/stringintech/kernel-bindings-tests/runner"
"github.com/stringintech/kernel-bindings-tests/testdata"
)
func main() {
handlerPath := pflag.String("handler", "", "Path to handler binary")
handlerTimeout := pflag.Duration("handler-timeout", 10*time.Second, "Max time to wait for handler to respond to each test case (e.g., 10s, 500ms)")
timeout := pflag.Duration("timeout", 30*time.Second, "Total timeout for executing all test suites (e.g., 30s, 1m)")
verboseCount := pflag.CountP("verbose", "v", "Verbose mode: -v shows all requests needed to reproduce failed tests, plus received/expected responses; -vv shows this for all tests (passed and failed)")
pflag.Parse()
// Convert verbose count to verbosity level
verbosity := runner.VerbosityQuiet
if *verboseCount >= 2 {
verbosity = runner.VerbosityAlways
} else if *verboseCount == 1 {
verbosity = runner.VerbosityOnFailure
}
if *handlerPath == "" {
fmt.Fprintf(os.Stderr, "Error: --handler flag is required\n")
pflag.Usage()
os.Exit(1)
}
// Collect embedded test files
testFiles, err := fs.Glob(testdata.FS, "*.json")
if err != nil {
fmt.Fprintf(os.Stderr, "Error finding test files: %v\n", err)
os.Exit(1)
}
if len(testFiles) == 0 {
fmt.Fprintf(os.Stderr, "No test files found\n")
os.Exit(1)
}
// Sort test files alphabetically for deterministic execution order
sort.Strings(testFiles)
// Create test runner
testRunner, err := runner.NewTestRunner(*handlerPath, *handlerTimeout, *timeout)
if err != nil {
fmt.Fprintf(os.Stderr, "Error creating test runner: %v\n", err)
os.Exit(1)
}
defer testRunner.CloseHandler()
// Create context with total execution timeout
ctx, cancel := context.WithTimeout(context.Background(), *timeout)
defer cancel()
// Run tests
totalPassed := 0
totalFailed := 0
totalTests := 0
for _, testFile := range testFiles {
fmt.Printf("\n=== Running test suite: %s ===\n", testFile)
// Load test suite from embedded FS
suite, err := runner.LoadTestSuiteFromFS(testdata.FS, testFile)
if err != nil {
fmt.Fprintf(os.Stderr, "Error loading test suite: %v\n", err)
continue
}
// Run suite
result := testRunner.RunTestSuite(ctx, *suite, verbosity)
printResults(suite, result)
totalPassed += result.PassedTests
totalFailed += result.FailedTests
totalTests += result.TotalTests
// Close handler after stateful suites to prevent state leaks.
// A new handler process will be spawned on-demand when the next request is sent.
if suite.Stateful {
testRunner.CloseHandler()
}
}
fmt.Printf("\n" + strings.Repeat("=", 60) + "\n")
fmt.Printf("TOTAL SUMMARY\n")
fmt.Printf(strings.Repeat("=", 60) + "\n")
fmt.Printf("Total Tests: %d\n", totalTests)
fmt.Printf("Passed: %d\n", totalPassed)
fmt.Printf("Failed: %d\n", totalFailed)
fmt.Printf(strings.Repeat("=", 60) + "\n")
if totalFailed > 0 {
os.Exit(1)
}
}
func printResults(suite *runner.TestSuite, result runner.TestResult) {
fmt.Printf("\nTest Suite: %s\n", result.SuiteName)
if suite.Description != "" {
fmt.Printf("Description: %s\n", suite.Description)
}
fmt.Printf("Total: %d, Passed: %d, Failed: %d\n\n", result.TotalTests, result.PassedTests, result.FailedTests)
for i, tr := range result.TestResults {
status := "✓"
if !tr.Passed {
status = "✗"
}
// Print test ID and description if available
if suite.Tests[i].Description != "" {
fmt.Printf(" %s %s (%s)\n", status, tr.TestID, suite.Tests[i].Description)
} else {
fmt.Printf(" %s %s\n", status, tr.TestID)
}
// Print message indented
fmt.Printf(" %s\n", tr.Message)
}
fmt.Printf("\n")
}