/
test_run.go
225 lines (184 loc) · 5.6 KB
/
test_run.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
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
217
218
219
220
221
222
223
224
225
/*
* Copyright 2021 Comcast Cable Communications Management, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
package dsl
import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"gopkg.in/yaml.v3"
"github.com/Comcast/plax/cmd/plaxrun/async"
"github.com/Comcast/plax/cmd/plaxrun/plugins/report"
"github.com/Comcast/plax/junit"
plaxDsl "github.com/Comcast/plax/dsl"
)
// Ctx is the context type
type Ctx struct {
*plaxDsl.Ctx
ReportPluginDir string
}
// NewCtx builds a new Ctx
func NewCtx(ctx context.Context) *Ctx {
return &Ctx{
Ctx: plaxDsl.NewCtx(ctx),
ReportPluginDir: ".",
}
}
// TestRun is the top-level type for a test run.
type TestRun struct {
Name string `yaml:"name" json:"name"`
Version string `yaml:"version" json:"version"`
Tests TestDefMap `yaml:"tests" json:"-"`
Groups TestGroupMap `yaml:"groups" json:"-"`
Params TestParamBindingMap `yaml:"params" json:"-"`
Reports TestReportPluginMap `yaml:"reports" json:"-"`
trps *TestRunParams `json:"-"`
tfs []*async.TaskFunc `json:"-"`
}
// NewTestRun makes a new TestRun with the given TestRunParams
func NewTestRun(ctx *Ctx, trps *TestRunParams) (*TestRun, error) {
tr := TestRun{}
if trps.Dir == nil {
return nil, fmt.Errorf("TestRunParams.Dir is nil")
}
ctx.Dir = *trps.Dir
ctx.LogLevel = *trps.LogLevel
ctx.IncludeDirs = trps.IncludeDirs
ctx.Redact = *trps.Redact
reportPluginDir, err := filepath.Abs(*trps.ReportPluginDir)
if err != nil {
return nil, fmt.Errorf("failed to find path to report plugins: %w", err)
}
ctx.ReportPluginDir = reportPluginDir
var filename string
if trps.Filename != nil {
filename = *trps.Filename
}
// Add the test run directory to the end of the includeDirs.
dir, err := filepath.Abs(filepath.Dir(filename))
if err != nil {
return nil, fmt.Errorf("failed to find path to test run file: %w", err)
}
ctx.IncludeDirs = append(ctx.IncludeDirs, dir)
bs, err := ioutil.ReadFile(filename)
if err != nil {
return nil, fmt.Errorf("failed to read test runner configuration file: %w", err)
}
ctx.Redactf("Test Bindings: %v\n", trps.Bindings)
err = os.Chdir(*trps.Dir)
if err != nil {
return nil, fmt.Errorf("failed to change directory: %w", err)
}
ctx.IncludeDirs = append(ctx.IncludeDirs, *trps.Dir)
bs, err = plaxDsl.IncludeYAML(ctx.Ctx, bs)
if err != nil {
return nil, fmt.Errorf("failed to process include YAML: %w", err)
}
if err := yaml.Unmarshal(bs, &tr); err != nil {
return nil, fmt.Errorf("test runner configuration parse error: %w", err)
}
ctx.Logdf("TestRun: %v\n", tr)
tr.trps = trps
tfs, err := trps.Groups.getTaskFuncs(ctx.Ctx, tr)
if err != nil {
return nil, fmt.Errorf("failed to process test groups to execute: %w", err)
}
tr.tfs = append(tr.tfs, tfs...)
if trps.SuiteName != nil && *trps.SuiteName != "" {
testSuite := TestSuiteRef{
name: *trps.SuiteName,
tests: trps.Tests,
}
tf, err := testSuite.getTaskFunc(ctx.Ctx, tr)
if err != nil {
return nil, fmt.Errorf("failed to process tests to execute: %w", err)
}
tr.tfs = append(tr.tfs, tf)
} else {
tfs, err = trps.Tests.getTaskFuncs(ctx.Ctx, tr)
if err != nil {
return nil, fmt.Errorf("failed to process tests to execute: %w", err)
}
tr.tfs = append(tr.tfs, tfs...)
}
return &tr, nil
}
// Exec the TestRun
func (tr *TestRun) Exec(ctx *Ctx) error {
testReport := report.NewTestReport()
testReport.Name = tr.Name
testReport.Version = tr.Version
taskResults, err := async.Sequential(ctx, tr.tfs...)
if err != nil {
return fmt.Errorf("failed to execute tasks: %w", err)
}
for _, taskResult := range taskResults {
if ts, ok := taskResult.Result.(*junit.TestSuite); ok {
if ts != nil {
testReport.TestSuite = append(testReport.TestSuite, ts)
testReport.Total += ts.Total
testReport.Passed += ts.Passed
testReport.Skipped += ts.Skipped
testReport.Failures += ts.Failures
testReport.Errors += ts.Errors
}
}
}
testReport.Finish()
err = tr.Reports.Generate(ctx.Ctx, tr.Params, tr.trps.Bindings, testReport, *tr.trps.EmitJSON)
if err != nil {
ctx.Logf(err.Error())
}
if taskResults.HasError() {
ctx.Logdf("TaskResult Error: %s", taskResults.Error())
return fmt.Errorf(taskResults.Error())
}
return nil
}
// IncludeDirList are the directories to search when YAML-including.
//
// We make an explicit type to enable flag.Var to parse multiple
// parameters.
type IncludeDirList []string
// String representation
func (idl *IncludeDirList) String() string {
return "value=[Directory Path]"
}
// Set the include directories
func (idl *IncludeDirList) Set(value string) error {
*idl = append(*idl, value)
return nil
}
// TestRunParams used to exec a TestRun
type TestRunParams struct {
Bindings plaxDsl.Bindings
Groups TestGroupList
Tests TestList
SuiteName *string
IncludeDirs IncludeDirList
Filename *string
Dir *string
ReportPluginDir *string
EmitJSON *bool
Verbose *bool
LogLevel *string
Labels *string
Priority *int
Redact *bool
}