-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlisten_fds_test.go
123 lines (103 loc) · 2.81 KB
/
listen_fds_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
// Copyright 2017 Grigory Zubankov. All rights reserved.
// Use of this source code is governed by a MIT license
// that can be found in the LICENSE file.
//
// +build linux darwin
package zerodt
import (
"fmt"
"os"
"strconv"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestPrepareEnv(t *testing.T) {
os.Setenv("TEST_PREPARE_ENV", "EXISTS")
env := prepareEnv(7)
if len(env) < 3 {
t.Fail()
}
assert.NotEmpty(t, stringInSlice(env, "LISTEN_FDS=7"))
assert.NotEmpty(t, stringInSlice(env, "LISTEN_PID=0"))
assert.NotEmpty(t, stringInSlice(env, "TEST_PREPARE_ENV=EXISTS"))
}
func TestUnsetEnvAll(t *testing.T) {
os.Setenv("LISTEN_FDS", "7")
os.Setenv("LISTEN_PID", "0")
unsetEnvAll()
assert.Equal(t, "", os.Getenv("LISTEN_FDS"))
assert.Equal(t, "", os.Getenv("LISTEN_PID"))
}
func TestListenFdsCount(t *testing.T) {
// Normal exit without activation
setEnv("", "")
count, err := listenFdsCount()
require.NoError(t, err)
assert.Equal(t, 0, count)
// Bad LISTEN_PID
setEnv("not a pid", "")
count, err = listenFdsCount()
assertErr(t, err, "^bad environment variable: LISTEN_PID=not a pid$")
setEnv("1", "")
count, err = listenFdsCount()
assertErr(t, err, fmt.Sprintf("^bad environment variable: LISTEN_PID=1 with pid=%d$", os.Getpid()))
// No LISTEN_FDS
setEnv(strconv.Itoa(os.Getpid()), "")
count, err = listenFdsCount()
assertErr(t, err, "^mandatory environment variable does not exist: LISTEN_FDS$")
// Bad LISTEN_FDS
setEnv(strconv.Itoa(os.Getpid()), "not a number")
count, err = listenFdsCount()
assertErr(t, err, fmt.Sprintf("^bad environment variable: LISTEN_FDS=not a number$"))
setEnv(strconv.Itoa(os.Getpid()), "-2")
count, err = listenFdsCount()
assertErr(t, err, fmt.Sprintf("^bad environment variable: LISTEN_FDS=-2$"))
// All ok
setEnv(strconv.Itoa(os.Getpid()), "7")
count, err = listenFdsCount()
require.NoError(t, err)
assert.Equal(t, 7, count)
// All ok with default LISTEN_PID
setEnv("0", "148")
count, err = listenFdsCount()
require.NoError(t, err)
assert.Equal(t, 148, count)
}
func TestListenFds(t *testing.T) {
// Normal exit without activation
setEnv("", "")
fds, err := listenFds()
require.NoError(t, err)
assert.Equal(t, 0, len(fds))
// Normal exit with activation
setEnv("0", "2")
fds, err = listenFds()
require.NoError(t, err)
assert.Equal(t, []int{3, 4}, fds)
// Bad env
setEnv("not a pid", "")
_, err = listenFds()
assert.Error(t, err)
}
func setEnv(pid, fds string) {
unsetEnvAll()
if pid != "" {
os.Setenv(envListenPID, pid)
}
if fds != "" {
os.Setenv(envListenFDS, fds)
}
}
func stringInSlice(a []string, v string) bool {
for _, s := range a {
if s == v {
return true
}
}
return false
}
func assertErr(t *testing.T, e error, rx interface{}) {
assert.Error(t, e)
assert.Regexp(t, rx, e.Error())
}