forked from cloudfoundry/auctioneer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_test.go
118 lines (94 loc) · 3.51 KB
/
client_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
package auctioneer_test
import (
"net/http"
"os"
"path"
"time"
"code.cloudfoundry.org/auctioneer"
"code.cloudfoundry.org/lager"
"code.cloudfoundry.org/lager/lagertest"
"code.cloudfoundry.org/tlsconfig"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/ghttp"
)
var _ = Describe("Auctioneer Client", func() {
Describe("NewClient", func() {
var (
fakeAuctioneerServer *ghttp.Server
dummyLogger lager.Logger
)
BeforeEach(func() {
fakeAuctioneerServer = ghttp.NewServer()
fakeAuctioneerServer.AppendHandlers(ghttp.CombineHandlers(
func(rw http.ResponseWriter, r *http.Request) {
time.Sleep(2 * time.Second)
},
ghttp.RespondWith(http.StatusAccepted, nil),
))
dummyLogger = lagertest.NewTestLogger("client_test")
})
It("works", func() {
c := auctioneer.NewClient(fakeAuctioneerServer.URL(), 5*time.Second)
err := c.RequestLRPAuctions(dummyLogger, []*auctioneer.LRPStartRequest{})
Expect(err).NotTo(HaveOccurred())
})
It("times out if the request takes too long", func() {
c := auctioneer.NewClient(fakeAuctioneerServer.URL(), 1*time.Second)
err := c.RequestLRPAuctions(dummyLogger, []*auctioneer.LRPStartRequest{})
Expect(err.Error()).To(ContainSubstring("request canceled"))
})
})
Describe("NewSecureClient", func() {
var (
caFile, certFile, keyFile string
fakeAuctioneerServer *ghttp.Server
dummyLogger lager.Logger
)
BeforeEach(func() {
basePath := path.Join(os.Getenv("GOPATH"), "src/code.cloudfoundry.org/auctioneer/cmd/auctioneer/fixtures")
caFile = path.Join(basePath, "green-certs", "ca.crt")
certFile = path.Join(basePath, "green-certs", "client.crt")
keyFile = path.Join(basePath, "green-certs", "client.key")
fakeAuctioneerServer = ghttp.NewUnstartedServer()
tlsConfig, err := tlsconfig.Build(
tlsconfig.WithInternalServiceDefaults(),
tlsconfig.WithIdentityFromFile(
path.Join(basePath, "green-certs", "server.crt"),
path.Join(basePath, "green-certs", "server.key"),
),
).Server(tlsconfig.WithClientAuthenticationFromFile(caFile))
Expect(err).NotTo(HaveOccurred())
fakeAuctioneerServer.HTTPTestServer.TLS = tlsConfig
fakeAuctioneerServer.HTTPTestServer.StartTLS()
fakeAuctioneerServer.AppendHandlers(ghttp.CombineHandlers(
func(rw http.ResponseWriter, r *http.Request) {
time.Sleep(2 * time.Second)
},
ghttp.RespondWith(http.StatusAccepted, nil),
))
dummyLogger = lagertest.NewTestLogger("client_test")
})
It("works", func() {
c, err := auctioneer.NewSecureClient(fakeAuctioneerServer.URL(), caFile, certFile, keyFile, true, 5*time.Second)
Expect(err).NotTo(HaveOccurred())
err = c.RequestLRPAuctions(dummyLogger, []*auctioneer.LRPStartRequest{})
Expect(err).NotTo(HaveOccurred())
})
It("times out if the request takes too long", func() {
c, err := auctioneer.NewSecureClient(fakeAuctioneerServer.URL(), caFile, certFile, keyFile, true, 1*time.Second)
Expect(err).NotTo(HaveOccurred())
err = c.RequestLRPAuctions(dummyLogger, []*auctioneer.LRPStartRequest{})
Expect(err.Error()).To(ContainSubstring("request canceled"))
})
Context("when the tls config is invalid", func() {
BeforeEach(func() {
certFile = "cmd/auctioneer/fixtures/blue-certs/client.crt"
})
It("returns an error", func() {
_, err := auctioneer.NewSecureClient(fakeAuctioneerServer.URL(), caFile, certFile, keyFile, true, time.Second)
Expect(err.Error()).To(MatchRegexp("failed to load keypair.*"))
})
})
})
})