mirror of
https://github.com/kubernetes-sigs/node-feature-discovery.git
synced 2024-12-14 11:57:51 +00:00
source/kernel: add unit tests for kernel version parsing
This commit is contained in:
parent
e0b8a52f8b
commit
32b1088f84
3 changed files with 83 additions and 7 deletions
|
@ -122,7 +122,7 @@ func (s *kernelSource) Discover() error {
|
|||
s.features = nfdv1alpha1.NewFeatures()
|
||||
|
||||
// Read kernel version
|
||||
if version, err := parseVersion(); err != nil {
|
||||
if version, err := discoverVersion(); err != nil {
|
||||
klog.ErrorS(err, "failed to get kernel version")
|
||||
} else {
|
||||
s.features.Attributes[VersionFeature] = nfdv1alpha1.NewAttributeFeatures(version)
|
||||
|
|
|
@ -23,17 +23,21 @@ import (
|
|||
)
|
||||
|
||||
// Read and parse kernel version
|
||||
func parseVersion() (map[string]string, error) {
|
||||
version := map[string]string{}
|
||||
|
||||
full, err := getVersion()
|
||||
func discoverVersion() (map[string]string, error) {
|
||||
raw, err := getVersion()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return parseVersion(raw), nil
|
||||
}
|
||||
|
||||
func parseVersion(raw string) map[string]string {
|
||||
version := map[string]string{}
|
||||
|
||||
// Replace forbidden symbols
|
||||
fullRegex := regexp.MustCompile("[^-A-Za-z0-9_.]")
|
||||
full = fullRegex.ReplaceAllString(full, "_")
|
||||
full := fullRegex.ReplaceAllString(raw, "_")
|
||||
// Label values must start and end with an alphanumeric
|
||||
full = strings.Trim(full, "-_.")
|
||||
|
||||
|
@ -49,7 +53,7 @@ func parseVersion() (map[string]string, error) {
|
|||
}
|
||||
}
|
||||
|
||||
return version, nil
|
||||
return version
|
||||
}
|
||||
|
||||
func getVersion() (string, error) {
|
||||
|
|
72
source/kernel/version_test.go
Normal file
72
source/kernel/version_test.go
Normal file
|
@ -0,0 +1,72 @@
|
|||
/*
|
||||
Copyright 2024 The Kubernetes 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 kernel
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestParseVersion(t *testing.T) {
|
||||
tests := []struct {
|
||||
raw string
|
||||
expected map[string]string
|
||||
}{
|
||||
{
|
||||
raw: "6.2.3",
|
||||
expected: map[string]string{
|
||||
"full": "6.2.3",
|
||||
"major": "6",
|
||||
"minor": "2",
|
||||
"revision": "3",
|
||||
},
|
||||
},
|
||||
{
|
||||
raw: "6.0.0-beta",
|
||||
expected: map[string]string{
|
||||
"full": "6.0.0-beta",
|
||||
"major": "6",
|
||||
"minor": "0",
|
||||
"revision": "0",
|
||||
},
|
||||
},
|
||||
{
|
||||
raw: "6.8",
|
||||
expected: map[string]string{
|
||||
"full": "6.8",
|
||||
"major": "6",
|
||||
"minor": "8",
|
||||
"revision": "",
|
||||
},
|
||||
},
|
||||
{
|
||||
raw: "6.0.10-100+123.x86_64~",
|
||||
expected: map[string]string{
|
||||
"full": "6.0.10-100_123.x86_64",
|
||||
"major": "6",
|
||||
"minor": "0",
|
||||
"revision": "10",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
actual := parseVersion(test.raw)
|
||||
assert.Equal(t, test.expected, actual)
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue