github.com/openebs/node-disk-manager@v1.9.1-0.20230225014141-4531f06ffa1e/pkg/sysfs/util_test.go (about)

     1  /*
     2  Copyright 2020 The OpenEBS Authors
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package sysfs
    18  
    19  import (
    20  	"os"
    21  	"testing"
    22  
    23  	"github.com/stretchr/testify/assert"
    24  )
    25  
    26  func TestAddDevPrefix(t *testing.T) {
    27  	tests := map[string]struct {
    28  		devPaths []string
    29  		want     []string
    30  	}{
    31  		"single device name given": {
    32  			devPaths: []string{"sda"},
    33  			want:     []string{"/dev/sda"},
    34  		},
    35  	}
    36  	for name, tt := range tests {
    37  		t.Run(name, func(t *testing.T) {
    38  			got := addDevPrefix(tt.devPaths)
    39  			assert.Equal(t, tt.want, got)
    40  		})
    41  	}
    42  }
    43  
    44  func TestReadSysFSFileAsString(t *testing.T) {
    45  	tests := map[string]struct {
    46  		path        string
    47  		fileName    string
    48  		fileContent string
    49  		want        string
    50  		wantErr     bool
    51  	}{
    52  		"valid sysfs path for dm uuid": {
    53  			path:        "/tmp/dm-0/dm/",
    54  			fileName:    "uuid",
    55  			fileContent: "LVM-OSlVs5gIXuqSKVPukc2aGPh0AeJw31TJqYIRuRHoodYg9Jwkmyvvk0QNYK4YulHt",
    56  			want:        "LVM-OSlVs5gIXuqSKVPukc2aGPh0AeJw31TJqYIRuRHoodYg9Jwkmyvvk0QNYK4YulHt",
    57  			wantErr:     false,
    58  		},
    59  		"valid sysfs path with tailing new line": {
    60  			path:        "/tmp/dm-0/dm/",
    61  			fileName:    "uuid",
    62  			fileContent: "LVM-OSlVs5gIXuqSKVPukc2aGPh0AeJw31TJqYIRuRHoodYg9Jwkmyvvk0QNYK4YulHt\n",
    63  			want:        "LVM-OSlVs5gIXuqSKVPukc2aGPh0AeJw31TJqYIRuRHoodYg9Jwkmyvvk0QNYK4YulHt",
    64  			wantErr:     false,
    65  		},
    66  	}
    67  	for name, tt := range tests {
    68  		t.Run(name, func(t *testing.T) {
    69  			filePath := tt.path + tt.fileName
    70  			os.MkdirAll(tt.path, 0700)
    71  			file, err := os.Create(filePath)
    72  			if err != nil {
    73  				t.Fatalf("unable to write to file %s, %v", filePath, err)
    74  				return
    75  			}
    76  			file.Write([]byte(tt.fileContent))
    77  			file.Close()
    78  			got, err := readSysFSFileAsString(filePath)
    79  			if err != nil {
    80  				if !tt.wantErr {
    81  					t.Errorf("readSysFSFileAsString() error = %v, wantErr %v", err, tt.wantErr)
    82  				}
    83  				return
    84  			}
    85  			assert.Equal(t, tt.want, got)
    86  		})
    87  	}
    88  }
    89  
    90  func TestReadSysFSFileAsInt64(t *testing.T) {
    91  	tests := map[string]struct {
    92  		path        string
    93  		fileName    string
    94  		fileContent string
    95  		want        int64
    96  		wantErr     bool
    97  	}{
    98  		"valid no of block sizes for device size": {
    99  			path:        "/tmp/sda/queue",
   100  			fileName:    "hw_sector_size",
   101  			fileContent: "512",
   102  			want:        512,
   103  			wantErr:     false,
   104  		},
   105  	}
   106  	for name, tt := range tests {
   107  		t.Run(name, func(t *testing.T) {
   108  			filePath := tt.path + tt.fileName
   109  			os.MkdirAll(tt.path, 0700)
   110  			file, err := os.Create(filePath)
   111  			if err != nil {
   112  				t.Fatalf("unable to write to file %s, %v", filePath, err)
   113  				return
   114  			}
   115  			file.Write([]byte(tt.fileContent))
   116  			file.Close()
   117  			got, err := readSysFSFileAsInt64(filePath)
   118  			if err != nil {
   119  				if !tt.wantErr {
   120  					t.Errorf("readSysFSFileAsInt64() error = %v, wantErr %v", err, tt.wantErr)
   121  				}
   122  				return
   123  			}
   124  			assert.Equal(t, tt.want, got)
   125  		})
   126  	}
   127  }