This repository has been archived by the owner on Feb 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 282
/
tasks_test.go
143 lines (116 loc) · 3.55 KB
/
tasks_test.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
package pcf_pipelines_test
import (
"bufio"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/concourse/atc"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"gopkg.in/yaml.v2"
)
var set_eu_regexp = regexp.MustCompile("^set -eu$")
var shebang_regexp = regexp.MustCompile("^#!/bin/bash$")
var _ = Describe("Tasks", func() {
cwd, err := os.Getwd()
if err != nil {
log.Fatalf("failed to get working dir: %s", err)
}
root := filepath.Dir(cwd)
var taskPaths []string
err = filepath.Walk(cwd, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
fi, err := os.Lstat(path)
if err != nil {
return err
}
if !fi.IsDir() && fi.Mode().IsRegular() && strings.Contains(path, "tasks") && strings.HasSuffix(path, ".yml") {
relTaskPath, err := filepath.Rel(cwd, path)
if err != nil {
return err
}
taskPaths = append(taskPaths, relTaskPath)
}
return nil
})
if err != nil {
log.Fatalf("failed to walk: %s", err)
}
for _, path := range taskPaths {
taskPath := path
var task atc.TaskConfig
configBytes, err := ioutil.ReadFile(taskPath)
if err != nil {
log.Fatalf("failed to load task file: %s", err)
}
err = yaml.Unmarshal(configBytes, &task)
if err != nil {
log.Fatalf("failed to unmarshal task at %s: %s", taskPath, err)
}
if strings.HasPrefix(task.Run.Path, "pcf-pipelines") {
failMessage := fmt.Sprintf(`
Found error with the following task:
%s
`, taskPath)
Context(fmt.Sprintf("task at %s", taskPath), func() {
It("includes a pcf-pipelines input", func() {
match, err := ContainElement(atc.TaskInputConfig{Name: "pcf-pipelines", Path: ""}).Match(task.Inputs)
Expect(err).NotTo(HaveOccurred())
if !match {
Fail(fmt.Sprintf("%s\n%s", failMessage, "Fix it by adding the following input:\n- name: pcf-pipelines"))
}
})
It("does not pass any args", func() {
match, err := HaveLen(0).Match(task.Run.Args)
Expect(err).NotTo(HaveOccurred())
if !match {
bs, err := yaml.Marshal(task.Run.Args)
Expect(err).NotTo(HaveOccurred())
Fail(fmt.Sprintf("%s\nFix it by removing this:\nargs:\n%s", failMessage, bs))
}
})
It("points to a task.sh that is executable", func() {
fi, err := os.Lstat(filepath.Join(root, task.Run.Path))
Expect(err).NotTo(HaveOccurred())
Expect(fi.Mode().IsRegular()).To(BeTrue())
Expect(fi.Mode().IsDir()).To(BeFalse())
if fi.Mode() < os.FileMode(0700) {
Fail(fmt.Sprintf("Expected '%s' to be executable (>= 0700), but was 0%o", task.Run.Path, fi.Mode()))
}
})
It("points to a task.sh that has the correct bash flags set", func() {
f, err := os.Open(filepath.Join(root, task.Run.Path))
Expect(err).NotTo(HaveOccurred())
defer f.Close()
var has_correct_set_eu bool
var has_flags_in_shebang_line bool
scanner := bufio.NewScanner(f)
for scanner.Scan() && !(has_correct_set_eu || has_flags_in_shebang_line) {
if set_eu_regexp.Match(scanner.Bytes()) {
has_correct_set_eu = true
}
if shebang_regexp.Match(scanner.Bytes()) {
has_flags_in_shebang_line = true
}
}
if !(has_correct_set_eu || has_flags_in_shebang_line) {
msg := "incorrect bash args specified;"
if has_flags_in_shebang_line {
msg = fmt.Sprintf("%s\n%s", msg, "remove flags from shebang line")
}
if !has_correct_set_eu {
msg = fmt.Sprintf("%s\n%s", msg, "add 'set -eu' two lines after shebang line")
}
Fail(msg)
}
})
})
}
}
})