From 6b87ecfc1b4b2c2c8c9ccb0147e503b820d10e71 Mon Sep 17 00:00:00 2001 From: Samuel Ortiz Date: Thu, 20 Dec 2018 13:07:25 +0100 Subject: [PATCH] virtcontainers: store: Keep track of newly created Stores 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 --- virtcontainers/store/manager.go | 15 +++++++++++++-- virtcontainers/store/manager_test.go | 16 ++++++---------- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/virtcontainers/store/manager.go b/virtcontainers/store/manager.go index d2238538a4..de64b1aea9 100644 --- a/virtcontainers/store/manager.go +++ b/virtcontainers/store/manager.go @@ -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") diff --git a/virtcontainers/store/manager_test.go b/virtcontainers/store/manager_test.go index f2a3f52da8..61a3aa0797 100644 --- a/virtcontainers/store/manager_test.go +++ b/virtcontainers/store/manager_test.go @@ -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) @@ -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") @@ -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)