mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-08-15 14:43:51 +00:00
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:
parent
efd50ecac9
commit
6b87ecfc1b
@ -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.
|
// If there is already a Store for the URL, we will re-use it.
|
||||||
// Otherwise a new Store is created.
|
// Otherwise a new Store is created.
|
||||||
func New(ctx context.Context, storeURL string) (*Store, error) {
|
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)
|
u, err := url.Parse(storeURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &Store{
|
s := &Store{
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
url: storeURL,
|
url: storeURL,
|
||||||
scheme: u.Scheme,
|
scheme: u.Scheme,
|
||||||
path: u.Path,
|
path: u.Path,
|
||||||
host: u.Host,
|
host: u.Host,
|
||||||
}, nil
|
}
|
||||||
|
|
||||||
|
if err := stores.addStore(s); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return s, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var storeLog = logrus.WithField("source", "virtcontainers/store")
|
var storeLog = logrus.WithField("source", "virtcontainers/store")
|
||||||
|
@ -25,9 +25,11 @@ func TestNewStore(t *testing.T) {
|
|||||||
func TestManagerAddStore(t *testing.T) {
|
func TestManagerAddStore(t *testing.T) {
|
||||||
s, err := New(context.Background(), storeRoot)
|
s, err := New(context.Background(), storeRoot)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
err = stores.addStore(s)
|
|
||||||
defer stores.removeStore(storeRoot)
|
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
|
// Duplicate, should fail
|
||||||
err = stores.addStore(s)
|
err = stores.addStore(s)
|
||||||
@ -43,12 +45,9 @@ func TestManagerAddStore(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestManagerRemoveStore(t *testing.T) {
|
func TestManagerRemoveStore(t *testing.T) {
|
||||||
s, err := New(context.Background(), storeRoot)
|
_, err := New(context.Background(), storeRoot)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
err = stores.addStore(s)
|
|
||||||
assert.Nil(t, err, "addStore failed")
|
|
||||||
|
|
||||||
// Positive find
|
// Positive find
|
||||||
newStore := stores.findStore(storeRoot)
|
newStore := stores.findStore(storeRoot)
|
||||||
assert.NotNil(t, newStore, "findStore failed")
|
assert.NotNil(t, newStore, "findStore failed")
|
||||||
@ -69,12 +68,9 @@ func TestManagerRemoveStore(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestManagerFindStore(t *testing.T) {
|
func TestManagerFindStore(t *testing.T) {
|
||||||
s, err := New(context.Background(), storeRoot)
|
_, err := New(context.Background(), storeRoot)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
err = stores.addStore(s)
|
|
||||||
defer stores.removeStore(storeRoot)
|
defer stores.removeStore(storeRoot)
|
||||||
assert.Nil(t, err, "addStore failed")
|
|
||||||
|
|
||||||
// Positive find
|
// Positive find
|
||||||
newStore := stores.findStore(storeRoot)
|
newStore := stores.findStore(storeRoot)
|
||||||
|
Loading…
Reference in New Issue
Block a user