-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcommands_test.go
159 lines (136 loc) · 3.74 KB
/
commands_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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
* Copyright (C) 2020-2021 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package commands_test
import (
"io/ioutil"
"os"
"os/exec"
"os/user"
"path/filepath"
"testing"
"github.com/anonymouse64/etrace/internal/commands"
. "gopkg.in/check.v1"
)
func Test(t *testing.T) { TestingT(t) }
type commandsTestSuite struct {
tmpDir string
}
var _ = Suite(&commandsTestSuite{})
func (s *commandsTestSuite) SetUpTest(c *C) {
}
func (s *commandsTestSuite) TestAddSudoIfNeededCaches(c *C) {
n := 0
restore := commands.MockUserCurrent(func() (*user.User, error) {
n++
return &user.User{
Uid: "0",
}, nil
})
defer restore()
cmd := exec.Command("hello", "world")
err := commands.AddSudoIfNeeded(cmd)
c.Assert(err, IsNil)
// only called once so far
c.Assert(n, Equals, 1)
// not called again
err = commands.AddSudoIfNeeded(cmd)
c.Assert(err, IsNil)
// only called once so far
c.Assert(n, Equals, 1)
}
func (s *commandsTestSuite) TestAddSudoIfNeeded(c *C) {
// set PATH to a tmp dir to mock exec.LookPath
tmpDir := c.MkDir()
oldPath := os.Getenv("PATH")
os.Setenv("PATH", tmpDir)
defer func() {
os.Setenv("PATH", oldPath)
}()
sudoPath := filepath.Join(tmpDir, "sudo")
tt := []struct {
sudoExists bool
uid string
expectedErrPattern string
cmd *exec.Cmd
expectedCmd *exec.Cmd
sudoArgs []string
comment string
}{
{
sudoExists: true,
uid: "0",
cmd: &exec.Cmd{Args: []string{"foo"}},
expectedCmd: &exec.Cmd{Args: []string{"foo"}},
comment: "running as root, sudo exists",
},
{
uid: "0",
cmd: &exec.Cmd{Args: []string{"foo"}},
expectedCmd: &exec.Cmd{Args: []string{"foo"}},
comment: "running as root, sudo does not exist",
},
{
sudoExists: true,
uid: "1000",
cmd: &exec.Cmd{Args: []string{"foo"}},
expectedCmd: &exec.Cmd{Path: sudoPath, Args: []string{sudoPath, "foo"}},
comment: "running as user, sudo exists",
},
{
uid: "1000",
cmd: &exec.Cmd{Args: []string{"foo"}},
expectedErrPattern: `cannot use strace without running as root or without sudo: exec: "sudo": executable file not found in \$PATH`,
comment: "running as user, sudo does not exists",
},
}
var restore func()
for _, t := range tt {
// mock sudo executable
if t.sudoExists {
err := ioutil.WriteFile(sudoPath, []byte{}, 0755)
c.Assert(err, IsNil, Commentf(t.comment))
}
// mock the current user
if t.uid != "" {
restore = commands.MockUserCurrent(func() (*user.User, error) {
return &user.User{
Uid: t.uid,
}, nil
})
}
// do the test
err := commands.AddSudoIfNeeded(t.cmd, t.sudoArgs...)
if t.expectedErrPattern != "" {
// check the error
c.Assert(err, ErrorMatches, t.expectedErrPattern, Commentf(t.comment))
} else {
c.Assert(err, IsNil)
// check the cmd
c.Assert(t.cmd, DeepEquals, t.expectedCmd, Commentf(t.comment))
}
// un-mock the current user
if t.uid != "" {
restore()
}
// un-mock sudo
if t.sudoExists {
c.Assert(os.Remove(sudoPath), IsNil, Commentf(t.comment))
}
// reset the caching
commands.ResetInitialized()
}
}