-
Notifications
You must be signed in to change notification settings - Fork 148
/
helpers.go
175 lines (144 loc) · 4.32 KB
/
helpers.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
package helpers
import (
"crypto/tls"
"errors"
"fmt"
"net"
"strconv"
"time"
"code.cloudfoundry.org/workpool"
. "github.com/onsi/gomega"
. "lats/config"
"github.com/cloudfoundry/dropsonde/envelope_extensions"
"github.com/cloudfoundry/noaa/consumer"
"github.com/cloudfoundry/sonde-go/events"
"github.com/cloudfoundry/storeadapter"
"github.com/cloudfoundry/storeadapter/etcdstoreadapter"
)
const ORIGIN_NAME = "LATs"
var config *TestConfig
func Initialize(testConfig *TestConfig) {
config = testConfig
}
func ConnectToStream(appID string) (<-chan *events.Envelope, <-chan error) {
connection, printer := SetUpConsumer()
msgChan, errorChan := connection.Stream(appID, "")
readErrs := func() error {
select {
case err := <-errorChan:
return err
default:
return nil
}
}
Consistently(readErrs).Should(BeNil())
WaitForWebsocketConnection(printer)
return msgChan, errorChan
}
func ConnectToFirehose() (<-chan *events.Envelope, <-chan error) {
connection, printer := SetUpConsumer()
randomString := strconv.FormatInt(time.Now().UnixNano(), 10)
subscriptionId := "firehose-" + randomString[len(randomString)-5:]
msgChan, errorChan := connection.Firehose(subscriptionId, "")
readErrs := func() error {
select {
case err := <-errorChan:
return err
default:
return nil
}
}
Consistently(readErrs).Should(BeNil())
WaitForWebsocketConnection(printer)
return msgChan, errorChan
}
func RequestContainerMetrics(appID string) ([]*events.ContainerMetric, error) {
consumer, _ := SetUpConsumer()
return consumer.ContainerMetrics(appID, "")
}
func RequestRecentLogs(appID string) ([]*events.LogMessage, error) {
consumer, _ := SetUpConsumer()
return consumer.RecentLogs(appID, "")
}
func SetUpConsumer() (*consumer.Consumer, *TestDebugPrinter) {
tlsConfig := tls.Config{InsecureSkipVerify: config.SkipSSLVerify}
printer := &TestDebugPrinter{}
connection := consumer.New(config.DopplerEndpoint, &tlsConfig, nil)
connection.SetDebugPrinter(printer)
return connection, printer
}
func WaitForWebsocketConnection(printer *TestDebugPrinter) {
Eventually(printer.Dump, 2*time.Second).Should(ContainSubstring("101 Switching Protocols"))
}
func EmitToMetron(envelope *events.Envelope) {
metronConn, err := net.Dial("udp4", fmt.Sprintf("localhost:%d", config.DropsondePort))
Expect(err).NotTo(HaveOccurred())
b, err := envelope.Marshal()
Expect(err).NotTo(HaveOccurred())
_, err = metronConn.Write(b)
Expect(err).NotTo(HaveOccurred())
}
func FindMatchingEnvelope(msgChan <-chan *events.Envelope, envelope *events.Envelope) *events.Envelope {
timeout := time.After(10 * time.Second)
for {
select {
case receivedEnvelope := <-msgChan:
if receivedEnvelope.GetTags()["UniqueName"] == envelope.GetTags()["UniqueName"] {
return receivedEnvelope
}
case <-timeout:
return nil
}
}
}
func FindMatchingEnvelopeByOrigin(msgChan <-chan *events.Envelope, origin string) *events.Envelope {
timeout := time.After(10 * time.Second)
for {
select {
case receivedEnvelope := <-msgChan:
if receivedEnvelope.GetOrigin() == origin {
return receivedEnvelope
}
case <-timeout:
return nil
}
}
}
func FindMatchingEnvelopeByID(id string, msgChan <-chan *events.Envelope) (*events.Envelope, error) {
timeout := time.After(10 * time.Second)
for {
select {
case receivedEnvelope := <-msgChan:
receivedID := envelope_extensions.GetAppId(receivedEnvelope)
if receivedID == id {
return receivedEnvelope, nil
}
return nil, fmt.Errorf("Expected messages with app id: %s, got app id: %s", id, receivedID)
case <-timeout:
return nil, errors.New("Timed out while waiting for message")
}
}
}
func WriteToEtcd(urls []string, key, value string) func() {
etcdOptions := &etcdstoreadapter.ETCDOptions{
IsSSL: true,
CertFile: config.EtcdTLSClientConfig.CertFile,
KeyFile: config.EtcdTLSClientConfig.KeyFile,
CAFile: config.EtcdTLSClientConfig.CAFile,
ClusterUrls: urls,
}
workPool, err := workpool.NewWorkPool(10)
Expect(err).NotTo(HaveOccurred())
adapter, err := etcdstoreadapter.New(etcdOptions, workPool)
Expect(err).NotTo(HaveOccurred())
err = adapter.Create(storeadapter.StoreNode{
Key: key,
Value: []byte(value),
TTL: uint64(time.Minute),
})
Expect(err).NotTo(HaveOccurred())
return func() {
err = adapter.Delete(key)
Expect(err).ToNot(HaveOccurred())
}
}