github.com/kata-containers/runtime@v0.0.0-20210505125100-04f29832a923/virtcontainers/pkg/types/pcipath_test.go (about)

     1  // Copyright (c) 2020 Red Hat
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  //
     5  
     6  package types
     7  
     8  import (
     9  	"testing"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func TestPciSlot(t *testing.T) {
    15  	assert := assert.New(t)
    16  
    17  	// Valid slots
    18  	slot, err := PciSlotFromInt(0x00)
    19  	assert.NoError(err)
    20  	assert.Equal(slot, PciSlot{})
    21  	assert.Equal(slot.String(), "00")
    22  
    23  	slot, err = PciSlotFromString("00")
    24  	assert.NoError(err)
    25  	assert.Equal(slot, PciSlot{})
    26  
    27  	slot, err = PciSlotFromInt(31)
    28  	assert.NoError(err)
    29  	slot2, err := PciSlotFromString("1f")
    30  	assert.NoError(err)
    31  	assert.Equal(slot, slot2)
    32  
    33  	// Bad slots
    34  	_, err = PciSlotFromInt(-1)
    35  	assert.Error(err)
    36  
    37  	_, err = PciSlotFromInt(32)
    38  	assert.Error(err)
    39  
    40  	_, err = PciSlotFromString("20")
    41  	assert.Error(err)
    42  
    43  	_, err = PciSlotFromString("xy")
    44  	assert.Error(err)
    45  
    46  	_, err = PciSlotFromString("00/")
    47  	assert.Error(err)
    48  
    49  	_, err = PciSlotFromString("")
    50  	assert.Error(err)
    51  }
    52  
    53  func TestPciPath(t *testing.T) {
    54  	assert := assert.New(t)
    55  
    56  	slot3, err := PciSlotFromInt(0x03)
    57  	assert.NoError(err)
    58  	slot4, err := PciSlotFromInt(0x04)
    59  	assert.NoError(err)
    60  	slot5, err := PciSlotFromInt(0x05)
    61  	assert.NoError(err)
    62  
    63  	// Empty/nil paths
    64  	pcipath := PciPath{}
    65  	assert.True(pcipath.IsNil())
    66  
    67  	pcipath, err = PciPathFromString("")
    68  	assert.NoError(err)
    69  	assert.True(pcipath.IsNil())
    70  	assert.Equal(pcipath, PciPath{})
    71  
    72  	// Valid paths
    73  	pcipath, err = PciPathFromSlots(slot3)
    74  	assert.NoError(err)
    75  	assert.False(pcipath.IsNil())
    76  	assert.Equal(pcipath.String(), "03")
    77  	pcipath2, err := PciPathFromString("03")
    78  	assert.NoError(err)
    79  	assert.Equal(pcipath, pcipath2)
    80  
    81  	pcipath, err = PciPathFromSlots(slot3, slot4)
    82  	assert.NoError(err)
    83  	assert.False(pcipath.IsNil())
    84  	assert.Equal(pcipath.String(), "03/04")
    85  	pcipath2, err = PciPathFromString("03/04")
    86  	assert.NoError(err)
    87  	assert.Equal(pcipath, pcipath2)
    88  
    89  	pcipath, err = PciPathFromSlots(slot3, slot4, slot5)
    90  	assert.NoError(err)
    91  	assert.False(pcipath.IsNil())
    92  	assert.Equal(pcipath.String(), "03/04/05")
    93  	pcipath2, err = PciPathFromString("03/04/05")
    94  	assert.NoError(err)
    95  	assert.Equal(pcipath, pcipath2)
    96  
    97  	// Bad paths
    98  	_, err = PciPathFromSlots()
    99  	assert.Error(err)
   100  
   101  	_, err = PciPathFromString("20")
   102  	assert.Error(err)
   103  
   104  	_, err = PciPathFromString("//")
   105  	assert.Error(err)
   106  
   107  	_, err = PciPathFromString("xyz")
   108  	assert.Error(err)
   109  
   110  }