-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcpu_test.go
145 lines (121 loc) · 4.38 KB
/
cpu_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
package main
import (
"errors"
"fmt"
"strconv"
"testing"
"time"
"github.com/shirou/gopsutil/cpu"
"github.com/stretchr/testify/assert"
)
// TestGetCPUStat test the returned fields values of `getCPUStat()`
func TestGetCPUStat(t *testing.T) {
// setup the faking of `cpu.Info()`
oldcpuInfo := cpuInfo
cpuInfo = func() ([]cpu.InfoStat, error) {
ret := []cpu.InfoStat{
{
VendorID: string("vendor"),
ModelName: string("model"),
Mhz: float64(100),
},
{
VendorID: string("vendor"), // two CPUs --> cpuinfo.count = "2"
ModelName: string("model"),
Mhz: float64(100),
},
}
return ret, nil
}
// test
expected := cpuStat{
count: strconv.FormatInt(2, 10),
vendorID: "vendor",
modelName: "model",
mhz: strconv.FormatInt(100, 10),
}
actual, err := getCPUStat()
assert.NoError(t, err, "`getCPUStat()` should not have returned an error")
assert.Equal(t, expected, actual, "`getCPUStat()` should be equal to main.cpuStat{count:\"2\", vendorID:\"vendor\", modelName:\"model\", mhz:\"100\"}")
// teardown
cpuInfo = oldcpuInfo
}
// TestGetCPUStatErrorCase1 test than getCPUStat() transmit the error from cpu.Info()
func TestGetCPUStatErrorCase1(t *testing.T) {
// setup the faking of `cpu.Info()`
oldcpuInfo := cpuInfo
cpuInfo = func() ([]cpu.InfoStat, error) {
err := errors.New("Error 1")
return nil, err
}
//test
expected := errors.New("Error 1")
_, actual := getCPUStat()
assert.EqualError(t, expected, fmt.Sprintf("%v", actual), "`getCPUStat()` should be an error equal to \"Error 1\"")
// teardown
cpuInfo = oldcpuInfo
}
// TestGetCPUStatType test if `getCPUStat()` return a `cpuStat` type and if each fields have the correct types
func TestGetCPUStatType(t *testing.T) {
expected := cpuStat{
count: "", // the result values of the fields are not tested
vendorID: "",
modelName: "",
mhz: "",
}
actual, _ := getCPUStat()
assert.IsType(t, expected, actual, "`getCPUStat()` should return a `cpuStat` type")
assert.IsType(t, expected.count, actual.count, "`getCPUStat()` should return a `count` field with a string type")
assert.IsType(t, expected.vendorID, actual.vendorID, "`getCPUStat()` should return a `vendorID` field with a string type")
assert.IsType(t, expected.modelName, actual.modelName, "`getCPUStat()` should return a `modelName` field with a string type")
assert.IsType(t, expected.mhz, actual.mhz, "`getCPUStat()` should return a `mhz` field with a string type")
}
// TestGetCPUPercent test the returned value of `getCPUPercent()`
func TestGetCPUPercent(t *testing.T) {
// setup the faking of `cpu.Percent()`
oldcpuPercent := cpuPercent
cpuPercent = func(interval time.Duration, percpu bool) ([]float64, error) {
ret := []float64{100}
return ret, nil
}
// test
expected := 100
actual, err := getCPUPercent()
assert.NoError(t, err, "`getCPUPercent()` should not have returned an error")
assert.Equal(t, expected, actual, "`getCPUPercent` should be equal to --> 100")
// teardown
cpuPercent = oldcpuPercent
}
// TestGetCPUPercentErrorCase1 test than getCPUPercent() transmit the error from cpu.Percent()
func TestGetCPUPercentErrorCase1(t *testing.T) {
// setup the faking of `cpu.Percent()`
oldcpuPercent := cpuPercent
cpuPercent = func(interval time.Duration, percpu bool) ([]float64, error) {
err := errors.New("Error 1")
return nil, err
}
//test
expected := errors.New("Error 1")
_, actual := getCPUPercent()
assert.EqualError(t, expected, fmt.Sprintf("%v", actual), "`getCPUPercent()` should be an error equal to \"Error 1\"")
// teardown
cpuPercent = oldcpuPercent
}
// TestGetCPUPercentType test if `getCPUPercent()` return a value with a int type
func TestGetCPUPercentType(t *testing.T) {
expected := int(0) // the result value is not tested
actual, _ := getCPUPercent()
assert.IsType(t, expected, actual, "`getCPUPercent()` should return an `int`")
}
// TestCpuInfo test if `cpu.Info()` return a value with a []cpu.InfoStat slice
func TestCpuInfo(t *testing.T) {
expected := []cpu.InfoStat{}
actual, _ := cpu.Info()
assert.IsType(t, expected, actual, "`cpuInfo()` should return a []cpu.InfoStat slice")
}
// TestCpuPercent test if `cpu.Percent()` return a value with a []float64 slice
func TestCpuPercent(t *testing.T) {
expected := []float64{}
actual, _ := cpuPercent((500 * time.Millisecond), false)
assert.IsType(t, expected, actual, "`cpuPercent()` should return a []float64 slice")
}