forked from canonical/lxd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_linux_test.go
52 lines (45 loc) · 1.32 KB
/
client_linux_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
package lxd
import (
"fmt"
"io/ioutil"
"os"
"syscall"
"testing"
)
func assertNoError(t *testing.T, err error, msg string) {
if err != nil {
t.Fatalf("Error: %s, action: %s", err, msg)
}
}
func TestLocalLXDError(t *testing.T) {
f, err := ioutil.TempFile("", "lxd-test.socket")
assertNoError(t, err, "ioutil.TempFile to create fake socket file")
defer os.RemoveAll(f.Name())
c := &Client{
Name: "test",
Config: DefaultConfig,
Remote: &RemoteConfig{
Addr: fmt.Sprintf("unix:%s", f.Name()),
Static: true,
Public: false,
},
}
runTest := func(exp error) {
lxdErr := GetLocalLXDErr(connectViaUnix(c, c.Remote))
if lxdErr != exp {
t.Fatalf("GetLocalLXDErr returned the wrong error, EXPECTED: %s, ACTUAL: %s", exp, lxdErr)
}
}
// The fake socket file should mimic a socket with nobody listening.
runTest(syscall.ECONNREFUSED)
// Remove R/W permissions to mimic the user not having lxd group permissions.
// Skip this test for root, as root ignores permissions and connect will fail
// with ECONNREFUSED instead of EACCES.
if os.Geteuid() != 0 {
assertNoError(t, f.Chmod(0100), "f.Chmod on fake socket file")
runTest(syscall.EACCES)
}
// Remove the fake socket to mimic LXD not being installed.
assertNoError(t, os.RemoveAll(f.Name()), "osRemoveAll on fake socket file")
runTest(syscall.ENOENT)
}