Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
virtcontainers/persist: update GetDriver to support rootless fs
Browse files Browse the repository at this point in the history
GetDriver returns new PersistDriver according to current needs, a mock fs
driver is returned when mockTesting is enabled, a rootless fs is returned when
rootless is detected, otherwise a fs driver is used.

Signed-off-by: Julio Montes <[email protected]>
  • Loading branch information
Julio Montes committed Feb 12, 2020
1 parent dd2762f commit 71f48a3
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 6 deletions.
44 changes: 41 additions & 3 deletions virtcontainers/persist/manager.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright (c) 2019 Huawei Corporation
// Copyright (c) 2020 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
//
Expand All @@ -9,12 +10,18 @@ import (
"fmt"

exp "github.com/kata-containers/runtime/virtcontainers/experimental"
"github.com/kata-containers/runtime/virtcontainers/persist/api"
persistapi "github.com/kata-containers/runtime/virtcontainers/persist/api"
"github.com/kata-containers/runtime/virtcontainers/persist/fs"
"github.com/kata-containers/runtime/virtcontainers/pkg/rootless"
)

type initFunc (func() (persistapi.PersistDriver, error))

const (
RootFSName = "fs"
RootlessFSName = "rootlessfs"
)

var (
// NewStoreFeature is an experimental feature
NewStoreFeature = exp.Feature{
Expand All @@ -25,16 +32,22 @@ var (
expErr error
supportedDrivers = map[string]initFunc{

"fs": fs.Init,
RootFSName: fs.Init,
RootlessFSName: fs.RootlessInit,
}
mockTesting = false
)

func init() {
expErr = exp.Register(NewStoreFeature)
}

func EnableMockTesting() {
mockTesting = true
}

// GetDriver returns new PersistDriver according to driver name
func GetDriver(name string) (persistapi.PersistDriver, error) {
func GetDriverByName(name string) (persistapi.PersistDriver, error) {
if expErr != nil {
return nil, expErr
}
Expand All @@ -45,3 +58,28 @@ func GetDriver(name string) (persistapi.PersistDriver, error) {

return nil, fmt.Errorf("failed to get storage driver %q", name)
}

// GetDriver returns new PersistDriver according to current needs.
// For example, a rootless FS driver is returned if the process is running
// as unprivileged process.
func GetDriver() (persistapi.PersistDriver, error) {
if expErr != nil {
return nil, expErr
}

if mockTesting {
return fs.MockFSInit()
}

if rootless.IsRootless() {
if f, ok := supportedDrivers[RootlessFSName]; ok {
return f()
}
}

if f, ok := supportedDrivers[RootFSName]; ok {
return f()
}

return nil, fmt.Errorf("Could not find a FS driver")
}
41 changes: 38 additions & 3 deletions virtcontainers/persist/manager_test.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,57 @@
// Copyright (c) 2019 Huawei Corporation
// Copyright (c) 2020 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
//

package persist

import (
"os"
"testing"

persistapi "github.com/kata-containers/runtime/virtcontainers/persist/api"
"github.com/kata-containers/runtime/virtcontainers/persist/fs"
"github.com/stretchr/testify/assert"
)

func TestGetDriver(t *testing.T) {
nonexist, err := GetDriver("non-exist")
func TestGetDriverByName(t *testing.T) {
nonexist, err := GetDriverByName("non-exist")
assert.NotNil(t, err)
assert.Nil(t, nonexist)

fsDriver, err := GetDriver("fs")
fsDriver, err := GetDriverByName("fs")
assert.Nil(t, err)
assert.NotNil(t, fsDriver)
}

func TestGetDriver(t *testing.T) {
assert := assert.New(t)
orgMockTesting := mockTesting
defer func() {
mockTesting = orgMockTesting
}()

mockTesting = false

fsd, err := GetDriver()
assert.NoError(err)

var expectedFS persistapi.PersistDriver
if os.Getuid() != 0 {
expectedFS, err = fs.RootlessInit()
} else {
expectedFS, err = fs.Init()
}

assert.NoError(err)
assert.Equal(expectedFS, fsd)

// Testing mock driver
mockTesting = true
fsd, err = GetDriver()
assert.NoError(err)
expectedFS, err = fs.MockFSInit()
assert.NoError(err)
assert.Equal(expectedFS, fsd)
}

0 comments on commit 71f48a3

Please sign in to comment.