-
Notifications
You must be signed in to change notification settings - Fork 89
/
username_test.go
87 lines (74 loc) · 1.68 KB
/
username_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
// Copyright 2015 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package utils_test
import (
"github.com/juju/errors"
"github.com/juju/testing"
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
"github.com/juju/utils/v4"
)
var _ = gc.Suite(&usernameSuite{})
type usernameSuite struct {
testing.IsolationSuite
}
func (s *usernameSuite) TestResolveUsername(c *gc.C) {
type test struct {
userEnv string
sudoEnv string
userOS string
expected string
err string
}
tests := []test{{
userEnv: "someone",
sudoEnv: "notroot",
userOS: "other",
expected: "someone",
}, {
userOS: "other",
expected: "other",
}, {
userEnv: "root",
expected: "root",
}, {
userEnv: "root",
sudoEnv: "other",
expected: "other",
}, {
err: "failed to determine username for namespace: oh noes",
}}
resolveUsername := func(t test) (string, error) {
if t.err != "" {
return "", errors.Errorf(t.err)
}
var funcs []func() (string, error)
if t.userEnv != "" {
funcs = append(funcs, func() (string, error) {
return t.userEnv, nil
})
}
if t.userOS != "" {
funcs = append(funcs, func() (string, error) {
return t.userOS, nil
})
}
resolveSudo := func(username string) string {
return utils.ResolveSudoByFunc(username, func(string) string {
return t.sudoEnv
})
}
return utils.ResolveUsername(resolveSudo, funcs...)
}
for i, test := range tests {
c.Logf("test %d: %v", i, test)
username, err := resolveUsername(test)
if test.err == "" {
if c.Check(err, jc.ErrorIsNil) {
c.Check(username, gc.Equals, test.expected)
}
} else {
c.Check(err, gc.ErrorMatches, test.err)
}
}
}