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

Commit

Permalink
virtcontainers: store: Keep track of newly created Stores
Browse files Browse the repository at this point in the history
When a component creates a new store from a given root path, we add it
to the store manager and return it back when another component asks for
it.

Signed-off-by: Samuel Ortiz <[email protected]>
  • Loading branch information
Samuel Ortiz committed Feb 6, 2019
1 parent efd50ec commit 6b87ecf
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
15 changes: 13 additions & 2 deletions virtcontainers/store/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,18 +134,29 @@ func (m *manager) findStore(url string) *Store {
// If there is already a Store for the URL, we will re-use it.
// Otherwise a new Store is created.
func New(ctx context.Context, storeURL string) (*Store, error) {
// Do we already have such store?
if s := stores.findStore(storeURL); s != nil {
return s, nil
}

u, err := url.Parse(storeURL)
if err != nil {
return nil, err
}

return &Store{
s := &Store{
ctx: ctx,
url: storeURL,
scheme: u.Scheme,
path: u.Path,
host: u.Host,
}, nil
}

if err := stores.addStore(s); err != nil {
return nil, err
}

return s, nil
}

var storeLog = logrus.WithField("source", "virtcontainers/store")
Expand Down
16 changes: 6 additions & 10 deletions virtcontainers/store/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ func TestNewStore(t *testing.T) {
func TestManagerAddStore(t *testing.T) {
s, err := New(context.Background(), storeRoot)
assert.Nil(t, err)
err = stores.addStore(s)
defer stores.removeStore(storeRoot)
assert.Nil(t, err, "addStore failed")

// Positive find
newStore := stores.findStore(storeRoot)
assert.NotNil(t, newStore, "findStore failed")

// Duplicate, should fail
err = stores.addStore(s)
Expand All @@ -43,12 +45,9 @@ func TestManagerAddStore(t *testing.T) {
}

func TestManagerRemoveStore(t *testing.T) {
s, err := New(context.Background(), storeRoot)
_, err := New(context.Background(), storeRoot)
assert.Nil(t, err)

err = stores.addStore(s)
assert.Nil(t, err, "addStore failed")

// Positive find
newStore := stores.findStore(storeRoot)
assert.NotNil(t, newStore, "findStore failed")
Expand All @@ -69,12 +68,9 @@ func TestManagerRemoveStore(t *testing.T) {
}

func TestManagerFindStore(t *testing.T) {
s, err := New(context.Background(), storeRoot)
_, err := New(context.Background(), storeRoot)
assert.Nil(t, err)

err = stores.addStore(s)
defer stores.removeStore(storeRoot)
assert.Nil(t, err, "addStore failed")

// Positive find
newStore := stores.findStore(storeRoot)
Expand Down

0 comments on commit 6b87ecf

Please sign in to comment.