forked from vmware-archive/tsa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
retirer_test.go
129 lines (104 loc) · 3.28 KB
/
retirer_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
package tsa_test
import (
"github.com/concourse/tsa"
"code.cloudfoundry.org/lager/lagertest"
"github.com/concourse/atc"
"github.com/concourse/tsa/tsafakes"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/ghttp"
"github.com/tedsuo/rata"
)
var _ = Describe("Retirer", func() {
var (
retirer *tsa.Retirer
logger *lagertest.TestLogger
worker atc.Worker
fakeTokenGenerator *tsafakes.FakeTokenGenerator
fakeATC *ghttp.Server
)
BeforeEach(func() {
logger = lagertest.NewTestLogger("test")
worker = atc.Worker{
Name: "some-worker",
}
fakeTokenGenerator = new(tsafakes.FakeTokenGenerator)
fakeTokenGenerator.GenerateSystemTokenReturns("no", nil)
fakeTokenGenerator.GenerateTeamTokenReturns("yo", nil)
fakeATC = ghttp.NewServer()
atcEndpoint := rata.NewRequestGenerator(fakeATC.URL(), atc.Routes)
retirer = &tsa.Retirer{
ATCEndpoint: atcEndpoint,
TokenGenerator: fakeTokenGenerator,
}
})
AfterEach(func() {
fakeATC.Close()
})
Context("when the worker request is for a team-owned worker", func() {
BeforeEach(func() {
worker.Team = "some-team"
})
It("generates a team-specific token", func() {
fakeATC.AppendHandlers(ghttp.CombineHandlers(
ghttp.VerifyRequest("PUT", "/api/v1/workers/some-worker/retire"),
ghttp.VerifyHeaderKV("Authorization", "Bearer yo"),
ghttp.RespondWith(200, nil, nil),
))
err := retirer.Retire(logger, worker)
Expect(err).NotTo(HaveOccurred())
Expect(fakeATC.ReceivedRequests()).To(HaveLen(1))
})
})
It("tells the ATC to retire the worker", func() {
fakeATC.AppendHandlers(ghttp.CombineHandlers(
ghttp.VerifyRequest("PUT", "/api/v1/workers/some-worker/retire"),
ghttp.VerifyHeaderKV("Authorization", "Bearer no"),
ghttp.RespondWith(200, nil, nil),
))
err := retirer.Retire(logger, worker)
Expect(err).NotTo(HaveOccurred())
Expect(fakeATC.ReceivedRequests()).To(HaveLen(1))
})
Context("when the ATC responds with a 403", func() {
BeforeEach(func() {
fakeATC.AppendHandlers(ghttp.CombineHandlers(
ghttp.VerifyRequest("PUT", "/api/v1/workers/some-worker/retire"),
ghttp.RespondWith(403, nil, nil),
))
})
It("errors", func() {
err := retirer.Retire(logger, worker)
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError(ContainSubstring("403")))
Expect(fakeATC.ReceivedRequests()).To(HaveLen(1))
})
})
Context("when the ATC responds with a 404", func() {
BeforeEach(func() {
fakeATC.AppendHandlers(ghttp.CombineHandlers(
ghttp.VerifyRequest("PUT", "/api/v1/workers/some-worker/retire"),
ghttp.RespondWith(404, nil, nil),
))
})
It("exits successfully", func() {
err := retirer.Retire(logger, worker)
Expect(err).NotTo(HaveOccurred())
Expect(fakeATC.ReceivedRequests()).To(HaveLen(1))
})
})
Context("when the ATC does not respond to retire the worker", func() {
BeforeEach(func() {
fakeATC.AppendHandlers(ghttp.CombineHandlers(
ghttp.VerifyRequest("PUT", "/api/v1/workers/some-worker/retire"),
ghttp.RespondWith(500, nil, nil),
))
})
It("errors", func() {
err := retirer.Retire(logger, worker)
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError(ContainSubstring("500")))
Expect(fakeATC.ReceivedRequests()).To(HaveLen(1))
})
})
})