forked from codeskyblue/kexec
-
Notifications
You must be signed in to change notification settings - Fork 2
/
kexec_test.go
65 lines (55 loc) · 1.32 KB
/
kexec_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
package kexec
import (
"os"
"os/user"
"syscall"
"testing"
"time"
. "github.com/smartystreets/goconvey/convey"
)
func TestCommand(t *testing.T) {
Convey("1 should equal 1", t, func() {
So(1, ShouldEqual, 1)
})
Convey("kexec should work as normal os/exec", t, func() {
cmd := Command("echo", "-n", "123")
data, err := cmd.Output()
So(err, ShouldBeNil)
So(string(data), ShouldEqual, "123")
})
Convey("the terminate should kill proc", t, func() {
cmd := CommandString("sleep 51")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Start()
time.Sleep(time.Millisecond * 50)
cmd.Terminate(syscall.SIGINT)
err := cmd.Wait()
So(err, ShouldNotBeNil)
//So(err.Error(), ShouldEqual, "signal: interrupt")
})
Convey("Should ok with call Wait twice", t, func() {
cmd := CommandString("not-exists-command-xxl213 true")
var err error
err = cmd.Start()
So(err, ShouldBeNil)
err1 := cmd.Wait()
So(err1, ShouldNotBeNil)
err2 := cmd.Wait()
So(err1, ShouldEqual, err2)
})
Convey("Set user works", t, func() {
u, err := user.Current()
So(err, ShouldBeNil)
// Set user must be root
if u.Uid != "0" {
return
}
cmd := Command("whoami")
err = cmd.SetUser("qard2")
So(err, ShouldBeNil)
output, err := cmd.Output()
So(err, ShouldBeNil)
So(string(output), ShouldEqual, "qard2\n")
})
}