You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
186 lines
6.2 KiB
Go
186 lines
6.2 KiB
Go
package hypervisor
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"go/ast"
|
|
"go/parser"
|
|
"go/token"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func validStorageConfigurationRequest() StorageConfigurationRequest {
|
|
return StorageConfigurationRequest{
|
|
ManagedDirectory: "/srv/virt/vms",
|
|
TemplateDirectory: "/srv/virt/templates", SeedDirectory: "/srv/virt/seeds",
|
|
UserISODirectory: "/srv/virt/user-isos", RescueDirectory: "/srv/virt/rescue-isos",
|
|
}
|
|
}
|
|
|
|
func TestValidateStorageDirectoriesRequiresExistingRealAllowedChildren(t *testing.T) {
|
|
root, err := filepath.EvalSymlinks(t.TempDir())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
request := StorageConfigurationRequest{}
|
|
paths := []*string{&request.ManagedDirectory, &request.TemplateDirectory, &request.SeedDirectory, &request.UserISODirectory, &request.RescueDirectory}
|
|
for index, target := range paths {
|
|
*target = filepath.Join(root, string(rune('a'+index)))
|
|
if err := os.Mkdir(*target, 0o750); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if err := validateStorageDirectories(request, []string{root}); err != nil {
|
|
t.Fatalf("valid storage directories rejected: %v", err)
|
|
}
|
|
outside, err := filepath.EvalSymlinks(t.TempDir())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
request.ManagedDirectory = outside
|
|
if err := validateStorageDirectories(request, []string{root}); err == nil {
|
|
t.Fatal("directory outside allow-list was accepted")
|
|
}
|
|
request.ManagedDirectory = filepath.Join(root, "a")
|
|
link := filepath.Join(root, "link")
|
|
if err := os.Symlink(request.ManagedDirectory, link); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
request.ManagedDirectory = link
|
|
if err := validateStorageDirectories(request, []string{root}); err == nil {
|
|
t.Fatal("symbolic-link directory was accepted")
|
|
}
|
|
}
|
|
|
|
func TestStorageFileInfoAlreadyUsedDetectsSameFilesystemObject(t *testing.T) {
|
|
firstPath := t.TempDir()
|
|
secondPath := t.TempDir()
|
|
first, err := os.Stat(firstPath)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
second, err := os.Stat(secondPath)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !storageFileInfoAlreadyUsed([]os.FileInfo{first}, first) {
|
|
t.Fatal("same filesystem object was not detected")
|
|
}
|
|
if storageFileInfoAlreadyUsed([]os.FileInfo{first}, second) {
|
|
t.Fatal("distinct filesystem objects were treated as aliases")
|
|
}
|
|
}
|
|
|
|
func TestStoragePathDangerousProtectsAgentState(t *testing.T) {
|
|
for _, path := range []string{"/var/lib/virt-agent", "/var/lib/virt-agent/storage"} {
|
|
if !storagePathIsDangerous(path) {
|
|
t.Fatalf("Agent state path %q was not protected", path)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestValidateStorageConfigurationRequest(t *testing.T) {
|
|
if err := ValidateStorageConfigurationRequest(validStorageConfigurationRequest()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for name, mutate := range map[string]func(*StorageConfigurationRequest){
|
|
"relative": func(value *StorageConfigurationRequest) { value.ManagedDirectory = "srv/virt/vms" },
|
|
"root": func(value *StorageConfigurationRequest) { value.ManagedDirectory = "/" },
|
|
"unclean": func(value *StorageConfigurationRequest) { value.ManagedDirectory = "/srv/virt/../vms" },
|
|
"duplicate": func(value *StorageConfigurationRequest) { value.ManagedDirectory = value.SeedDirectory },
|
|
"nested": func(value *StorageConfigurationRequest) { value.ManagedDirectory = value.SeedDirectory + "/vms" },
|
|
"oversized": func(value *StorageConfigurationRequest) {
|
|
value.ManagedDirectory = "/" + strings.Repeat("a", maximumStoragePathBytes)
|
|
},
|
|
} {
|
|
t.Run(name, func(t *testing.T) {
|
|
value := validStorageConfigurationRequest()
|
|
mutate(&value)
|
|
if err := ValidateStorageConfigurationRequest(value); err == nil {
|
|
t.Fatal("invalid storage configuration was accepted")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestStorageConfigurationPathDigestsDoNotExposePaths(t *testing.T) {
|
|
request := validStorageConfigurationRequest()
|
|
digests := StorageConfigurationPathDigests(request)
|
|
if len(digests) != 5 || len(digests[StorageRoleManaged]) != 64 ||
|
|
strings.Contains(digests[StorageRoleManaged], request.ManagedDirectory) {
|
|
t.Fatalf("path digests = %#v", digests)
|
|
}
|
|
}
|
|
|
|
func TestStorageConfigurationNeverRollsBackFixedPools(t *testing.T) {
|
|
file, err := parser.ParseFile(token.NewFileSet(), "libvirt_storage_configuration.go", nil, 0)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ast.Inspect(file, func(node ast.Node) bool {
|
|
switch value := node.(type) {
|
|
case *ast.FuncDecl:
|
|
if value.Name.Name == "rollbackDefinedStoragePools" {
|
|
t.Error("fixed storage pool rollback helper must not exist")
|
|
}
|
|
case *ast.CallExpr:
|
|
selector, ok := value.Fun.(*ast.SelectorExpr)
|
|
if ok && (selector.Sel.Name == "Destroy" || selector.Sel.Name == "Undefine") {
|
|
t.Errorf("storage configuration must not call %s on a fixed pool", selector.Sel.Name)
|
|
}
|
|
}
|
|
return true
|
|
})
|
|
}
|
|
|
|
func TestConfigureStorageRolesRetainsPartialProgressForSuccessor(t *testing.T) {
|
|
request := validStorageConfigurationRequest()
|
|
retained := make(map[string]string)
|
|
failure := errors.New("seed readiness failed")
|
|
err := configureStorageRoles(context.Background(), request, func(role, path string) error {
|
|
if role == StorageRoleSeed {
|
|
return failure
|
|
}
|
|
retained[role] = path
|
|
return nil
|
|
})
|
|
if !errors.Is(err, failure) {
|
|
t.Fatalf("first attempt error = %v", err)
|
|
}
|
|
if len(retained) != 2 || retained[StorageRoleManaged] != request.ManagedDirectory ||
|
|
retained[StorageRoleTemplate] != request.TemplateDirectory {
|
|
t.Fatalf("partial fixed-pool progress was not retained: %#v", retained)
|
|
}
|
|
|
|
err = configureStorageRoles(context.Background(), request, func(role, path string) error {
|
|
if existing, ok := retained[role]; ok && existing != path {
|
|
return errors.New("fixed pool path was rebound")
|
|
}
|
|
retained[role] = path
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("same-path successor did not converge: %v", err)
|
|
}
|
|
if len(retained) != len(storageRoles()) {
|
|
t.Fatalf("successor completed %d fixed roles, want %d", len(retained), len(storageRoles()))
|
|
}
|
|
}
|
|
|
|
func TestConfigureStorageRolesStopsBetweenRolesOnCancellation(t *testing.T) {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
var calls []string
|
|
err := configureStorageRoles(ctx, validStorageConfigurationRequest(), func(role, _ string) error {
|
|
calls = append(calls, role)
|
|
cancel()
|
|
return nil
|
|
})
|
|
if !errors.Is(err, context.Canceled) || len(calls) != 1 || calls[0] != StorageRoleManaged {
|
|
t.Fatalf("canceled role traversal calls=%v err=%v", calls, err)
|
|
}
|
|
}
|