mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-06-27 15:57:09 +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)}
|
||||
|
||||
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) {
|
||||
@ -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
|
||||
}
|
||||
|
||||
|
@ -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")
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user