-
Notifications
You must be signed in to change notification settings - Fork 315
/
util_test_tool.go
220 lines (187 loc) · 5.34 KB
/
util_test_tool.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
Copyright 2022 The Koordinator Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package system
import (
"io/ioutil"
"os"
"path"
"runtime"
"testing"
"github.com/vishvananda/netlink"
"github.com/vishvananda/netns"
)
type FileTestUtil struct {
// Temporary directory to store mock cgroup filesystem.
TempDir string
t *testing.T
}
// Creates a new test util for the specified subsystem
func NewFileTestUtil(t *testing.T) *FileTestUtil {
tempDir, err := ioutil.TempDir("/tmp", "koordlet_test")
HostSystemInfo.IsAliOS = true
if err != nil {
t.Fatal(err)
}
Conf.ProcRootDir = path.Join(tempDir, "proc")
os.MkdirAll(Conf.ProcRootDir, 0777)
Conf.CgroupRootDir = tempDir
CommonRootDir = tempDir
return &FileTestUtil{TempDir: tempDir, t: t}
}
func (c *FileTestUtil) Cleanup() {
os.RemoveAll(c.TempDir)
}
func (c *FileTestUtil) CreateFiles(files []string) {
for _, file := range files {
filePath := path.Join(c.TempDir, file)
if _, err := os.Create(filePath); err != nil {
c.t.Fatal(err)
}
}
}
func (c *FileTestUtil) MkDirAll(dirRelativePath string) {
dir := path.Join(c.TempDir, dirRelativePath)
if err := os.MkdirAll(dir, 0777); err != nil {
c.t.Fatal(err)
}
}
func (c *FileTestUtil) CreateFile(fileRelativePath string) {
filePath := path.Join(c.TempDir, fileRelativePath)
dir, _ := path.Split(filePath)
if err := os.MkdirAll(dir, 0777); err != nil {
c.t.Fatal(err)
}
if _, err := os.Create(filePath); err != nil {
c.t.Fatal(err)
}
}
func (c *FileTestUtil) WriteFileContents(fileRelativePath, contents string) {
filePath := path.Join(c.TempDir, fileRelativePath)
err := ioutil.WriteFile(filePath, []byte(contents), 0644)
if err != nil {
c.t.Fatal(err)
}
}
func (c *FileTestUtil) ReadFileContents(fileRelativePath string) string {
filePath := path.Join(c.TempDir, fileRelativePath)
contents, err := ioutil.ReadFile(filePath)
if err != nil {
c.t.Fatal(err)
}
return string(contents)
}
func (c *FileTestUtil) GetAbsoluteFilePath(fileRelativePath string) string {
return path.Join(c.TempDir, fileRelativePath)
}
func (c *FileTestUtil) MkProcSubDirAll(dirRelativePath string) {
dir := path.Join(Conf.ProcRootDir, dirRelativePath)
if err := os.MkdirAll(dir, 0777); err != nil {
c.t.Fatal(err)
}
}
func (c *FileTestUtil) CreateProcSubFile(fileRelativePath string) {
file := path.Join(Conf.ProcRootDir, fileRelativePath)
dir, _ := path.Split(file)
if err := os.MkdirAll(dir, 0777); err != nil {
c.t.Fatal(err)
}
if _, err := os.Create(file); err != nil {
c.t.Fatal(err)
}
}
func (c *FileTestUtil) WriteProcSubFileContents(relativeFilePath string, contents string) {
file := path.Join(Conf.ProcRootDir, relativeFilePath)
if !FileExists(file) {
c.CreateProcSubFile(file)
}
err := ioutil.WriteFile(file, []byte(contents), 0644)
if err != nil {
c.t.Fatal(err)
}
}
func (c *FileTestUtil) ReadProcSubFileContents(relativeFilePath string) string {
file := path.Join(Conf.ProcRootDir, relativeFilePath)
contents, err := ioutil.ReadFile(file)
if err != nil {
c.t.Fatal(err)
}
return string(contents)
}
func (c *FileTestUtil) CreateCgroupFile(taskDir string, file CgroupFile) {
filePath := GetCgroupFilePath(taskDir, file)
dir, _ := path.Split(filePath)
if err := os.MkdirAll(dir, 0777); err != nil {
c.t.Fatal(err)
}
if _, err := os.Create(filePath); err != nil {
c.t.Fatal(err)
}
}
func (c *FileTestUtil) WriteCgroupFileContents(taskDir string, file CgroupFile, contents string) {
filePath := GetCgroupFilePath(taskDir, file)
if !FileExists(filePath) {
c.CreateCgroupFile(taskDir, file)
}
err := CgroupFileWrite(taskDir, file, contents)
if err != nil {
c.t.Fatal(err)
}
}
func (c *FileTestUtil) ReadCgroupFileContents(taskDir string, file CgroupFile) string {
contents, err := CgroupFileRead(taskDir, file)
if err != nil {
c.t.Fatal(err)
}
return contents
}
type NetworkTestUtil struct {
t *testing.T
ns netns.NsHandle
}
func NewNetworkTestUtil(t *testing.T) *NetworkTestUtil {
if os.Getuid() != 0 {
t.Skip("Test requires root privileges.")
}
// new temporary namespace so we don't pollute the host
// lock thread since the namespace is thread local
runtime.LockOSThread()
var err error
ns, err := netns.New()
if err != nil {
t.Fatal("Failed to create newns", ns)
}
return &NetworkTestUtil{t: t, ns: ns}
}
func (n *NetworkTestUtil) Cleanup() {
n.ns.Close()
runtime.UnlockOSThread()
}
func (n *NetworkTestUtil) SetupDummyLinkWithAddr() *netlink.Dummy {
dummyLink := netlink.Dummy{
LinkAttrs: netlink.LinkAttrs{
Index: 24,
Name: "foo",
},
}
dummyAddr, _ := netlink.ParseAddr("192.168.10.1/24")
if err := netlink.LinkAdd(&dummyLink); err != nil {
n.t.Fatal("Failed to create link", err)
}
if err := netlink.LinkSetUp(&dummyLink); err != nil {
n.t.Fatal("Failed to up link", err)
}
if err := netlink.AddrAdd(&dummyLink, dummyAddr); err != nil {
n.t.Fatal("Failed to add address to link", err)
}
return &dummyLink
}