diff --git a/virtcontainers/store/backend.go b/virtcontainers/store/backend.go index f61d2df59a..c5a6a7e39d 100644 --- a/virtcontainers/store/backend.go +++ b/virtcontainers/store/backend.go @@ -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 } diff --git a/virtcontainers/store/filesystem_backend.go b/virtcontainers/store/filesystem_backend.go index 4e5fb2c8df..efe3f26f8c 100644 --- a/virtcontainers/store/filesystem_backend.go +++ b/virtcontainers/store/filesystem_backend.go @@ -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 { diff --git a/virtcontainers/store/filesystem_backend_test.go b/virtcontainers/store/filesystem_backend_test.go index 460a61bd83..b8a3bbd92a 100644 --- a/virtcontainers/store/filesystem_backend_test.go +++ b/virtcontainers/store/filesystem_backend_test.go @@ -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) +} diff --git a/virtcontainers/store/manager.go b/virtcontainers/store/manager.go index cfae50bb49..ef4a09f78b 100644 --- a/virtcontainers/store/manager.go +++ b/virtcontainers/store/manager.go @@ -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 +} diff --git a/virtcontainers/store/manager_test.go b/virtcontainers/store/manager_test.go index 066e586256..f8668a0e2f 100644 --- a/virtcontainers/store/manager_test.go +++ b/virtcontainers/store/manager_test.go @@ -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)