1
0
Fork 0
mirror of https://github.com/kubernetes-sigs/node-feature-discovery.git synced 2025-03-15 13:07:48 +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:
TessaIO 2024-07-10 20:48:22 +02:00
parent 393af96a88
commit 316fe71918
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 // detectSwap detects Swap node information
func detectSwap() (map[string]string, error) { func detectSwap() (map[string]string, error) {
procBasePath := hostpath.ProcDir.Path("swaps") procBasePath := hostpath.ProcDir.Path("swaps")
lines, err := getNumberOfLinesFromFile(procBasePath) lines, err := getNumberOfNonEmptyLinesFromFile(procBasePath)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to read swaps file: %w", err) 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) return *nfdv1alpha1.NewInstanceFeature(attrs)
} }
func getNumberOfLinesFromFile(path string) (int, error) { func getNumberOfNonEmptyLinesFromFile(path string) (int, error) {
data, err := os.ReadFile(path) data, err := os.ReadFile(path)
if err != nil { if err != nil {
return 0, err 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() { func init() {

View file

@ -54,7 +54,7 @@ func TestGetNumberofLinesFromFile(t *testing.T) {
}, },
} }
for _, tc := range tc { for _, tc := range tc {
actual, err := getNumberOfLinesFromFile(tc.path) actual, err := getNumberOfNonEmptyLinesFromFile(tc.path)
if tc.expectErr { if tc.expectErr {
assert.NotNil(t, err, "should get an error") 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 Filename Type Size Used Priority
dummyfile partition 65555 0 -1 dummyfile partition 65555 0 -1