Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
virtcontainers: store: Add a Delete API
Browse files Browse the repository at this point in the history
It's going to be used to completely clean a Store away.

Signed-off-by: Samuel Ortiz <[email protected]>
  • Loading branch information
Samuel Ortiz committed Feb 6, 2019
1 parent f2ab58d commit ef11bf5
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 2 deletions.
1 change: 1 addition & 0 deletions virtcontainers/store/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions virtcontainers/store/filesystem_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
23 changes: 23 additions & 0 deletions virtcontainers/store/filesystem_backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package store
import (
"context"
"io/ioutil"
"os"
"path/filepath"
"testing"

Expand Down Expand Up @@ -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)
}
26 changes: 26 additions & 0 deletions virtcontainers/store/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
12 changes: 12 additions & 0 deletions virtcontainers/store/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit ef11bf5

Please sign in to comment.