Skip to content

Commit 2b50e4a

Browse files
committed
add testcases for cos and ubuntu to retrieve modules
1 parent 944efce commit 2b50e4a

File tree

5 files changed

+117
-31
lines changed

5 files changed

+117
-31
lines changed

pkg/util/metrics/system/common.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
)
2020

2121
// ReadFile reads contents from a file and returns lines.
22-
func ReadFile(filename string) ([]string, error) {
22+
func ReadFileIntoLines(filename string) ([]string, error) {
2323
file, err := os.Open(filename)
2424
if err != nil {
2525
return nil, err

pkg/util/metrics/system/module_stats.go

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,29 @@ import (
2020
"strings"
2121
)
2222

23-
type ModuleStat struct {
23+
var modulesFilePath = "/proc/modules"
24+
25+
type Module struct {
2426
ModuleName string `json:"moduleName"`
2527
Instances uint64 `json:"instances"`
2628
Proprietary bool `json:"proprietary"`
2729
OutOfTree bool `json:"outOfTree"`
2830
Unsigned bool `json:"unsigned"`
2931
}
3032

31-
func (d ModuleStat) String() string {
33+
func (d Module) String() string {
3234
s, _ := json.Marshal(d)
3335
return string(s)
3436
}
3537

3638
// Module returns all the kernel modules and their
3739
// usage. It is read from cat /proc/modules.
38-
func Modules() ([]ModuleStat, error) {
39-
filename := "/proc/modules"
40-
lines, err := ReadFile(filename)
40+
func Modules() ([]Module, error) {
41+
lines, err := ReadFileIntoLines(modulesFilePath)
4142
if err != nil {
42-
return nil, fmt.Errorf("Error reading the contents of %s: %s", filename, err)
43+
return nil, fmt.Errorf("error reading the contents of %s: %s", modulesFilePath, err)
4344
}
44-
var result = make([]ModuleStat, 0, len(lines))
45+
var result = make([]Module, 0, len(lines))
4546

4647
/* a line of /proc/modules has the following structure
4748
nf_nat 61440 2 xt_MASQUERADE,iptable_nat, Live 0x0000000000000000 (O)
@@ -58,28 +59,23 @@ func Modules() ([]ModuleStat, error) {
5859
fields := strings.Fields(line)
5960
moduleName := fields[0] // name of the module
6061
numberOfInstances, err :=
61-
strconv.ParseUint((fields[1]), 10, 64) // instances of the module are currently loaded
62+
strconv.ParseUint((fields[2]), 10, 64) // instances of the module are currently loaded
6263
if err != nil {
63-
return nil, err
64+
numberOfInstances = 0
6465
}
6566

66-
var isProprietary = false
67-
var isOutofTree = false
68-
var isUnsigned = false
67+
var module = Module{
68+
ModuleName: moduleName,
69+
Instances: numberOfInstances,
70+
}
6971
// if the len of the fields is greater than 6, then the kernel taint state is available.
7072
if len(fields) > 6 {
71-
isProprietary = strings.Contains(fields[6], "P")
72-
isOutofTree = strings.Contains(fields[6], "O")
73-
isUnsigned = strings.Contains(fields[6], "E")
73+
module.Proprietary = strings.Contains(fields[6], "P")
74+
module.OutOfTree = strings.Contains(fields[6], "O")
75+
module.Unsigned = strings.Contains(fields[6], "E")
7476
}
75-
var stats = ModuleStat{
76-
ModuleName: moduleName,
77-
Instances: numberOfInstances,
78-
Proprietary: isProprietary,
79-
OutOfTree: isOutofTree,
80-
Unsigned: isUnsigned,
81-
}
82-
result = append(result, stats)
77+
78+
result = append(result, module)
8379
}
8480
return result, nil
8581
}

pkg/util/metrics/system/module_stats_test.go

Lines changed: 89 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,106 @@ import (
2121
)
2222

2323
func TestModules(t *testing.T) {
24-
modules, err := Modules()
25-
if err != nil {
26-
t.Errorf("error %v", err)
24+
testcases := []struct {
25+
name string
26+
fakeModuleFilePath string
27+
expectedModules []Module
28+
}{
29+
{
30+
name: "default_cos",
31+
fakeModuleFilePath: "testdata/modules_cos.txt",
32+
expectedModules: []Module{
33+
{
34+
ModuleName: "crypto_simd",
35+
Instances: 0x1,
36+
Proprietary: false,
37+
OutOfTree: false,
38+
Unsigned: false,
39+
},
40+
{
41+
ModuleName: "virtio_balloon",
42+
Instances: 0x0,
43+
Proprietary: false,
44+
OutOfTree: false,
45+
Unsigned: false,
46+
},
47+
{
48+
ModuleName: "cryptd",
49+
Instances: 0x1,
50+
Proprietary: false,
51+
OutOfTree: false,
52+
Unsigned: false,
53+
},
54+
{
55+
ModuleName: "loadpin_trigger",
56+
Instances: 0x0,
57+
Proprietary: false,
58+
OutOfTree: true,
59+
Unsigned: false,
60+
},
61+
},
62+
},
63+
{
64+
name: "default_ubuntu",
65+
fakeModuleFilePath: "testdata/modules_ubuntu.txt",
66+
expectedModules: []Module{
67+
{
68+
ModuleName: "drm",
69+
Instances: 0x0,
70+
Proprietary: false,
71+
OutOfTree: false,
72+
Unsigned: false,
73+
},
74+
{
75+
ModuleName: "virtio_rng",
76+
Instances: 0x0,
77+
Proprietary: false,
78+
OutOfTree: false,
79+
Unsigned: false,
80+
},
81+
{
82+
ModuleName: "x_tables",
83+
Instances: 0x1,
84+
Proprietary: false,
85+
OutOfTree: false,
86+
Unsigned: false,
87+
},
88+
{
89+
ModuleName: "autofs4",
90+
Instances: 0x2,
91+
Proprietary: false,
92+
OutOfTree: false,
93+
Unsigned: false,
94+
},
95+
},
96+
},
2797
}
28-
if modules == nil {
29-
t.Error("Error retrieving modules")
98+
for _, test := range testcases {
99+
t.Run(test.name, func(t *testing.T) {
100+
originalModuleFilePath := modulesFilePath
101+
defer func() {
102+
modulesFilePath = originalModuleFilePath
103+
}()
104+
105+
modulesFilePath = test.fakeModuleFilePath
106+
modules, err := Modules()
107+
if err != nil {
108+
t.Errorf("Unexpected error retrieving modules: %v\nModulesFilePath: %s\n", err, modulesFilePath)
109+
}
110+
assert.Equal(t, modules, test.expectedModules, "unpected modules retrieved: %v, expected: %v", modules, test.expectedModules)
111+
})
30112
}
31113
}
32114

33115
func TestModuleStat_String(t *testing.T) {
34-
v := ModuleStat{
116+
v := Module{
35117
ModuleName: "test",
36118
Instances: 2,
37119
OutOfTree: false,
38120
Unsigned: false,
39121
}
40122
e := `{"moduleName":"test","instances":2,"proprietary":false,"outOfTree":false,"unsigned":false}`
41123
assert.Equal(t,
42-
e, fmt.Sprintf("%v", v), "ModuleStat string is invalid: %v", v)
124+
e, fmt.Sprintf("%v", v), "Module string is invalid: %v", v)
43125

44126
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
crypto_simd 16384 1 aesni_intel, Live 0x0000000000000000
2+
virtio_balloon 24576 0 - Live 0x0000000000000000
3+
cryptd 24576 1 crypto_simd, Live 0x0000000000000000
4+
loadpin_trigger 12288 0 [permanent], Live 0x0000000000000000 (O)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
drm 491520 0 - Live 0x0000000000000000
2+
virtio_rng 16384 0 - Live 0x0000000000000000
3+
x_tables 40960 1 ip_tables, Live 0x0000000000000000
4+
autofs4 45056 2 - Live 0x0000000000000000

0 commit comments

Comments
 (0)