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

     1  // Copyright (c) 2019 Intel Corporation
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  //
     5  
     6  package store
     7  
     8  import (
     9  	"context"
    10  	"io/ioutil"
    11  	"os"
    12  	"path/filepath"
    13  	"testing"
    14  
    15  	"github.com/stretchr/testify/assert"
    16  )
    17  
    18  const testSandboxID = "7f49d00d-1995-4156-8c79-5f5ab24ce138"
    19  
    20  var sandboxDirConfig = ""
    21  var sandboxFileConfig = ""
    22  var sandboxDirState = ""
    23  var sandboxDirLock = ""
    24  var sandboxFileState = ""
    25  var sandboxFileLock = ""
    26  var storeRoot, storeRootDir = func() (string, string) {
    27  	dir, _ := ioutil.TempDir("", "")
    28  	return "file://" + dir, dir
    29  }()
    30  var testDir = ""
    31  var ConfigStoragePathSaved = func() string { return "" }
    32  var RunStoragePathSaved = func() string { return "" }
    33  
    34  func TestNewStore(t *testing.T) {
    35  	s, err := New(context.Background(), storeRoot)
    36  	assert.Nil(t, err)
    37  	assert.Equal(t, s.scheme, "file")
    38  	assert.Equal(t, s.host, "")
    39  	assert.Equal(t, s.path, storeRootDir)
    40  }
    41  
    42  func TestDeleteStore(t *testing.T) {
    43  	s, err := New(context.Background(), storeRoot)
    44  	assert.Nil(t, err)
    45  
    46  	err = s.Delete()
    47  	assert.Nil(t, err)
    48  
    49  	// We should no longer find storeRoot
    50  	newStore := stores.findStore(storeRoot)
    51  	assert.Nil(t, newStore, "findStore should not have found %s", storeRoot)
    52  }
    53  
    54  func TestManagerAddStore(t *testing.T) {
    55  	s, err := New(context.Background(), storeRoot)
    56  	assert.Nil(t, err)
    57  	defer stores.removeStore(storeRoot)
    58  
    59  	// Positive find
    60  	newStore := stores.findStore(storeRoot)
    61  	assert.NotNil(t, newStore, "findStore failed")
    62  
    63  	// Duplicate, should fail
    64  	ns, err := stores.addStore(s)
    65  	assert.Nil(t, err, "addStore should not failed")
    66  	assert.Equal(t, s, ns)
    67  
    68  	// Try with an empty URL
    69  	sEmpty, err := New(context.Background(), storeRoot)
    70  	assert.Nil(t, err)
    71  	sEmpty.url = ""
    72  	_, err = stores.addStore(sEmpty)
    73  	assert.NotNil(t, err, "addStore should have failed on an empty store URL")
    74  
    75  }
    76  
    77  func TestManagerRemoveStore(t *testing.T) {
    78  	_, err := New(context.Background(), storeRoot)
    79  	assert.Nil(t, err)
    80  
    81  	// Positive find
    82  	newStore := stores.findStore(storeRoot)
    83  	assert.NotNil(t, newStore, "findStore failed")
    84  
    85  	// Negative removal
    86  	stores.removeStore(storeRoot + "foobar")
    87  
    88  	// We should still find storeRoot
    89  	newStore = stores.findStore(storeRoot)
    90  	assert.NotNil(t, newStore, "findStore failed")
    91  
    92  	// Positive removal
    93  	stores.removeStore(storeRoot)
    94  
    95  	// We should no longer find storeRoot
    96  	newStore = stores.findStore(storeRoot)
    97  	assert.Nil(t, newStore, "findStore should not have found %s", storeRoot)
    98  }
    99  
   100  func TestManagerFindStore(t *testing.T) {
   101  	_, err := New(context.Background(), storeRoot)
   102  	assert.Nil(t, err)
   103  	defer stores.removeStore(storeRoot)
   104  
   105  	// Positive find
   106  	newStore := stores.findStore(storeRoot)
   107  	assert.NotNil(t, newStore, "findStore failed")
   108  
   109  	// Negative find
   110  	newStore = stores.findStore(storeRoot + "foobar")
   111  	assert.Nil(t, newStore, "findStore should not have found a new store")
   112  }
   113  
   114  // TestMain is the common main function used by ALL the test functions
   115  // for the store.
   116  func TestMain(m *testing.M) {
   117  	setup()
   118  	rt := m.Run()
   119  	shutdown()
   120  	os.Exit(rt)
   121  }
   122  
   123  func shutdown() {
   124  	os.RemoveAll(testDir)
   125  	ConfigStoragePath = ConfigStoragePathSaved
   126  	RunStoragePath = RunStoragePathSaved
   127  }
   128  
   129  func setup() {
   130  	var err error
   131  	testDir, err = ioutil.TempDir("", "store-tmp-")
   132  	if err != nil {
   133  		panic(err)
   134  	}
   135  
   136  	ConfigStoragePathSaved = ConfigStoragePath
   137  	RunStoragePathSaved = RunStoragePath
   138  	// allow the tests to run without affecting the host system.
   139  	ConfigStoragePath = func() string { return filepath.Join(testDir, StoragePathSuffix, "config") }
   140  	RunStoragePath = func() string { return filepath.Join(testDir, StoragePathSuffix, "run") }
   141  
   142  	// set now that ConfigStoragePath has been overridden.
   143  	sandboxDirConfig = filepath.Join(ConfigStoragePath(), testSandboxID)
   144  	sandboxFileConfig = filepath.Join(ConfigStoragePath(), testSandboxID, ConfigurationFile)
   145  	sandboxDirState = filepath.Join(RunStoragePath(), testSandboxID)
   146  	sandboxDirLock = filepath.Join(RunStoragePath(), testSandboxID)
   147  	sandboxFileState = filepath.Join(RunStoragePath(), testSandboxID, StateFile)
   148  	sandboxFileLock = filepath.Join(RunStoragePath(), testSandboxID, LockFile)
   149  }