Skip to content

Commit a5de1f7

Browse files
authored
Merge pull request #1588 from marquiz/devel/kernel-unit-test
source/kernel: add unit tests for kernel version parsing
2 parents e0b8a52 + 32b1088 commit a5de1f7

File tree

3 files changed

+83
-7
lines changed

3 files changed

+83
-7
lines changed

source/kernel/kernel.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ func (s *kernelSource) Discover() error {
122122
s.features = nfdv1alpha1.NewFeatures()
123123

124124
// Read kernel version
125-
if version, err := parseVersion(); err != nil {
125+
if version, err := discoverVersion(); err != nil {
126126
klog.ErrorS(err, "failed to get kernel version")
127127
} else {
128128
s.features.Attributes[VersionFeature] = nfdv1alpha1.NewAttributeFeatures(version)

source/kernel/version.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,21 @@ import (
2323
)
2424

2525
// Read and parse kernel version
26-
func parseVersion() (map[string]string, error) {
27-
version := map[string]string{}
28-
29-
full, err := getVersion()
26+
func discoverVersion() (map[string]string, error) {
27+
raw, err := getVersion()
3028
if err != nil {
3129
return nil, err
3230
}
3331

32+
return parseVersion(raw), nil
33+
}
34+
35+
func parseVersion(raw string) map[string]string {
36+
version := map[string]string{}
37+
3438
// Replace forbidden symbols
3539
fullRegex := regexp.MustCompile("[^-A-Za-z0-9_.]")
36-
full = fullRegex.ReplaceAllString(full, "_")
40+
full := fullRegex.ReplaceAllString(raw, "_")
3741
// Label values must start and end with an alphanumeric
3842
full = strings.Trim(full, "-_.")
3943

@@ -49,7 +53,7 @@ func parseVersion() (map[string]string, error) {
4953
}
5054
}
5155

52-
return version, nil
56+
return version
5357
}
5458

5559
func getVersion() (string, error) {

source/kernel/version_test.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
Copyright 2024 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package kernel
18+
19+
import (
20+
"testing"
21+
22+
"github.com/stretchr/testify/assert"
23+
)
24+
25+
func TestParseVersion(t *testing.T) {
26+
tests := []struct {
27+
raw string
28+
expected map[string]string
29+
}{
30+
{
31+
raw: "6.2.3",
32+
expected: map[string]string{
33+
"full": "6.2.3",
34+
"major": "6",
35+
"minor": "2",
36+
"revision": "3",
37+
},
38+
},
39+
{
40+
raw: "6.0.0-beta",
41+
expected: map[string]string{
42+
"full": "6.0.0-beta",
43+
"major": "6",
44+
"minor": "0",
45+
"revision": "0",
46+
},
47+
},
48+
{
49+
raw: "6.8",
50+
expected: map[string]string{
51+
"full": "6.8",
52+
"major": "6",
53+
"minor": "8",
54+
"revision": "",
55+
},
56+
},
57+
{
58+
raw: "6.0.10-100+123.x86_64~",
59+
expected: map[string]string{
60+
"full": "6.0.10-100_123.x86_64",
61+
"major": "6",
62+
"minor": "0",
63+
"revision": "10",
64+
},
65+
},
66+
}
67+
68+
for _, test := range tests {
69+
actual := parseVersion(test.raw)
70+
assert.Equal(t, test.expected, actual)
71+
}
72+
}

0 commit comments

Comments
 (0)