-
Notifications
You must be signed in to change notification settings - Fork 5
/
kmod_test.go
178 lines (149 loc) · 3.46 KB
/
kmod_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
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
package kmod
import (
"bytes"
"log"
"os"
"path/filepath"
"testing"
"golang.org/x/sys/unix"
)
func createFiles() (string, []module) {
writeFile := func(path, text string) {
if err := os.WriteFile(path, []byte(text), 0o600); err != nil {
log.Fatal(err)
}
}
var u unix.Utsname
if err := unix.Uname(&u); err != nil {
log.Fatal(err)
}
kernelVersion := string(u.Release[:bytes.IndexByte(u.Release[:], 0)])
rootdir, err := os.MkdirTemp("", "kmod")
if err != nil {
log.Fatal(err)
}
moddir := filepath.Join(rootdir, kernelVersion)
if err := os.Mkdir(moddir, 0o700); err != nil {
log.Fatal(err)
}
text := "kernel/a/foo.ko:\n"
text += "kernel/a/bar.ko: kernel/a/foo.ko\n"
text += "kernel/a/baz.ko: kernel/a/bar.ko kernel/a/foo.ko\n"
writeFile(filepath.Join(moddir, "modules.dep"), text)
text = "kernel/a/foo_bi.ko\n"
text += "kernel/a/bar-bi.ko.gz\n"
writeFile(filepath.Join(moddir, "modules.builtin"), text)
text = "alias ignore ignore\n"
text += "alias foo_a foo\n"
text += "alias bar-a bar\n"
writeFile(filepath.Join(moddir, "modules.alias"), text)
text = "options foo foo1 foo2\n"
text += "options bar bar1\n"
writeFile(filepath.Join(rootdir, "modprobe.conf"), text)
modules := []module{
{name: "foo", path: "kernel/a/foo.ko", params: "foo1 foo2"},
{name: "bar", path: "kernel/a/bar.ko", params: "bar1"},
{name: "baz", path: "kernel/a/baz.ko"},
}
return rootdir, modules
}
func TestIsBuiltin(t *testing.T) {
rootdir, _ := createFiles()
defer os.RemoveAll(rootdir)
k, err := New(SetRootDir(rootdir))
if err != nil {
t.Fatal(err)
}
tests := []struct {
name string
want bool
}{
{"", false},
{"none", false},
{"foo_bi", true},
{"bar_bi", true},
}
for _, tt := range tests {
ok, err := k.isBuiltin(tt.name)
if err != nil {
t.Fatal(err)
}
if ok != tt.want {
t.Fatalf("%s want:%t got:%t", tt.name, tt.want, ok)
}
}
}
func TestApplyConfig(t *testing.T) {
rootdir, modules := createFiles()
defer os.RemoveAll(rootdir)
path := filepath.Join(rootdir, "modprobe.conf")
k, err := New(SetRootDir(rootdir), SetConfigFile(path))
if err != nil {
t.Fatal(err)
}
var tests []module
for _, m := range modules {
m.params = ""
tests = append(tests, m)
}
if err := k.applyConfig(tests); err != nil {
t.Fatal(err)
}
for i := range tests {
if modules[i] != tests[i] {
t.Fatalf("want:%v got:%v", modules[i], tests[i])
}
}
}
func TestOptionIgnoreStatus(t *testing.T) {
rootdir, _ := createFiles()
defer os.RemoveAll(rootdir)
{
k, err := New(SetRootDir(rootdir))
if err != nil {
t.Fatal(err)
}
want := unloaded
status, err := k.modStatus("foo")
if err != nil {
t.Fatal(err)
}
if status != want {
t.Fatalf("want:%v got:%v", want, status)
}
}
{
k, err := New(SetRootDir(rootdir), SetIgnoreStatus())
if err != nil {
t.Fatal(err)
}
want := unknown
status, err := k.modStatus("foo")
if err != nil {
t.Fatal(err)
}
if status != want {
t.Fatalf("want:%v got:%v", want, status)
}
}
}
func TestDependencies(t *testing.T) {
rootdir, modules := createFiles()
defer os.RemoveAll(rootdir)
k, err := New(SetRootDir(rootdir))
if err != nil {
t.Fatal(err)
}
list, err := k.Dependencies("baz")
if err != nil {
t.Fatal(err)
}
if len(list) != len(modules) {
t.Fatalf("len(dependency list) want: %d got:%d", len(modules), len(list))
}
for i := len(modules) - 2; i <= 0; i-- {
if modules[i].path != list[i] {
t.Fatalf("want:%v got:%v", modules[i].path, list[i])
}
}
}