-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprocimock.go
97 lines (85 loc) · 2.66 KB
/
procimock.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
package proci
import (
"fmt"
)
type ProcessMock struct{
Pid uint32
Path string
CommandLine string
MemoryUsage uint64
DoFailPath bool // If true, fail GetProcessPath
DoFailCommandLine bool // If true, fail GetProcessCommandLine
DoFailMemoryUsage bool // If true, fail GetProcessMemoryUsage
}
// ProciMock is a mock implementation for the proci Interface. It is intended
// for mocking of proci during unit testing.
type ProciMock struct{
MemStatus *MemoryStatus
DoFailMemStatus bool // If true, fail GetMemoryStatus
Processes map[uint32]*ProcessMock
}
// GenerateMock generate a mock with mock processes. It will start from
// PID 0 up to numberOfProcesses - 1.
func GenerateMock(numberOfProcesses int) *ProciMock {
memoryStatus := MemoryStatus{MemoryLoad: 50, TotalPhys: 4 * 1024*1024*1024, AvailPhys: 2 * 1024*1024*1024}
processes := make(map[uint32]*ProcessMock)
for i := 0; i < numberOfProcesses; i++ {
pid := uint32(i)
process := ProcessMock{
Pid : pid,
Path : fmt.Sprintf("path_%d", i),
CommandLine : fmt.Sprintf("command_line_%d", i),
MemoryUsage : uint64(1024 + i * 1024),
DoFailPath : false,
DoFailCommandLine : false,
DoFailMemoryUsage : false}
processes[pid] = &process
}
return &ProciMock{
MemStatus : &memoryStatus,
DoFailMemStatus : false,
Processes : processes}
}
func (s ProciMock) GetMemoryStatus() (*MemoryStatus, error) {
if s.DoFailMemStatus {
return nil, fmt.Errorf("GetMemoryStatus Mock intentional failure")
}
return s.MemStatus, nil
}
func (s ProciMock) GetProcessPids() []uint32 {
pids := make([]uint32, 0, len(s.Processes))
for pid, _ := range s.Processes {
pids = append(pids, pid)
}
return pids
}
func (s ProciMock) GetProcessMemoryUsage(pid uint32) (uint64, error) {
process, hasPid := s.Processes[pid]
if !hasPid {
return 0, fmt.Errorf("PID %d does not exist", pid)
}
if process.DoFailMemoryUsage {
return 0, fmt.Errorf("GetProcessMemoryUsage Mock intentional failure")
}
return process.MemoryUsage, nil
}
func (s ProciMock) GetProcessPath(pid uint32) (string, error) {
process, hasPid := s.Processes[pid]
if !hasPid {
return "", fmt.Errorf("PID %d does not exist", pid)
}
if process.DoFailPath {
return "", fmt.Errorf("GetProcessPath Mock intentional failure")
}
return process.Path, nil
}
func (s ProciMock) GetProcessCommandLine(pid uint32) (string, error) {
process, hasPid := s.Processes[pid]
if !hasPid {
return "", fmt.Errorf("PID %d does not exist", pid)
}
if process.DoFailCommandLine {
return "", fmt.Errorf("GetProcessCommandLine Mock intentional failure")
}
return process.CommandLine, nil
}