forked from kolesnikovae/go-winjob
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjob_object_test.go
More file actions
152 lines (133 loc) · 3.94 KB
/
Copy pathjob_object_test.go
File metadata and controls
152 lines (133 loc) · 3.94 KB
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
//go:build windows
package winjob_test
import (
"context"
"os"
"os/exec"
"strconv"
"syscall"
"testing"
"time"
"golang.org/x/sys/windows"
"github.com/aperturerobotics/go-winjob"
)
const (
commandName = "notepad.exe"
jobTestTimeout = 10 * time.Second
)
func newTestJobObject() (*winjob.JobObject, error) {
return winjob.Create("go-winjob-testing-" + strconv.FormatInt(time.Now().UnixNano(), 10))
}
// runTestWithTestJobObjectWithProcess creates a new test job object with
// a process and passes them to the provided jobTestFn.
func runTestWithTestJobObjectWithProcess(t *testing.T, jobTestFn func(*winjob.JobObject, *os.Process)) {
ctx, cancel := context.WithTimeout(context.Background(), jobTestTimeout)
defer cancel()
cmd := exec.CommandContext(ctx, commandName)
cmd.SysProcAttr = &windows.SysProcAttr{
CreationFlags: windows.CREATE_SUSPENDED,
}
requireNoError(t, cmd.Start(), "Starting process")
job, err := newTestJobObject()
requireNoError(t, err, "Creating job object")
defer func() {
requireNoError(t, job.Close(), "Closing job object")
}()
requireNoError(t, job.Assign(cmd.Process), "Assigning process to job object")
requireNoError(t, winjob.Resume(cmd), "Resuming")
defer func() {
requireNoError(t, job.Terminate(), "Terminate test job object")
}()
jobTestFn(job, cmd.Process)
}
func runTestWithEmptyJobObject(t *testing.T, jobTestFn func(*winjob.JobObject)) {
job, err := newTestJobObject()
requireNoError(t, err, "Creating job object")
defer func() {
requireNoError(t, job.Close(), "Closing job object")
}()
jobTestFn(job)
}
func requireNoError(t *testing.T, err error, msg ...string) {
if err != nil {
if len(msg) > 0 {
t.Fatalf("%s: %v", msg, err)
}
t.Fatalf("%v", err)
}
}
func TestInvalidJobObjectHandle(t *testing.T) {
var (
job = &winjob.JobObject{Handle: syscall.InvalidHandle}
process = new(os.Process)
counters winjob.Counters
)
requireError := func(t *testing.T, err error) {
if err == nil {
t.Fatalf("Expected error, got nil")
}
}
requireError(t, job.Assign(process))
requireError(t, job.Terminate())
_, err := job.Contains(process)
requireError(t, err)
requireError(t, job.QueryLimits())
requireError(t, job.QueryCounters(&counters))
_, err = job.HasLimits()
requireError(t, err)
requireError(t, job.ResetLimits())
requireError(t, job.ResetLimit(winjob.LimitBreakawayOK))
requireError(t, job.SetLimit(winjob.LimitCPU))
}
func TestCreateWithLimits(t *testing.T) {
job, err := winjob.Create("", winjob.WithBreakawayOK())
requireNoError(t, err)
defer func() {
requireNoError(t, job.Close())
}()
requireNoError(t, job.QueryLimits())
if !winjob.LimitBreakawayOK.IsSet(job) {
t.Fatal("Job object limit is not set")
}
}
func TestTerminate(t *testing.T) {
runTestWithTestJobObjectWithProcess(t, func(job *winjob.JobObject, p *os.Process) {
const exitCode = 3
requireNoError(t, job.TerminateWithExitCode(exitCode))
s, err := p.Wait()
requireNoError(t, err)
if s.ExitCode() != exitCode {
t.Fatalf("expected exit code %d, got %d", exitCode, s.ExitCode())
}
})
}
func TestContainsProcess(t *testing.T) {
runTestWithTestJobObjectWithProcess(t, func(job *winjob.JobObject, p *os.Process) {
contains, err := job.Contains(p)
requireNoError(t, err)
if !contains {
t.Fatal("Job does not contain the process specified")
}
})
}
func TestOpenJobObject(t *testing.T) {
runTestWithTestJobObjectWithProcess(t, func(job *winjob.JobObject, _ *os.Process) {
_, err := winjob.Open(job.Name)
requireNoError(t, err)
})
}
func TestOpenNonexistentJobObject(t *testing.T) {
if _, err := winjob.Open(time.Now().String()); err == nil {
t.Fatal("Open: expected error, got nil")
}
}
func TestCounters(t *testing.T) {
runTestWithTestJobObjectWithProcess(t, func(job *winjob.JobObject, _ *os.Process) {
counters, err := job.Counters()
requireNoError(t, err)
requireNoError(t, job.QueryCounters(counters))
if counters.ActiveProcesses == 0 {
t.Fatal("Empty counters")
}
})
}