github.com/mvdan/u-root-coreutils@v0.0.0-20230122170626-c2eef2898555/pkg/finddrive/finddrive_test.go (about)

     1  // Copyright 2022 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 finddrive
     6  
     7  import (
     8  	"os"
     9  	"path/filepath"
    10  	"testing"
    11  
    12  	"github.com/mvdan/u-root-coreutils/pkg/smbios"
    13  )
    14  
    15  const (
    16  	matchingSlotType    = 0xBB
    17  	nonMatchingSlotType = 0x66
    18  	missingSlotType     = 0x43
    19  	devName             = "nvme0n1"
    20  	matchedSlotPath     = "/dev/" + devName
    21  )
    22  
    23  var (
    24  	matchingSlot = smbios.SystemSlots{
    25  		SlotType:             matchingSlotType,
    26  		SegmentGroupNumber:   0x0A5A,
    27  		BusNumber:            0x44,
    28  		DeviceFunctionNumber: 0x96,
    29  	}
    30  	nonMatchingSlot = smbios.SystemSlots{
    31  		SlotType: nonMatchingSlotType,
    32  	}
    33  )
    34  
    35  func mockSysDir(t *testing.T) string {
    36  	sysDir := t.TempDir()
    37  
    38  	devicesPath := filepath.Join(sysDir, "devices/pci0a5a:44/0a5a:44:12.6/0a5a:00:00.0/nvme/nvme0/")
    39  	err := os.MkdirAll(devicesPath, 0o777)
    40  	if err != nil {
    41  		t.Errorf("Error creating path %s: %v", devicesPath, err)
    42  	}
    43  
    44  	err = os.MkdirAll(filepath.Join(sysDir, "block/"), 0o777)
    45  	if err != nil {
    46  		t.Errorf("Error creating path %s: %v", filepath.Join(sysDir, "block/"), err)
    47  	}
    48  
    49  	devicesFile := filepath.Join(devicesPath, devName)
    50  	f, err := os.Create(devicesFile)
    51  	if err != nil {
    52  		t.Errorf("Error creating file %s: %v", devicesFile, err)
    53  	}
    54  	f.Close()
    55  
    56  	err = os.Symlink(devicesFile, filepath.Join(sysDir, "block/nvme0n1"))
    57  	if err != nil {
    58  		t.Errorf("Error creating symlink: %v", err)
    59  	}
    60  
    61  	return sysDir
    62  }
    63  
    64  func TestFindSlotType(t *testing.T) {
    65  	sysDir := mockSysDir(t)
    66  	slots := []*smbios.SystemSlots{&nonMatchingSlot, &matchingSlot, &nonMatchingSlot, &matchingSlot, &nonMatchingSlot}
    67  
    68  	paths, err := findSlotType(sysDir, slots, matchingSlotType)
    69  
    70  	if err != nil {
    71  		t.Errorf("findSlotType(%v, %v, %v) returned error: %v, want: nil", sysDir, slots, matchingSlotType, err)
    72  	}
    73  	if len(paths) != 2 || paths[0] != matchedSlotPath || paths[1] != matchedSlotPath {
    74  		t.Errorf("findSlotType(%v, %v, %v) returned paths: %v, want: %v", sysDir, slots, matchingSlotType, paths, []string{matchedSlotPath, matchedSlotPath})
    75  	}
    76  }
    77  
    78  func TestFindSlotTypeMissing(t *testing.T) {
    79  	sysDir := mockSysDir(t)
    80  	slots := []*smbios.SystemSlots{&nonMatchingSlot, &matchingSlot, &nonMatchingSlot, &matchingSlot, &nonMatchingSlot}
    81  
    82  	paths, err := findSlotType(sysDir, slots, missingSlotType)
    83  
    84  	if err != nil {
    85  		t.Errorf("findSlotType(%v, %v, %v) returned error: %v, want: nil", sysDir, slots, missingSlotType, err)
    86  	}
    87  	if len(paths) != 0 {
    88  		t.Errorf("findSlotType(%v, %v, %v) returned paths: %v, want: []", sysDir, slots, missingSlotType, paths)
    89  	}
    90  }
    91  
    92  func TestFindSlotTypeNoSlots(t *testing.T) {
    93  	sysDir := mockSysDir(t)
    94  	slots := []*smbios.SystemSlots{}
    95  
    96  	paths, err := findSlotType(sysDir, slots, matchingSlotType)
    97  
    98  	if err != nil {
    99  		t.Errorf("findSlotType(%v, %v, %v) returned error: %v, want: nil", sysDir, slots, matchingSlotType, err)
   100  	}
   101  	if len(paths) != 0 {
   102  		t.Errorf("findSlotType(%v, %v, %v) returned paths: %v, want: []", sysDir, slots, matchingSlotType, paths)
   103  	}
   104  }