Skip to content

Commit

Permalink
fix(sysfs): trim newline while parsing sysfs files (#666) (#667)
Browse files Browse the repository at this point in the history
sysfs files like dm/uuid etc are sometimes suffixed by a newline
character which need to be trimmed off for correct parsing, else
it will result in incorrect device type being detected

Signed-off-by: Akhil Mohan <[email protected]>
  • Loading branch information
akhilerm authored Dec 28, 2021
1 parent a1e8959 commit a118ed8
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 3 deletions.
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

0 comments on commit a118ed8

Please sign in to comment.