-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
216 lines (169 loc) · 4.92 KB
/
main.go
File metadata and controls
216 lines (169 loc) · 4.92 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package main
import (
"context"
"flag"
"fmt"
"os"
"runtime/debug"
admissionregv1 "k8s.io/api/admissionregistration/v1"
"github.com/zemanlx/kat/internal/evaluator"
"github.com/zemanlx/kat/internal/loader"
"github.com/zemanlx/kat/internal/reporter"
)
const defaultVersion = "(devel)"
var version = defaultVersion
type config struct {
runPattern string
verbose bool
jsonOutput bool
version bool
testPaths []string
}
func main() {
if err := run(context.Background(), os.Args, os.Getenv, os.Stdin, os.Stdout); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
// run is testable: inject args/getenv/stdin/stdout.
func run(_ context.Context, args []string, _ func(string) string, _ *os.File, stdout *os.File) error {
cfg, err := parseFlags(args, stdout)
if err != nil {
return err
}
if cfg.version {
fmt.Fprintln(stdout, getVersion())
return nil
}
suites, err := loadSuites(cfg.testPaths, cfg.runPattern)
if err != nil {
return err
}
return executeTests(suites, cfg, stdout)
}
func parseFlags(args []string, stdout *os.File) (*config, error) {
fs := flag.NewFlagSet(args[0], flag.ExitOnError)
fs.SetOutput(stdout)
runPattern := fs.String("run", "", "run only tests matching pattern")
verbose := fs.Bool("v", false, "verbose output")
jsonOutput := fs.Bool("json", false, "output test results in JSON format")
showVersion := fs.Bool("version", false, "print version and exit")
if err := fs.Parse(args[1:]); err != nil {
return nil, fmt.Errorf("parse flags: %w", err)
}
testPaths := []string{"."}
if fs.NArg() > 0 {
testPaths = fs.Args()
}
return &config{
runPattern: *runPattern,
verbose: *verbose,
jsonOutput: *jsonOutput,
version: *showVersion,
testPaths: testPaths,
}, nil
}
func loadSuites(paths []string, pattern string) ([]*loader.TestSuite, error) {
var suites []*loader.TestSuite
for _, path := range paths {
pathSuites, err := loader.Load(path, pattern)
if err != nil {
return nil, fmt.Errorf("load test suites from %s: %w", path, err)
}
suites = append(suites, pathSuites...)
}
return suites, nil
}
func executeTests(suites []*loader.TestSuite, cfg *config, stdout *os.File) error {
eval, err := evaluator.New()
if err != nil {
return fmt.Errorf("create evaluator: %w", err)
}
rep := reporter.New(stdout)
configureReporter(rep, cfg)
for _, suite := range suites {
if err := runSuite(eval, rep, suite); err != nil {
return err
}
}
if err := rep.Summary(); err != nil {
return fmt.Errorf("test summary: %w", err)
}
return nil
}
func configureReporter(rep *reporter.Reporter, cfg *config) {
switch {
case cfg.jsonOutput:
rep.SetFormat(reporter.FormatJSON)
case cfg.verbose:
rep.SetFormat(reporter.FormatVerbose)
default:
rep.SetFormat(reporter.FormatDefault)
}
}
func runSuite(eval *evaluator.Evaluator, rep *reporter.Reporter, suite *loader.TestSuite) error {
suiteRep := rep.StartSuite(suite.Name)
defer suiteRep.End()
for _, test := range suite.Tests {
suiteRep.StartTest(test.Name)
mutatingPolicy, mutatingBinding, validatingPolicy, validatingBinding := findPolicies(suite, test.PolicyName)
if mutatingPolicy == nil && validatingPolicy == nil {
suiteRep.ReportFail(test.Name, fmt.Sprintf("policy %q not found", test.PolicyName))
continue
}
// Evaluate test
result := eval.EvaluateTest(mutatingPolicy, mutatingBinding, validatingPolicy, validatingBinding, test)
suiteRep.ReportResult(test.Name, result)
}
return nil
}
func findPolicies(suite *loader.TestSuite, policyName string) (*admissionregv1.MutatingAdmissionPolicy, *admissionregv1.MutatingAdmissionPolicyBinding, *admissionregv1.ValidatingAdmissionPolicy, *admissionregv1.ValidatingAdmissionPolicyBinding) {
var (
mutatingPolicy *admissionregv1.MutatingAdmissionPolicy
mutatingBinding *admissionregv1.MutatingAdmissionPolicyBinding
validatingPolicy *admissionregv1.ValidatingAdmissionPolicy
validatingBinding *admissionregv1.ValidatingAdmissionPolicyBinding
)
for _, policy := range suite.MutatingPolicies {
if policy.Name == policyName {
mutatingPolicy = policy
// Find matching binding
for _, binding := range suite.MutatingBindings {
if binding.Spec.PolicyName == policy.Name {
mutatingBinding = binding
break
}
}
break
}
}
if mutatingPolicy == nil {
for _, policy := range suite.ValidatingPolicies {
if policy.Name == policyName {
validatingPolicy = policy
// Find matching binding
for _, binding := range suite.ValidatingBindings {
if binding.Spec.PolicyName == policy.Name {
validatingBinding = binding
break
}
}
break
}
}
}
return mutatingPolicy, mutatingBinding, validatingPolicy, validatingBinding
}
func getVersion() string {
if version != defaultVersion {
return version
}
info, ok := debug.ReadBuildInfo()
if !ok {
return version
}
if info.Main.Version == "" || info.Main.Version == defaultVersion {
return version
}
return info.Main.Version
}