-
Notifications
You must be signed in to change notification settings - Fork 0
/
ifstat_test.go
98 lines (84 loc) · 1.51 KB
/
ifstat_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
package main
import (
"bytes"
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func Test_checkIfaceExists(t *testing.T) {
tests := []struct {
name string
ifname string
wantErr bool
}{
{
name: "lo",
ifname: "lo",
},
{
name: "really not interface",
wantErr: true,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
err := checkIfaceExists(tt.ifname)
if (err != nil) != tt.wantErr {
assert.NoError(t, err)
}
})
}
t.Run("new stat", func(t *testing.T) {
newStat(time.Second, "eno1", "eno1", "", "error")
})
}
func TestIfStat_runDetached(t *testing.T) {
var (
out = new(bytes.Buffer)
stat = &ifStat{delay: time.Second, out: out}
data = make(chan pair)
item = pair{1, 1}
line = fmt.Sprintf(printFormat, item.rx, item.tx)
lineCount = 10
wantLines string
)
for i := 0; i < lineCount-2; i++ {
wantLines += line
}
go stat.printDetached(data)
defer close(data)
for i := 0; i < lineCount; i++ {
data <- item
item.rx++
item.tx++
}
assert.Equal(t, wantLines, out.String())
}
func TestByteSpeedString(t *testing.T) {
tests := []struct {
args netSpeed
wantRes string
}{
{
args: 1,
wantRes: " 1.0B ",
},
{
args: 1024,
wantRes: " 1.0Kb",
},
{
args: 2 * 1025,
wantRes: " 2.0Kb",
},
{
args: 5 * 1024 * 1024,
wantRes: " 5.0Mb",
},
}
for _, tt := range tests {
assert.Equal(t, tt.wantRes, tt.args.String())
}
}