1
0
Fork 0
mirror of https://github.com/kubernetes-sigs/node-feature-discovery.git synced 2024-12-14 11:57:51 +00:00

Merge pull request #1781 from TessaIO/fix-swap-empty-line

fix: take into consideration possibility of having empty line in swap file
This commit is contained in:
Kubernetes Prow Robot 2024-07-11 21:39:51 -07:00 committed by GitHub
commit 98e9091084
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 12 additions and 6 deletions

View file

@ -131,7 +131,7 @@ func (s *memorySource) GetFeatures() *nfdv1alpha1.Features {
// detectSwap detects Swap node information
func detectSwap() (map[string]string, error) {
procBasePath := hostpath.ProcDir.Path("swaps")
lines, err := getNumberOfLinesFromFile(procBasePath)
lines, err := getNumberOfNonEmptyLinesFromFile(procBasePath)
if err != nil {
return nil, fmt.Errorf("failed to read swaps file: %w", err)
}
@ -195,12 +195,18 @@ func readNdDeviceInfo(path string) nfdv1alpha1.InstanceFeature {
return *nfdv1alpha1.NewInstanceFeature(attrs)
}
func getNumberOfLinesFromFile(path string) (int, error) {
func getNumberOfNonEmptyLinesFromFile(path string) (int, error) {
data, err := os.ReadFile(path)
if err != nil {
return 0, err
}
return len(strings.Split(string(data), "\n")), nil
length := 0
for _, line := range strings.Split(string(data), "\n") {
if strings.TrimSpace(line) != "" {
length++
}
}
return length, nil
}
func init() {

View file

@ -54,7 +54,7 @@ func TestGetNumberofLinesFromFile(t *testing.T) {
},
}
for _, tc := range tc {
actual, err := getNumberOfLinesFromFile(tc.path)
actual, err := getNumberOfNonEmptyLinesFromFile(tc.path)
if tc.expectErr {
assert.NotNil(t, err, "should get an error")
}

View file

@ -1 +1 @@
Filename Type Size Used Priority
Filename Type Size Used Priority

View file

@ -1,2 +1,2 @@
Filename Type Size Used Priority
dummyfile partition 65555 0 -1
dummyfile partition 65555 0 -1