Files
kata-containers/src/runtime/virtcontainers/device/config/config_test.go
bin 03546f75a6 runtime: change io/ioutil to io/os packages
Change io/ioutil to io/os packages because io/ioutil package
is deprecated from 1.16:

Discard => io.Discard
NopCloser => io.NopCloser
ReadAll => io.ReadAll
ReadDir => os.ReadDir
ReadFile => os.ReadFile
TempDir => os.MkdirTemp
TempFile => os.CreateTemp
WriteFile => os.WriteFile

Details: https://go.dev/doc/go1.16#ioutil

Fixes: #3265

Signed-off-by: bin <bin@hyper.sh>
2021-12-15 07:31:48 +08:00

73 lines
1.4 KiB
Go

// Copyright (c) 2020 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
//
package config
import (
"fmt"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
)
func TestGetBackingFile(t *testing.T) {
assert := assert.New(t)
dir, err := os.MkdirTemp("", "backing")
assert.NoError(err)
defer os.RemoveAll(dir)
orgGetSysDevPath := getSysDevPath
getSysDevPath = func(info DeviceInfo) string {
return dir
}
defer func() { getSysDevPath = orgGetSysDevPath }()
info := DeviceInfo{}
path, err := getBackingFile(info)
assert.Error(err)
assert.Empty(path)
loopDir := filepath.Join(dir, "loop")
err = os.Mkdir(loopDir, os.FileMode(0755))
assert.NoError(err)
backingFile := "/fake-img"
err = os.WriteFile(filepath.Join(loopDir, "backing_file"), []byte(backingFile), os.FileMode(0755))
assert.NoError(err)
path, err = getBackingFile(info)
assert.NoError(err)
assert.Equal(backingFile, path)
}
func TestGetSysDevPathImpl(t *testing.T) {
assert := assert.New(t)
info := DeviceInfo{
DevType: "",
Major: 127,
Minor: 0,
}
path := getSysDevPathImpl(info)
assert.Empty(path)
expectedFormat := fmt.Sprintf("%d:%d", info.Major, info.Minor)
info.DevType = "c"
path = getSysDevPathImpl(info)
assert.Contains(path, expectedFormat)
assert.Contains(path, "char")
info.DevType = "b"
path = getSysDevPathImpl(info)
assert.Contains(path, expectedFormat)
assert.Contains(path, "block")
}