-
Notifications
You must be signed in to change notification settings - Fork 326
/
main_test.go
100 lines (84 loc) · 2.46 KB
/
main_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
package main
import (
"io/fs"
"os"
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/vouch/vouch-proxy/pkg/cfg"
)
func Test_listenUds(t *testing.T) {
setUp(t, "testing/socket_basic.yml")
defer cleanUp()
tempDir, err := os.MkdirTemp("", "")
assert.NoError(t, err)
defer func() {
_ = os.RemoveAll(tempDir)
}()
socketPath := filepath.Join(tempDir, "socket0")
cfg.Cfg.Listen = strings.Join([]string{"unix", socketPath}, ":")
lis, cleanupFn, err := listen()
assert.NoError(t, err)
assertSocket(t, socketPath)
fi, err := os.Stat(socketPath)
assert.NoError(t, err)
assert.Equal(t, fs.FileMode(0660), fi.Mode().Perm())
assert.NotNil(t, lis)
assert.NoError(t, lis.Close())
cleanupFn()
_, err = os.Stat(socketPath)
assert.True(t, os.IsNotExist(err))
}
// check that socket listening works when the socket path already exists
func Test_listenUds_alreadyExists(t *testing.T) {
setUp(t, "testing/socket_basic.yml")
defer cleanUp()
tempDir, err := os.MkdirTemp("", "")
assert.NoError(t, err)
defer func() {
_ = os.RemoveAll(tempDir)
}()
socketPath := filepath.Join(tempDir, "socket0")
assert.NoError(t, os.WriteFile(socketPath, []byte("stuff in the socket file"), 0600))
cfg.Cfg.Listen = strings.Join([]string{"unix", socketPath}, ":")
lis, cleanupFn, err := listen()
assert.NoError(t, err)
assertSocket(t, socketPath)
assert.NotNil(t, lis)
assert.NoError(t, lis.Close())
cleanupFn()
}
// check that the socket mode is adjusted when the SocketMode configuration is present
func Test_listenUds_mode(t *testing.T) {
setUp(t, "config/testing/socket_mode.yml")
defer cleanUp()
tempDir, err := os.MkdirTemp("", "")
assert.NoError(t, err)
defer func() {
_ = os.RemoveAll(tempDir)
}()
socketPath := filepath.Join(tempDir, "socket0")
cfg.Cfg.Listen = strings.Join([]string{"unix", socketPath}, ":")
lis, cleanupFn, err := listen()
assert.NoError(t, err)
assert.NotNil(t, lis)
assertSocket(t, socketPath)
stat, err := os.Stat(socketPath)
assert.NoError(t, err)
assert.Equal(t, fs.FileMode(cfg.Cfg.SocketMode), stat.Mode().Perm())
assert.NoError(t, lis.Close())
cleanupFn()
}
func assertSocket(t *testing.T, socketPath string) {
fi, err := os.Stat(socketPath)
assert.NoError(t, err)
assert.Equal(t, os.ModeSocket, fi.Mode()&os.ModeSocket)
}
func setUp(t *testing.T, configFile string) {
assert.NoError(t, os.Setenv(cfg.Branding.UCName+"_CONFIG", configFile))
cfg.InitForTestPurposes()
}
func cleanUp() {
os.Clearenv()
}