Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sysfs): trim newline while parsing sysfs files #666

Merged
merged 2 commits into from
Dec 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelogs/unreleased/666-akhilerm
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fix sysfs parsing by trimming newline suffix when reading from sysfs files
3 changes: 2 additions & 1 deletion pkg/sysfs/syspath.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ package sysfs

import (
"fmt"
"github.com/openebs/node-disk-manager/blockdevice"
"io/ioutil"
"os"
"path/filepath"
"strings"

"github.com/openebs/node-disk-manager/blockdevice"
)

const (
Expand Down
14 changes: 14 additions & 0 deletions pkg/sysfs/syspath_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,20 @@ func TestSysFsDeviceGetDeviceType(t *testing.T) {
want: "raid0",
wantErr: false,
},
"device is a dm device with empty uuid": {
sysfsDevice: &Device{
deviceName: "dm-16",
path: "/dev/dm-16",
sysPath: filepath.Join(tmpDir,
"sys/devices/virtual/block/dm-16") + "/",
},
devType: blockdevice.BlockDeviceTypeDisk,
subDirectoryName: "dm",
subFileName: "uuid",
subFileContent: "\n",
want: blockdevice.BlockDeviceTypeDMDevice,
wantErr: false,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
Expand Down
4 changes: 3 additions & 1 deletion pkg/sysfs/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func readSysFSFileAsInt64(sysFilePath string) (int64, error) {
if err != nil {
return 0, err
}
// Remove tailing newline (usual in sysfs) before parsing
return strconv.ParseInt(strings.TrimSuffix(string(b), "\n"), 10, 64)
}

Expand All @@ -38,7 +39,8 @@ func readSysFSFileAsString(sysFilePath string) (string, error) {
if err != nil {
return "", err
}
return string(b), nil
// Remove tailing newline (usual in sysfs)
return strings.TrimSuffix(string(b), "\n"), nil
}

// addDevPrefix adds the /dev prefix to all the device names
Expand Down
10 changes: 9 additions & 1 deletion pkg/sysfs/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ limitations under the License.
package sysfs

import (
"github.com/stretchr/testify/assert"
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func TestAddDevPrefix(t *testing.T) {
Expand Down Expand Up @@ -55,6 +56,13 @@ func TestReadSysFSFileAsString(t *testing.T) {
want: "LVM-OSlVs5gIXuqSKVPukc2aGPh0AeJw31TJqYIRuRHoodYg9Jwkmyvvk0QNYK4YulHt",
wantErr: false,
},
"valid sysfs path with tailing new line": {
path: "/tmp/dm-0/dm/",
fileName: "uuid",
fileContent: "LVM-OSlVs5gIXuqSKVPukc2aGPh0AeJw31TJqYIRuRHoodYg9Jwkmyvvk0QNYK4YulHt\n",
want: "LVM-OSlVs5gIXuqSKVPukc2aGPh0AeJw31TJqYIRuRHoodYg9Jwkmyvvk0QNYK4YulHt",
wantErr: false,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
Expand Down