mirror of
https://github.com/aljazceru/kata-containers.git
synced 2025-12-22 16:54:25 +01:00
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>
36 lines
650 B
Go
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)
|
|
}
|