This repository has been archived by the owner on May 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 373
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
virtcontainers: implement function to get the backing file
Implement function the get the backing file from a loop device. The backing file can be used as backend file for a NVDIMM device in the guest Signed-off-by: Julio Montes <[email protected]>
- Loading branch information
Julio Montes
committed
Mar 20, 2020
1 parent
0a4e2ed
commit 9ff44db
Showing
2 changed files
with
108 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Copyright (c) 2020 Intel Corporation | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
package config | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGetBackingFile(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
dir, err := ioutil.TempDir("", "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 = ioutil.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") | ||
} |