mirror of
https://github.com/kubernetes-sigs/node-feature-discovery.git
synced 2024-12-14 11:57:51 +00:00
fix: take into consideration possibility of having empty line in swap file
Signed-off-by: TessaIO <ahmedgrati1999@gmail.com>
This commit is contained in:
parent
393af96a88
commit
316fe71918
4 changed files with 12 additions and 6 deletions
|
@ -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() {
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
|
|
2
source/memory/testdata/noswap
vendored
2
source/memory/testdata/noswap
vendored
|
@ -1 +1 @@
|
|||
Filename Type Size Used Priority
|
||||
Filename Type Size Used Priority
|
||||
|
|
2
source/memory/testdata/swap
vendored
2
source/memory/testdata/swap
vendored
|
@ -1,2 +1,2 @@
|
|||
Filename Type Size Used Priority
|
||||
dummyfile partition 65555 0 -1
|
||||
dummyfile partition 65555 0 -1
|
||||
|
|
Loading…
Reference in a new issue