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 <sameo@linux.intel.com>
This commit is contained in:
Samuel Ortiz 2018-12-20 13:07:25 +01:00
parent efd50ecac9
commit 6b87ecfc1b
2 changed files with 19 additions and 12 deletions

View File

@ -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")

View File

@ -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)