forked from concourse/resource-types-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdutyfree_test.go
210 lines (166 loc) · 6.93 KB
/
dutyfree_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
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
package main_test
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"os/exec"
"path/filepath"
"github.com/PuerkitoBio/goquery"
. "github.com/concourse/dutyfree/matchers"
"github.com/onsi/gomega/ghttp"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gbytes"
"github.com/onsi/gomega/gexec"
)
var _ = Describe("Dutyfree", func() {
var (
server *ghttp.Server
outputDir string
resources *os.File
)
BeforeEach(func() {
var err error
server = ghttp.NewServer()
outputDir, err = ioutil.TempDir("", "dutyfree")
Expect(err).ToNot(HaveOccurred())
resources, err = ioutil.TempFile("", "resources.yml")
Expect(err).ToNot(HaveOccurred())
_, err = fmt.Fprint(resources, `---
- repository: https://github.com/concourse/git-resource
name: git resource
desc: git resource description
- repository: https://github.com/concourse/hg-resource
name: hg resource
desc:
- repository: https://github.com/concourse/foo-resource
name: foo resource
`)
Expect(err).ToNot(HaveOccurred())
})
Describe("when a directory and a resources file are provided", func() {
BeforeEach(func() {
server.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("GET", "/repos/concourse/git-resource/readme"),
ghttp.VerifyHeaderKV("Accept", "application/vnd.github.VERSION.html"),
ghttp.VerifyHeaderKV("Authorization", "token SOMEGITHUBTOKEN"),
ghttp.RespondWith(http.StatusOK, `<div id="readme">git foo</div>`),
),
ghttp.CombineHandlers(
ghttp.VerifyRequest("GET", "/repos/concourse/hg-resource/readme"),
ghttp.VerifyHeaderKV("Accept", "application/vnd.github.VERSION.html"),
ghttp.VerifyHeaderKV("Authorization", "token SOMEGITHUBTOKEN"),
ghttp.RespondWith(http.StatusOK, `<div id="readme">hg foo</div>`),
),
ghttp.CombineHandlers(
ghttp.VerifyRequest("GET", "/repos/concourse/foo-resource/readme"),
ghttp.VerifyHeaderKV("Accept", "application/vnd.github.VERSION.html"),
ghttp.VerifyHeaderKV("Authorization", "token SOMEGITHUBTOKEN"),
ghttp.RespondWith(http.StatusOK, `<div id="readme">foo foo</div>`),
))
})
It("generates all the website in the output folder", func() {
cmd := exec.Command(pathToBin, outputDir, resources.Name())
cmd.Env = append(cmd.Env, "GITHUB_API_ENDPOINT="+server.URL(), "GITHUB_TOKEN=SOMEGITHUBTOKEN")
session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
Expect(err).ToNot(HaveOccurred())
Eventually(session).Should(gexec.Exit(0))
By("creating the index.html page")
indexHTML, err := os.Open(filepath.Join(outputDir, "index.html"))
Expect(err).ToNot(HaveOccurred())
doc, err := goquery.NewDocumentFromReader(indexHTML)
Expect(err).ToNot(HaveOccurred())
Expect(doc).To(
SatisfyAll(
ContainSelectorWithText("title", Equal("Duty Free")),
ContainSelector(`a[href="resources/concourse-git-resource.html"]`),
ContainSelector(`a[href="resources/concourse-hg-resource.html"]`)))
Expect(doc).To(SatisfyAll(
ContainSelectorWithText("#concourse-git-resource .desc", Equal("git resource description")),
ContainSelectorWithText("#concourse-hg-resource .desc", BeZero()),
ContainSelectorWithText("#concourse-foo-resource .desc", BeZero()),
))
By("copying the static folder")
staticSrcDir, err := ioutil.ReadDir(filepath.Join(outputDir, "static"))
Expect(err).ToNot(HaveOccurred())
staticDstDir, err := ioutil.ReadDir("static")
Expect(err).ToNot(HaveOccurred())
for i := range staticSrcDir {
Expect(staticDstDir[i].Name()).To(Equal(staticSrcDir[i].Name()))
Expect(staticDstDir[i].Size()).To(Equal(staticSrcDir[i].Size()))
}
By("creating a html page for each resource")
Expect(server.ReceivedRequests()).Should(HaveLen(3))
for _, resource := range []string{"git", "hg"} {
resourceHTML, err := os.Open(filepath.Join(outputDir, fmt.Sprintf("resources/concourse-%s-resource.html", resource)))
Expect(err).ToNot(HaveOccurred())
doc, err = goquery.NewDocumentFromReader(resourceHTML)
Expect(err).ToNot(HaveOccurred())
Expect(doc).To(SatisfyAll(
ContainSelectorWithText("title", ContainSubstring("Duty Free - %s resource", resource)),
ContainSelectorWithText("#github-readme #readme", ContainSubstring("%s foo", resource))))
}
})
AfterEach(func() {
os.RemoveAll(outputDir)
os.Remove(resources.Name())
})
})
Describe("when github returns a non 200 status", func() {
BeforeEach(func() {
server.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("GET", MatchRegexp("/repos/concourse/git-resource/readme")),
ghttp.VerifyHeaderKV("Accept", "application/vnd.github.VERSION.html"),
ghttp.VerifyHeaderKV("Authorization", "token SOMEGITHUBTOKEN"),
ghttp.RespondWith(500, "error message"),
))
})
It("exits 1 and print the error cause", func() {
cmd := exec.Command(pathToBin, outputDir, resources.Name())
cmd.Env = append(cmd.Env, "GITHUB_API_ENDPOINT="+server.URL(), "GITHUB_TOKEN=SOMEGITHUBTOKEN")
session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
Expect(err).ToNot(HaveOccurred())
Eventually(session).Should(gexec.Exit(1))
Expect(server.ReceivedRequests()).To(HaveLen(1))
Eventually(session.Err).Should(gbytes.Say("Unable to access readme for concourse/git-resource due to error: 500, reason: error message"))
})
})
Describe("when no parameter is passed", func() {
It("exits 1 and prints usage message", func() {
cmd := exec.Command(pathToBin)
session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
Expect(err).ToNot(HaveOccurred())
Eventually(session).Should(gexec.Exit(1))
Eventually(session.Err).Should(gbytes.Say("undefined output directory"))
Eventually(session.Err).Should(gbytes.Say("usage: %s <output-directory> <resource-file>", pathToBin))
})
})
Describe("when the directory does not exits", func() {
It("exits 1 and prints usage message", func() {
cmd := exec.Command(pathToBin, "/a/folder/that/does/not/exists", "a-resources.yaml")
session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
Expect(err).ToNot(HaveOccurred())
Eventually(session).Should(gexec.Exit(1))
Eventually(session.Err).Should(gbytes.Say("output directory cannot be found"))
Eventually(session.Err).Should(gbytes.Say("usage: %s <output-directory> <resource-file>", pathToBin))
})
})
Describe("when the resources file does not exists", func() {
It("exits 1 and prints usage message", func() {
outputDir, err := ioutil.TempDir("", "dutyfree")
Expect(err).ToNot(HaveOccurred())
cmd := exec.Command(pathToBin, outputDir, "a-resources-that-does-not-exists.yaml")
session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
Expect(err).ToNot(HaveOccurred())
Eventually(session).Should(gexec.Exit(1))
Eventually(session.Err).Should(gbytes.Say("cannot read resources file"))
Eventually(session.Err).Should(gbytes.Say("usage: %s <output-directory> <resource-file>", pathToBin))
})
})
AfterEach(func() {
server.Close()
})
})