github.com/dell/gofsutil@v1.15.0/gofsutil_unix_test.go (about)

     1  //go:build linux || darwin
     2  // +build linux darwin
     3  
     4  // Copyright © 2022 Dell Inc. or its subsidiaries. All Rights Reserved.
     5  //
     6  // Licensed under the Apache License, Version 2.0 (the "License");
     7  // you may not use this file except in compliance with the License.
     8  // You may obtain a copy of the License at
     9  //      http://www.apache.org/licenses/LICENSE-2.0
    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  package gofsutil_test
    17  
    18  import (
    19  	"context"
    20  	"fmt"
    21  	"os"
    22  	"strings"
    23  	"testing"
    24  
    25  	"github.com/dell/gofsutil"
    26  )
    27  
    28  func TestFCRescanSCSIHost(t *testing.T) {
    29  	var targets []string
    30  	// Scan the remote ports to find the array port WWNs
    31  	fcRemotePortsDir := "/sys/class/fc_remote_ports"
    32  	remotePortEntries, err := os.ReadDir(fcRemotePortsDir)
    33  	if err != nil {
    34  		t.Errorf("error reading %s: %s", fcRemotePortsDir, err)
    35  	}
    36  	for _, remotePort := range remotePortEntries {
    37  		if !strings.HasPrefix(remotePort.Name(), "rport-") {
    38  			continue
    39  		}
    40  
    41  		if !strings.HasPrefix(remotePort.Name(), "rport-") {
    42  			continue
    43  		}
    44  
    45  		arrayPortNameBytes, err := os.ReadFile(fcRemotePortsDir + "/" + remotePort.Name() + "/" + "port_name")
    46  		if err != nil {
    47  			continue
    48  		}
    49  		arrayPortName := strings.TrimSpace(string(arrayPortNameBytes))
    50  		if !strings.HasPrefix(arrayPortName, gofsutil.FCPortPrefix) {
    51  			continue
    52  		}
    53  		targets = append(targets, arrayPortName)
    54  
    55  	}
    56  
    57  	if len(targets) > 0 {
    58  		err := gofsutil.RescanSCSIHost(context.Background(), targets, "1")
    59  		if err != nil {
    60  			t.Errorf("RescanSCSIHost failed: %s", err)
    61  		}
    62  	}
    63  }
    64  
    65  func TestGetFCHostPortWWNs(t *testing.T) {
    66  	wwns, err := gofsutil.GetFCHostPortWWNs(context.Background())
    67  	if err != nil {
    68  		t.Error(err)
    69  	}
    70  	for _, wwn := range wwns {
    71  		fmt.Printf("local FC port wwn: %s\n", wwn)
    72  	}
    73  }
    74  
    75  func TestMountArgs(t *testing.T) {
    76  	tests := []struct {
    77  		src    string
    78  		tgt    string
    79  		fst    string
    80  		opts   []string
    81  		result string
    82  	}{
    83  		{
    84  			src:    "localhost:/data",
    85  			tgt:    "/mnt",
    86  			fst:    "nfs",
    87  			result: "-t nfs localhost:/data /mnt",
    88  		},
    89  		{
    90  			src:    "localhost:/data",
    91  			tgt:    "/mnt",
    92  			result: "localhost:/data /mnt",
    93  		},
    94  		{
    95  			src:    "localhost:/data",
    96  			tgt:    "/mnt",
    97  			fst:    "nfs",
    98  			opts:   []string{"tcp", "vers=4"},
    99  			result: "-t nfs -o tcp,vers=4 localhost:/data /mnt",
   100  		},
   101  		{
   102  			src:    "/dev/disk/mydisk",
   103  			tgt:    "/mnt/mydisk",
   104  			fst:    "xfs",
   105  			opts:   []string{"ro", "noatime", "ro"},
   106  			result: "-t xfs -o ro,noatime /dev/disk/mydisk /mnt/mydisk",
   107  		},
   108  		{
   109  			src:    "/dev/sdc",
   110  			tgt:    "/mnt",
   111  			opts:   []string{"rw", "", "noatime"},
   112  			result: "-o rw,noatime /dev/sdc /mnt",
   113  		},
   114  	}
   115  
   116  	for _, tt := range tests {
   117  		tt := tt
   118  		t.Run("", func(st *testing.T) {
   119  			st.Parallel()
   120  			opts := gofsutil.MakeMountArgs(
   121  				context.TODO(), tt.src, tt.tgt, tt.fst, tt.opts...)
   122  			optsStr := strings.Join(opts, " ")
   123  			if optsStr != tt.result {
   124  				t.Errorf("Formatting of mount args incorrect, got: %s want: %s",
   125  					optsStr, tt.result)
   126  			}
   127  		})
   128  	}
   129  }
   130  
   131  func TestWWNToDevicePath(t *testing.T) {
   132  	tests := []struct {
   133  		src    string
   134  		tgt    string
   135  		wwn    string
   136  		result string
   137  	}{
   138  		{
   139  			src:    "/dev/disk/by-id/wwn-0x60570970000197900046533030394146",
   140  			tgt:    "../../mydeva",
   141  			wwn:    "60570970000197900046533030394146",
   142  			result: "/dev/mydeva",
   143  		},
   144  		{
   145  			src:    "/dev/disk/by-id/dm-uuid-mpath-360570970000197900046533030394146",
   146  			tgt:    "../../mydevb",
   147  			wwn:    "60570970000197900046533030394146",
   148  			result: "/dev/mydevb",
   149  		},
   150  	}
   151  	for _, tt := range tests {
   152  		t.Run("", func(_ *testing.T) {
   153  			// Change directories
   154  			workingDirectory, _ := os.Getwd()
   155  			err := os.Chdir("/dev/disk/by-id")
   156  			if err != nil {
   157  				t.Errorf("Couldn't Chdir to /dev/disk/by/id: %s", err)
   158  			}
   159  			// Create a target
   160  			file, err := os.Create(tt.result)
   161  			if err != nil {
   162  				t.Errorf("Couldn't Create %s: %s", tt.result, err)
   163  			}
   164  			file.Close()
   165  			// Create a symlink
   166  			err = os.Symlink(tt.tgt, tt.src)
   167  			if err != nil {
   168  				t.Errorf("Couldn't create Symlink %s: %s", tt.tgt, err)
   169  			}
   170  			// Get the entry
   171  			a, b, err := gofsutil.WWNToDevicePathX(context.Background(), tt.wwn)
   172  			if err != nil {
   173  				t.Errorf("Couldn't find DevicePathX: %s", err)
   174  			}
   175  			if a != tt.src {
   176  				t.Errorf("Expected %s got %s", tt.src, a)
   177  			}
   178  			if b != tt.result {
   179  				t.Errorf("Expected %s got %s", tt.result, b)
   180  			}
   181  			// Get the entry
   182  			c, err := gofsutil.WWNToDevicePath(context.Background(), tt.wwn)
   183  			if err != nil {
   184  				t.Errorf("Couldn't find DevicePathX: %s", err)
   185  			}
   186  			if c != tt.result {
   187  				t.Errorf("Expected %s got %s", tt.result, c)
   188  			}
   189  			// Remove symlink
   190  			err = os.Remove(tt.src)
   191  			if err != nil {
   192  				t.Errorf("Couldn't remove %s: %s", tt.src, err)
   193  			}
   194  			// Remove target
   195  			err = os.Remove(tt.result)
   196  			if err != nil {
   197  				t.Errorf("Couldn't remove %s: %s", tt.result, err)
   198  			}
   199  			// Change directories
   200  			err = os.Chdir(workingDirectory)
   201  			if err != nil {
   202  				t.Errorf("Couldn't Chdir to /dev/disk/by/id: %s", err)
   203  			}
   204  		})
   205  	}
   206  }