mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-06-28 16:27:50 +00:00
vc/store: reuse store
As store.New() claims, we should reuse an existing store instead of failing on duplicating stores. Signed-off-by: Peng Tao <bergwolf@hyper.sh>
This commit is contained in:
parent
238f3cec56
commit
4863aa998e
@ -100,25 +100,23 @@ type manager struct {
|
|||||||
|
|
||||||
var stores = &manager{stores: make(map[string]*Store)}
|
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 {
|
if s == nil {
|
||||||
return fmt.Errorf("Store can not be nil")
|
return nil, fmt.Errorf("Store can not be nil")
|
||||||
}
|
}
|
||||||
|
|
||||||
if s.url == "" {
|
if s.url == "" {
|
||||||
return fmt.Errorf("Store URL can not be nil")
|
return nil, fmt.Errorf("Store URL can not be nil")
|
||||||
}
|
}
|
||||||
|
|
||||||
m.Lock()
|
m.Lock()
|
||||||
defer m.Unlock()
|
defer m.Unlock()
|
||||||
|
|
||||||
if m.stores[s.url] != nil {
|
if m.stores[s.url] == nil {
|
||||||
return fmt.Errorf("Store %s already added", s.url)
|
m.stores[s.url] = s
|
||||||
}
|
}
|
||||||
|
|
||||||
m.stores[s.url] = s
|
return m.stores[s.url], nil
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *manager) removeStore(url string) {
|
func (m *manager) removeStore(url string) {
|
||||||
@ -165,11 +163,11 @@ func New(ctx context.Context, storeURL string) (*Store, error) {
|
|||||||
s.backend = backend
|
s.backend = backend
|
||||||
|
|
||||||
// Create new 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
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := stores.addStore(s); err != nil {
|
if s, err = stores.addStore(s); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -55,14 +55,15 @@ func TestManagerAddStore(t *testing.T) {
|
|||||||
assert.NotNil(t, newStore, "findStore failed")
|
assert.NotNil(t, newStore, "findStore failed")
|
||||||
|
|
||||||
// Duplicate, should fail
|
// Duplicate, should fail
|
||||||
err = stores.addStore(s)
|
ns, err := stores.addStore(s)
|
||||||
assert.NotNil(t, err, "addStore should have failed")
|
assert.Nil(t, err, "addStore should not failed")
|
||||||
|
assert.Equal(t, s, ns)
|
||||||
|
|
||||||
// Try with an empty URL
|
// Try with an empty URL
|
||||||
sEmpty, err := New(context.Background(), storeRoot)
|
sEmpty, err := New(context.Background(), storeRoot)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
sEmpty.url = ""
|
sEmpty.url = ""
|
||||||
err = stores.addStore(sEmpty)
|
_, err = stores.addStore(sEmpty)
|
||||||
assert.NotNil(t, err, "addStore should have failed on an empty store URL")
|
assert.NotNil(t, err, "addStore should have failed on an empty store URL")
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user