-
Notifications
You must be signed in to change notification settings - Fork 460
/
client_unix_test.go
107 lines (97 loc) · 2.78 KB
/
client_unix_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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
//go:build !windows
// +build !windows
package plugin
import (
"fmt"
"os"
"os/exec"
"os/user"
"path/filepath"
"runtime"
"syscall"
"testing"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-plugin/internal/cmdrunner"
"github.com/hashicorp/go-plugin/runner"
)
func TestSetGroup(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("go-plugin doesn't support unix sockets on Windows")
}
group, err := user.LookupGroupId(fmt.Sprintf("%d", os.Getgid()))
if err != nil {
t.Fatal(err)
}
baseTempDir := t.TempDir()
baseTempDir, err = filepath.EvalSymlinks(baseTempDir)
if err != nil {
t.Fatal(err)
}
for name, tc := range map[string]struct {
group string
}{
"as integer": {fmt.Sprintf("%d", os.Getgid())},
"as name": {group.Name},
} {
t.Run(name, func(t *testing.T) {
process := helperProcess("mock")
c := NewClient(&ClientConfig{
HandshakeConfig: testHandshake,
Plugins: testPluginMap,
UnixSocketConfig: &UnixSocketConfig{
Group: tc.group,
TempDir: baseTempDir,
},
RunnerFunc: func(l hclog.Logger, cmd *exec.Cmd, tmpDir string) (runner.Runner, error) {
// Run tests inside the RunnerFunc to ensure we don't race
// with the code that deletes tmpDir when the client fails
// to start properly.
// Test that it creates a directory with the proper owners and permissions.
if filepath.Dir(tmpDir) != baseTempDir {
t.Errorf("Expected base TempDir to be %s, but tmpDir was %s", baseTempDir, tmpDir)
}
info, err := os.Lstat(tmpDir)
if err != nil {
t.Fatal(err)
}
if info.Mode()&os.ModePerm != 0o770 {
t.Fatal(info.Mode())
}
stat, ok := info.Sys().(*syscall.Stat_t)
if !ok {
t.Fatal()
}
if stat.Gid != uint32(os.Getgid()) {
t.Fatalf("Expected %d, but got %d", os.Getgid(), stat.Gid)
}
// Check the correct environment variables were set to forward
// Unix socket config onto the plugin.
var foundUnixSocketDir, foundUnixSocketGroup bool
for _, env := range cmd.Env {
if env == fmt.Sprintf("%s=%s", EnvUnixSocketDir, tmpDir) {
foundUnixSocketDir = true
}
if env == fmt.Sprintf("%s=%s", EnvUnixSocketGroup, tc.group) {
foundUnixSocketGroup = true
}
}
if !foundUnixSocketDir {
t.Errorf("Did not find correct %s env in %v", EnvUnixSocketDir, cmd.Env)
}
if !foundUnixSocketGroup {
t.Errorf("Did not find correct %s env in %v", EnvUnixSocketGroup, cmd.Env)
}
process.Env = append(process.Env, cmd.Env...)
return cmdrunner.NewCmdRunner(l, process)
},
})
defer c.Kill()
_, err := c.Start()
if err != nil {
t.Fatalf("err should be nil, got %s", err)
}
})
}
}