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

Commit

Permalink
vc/store: reuse store
Browse files Browse the repository at this point in the history
As store.New() claims, we should reuse an existing store instead of
failing on duplicating stores.

Signed-off-by: Peng Tao <bergwolf@hyper.sh>
  • Loading branch information
bergwolf committed Oct 8, 2019
1 parent 238f3ce commit 4863aa9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
18 changes: 8 additions & 10 deletions virtcontainers/store/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,25 +100,23 @@ type manager struct {

var stores = &manager{stores: make(map[string]*Store)}

func (m *manager) addStore(s *Store) (err error) {
func (m *manager) addStore(s *Store) (rs *Store, err error) {
if s == nil {
return fmt.Errorf("Store can not be nil")
return nil, fmt.Errorf("Store can not be nil")
}

if s.url == "" {
return fmt.Errorf("Store URL can not be nil")
return nil, fmt.Errorf("Store URL can not be nil")
}

m.Lock()
defer m.Unlock()

if m.stores[s.url] != nil {
return fmt.Errorf("Store %s already added", s.url)
if m.stores[s.url] == nil {
m.stores[s.url] = s
}

m.stores[s.url] = s

return nil
return m.stores[s.url], nil
}

func (m *manager) removeStore(url string) {
Expand Down Expand Up @@ -165,11 +163,11 @@ func New(ctx context.Context, storeURL string) (*Store, error) {
s.backend = backend

// Create new backend
if err := s.backend.new(ctx, s.path, s.host); err != nil {
if err = s.backend.new(ctx, s.path, s.host); err != nil {
return nil, err
}

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

Expand Down
7 changes: 4 additions & 3 deletions virtcontainers/store/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,15 @@ func TestManagerAddStore(t *testing.T) {
assert.NotNil(t, newStore, "findStore failed")

// Duplicate, should fail
err = stores.addStore(s)
assert.NotNil(t, err, "addStore should have failed")
ns, err := stores.addStore(s)
assert.Nil(t, err, "addStore should not failed")
assert.Equal(t, s, ns)

// Try with an empty URL
sEmpty, err := New(context.Background(), storeRoot)
assert.Nil(t, err)
sEmpty.url = ""
err = stores.addStore(sEmpty)
_, err = stores.addStore(sEmpty)
assert.NotNil(t, err, "addStore should have failed on an empty store URL")

}
Expand Down

0 comments on commit 4863aa9

Please sign in to comment.