This repository has been archived by the owner on Feb 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
cronner_test.go
128 lines (100 loc) · 2.42 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
// Copyright 2015 PagerDuty, Inc, et al. All rights reserved.
// Use of this source code is governed by the BSD 3-Clause
// license that can be found in the LICENSE file.
package main
import (
"bytes"
"fmt"
"math/rand"
"net"
"path"
"testing"
"time"
"github.com/PagerDuty/godspeed"
"github.com/codeskyblue/go-uuid"
"github.com/tideland/golib/logger"
. "gopkg.in/check.v1"
)
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,
},
}
var err error
t.h.gs, err = godspeed.NewDefault()
c.Assert(err, IsNil)
t.h.gs.SetNamespace("cronner")
t.lockFile = path.Join(t.h.opts.LockDir, "cronner-testCmd.lock")
}
func (t *TestSuite) TearDownSuite(c *C) {
t.h.gs.Conn.Close()
}
func (t *TestSuite) SetUpTest(c *C) {
t.l, t.ctrl, t.out = buildListener(8125)
// this goroutine will get cleaned up by the
// TearDownTest function
go listener(t.l, t.ctrl, t.out)
}
func (t *TestSuite) TearDownTest(c *C) {
close(t.ctrl)
t.l.Close()
time.Sleep(time.Millisecond * 10)
}
//
// 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)
_, err := l.Read(buffer)
if err != nil {
continue
}
c <- bytes.Trim(buffer, "\x00")
}
}
}
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)
}