github.com/kata-containers/runtime@v0.0.0-20210505125100-04f29832a923/virtcontainers/store/filesystem_backend_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  type TestNoopStructure struct {
    19  	Field1 string
    20  	Field2 string
    21  }
    22  
    23  var rootPath = func() string {
    24  	dir, _ := ioutil.TempDir("", "")
    25  	return dir
    26  }()
    27  
    28  var expectedFilesystemData = "{\"Field1\":\"value1\",\"Field2\":\"value2\"}"
    29  
    30  func TestStoreFilesystemStore(t *testing.T) {
    31  	f := filesystem{}
    32  
    33  	err := f.new(context.Background(), rootPath, "")
    34  	defer f.delete()
    35  	assert.Nil(t, err)
    36  
    37  	data := TestNoopStructure{
    38  		Field1: "value1",
    39  		Field2: "value2",
    40  	}
    41  
    42  	err = f.store(State, data)
    43  	assert.Nil(t, err)
    44  
    45  	filesystemData, err := ioutil.ReadFile(filepath.Join(rootPath, StateFile))
    46  	assert.Nil(t, err)
    47  	assert.Equal(t, string(filesystemData), expectedFilesystemData)
    48  }
    49  
    50  func TestStoreFilesystemLoad(t *testing.T) {
    51  	f := filesystem{}
    52  
    53  	err := f.new(context.Background(), rootPath, "")
    54  	defer f.delete()
    55  	assert.Nil(t, err)
    56  
    57  	data := TestNoopStructure{
    58  		Field1: "value1",
    59  		Field2: "value2",
    60  	}
    61  
    62  	// Store test data
    63  	err = f.store(State, data)
    64  	assert.Nil(t, err)
    65  
    66  	// Load and compare
    67  	newData := TestNoopStructure{}
    68  	err = f.load(State, &newData)
    69  	assert.Nil(t, err)
    70  	assert.Equal(t, newData, data)
    71  }
    72  
    73  func TestStoreFilesystemDelete(t *testing.T) {
    74  	f := filesystem{}
    75  
    76  	err := f.new(context.Background(), rootPath, "")
    77  	assert.Nil(t, err)
    78  
    79  	data := TestNoopStructure{
    80  		Field1: "value1",
    81  		Field2: "value2",
    82  	}
    83  
    84  	// Store test data
    85  	err = f.store(State, data)
    86  	assert.Nil(t, err)
    87  
    88  	err = f.delete()
    89  	assert.Nil(t, err)
    90  
    91  	_, err = os.Stat(f.path)
    92  	assert.NotNil(t, err)
    93  }
    94  
    95  func TestStoreFilesystemRaw(t *testing.T) {
    96  	f := filesystem{}
    97  
    98  	err := f.new(context.Background(), rootPath, "")
    99  	defer f.delete()
   100  	assert.Nil(t, err)
   101  
   102  	path, err := f.raw("roah")
   103  	assert.Nil(t, err)
   104  	assert.Equal(t, path, filesystemScheme+"://"+filepath.Join(rootPath, "raw", "roah"))
   105  }
   106  
   107  func TestStoreFilesystemLockShared(t *testing.T) {
   108  	f := filesystem{}
   109  
   110  	err := f.new(context.Background(), rootPath, "")
   111  	defer f.delete()
   112  	assert.Nil(t, err)
   113  
   114  	// Take 2 shared locks
   115  	token1, err := f.lock(Lock, false)
   116  	assert.Nil(t, err)
   117  
   118  	token2, err := f.lock(Lock, false)
   119  	assert.Nil(t, err)
   120  
   121  	err = f.unlock(Lock, token1)
   122  	assert.Nil(t, err)
   123  
   124  	err = f.unlock(Lock, token2)
   125  	assert.Nil(t, err)
   126  
   127  	err = f.unlock(Lock, token2)
   128  	assert.NotNil(t, err)
   129  }
   130  
   131  func TestStoreFilesystemLockExclusive(t *testing.T) {
   132  	f := filesystem{}
   133  
   134  	err := f.new(context.Background(), rootPath, "")
   135  	defer f.delete()
   136  	assert.Nil(t, err)
   137  
   138  	// Take 1 exclusive lock
   139  	token, err := f.lock(Lock, true)
   140  	assert.Nil(t, err)
   141  
   142  	err = f.unlock(Lock, token)
   143  	assert.Nil(t, err)
   144  
   145  	token, err = f.lock(Lock, true)
   146  	assert.Nil(t, err)
   147  
   148  	err = f.unlock(Lock, token)
   149  	assert.Nil(t, err)
   150  
   151  	err = f.unlock(Lock, token)
   152  	assert.NotNil(t, err)
   153  }