-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestpipe.go
326 lines (271 loc) · 7.41 KB
/
testpipe.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package testpipe
import (
"bytes"
"fmt"
"html/template"
"io/ioutil"
"log"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/concourse/atc"
yaml "gopkg.in/yaml.v2"
)
type Config struct {
ResourceMap map[string]string `yaml:"resource_map"`
}
type TestPipe struct {
path string
config Config
tmpl *template.Template
}
type TemplateData struct {
Type string
PipelinePath string
JobName string
TaskName string
Extras []string
Missing []string
}
const outputTemplate = `
Pipeline: {{.PipelinePath}}
Job: {{.JobName}}
Task: {{.TaskName}}
{{if .Extras}}
Extra {{.Type}} that should be removed:
{{- range .Extras}}
{{.}}
{{- end}}
{{end -}}
{{if .Missing }}
Missing {{.Type}} that should be added:
{{- range .Missing}}
{{.}}
{{- end}}
{{end -}}
`
func New(path string, config Config) *TestPipe {
return &TestPipe{
path: path,
config: config,
tmpl: template.Must(template.New("output").Parse(outputTemplate)),
}
}
var placeholderRegexp = regexp.MustCompile("{{([a-zA-Z0-9-_]+)}}")
func (t *TestPipe) Run() error {
configBytes, err := ioutil.ReadFile(t.path)
if err != nil {
return err
}
cleanConfigBytes := placeholderRegexp.ReplaceAll(configBytes, []byte("true"))
var config atc.Config
err = yaml.Unmarshal(cleanConfigBytes, &config)
if err != nil {
return fmt.Errorf("failed to unmarshal pipeline at %s: %s", t.path, err)
}
if len(config.Jobs) == 0 {
return fmt.Errorf("no jobs in pipeline: %s", config.Jobs)
}
for _, job := range config.Jobs {
var resources []string
var tasks []atc.PlanConfig
resourceMap := make(map[string]string, len(t.config.ResourceMap))
for k, v := range t.config.ResourceMap {
resourceMap[k] = v
}
for _, planConfig := range flattenedPlan(&job.Plan) {
switch {
case planConfig.Get != "":
resources = append(resources, planConfig.Get)
if planConfig.Resource != "" {
resources = append(resources, planConfig.Resource)
origPath := resourceMap[planConfig.Resource]
resourceMap[planConfig.Get] = origPath
}
case planConfig.Put != "":
resources = append(resources, planConfig.Put)
case planConfig.Task != "":
canonicalTask, err := flattenTask(resourceMap, &planConfig, job.Name)
if err != nil {
return err
}
err = testParityOfParams(canonicalTask, job.Name, t.path, t.tmpl)
if err != nil {
return err
}
err = testPresenceOfRequiredResources(resources, canonicalTask, job.Name, t.path, t.tmpl)
if err != nil {
return err
}
err = testExecutableTask(resources, canonicalTask, job.Name, t.path, t.tmpl, resourceMap)
if err != nil {
return err
}
tasks = append(tasks, *canonicalTask)
if canonicalTask.TaskConfig.Outputs != nil {
for i := range canonicalTask.TaskConfig.Outputs {
resources = append(resources, canonicalTask.TaskConfig.Outputs[i].Name)
}
}
for _, v := range canonicalTask.OutputMapping {
resources = append(resources, v)
}
}
}
}
return nil
}
func testExecutableTask(
resources []string,
task *atc.PlanConfig,
jobName string,
pipelinePath string,
tmpl *template.Template,
resourceMap map[string]string,
) error {
taskPathParts := strings.Split(task.TaskConfig.Run.Path, string(os.PathSeparator))
resource := taskPathParts[0]
resourcePath := resourceMap[resource]
fileInResource := filepath.Join(append([]string{resourcePath}, taskPathParts[1:]...)...)
fileInfo, err := os.Stat(fileInResource)
if err != nil {
return fmt.Errorf("Task run path not found: task path: %s, not found here: %s", task.TaskConfig.Run.Path, fileInResource)
}
if fileInfo.Mode().Perm()&0111 == 0 {
return fmt.Errorf("Task `path` exists but is not executable: task path: %s, found: %s", task.TaskConfig.Run.Path, fileInResource)
}
return nil
}
func testPresenceOfRequiredResources(
resources []string,
task *atc.PlanConfig,
jobName string,
pipelinePath string,
tmpl *template.Template,
) error {
var missing []string
OUTER:
for _, input := range task.TaskConfig.Inputs {
for _, resource := range resources {
if input.Name == resource {
continue OUTER
}
if v, ok := task.InputMapping[input.Name]; ok && v == resource {
continue OUTER
}
}
missing = append(missing, input.Name)
}
if len(missing) > 0 {
buf := &bytes.Buffer{}
data := TemplateData{
Type: "resources",
PipelinePath: pipelinePath,
JobName: jobName,
TaskName: task.Name(),
Missing: missing,
}
if err := tmpl.Execute(buf, data); err != nil {
log.Fatalf("failed to execute template: %s", err)
}
return fmt.Errorf("Task invocation is missing resources: %s", buf.String())
}
return nil
}
func testParityOfParams(
task *atc.PlanConfig,
jobName string,
pipelinePath string,
tmpl *template.Template,
) error {
var extras, missing []string
for k := range task.TaskConfig.Params {
if _, ok := task.Params[k]; !ok {
missing = append(missing, k)
}
}
for k := range task.Params {
if _, ok := task.TaskConfig.Params[k]; !ok {
extras = append(extras, k)
}
}
if len(missing) > 0 || len(extras) > 0 {
buf := &bytes.Buffer{}
data := TemplateData{
Type: "params",
PipelinePath: pipelinePath,
JobName: jobName,
TaskName: task.Name(),
Extras: extras,
Missing: missing,
}
if err := tmpl.Execute(buf, data); err != nil {
log.Fatalf("failed to execute template: %s", err)
}
return fmt.Errorf("Params do not have parity: %s", buf.String())
}
return nil
}
func flattenedPlan(seq *atc.PlanSequence) []atc.PlanConfig {
var flatPlan []atc.PlanConfig
for _, planConfig := range *seq {
switch {
case planConfig.Aggregate != nil:
flatPlan = append(flatPlan, flattenedPlan(planConfig.Aggregate)...)
case planConfig.Do != nil:
flatPlan = append(flatPlan, flattenedPlan(planConfig.Do)...)
case planConfig.Get != "", planConfig.Put != "", planConfig.Task != "":
flatPlan = append(flatPlan, planConfig)
}
}
return flatPlan
}
func flattenTask(
resourceMap map[string]string,
task *atc.PlanConfig,
jobName string,
) (*atc.PlanConfig, error) {
result := task
if task.TaskConfigPath != "" {
var err error
result, err = loadTask(resourceMap, task)
if err != nil {
return nil, err
}
}
if result.TaskConfig == nil {
return nil, fmt.Errorf("task %s/%s is missing a definition", jobName, task.Name())
}
if result.TaskConfig.Run.Path == "" {
return nil, fmt.Errorf("task %s/%s is missing a path", jobName, task.Name())
}
return result, nil
}
func loadTask(
resourceMap map[string]string,
task *atc.PlanConfig,
) (*atc.PlanConfig, error) {
if len(resourceMap) == 0 {
return nil, fmt.Errorf("failed to load %s; no config provided", task.TaskConfigPath)
}
resourceRoot := strings.Split(task.TaskConfigPath, string(os.PathSeparator))[0]
var path string
if resourcePath, ok := resourceMap[resourceRoot]; ok && resourcePath != "" {
path = filepath.Join(resourcePath, strings.Replace(task.TaskConfigPath, resourceRoot, "", -1))
} else {
return nil, fmt.Errorf("failed to find path for task: %s resourceRoot %s resourceMap %s", task.TaskConfigPath, resourceRoot, resourceMap)
}
bs, err := ioutil.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("failed to open task at %s", path)
}
var taskConfig atc.TaskConfig
err = yaml.Unmarshal(bs, &taskConfig)
if err != nil {
return nil, err
}
result := task
result.TaskConfig = &taskConfig
return result, nil
}