-
Notifications
You must be signed in to change notification settings - Fork 7
/
cf_debug_server_test.go
157 lines (127 loc) · 4.19 KB
/
cf_debug_server_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
package debugserver_test
import (
"bytes"
"flag"
"fmt"
"net"
"net/http"
"strconv"
cf_debug_server "code.cloudfoundry.org/debugserver"
lager "code.cloudfoundry.org/lager/v3"
"github.com/tedsuo/ifrit"
ginkgomon "github.com/tedsuo/ifrit/ginkgomon_v2"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gbytes"
)
var _ = Describe("CF Debug Server", func() {
var (
logBuf *gbytes.Buffer
sink *lager.ReconfigurableSink
process ifrit.Process
)
BeforeEach(func() {
logBuf = gbytes.NewBuffer()
sink = lager.NewReconfigurableSink(
lager.NewWriterSink(logBuf, lager.DEBUG),
// permit no logging by default, for log reconfiguration below
lager.FATAL+1,
)
})
AfterEach(func() {
ginkgomon.Interrupt(process)
})
Describe("AddFlags", func() {
It("adds flags to the flagset", func() {
flags := flag.NewFlagSet("test", flag.ContinueOnError)
cf_debug_server.AddFlags(flags)
f := flags.Lookup(cf_debug_server.DebugFlag)
Expect(f).NotTo(BeNil())
})
})
Describe("DebugAddress", func() {
Context("when flags are not added", func() {
It("returns the empty string", func() {
flags := flag.NewFlagSet("test", flag.ContinueOnError)
Expect(cf_debug_server.DebugAddress(flags)).To(Equal(""))
})
})
Context("when flags are added", func() {
var flags *flag.FlagSet
BeforeEach(func() {
flags = flag.NewFlagSet("test", flag.ContinueOnError)
cf_debug_server.AddFlags(flags)
})
Context("when set", func() {
It("returns the address", func() {
flags.Parse([]string{"-debugAddr", address})
Expect(cf_debug_server.DebugAddress(flags)).To(Equal(address))
})
})
Context("when not set", func() {
It("returns the empty string", func() {
Expect(cf_debug_server.DebugAddress(flags)).To(Equal(""))
})
})
})
})
Describe("Run", func() {
It("serves debug information", func() {
var err error
process, err = cf_debug_server.Run(address, sink)
Expect(err).NotTo(HaveOccurred())
debugResponse, err := http.Get(fmt.Sprintf("http://%s/debug/pprof/goroutine", address))
Expect(err).NotTo(HaveOccurred())
defer debugResponse.Body.Close()
})
Context("when the address is already in use", func() {
var listener net.Listener
BeforeEach(func() {
var err error
listener, err = net.Listen("tcp", address)
Expect(err).NotTo(HaveOccurred())
})
AfterEach(func() {
listener.Close()
})
It("returns an error", func() {
var err error
process, err = cf_debug_server.Run(address, sink)
Expect(err).To(HaveOccurred())
Expect(err).To(BeAssignableToTypeOf(&net.OpError{}))
netErr := err.(*net.OpError)
Expect(netErr.Op).To(Equal("listen"))
})
})
Context("checking log-level endpoint", func() {
validForms := map[lager.LogLevel][]string{
lager.DEBUG: []string{"debug", "DEBUG", "d", strconv.Itoa(int(lager.DEBUG))},
lager.INFO: []string{"info", "INFO", "i", strconv.Itoa(int(lager.INFO))},
lager.ERROR: []string{"error", "ERROR", "e", strconv.Itoa(int(lager.ERROR))},
lager.FATAL: []string{"fatal", "FATAL", "f", strconv.Itoa(int(lager.FATAL))},
}
//This will add another 16 unit tests to the suit
for level, acceptedForms := range validForms {
for _, form := range acceptedForms {
testLevel := level
testForm := form
It("can reconfigure the given sink with "+form, func() {
var err error
process, err = cf_debug_server.Run(address, sink)
Expect(err).NotTo(HaveOccurred())
sink.Log(lager.LogFormat{LogLevel: testLevel, Message: "hello before level change"})
Eventually(logBuf).ShouldNot(gbytes.Say("hello before level change"))
request, err := http.NewRequest("PUT", fmt.Sprintf("http://%s/log-level", address), bytes.NewBufferString(testForm))
Expect(err).NotTo(HaveOccurred())
response, err := http.DefaultClient.Do(request)
Expect(err).NotTo(HaveOccurred())
Expect(response.StatusCode).To(Equal(http.StatusOK))
response.Body.Close()
sink.Log(lager.LogFormat{LogLevel: testLevel, Message: "Logs sent with log-level " + testForm})
Eventually(logBuf).Should(gbytes.Say("Logs sent with log-level " + testForm))
})
}
}
})
})
})