-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathnetns_linux_test.go
More file actions
140 lines (135 loc) · 2.58 KB
/
netns_linux_test.go
File metadata and controls
140 lines (135 loc) · 2.58 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
package rootlessnetns
import (
"os"
"path/filepath"
"strconv"
"testing"
"github.com/stretchr/testify/assert"
)
func Test_refCount(t *testing.T) {
tests := []struct {
name string
content string
inc int
want int
}{
{
name: "init counter",
inc: 1,
want: 1,
},
{
name: "simple add",
inc: 5,
want: 5,
},
{
name: "add multiple with content",
content: "0",
inc: 5,
want: 5,
},
{
name: "add multiple with high number content",
content: "5500",
inc: 2,
want: 5502,
},
{
name: "simple dec",
content: "5",
inc: -5,
want: 0,
},
{
name: "dec negative should not go below 0",
content: "0",
inc: -5,
want: 0,
},
{
name: "dec multiple with high number content",
content: "9800",
inc: -100,
want: 9700,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
dir := t.TempDir()
file := filepath.Join(dir, refCountFile)
if tt.content != "" {
err := os.WriteFile(file, []byte(tt.content), 0o700)
assert.NoError(t, err, "write file error")
}
got, err := refCount(dir, tt.inc)
assert.NoError(t, err, "refCount() error")
assert.Equal(t, tt.want, got, "counter is equal")
content, err := os.ReadFile(file)
assert.NoError(t, err, "read file error")
assert.Equal(t, strconv.Itoa(tt.want), string(content), "file content after refCount()")
})
}
}
func TestReadPidFile(t *testing.T) {
tests := []struct {
name string
content string
create bool
wantErr error
wantPid int
}{
{
name: "valid pid",
content: "1234\n",
create: true,
wantPid: 1234,
},
{
name: "valid pid no newline",
content: "5678",
create: true,
wantPid: 5678,
},
{
name: "empty pid file",
content: "",
create: true,
wantErr: errEmptyPIDFile,
},
{
name: "only whitespace",
content: " \n ",
create: true,
wantErr: errEmptyPIDFile,
},
{
name: "non-existent file",
create: false,
wantErr: os.ErrNotExist,
},
{
name: "invalid content",
content: "abc",
create: true,
wantErr: strconv.ErrSyntax,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
path := filepath.Join(t.TempDir(), "pidfile")
if tt.create {
err := os.WriteFile(path, []byte(tt.content), 0o600)
assert.NoError(t, err)
}
pid, err := readPidFile(path)
if tt.wantErr != nil {
assert.Error(t, err)
assert.ErrorIs(t, err, tt.wantErr)
} else {
assert.NoError(t, err)
assert.Equal(t, tt.wantPid, pid)
}
})
}
}