Files
kata-containers/virtcontainers/utils/utils_linux_test.go
Julio Montes 9b283254c3 virtcontainers: Implement function to get a free context ID
FindContextID generates a random number between 3 and max uint32
and uses it as context ID.
Using ioctl findContextID checks if the context ID is free, if
the context ID is being used by other process, this function
iterates from over all valid context IDs until one is available.

`/dev/vhost-vsock` is used to check what context IDs are free,
we need it to ensure we are using a unique context ID to
create the vsocks.

Signed-off-by: Julio Montes <julio.montes@intel.com>
2018-07-31 10:30:05 -05:00

36 lines
650 B
Go

// Copyright (c) 2018 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
//
package utils
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
)
func TestFindContextID(t *testing.T) {
assert := assert.New(t)
ioctlFunc = func(fd uintptr, request int, arg1 uint32) error {
return errors.New("ioctl")
}
orgVHostVSockDevicePath := VHostVSockDevicePath
orgMaxUInt := maxUInt
defer func() {
VHostVSockDevicePath = orgVHostVSockDevicePath
maxUInt = orgMaxUInt
}()
VHostVSockDevicePath = "/dev/null"
maxUInt = uint32(1000000)
f, cid, err := FindContextID()
assert.Nil(f)
assert.Zero(cid)
assert.Error(err)
}