virtcontainers: store: Add a Delete API

It's going to be used to completely clean a Store away.

Signed-off-by: Samuel Ortiz <sameo@linux.intel.com>
This commit is contained in:
Samuel Ortiz 2018-12-20 19:05:32 +01:00
parent f2ab58d841
commit ef11bf52a6
5 changed files with 64 additions and 2 deletions

View File

@ -45,6 +45,7 @@ func newBackend(scheme string) (backend, error) {
type backend interface {
new(ctx context.Context, path string, host string) error
delete() error
load(item Item, data interface{}) error
store(item Item, data interface{}) error
}

View File

@ -173,8 +173,8 @@ func (f *filesystem) new(ctx context.Context, path string, host string) error {
return f.initialize()
}
func (f *filesystem) delete() {
os.RemoveAll(f.path)
func (f *filesystem) delete() error {
return os.RemoveAll(f.path)
}
func (f *filesystem) load(item Item, data interface{}) error {

View File

@ -8,6 +8,7 @@ package store
import (
"context"
"io/ioutil"
"os"
"path/filepath"
"testing"
@ -64,3 +65,25 @@ func TestStoreFilesystemLoad(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, newData, data)
}
func TestStoreFilesystemDelete(t *testing.T) {
f := filesystem{}
err := f.new(context.Background(), rootPath, "")
assert.Nil(t, err)
data := TestNoopStructure{
Field1: "value1",
Field2: "value2",
}
// Store test data
err = f.store(State, data)
assert.Nil(t, err)
err = f.delete()
assert.Nil(t, err)
_, err = os.Stat(f.path)
assert.NotNil(t, err)
}

View File

@ -173,6 +173,13 @@ func New(ctx context.Context, storeURL string) (*Store, error) {
return s, nil
}
// DeleteAll deletes all Stores from the manager.
func DeleteAll() {
for _, s := range stores.stores {
s.Delete()
}
}
var storeLog = logrus.WithField("source", "virtcontainers/store")
// Logger returns a logrus logger appropriate for logging Store messages
@ -222,3 +229,22 @@ func (s *Store) Store(item Item, data interface{}) error {
return s.backend.store(item, data)
}
// Delete deletes all artifacts created by a Store.
// The Store is also removed from the manager.
func (s *Store) Delete() error {
span, _ := s.trace("Store")
defer span.Finish()
s.Lock()
defer s.Unlock()
if err := s.backend.delete(); err != nil {
return err
}
stores.removeStore(s.url)
s.url = ""
return nil
}

View File

@ -22,6 +22,18 @@ func TestNewStore(t *testing.T) {
assert.Equal(t, s.path, "/tmp/root1/")
}
func TestDeleteStore(t *testing.T) {
s, err := New(context.Background(), storeRoot)
assert.Nil(t, err)
err = s.Delete()
assert.Nil(t, err)
// We should no longer find storeRoot
newStore := stores.findStore(storeRoot)
assert.Nil(t, newStore, "findStore should not have found %s", storeRoot)
}
func TestManagerAddStore(t *testing.T) {
s, err := New(context.Background(), storeRoot)
assert.Nil(t, err)