-
Notifications
You must be signed in to change notification settings - Fork 1
/
ironbird_test.go
143 lines (118 loc) · 4.4 KB
/
ironbird_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 ironbird_test
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"time"
. "github.com/EngineerBetter/ironbird"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/gbytes"
. "github.com/onsi/gomega/gexec"
)
var _ = Describe("", func() {
defer GinkgoRecover()
for _, specX := range specs {
// Re-assign as otherwise the single pointer is shared by the anonymous functions, and by the time that they
// execute, range will have made the single pointer point to something else. Possibly.
spec := specX
cwd, err := os.Getwd()
expectErrToNotHaveOccurred(err)
relativeTaskConfigPath, err := filepath.Rel(cwd, filepath.Join(spec.SpecDir, spec.Config))
expectErrToNotHaveOccurred(err)
Describe(relativeTaskConfigPath, func() {
for _, specCaseX := range spec.Cases {
specCase := specCaseX
Describe("when "+specCase.When, func() {
inputDirs := make(map[string]string)
if spec.MainInput.Name != "" {
sourceDir := spec.SpecDir
if spec.MainInput.RelativeRoot != "" {
sourceDir = spec.MainInput.RelativeRoot
}
tmpInputPath := createTmpInputDir(spec.MainInput.Name, sourceDir, spec.SpecDir)
inputDirs[spec.MainInput.Name] = tmpInputPath
}
for _, input := range specCase.It.HasInputs {
tmpInputPath := createTmpInputDir(input.Name, input.From, spec.SpecDir)
inputDirs[input.Name] = tmpInputPath
if input.Setup != "" {
MustBash(input.Setup, tmpInputPath)
}
}
outputDirs := make(map[string]string)
for _, outputExpectation := range specCase.It.HasOutputs {
outputPath, err := ioutil.TempDir("", outputExpectation.Name)
expectErrToNotHaveOccurred(err)
outputDirs[outputExpectation.Name] = outputPath
}
var session *Session
It(fmt.Sprintf("exits %d", specCase.It.Exits), func() {
var timeout = defaultTimeout
if specCase.Within != "" {
timeout, err = time.ParseDuration(specCase.Within)
Expect(err).ToNot(HaveOccurred())
}
timeout = timeout * time.Duration(timeoutFactorArg)
session = FlyExecute(targetArg, spec.SpecDir, spec.Config, specCase.Params, inputDirs, outputDirs, timeout)
interceptMessage := ""
if specCase.It.Exits != 0 && session.Out != nil {
pattern := regexp.MustCompile(`executing build (\d*) at http`)
buildNumber := pattern.FindStringSubmatch(string(session.Out.Contents()))
interceptMessage = fmt.Sprintf("\nTask failed unexpectedly, debug with:\nfly -t %s intercept -b %s\n---", targetArg, string(buildNumber[1]))
}
Expect(session).To(Exit(specCase.It.Exits), OutErrMessage(session)+interceptMessage)
Expect(session).To(Say("executing build"))
Expect(session).To(Say("initializing"))
})
for _, sayExpectationX := range specCase.It.Says {
sayExpectation := sayExpectationX
It("says "+sayExpectation, func() {
Expect(session).To(Say(sayExpectation))
})
}
for _, outputExpectationX := range specCase.It.HasOutputs {
outputExpectation := outputExpectationX
Describe(fmt.Sprintf(", it has an output '%s'", outputExpectation.Name), func() {
for _, forWhichX := range outputExpectation.ForWhich {
forWhich := forWhichX
Describe(fmt.Sprintf("for which '%s'", forWhich.Bash), func() {
var assertionSession *Session
It(fmt.Sprintf("exits %d", forWhich.Exits), func() {
// THE REDIRECT IS ABSOLUTE CHEDDAR
assertionSession = Bash(forWhich.Bash+" 2>&1", outputDirs[outputExpectation.Name])
Expect(assertionSession).To(Exit(forWhich.Exits), OutErrMessage(assertionSession))
})
for _, sayExpectationX := range forWhich.Says {
sayExpectation := sayExpectationX
It("says "+sayExpectation, func() {
Expect(assertionSession).To(Say(sayExpectation))
})
}
})
}
})
}
})
}
})
}
})
func createTmpInputDir(name, copyFrom, specDir string) string {
tmpInputPath, err := ioutil.TempDir("", name)
expectErrToNotHaveOccurred(err)
// Allow all users access, otherwise Concourse can't read dir when executing
MustBash("chmod 0777 "+tmpInputPath, "")
if copyFrom != "" {
MustBash("cp -r "+copyFrom+"/. "+tmpInputPath, specDir)
}
return tmpInputPath
}
func expectErrToNotHaveOccurred(err error) {
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}