github.com/kata-containers/runtime@v0.0.0-20210505125100-04f29832a923/virtcontainers/persist/manager_test.go (about)

     1  // Copyright (c) 2019 Huawei Corporation
     2  // Copyright (c) 2020 Intel Corporation
     3  //
     4  // SPDX-License-Identifier: Apache-2.0
     5  //
     6  
     7  package persist
     8  
     9  import (
    10  	"os"
    11  	"testing"
    12  
    13  	persistapi "github.com/kata-containers/runtime/virtcontainers/persist/api"
    14  	"github.com/kata-containers/runtime/virtcontainers/persist/fs"
    15  	"github.com/stretchr/testify/assert"
    16  )
    17  
    18  func TestGetDriverByName(t *testing.T) {
    19  	nonexist, err := GetDriverByName("non-exist")
    20  	assert.NotNil(t, err)
    21  	assert.Nil(t, nonexist)
    22  
    23  	fsDriver, err := GetDriverByName("fs")
    24  	assert.Nil(t, err)
    25  	assert.NotNil(t, fsDriver)
    26  }
    27  
    28  func TestGetDriver(t *testing.T) {
    29  	assert := assert.New(t)
    30  	orgMockTesting := mockTesting
    31  	defer func() {
    32  		mockTesting = orgMockTesting
    33  	}()
    34  
    35  	mockTesting = false
    36  
    37  	fsd, err := GetDriver()
    38  	assert.NoError(err)
    39  
    40  	var expectedFS persistapi.PersistDriver
    41  	if os.Getuid() != 0 {
    42  		expectedFS, err = fs.RootlessInit()
    43  	} else {
    44  		expectedFS, err = fs.Init()
    45  	}
    46  
    47  	assert.NoError(err)
    48  	assert.Equal(expectedFS, fsd)
    49  
    50  	// Testing mock driver
    51  	mockTesting = true
    52  	fsd, err = GetDriver()
    53  	assert.NoError(err)
    54  	expectedFS, err = fs.MockFSInit()
    55  	assert.NoError(err)
    56  	assert.Equal(expectedFS, fsd)
    57  }