-
Notifications
You must be signed in to change notification settings - Fork 6
/
cronner_test.go
176 lines (137 loc) · 3.68 KB
/
cronner_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
// Copyright 2015 PagerDuty, Inc., et al.
// Copyright 2016-2017 Tim Heckman
// Use of this source code is governed by the BSD 3-Clause
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"math/rand"
"net"
"path"
"strconv"
"testing"
"github.com/PagerDuty/godspeed"
"github.com/codeskyblue/go-uuid"
"github.com/tideland/golib/logger"
. "gopkg.in/check.v1"
)
const testCronnerUUID = "02a10ce3-e834-4285-b1ad-272460541f08"
func Test(t *testing.T) { TestingT(t) }
type TestSuite struct {
l *net.UDPConn
ctrl chan int
out chan []byte
lockFile string
h *cmdHandler
}
var _ = Suite(&TestSuite{})
func (t *TestSuite) SetUpSuite(c *C) {
// suppress application logging
logger.SetLevel(logger.LevelFatal)
workingDir := c.MkDir()
t.h = &cmdHandler{
hostname: "brainbox01",
uuid: uuid.New(),
opts: &binArgs{
Label: "testCmd",
LogFail: true,
LogPath: workingDir,
LockDir: workingDir,
},
}
t.lockFile = path.Join(t.h.opts.LockDir, "cronner-testCmd.lock")
}
func (t *TestSuite) TearDownSuite(c *C) {
t.h.gs.Conn.Close()
}
func addrStrToHostPort(addr string) (string, int, error) {
host, port, err := net.SplitHostPort(addr)
if err != nil {
return "", 0, err
}
portNum, err := strconv.ParseInt(port, 10, 64)
if err != nil {
return "", 0, err
}
return host, int(portNum), nil
}
func (t *TestSuite) SetUpTest(c *C) {
// using port 0 tells the kernel to give us a random (ephemeral) port
t.l, t.ctrl, t.out = buildListener(0)
// this goroutine will get cleaned up by the
// TearDownTest function
go listener(t.l, t.ctrl, t.out)
host, port, err := addrStrToHostPort(t.l.LocalAddr().String())
c.Assert(err, IsNil)
gs, err := godspeed.New(host, port, true)
c.Assert(err, IsNil)
gs.SetNamespace("cronner")
t.h.gs = gs
}
func (t *TestSuite) TearDownTest(c *C) {
close(t.ctrl)
t.l.Close()
}
func (*TestSuite) Test_setEnv_and_parseParentEnv(c *C) {
var event, metric []string
defer unsetEnv()
dummyHandler := &cmdHandler{
uuid: testCronnerUUID,
opts: &binArgs{
EventGroup: "testEventGroup",
Group: "testGroup",
Namespace: "testNamespace",
Label: "testLabel",
},
}
unsetEnv()
setEnv(dummyHandler)
event, metric = parseEnvForParent()
c.Assert(len(event), Equals, 2)
c.Assert(len(metric), Equals, 3)
c.Check(event[0], Equals, "cronner_parent_uuid:"+testCronnerUUID)
c.Check(event[1], Equals, "cronner_parent_event_group:"+dummyHandler.opts.EventGroup)
c.Check(metric[0], Equals, "cronner_parent_group:"+dummyHandler.opts.Group)
c.Check(metric[1], Equals, "cronner_parent_namespace:"+dummyHandler.opts.Namespace)
c.Check(metric[2], Equals, "cronner_parent_label:"+dummyHandler.opts.Label)
}
//
// Cronner testing helper functions
//
var chars = []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
func randString(size int) string {
buf := make([]byte, size)
for i := range buf {
buf[i] = chars[rand.Intn(len(chars))]
}
return string(buf)
}
func listener(l *net.UDPConn, ctrl <-chan int, c chan<- []byte) {
for {
select {
case _, ok := <-ctrl:
if !ok {
close(c)
return
}
default:
buffer := make([]byte, 8193)
n, err := l.Read(buffer)
if err != nil {
continue
}
c <- buffer[:n]
}
}
}
func buildListener(port uint16) (*net.UDPConn, chan int, chan []byte) {
addr, err := net.ResolveUDPAddr("udp", fmt.Sprintf("127.0.0.1:%d", port))
if err != nil {
panic(fmt.Sprintf("getting address for test listener failed, bailing out. Here's everything I know: %v", err))
}
l, err := net.ListenUDP("udp", addr)
if err != nil {
panic(fmt.Sprintf("unable to listen for traffic: %v", err))
}
return l, make(chan int), make(chan []byte)
}