github.com/u-root/u-root@v7.0.1-0.20200915234505-ad7babab0a8e+incompatible/pkg/mount/block/blockdev_test.go (about)

     1  // Copyright 2017-2019 the u-root Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package block
     6  
     7  import (
     8  	"fmt"
     9  	"reflect"
    10  	"testing"
    11  
    12  	"github.com/stretchr/testify/require"
    13  	"github.com/u-root/u-root/pkg/pci"
    14  	"github.com/u-root/u-root/pkg/testutil"
    15  )
    16  
    17  // TestFindMountPointNotExists checks that non existent
    18  // entry is checked and nil returned
    19  func TestFindMountPointNotExists(t *testing.T) {
    20  	LinuxMountsPath = "testdata/mounts"
    21  	_, err := GetMountpointByDevice("/dev/mapper/sys-oldxxxxxx")
    22  	require.Error(t, err)
    23  }
    24  
    25  // TestFindMountPointValid check for valid output of
    26  // test mountpoint.
    27  func TestFindMountPointValid(t *testing.T) {
    28  	LinuxMountsPath = "testdata/mounts"
    29  	mountpoint, err := GetMountpointByDevice("/dev/mapper/sys-old")
    30  	require.NoError(t, err)
    31  	require.Equal(t, *mountpoint, "/media/usb")
    32  }
    33  
    34  func TestParsePCIBlockList(t *testing.T) {
    35  	for _, tt := range []struct {
    36  		name        string
    37  		blockString string
    38  		want        pci.Devices
    39  		errStr      string
    40  	}{
    41  		{
    42  			name:        "one device",
    43  			blockString: "0x8086:0x1234",
    44  			want:        pci.Devices{&pci.PCI{Vendor: "0x8086", Device: "0x1234"}},
    45  			errStr:      "",
    46  		},
    47  		{
    48  			name:        "two devices",
    49  			blockString: "0x8086:0x1234,0x1234:0xabcd",
    50  			want: pci.Devices{
    51  				&pci.PCI{Vendor: "0x8086", Device: "0x1234"},
    52  				&pci.PCI{Vendor: "0x1234", Device: "0xabcd"},
    53  			},
    54  			errStr: "",
    55  		},
    56  		{
    57  			name:        "no 0x",
    58  			blockString: "8086:1234,1234:abcd",
    59  			want: pci.Devices{
    60  				&pci.PCI{Vendor: "0x8086", Device: "0x1234"},
    61  				&pci.PCI{Vendor: "0x1234", Device: "0xabcd"},
    62  			},
    63  			errStr: "",
    64  		},
    65  		{
    66  			name:        "capitals",
    67  			blockString: "0x8086:0x1234,0x1234:0xABCD",
    68  			want: pci.Devices{
    69  				&pci.PCI{Vendor: "0x8086", Device: "0x1234"},
    70  				&pci.PCI{Vendor: "0x1234", Device: "0xabcd"},
    71  			},
    72  			errStr: "",
    73  		},
    74  		{
    75  			name:        "not hex vendor",
    76  			blockString: "0xghij:0x1234",
    77  			want:        nil,
    78  			errStr:      "BlockList needs to contain a hex vendor ID, got 0xghij, err strconv.ParseUint: parsing \"ghij\": invalid syntax",
    79  		},
    80  		{
    81  			name:        "not hex vendor",
    82  			blockString: "0x1234:0xghij",
    83  			want:        nil,
    84  			errStr:      "BlockList needs to contain a hex device ID, got 0xghij, err strconv.ParseUint: parsing \"ghij\": invalid syntax",
    85  		},
    86  		{
    87  			name:        "bad format",
    88  			blockString: "0xghij,0x1234",
    89  			want:        nil,
    90  			errStr:      "BlockList needs to be of format vendor1:device1,vendor2:device2...! got 0xghij,0x1234",
    91  		},
    92  	} {
    93  		t.Run(tt.name, func(t *testing.T) {
    94  			devices, err := parsePCIBlockList(tt.blockString)
    95  			if e := testutil.CheckError(err, tt.errStr); e != nil {
    96  				t.Error(e)
    97  			}
    98  			if !reflect.DeepEqual(devices, tt.want) {
    99  				// Need to do this because stringer does not print device and vendor
   100  				s := "got:\n"
   101  				for _, d := range devices {
   102  					s = fmt.Sprintf("%s{Vendor: %v, Device %v}\n", s, d.Vendor, d.Device)
   103  				}
   104  				s = fmt.Sprintf("%swant:\n", s)
   105  				for _, d := range tt.want {
   106  					s = fmt.Sprintf("%s{Vendor: %v, Device %v}\n", s, d.Vendor, d.Device)
   107  				}
   108  				t.Error(s)
   109  			}
   110  		})
   111  	}
   112  }