-
Notifications
You must be signed in to change notification settings - Fork 121
/
Copy pathrunner.go
180 lines (150 loc) · 5.1 KB
/
runner.go
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
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
package static
import (
"fmt"
"os"
"path/filepath"
"github.com/elastic/elastic-package/internal/fields"
"github.com/elastic/elastic-package/internal/logger"
"github.com/elastic/elastic-package/internal/packages"
"github.com/elastic/elastic-package/internal/testrunner"
)
const sampleEventFilePattern = "sample_event*.json"
type runner struct {
options testrunner.TestOptions
}
var _ testrunner.TestRunner = new(runner)
func init() {
testrunner.RegisterRunner(&runner{})
}
const (
// TestType defining asset loading tests
TestType testrunner.TestType = "static"
)
func (r runner) Type() testrunner.TestType {
return TestType
}
func (r runner) String() string {
return "static files"
}
func (r runner) Run(options testrunner.TestOptions) ([]testrunner.TestResult, error) {
r.options = options
return r.run()
}
func (r runner) run() ([]testrunner.TestResult, error) {
result := testrunner.NewResultComposer(testrunner.TestResult{
TestType: TestType,
Package: r.options.TestFolder.Package,
DataStream: r.options.TestFolder.DataStream,
})
testConfig, err := newConfig(r.options.TestFolder.Path)
if err != nil {
return result.WithError(fmt.Errorf("unable to load asset loading test config file: %w", err))
}
if testConfig != nil && testConfig.Skip != nil {
logger.Warnf("skipping %s test for %s: %s (details: %s)",
TestType, r.options.TestFolder.Package,
testConfig.Skip.Reason, testConfig.Skip.Link.String())
return result.WithSkip(testConfig.Skip)
}
pkgManifest, err := packages.ReadPackageManifestFromPackageRoot(r.options.PackageRootPath)
if err != nil {
return result.WithError(fmt.Errorf("failed to read manifest: %w", err))
}
return r.verifySampleEvent(pkgManifest), nil
}
func (r runner) verifySampleEvent(pkgManifest *packages.PackageManifest) []testrunner.TestResult {
resultComposer := testrunner.NewResultComposer(testrunner.TestResult{
Name: "Verify sample events",
TestType: TestType,
Package: r.options.TestFolder.Package,
DataStream: r.options.TestFolder.DataStream,
})
sampleEventPaths, err := r.findSampleEventPaths()
if err != nil {
results, _ := resultComposer.WithError(err)
return results
}
if len(sampleEventPaths) == 0 {
// Nothing to do.
return []testrunner.TestResult{}
}
expectedDatasets, err := r.getExpectedDatasets(pkgManifest)
if err != nil {
results, _ := resultComposer.WithError(err)
return results
}
fieldsValidator, err := fields.CreateValidatorForDirectory(filepath.Dir(sampleEventPaths[0]),
fields.WithSpecVersion(pkgManifest.SpecVersion),
fields.WithDefaultNumericConversion(),
fields.WithExpectedDatasets(expectedDatasets),
fields.WithEnabledImportAllECSSChema(true),
)
if err != nil {
results, _ := resultComposer.WithError(fmt.Errorf("creating fields validator for data stream failed: %w", err))
return results
}
for _, sampleEventPath := range sampleEventPaths {
logger.Debugf("Validating fields in sample event %s", sampleEventPath)
content, err := os.ReadFile(sampleEventPath)
if err != nil {
results, _ := resultComposer.WithError(fmt.Errorf("can't read file: %w", err))
return results
}
multiErr := fieldsValidator.ValidateDocumentBody(content)
if len(multiErr) > 0 {
results, _ := resultComposer.WithError(testrunner.ErrTestCaseFailed{
Reason: fmt.Sprintf("one or more errors found in sample document \"%s\"", sampleEventPath),
Details: multiErr.Error(),
})
return results
}
}
results, _ := resultComposer.WithSuccess()
return results
}
func (r runner) findSampleEventPaths() ([]string, error) {
var pattern string
if r.options.TestFolder.DataStream != "" {
pattern = filepath.Join(
r.options.PackageRootPath,
"data_stream",
r.options.TestFolder.DataStream,
sampleEventFilePattern)
} else {
pattern = filepath.Join(r.options.PackageRootPath, sampleEventFilePattern)
}
files, err := filepath.Glob(pattern)
if err != nil {
return nil, fmt.Errorf("glob failed (pattern: %s): %w", pattern, err)
}
return files, nil
}
func (r runner) getExpectedDatasets(pkgManifest *packages.PackageManifest) ([]string, error) {
dsName := r.options.TestFolder.DataStream
if dsName == "" {
// TODO: This should return the package name plus the policy name, but we don't know
// what policy created this event, so we cannot reliably know it here. Skip the check
// by now.
return nil, nil
}
dataStreamManifest, err := packages.ReadDataStreamManifestFromPackageRoot(r.options.PackageRootPath, dsName)
if err != nil {
return nil, fmt.Errorf("failed to read data stream manifest: %w", err)
}
if ds := dataStreamManifest.Dataset; ds != "" {
return []string{ds}, nil
}
return []string{pkgManifest.Name + "." + dsName}, nil
}
func (r runner) TearDown() error {
return nil // it's a static test runner, no state is stored
}
func (r runner) CanRunPerDataStream() bool {
return true
}
func (r *runner) TestFolderRequired() bool {
return false
}