github.com/kata-containers/runtime@v0.0.0-20210505125100-04f29832a923/virtcontainers/utils/utils_linux_test.go (about)

     1  // Copyright (c) 2018 Intel Corporation
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  //
     5  
     6  package utils
     7  
     8  import (
     9  	"bytes"
    10  	"errors"
    11  	"os/exec"
    12  	"strings"
    13  	"testing"
    14  
    15  	"github.com/stretchr/testify/assert"
    16  )
    17  
    18  func TestFindContextID(t *testing.T) {
    19  	assert := assert.New(t)
    20  
    21  	ioctlFunc = func(fd uintptr, request, arg1 uintptr) error {
    22  		return errors.New("ioctl")
    23  	}
    24  
    25  	orgVHostVSockDevicePath := VHostVSockDevicePath
    26  	orgMaxUInt := maxUInt
    27  	defer func() {
    28  		VHostVSockDevicePath = orgVHostVSockDevicePath
    29  		maxUInt = orgMaxUInt
    30  	}()
    31  	VHostVSockDevicePath = "/dev/null"
    32  	maxUInt = uint64(1000000)
    33  
    34  	f, cid, err := FindContextID()
    35  	assert.Nil(f)
    36  	assert.Zero(cid)
    37  	assert.Error(err)
    38  }
    39  
    40  func TestGetDevicePathAndFsTypeEmptyMount(t *testing.T) {
    41  	assert := assert.New(t)
    42  	_, _, _, err := GetDevicePathAndFsTypeOptions("")
    43  	assert.Error(err)
    44  }
    45  
    46  func TestGetDevicePathAndFsTypeSuccessful(t *testing.T) {
    47  	assert := assert.New(t)
    48  
    49  	cmdStr := "grep ^proc  /proc/mounts"
    50  	cmd := exec.Command("sh", "-c", cmdStr)
    51  	output, err := cmd.Output()
    52  	assert.NoError(err)
    53  
    54  	data := bytes.Split(output, []byte(" "))
    55  	fstypeOut := string(data[2])
    56  	optsOut := strings.Split(string(data[3]), ",")
    57  
    58  	path, fstype, fsOptions, err := GetDevicePathAndFsTypeOptions("/proc")
    59  	assert.NoError(err)
    60  
    61  	assert.Equal(path, "proc")
    62  	assert.Equal(fstype, "proc")
    63  	assert.Equal(fstype, fstypeOut)
    64  	assert.Equal(fsOptions, optsOut)
    65  }