mirror of
https://github.com/aljazceru/kata-containers.git
synced 2026-02-18 21:14:37 +01:00
api: add FetchSandbox
It finds out and existing sandbox and returns it. Signed-off-by: Peng Tao <bergwolf@gmail.com>
This commit is contained in:
@@ -92,6 +92,24 @@ func DeleteSandbox(sandboxID string) (VCSandbox, error) {
|
||||
return p, nil
|
||||
}
|
||||
|
||||
// FetchSandbox is the virtcontainers sandbox fetching entry point.
|
||||
// FetchSandbox will find out and connect to an existing sandbox and
|
||||
// return the sandbox structure.
|
||||
func FetchSandbox(sandboxID string) (VCSandbox, error) {
|
||||
if sandboxID == "" {
|
||||
return nil, errNeedSandboxID
|
||||
}
|
||||
|
||||
lockFile, err := rwLockSandbox(sandboxID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer unlockSandbox(lockFile)
|
||||
|
||||
// Fetch the sandbox from storage and create it.
|
||||
return fetchSandbox(sandboxID)
|
||||
}
|
||||
|
||||
// StartSandbox is the virtcontainers sandbox starting entry point.
|
||||
// StartSandbox will talk to the given hypervisor to start an existing
|
||||
// sandbox and all its containers.
|
||||
|
||||
@@ -2251,3 +2251,25 @@ func BenchmarkStartStop10ContainerQemuHypervisorHyperstartAgentNetworkNoop(b *te
|
||||
createStartStopDeleteContainers(b, sandboxConfig, contConfigs)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFetchSandbox(t *testing.T) {
|
||||
cleanUp()
|
||||
|
||||
config := newTestSandboxConfigNoop()
|
||||
|
||||
s, err := CreateSandbox(config)
|
||||
if s == nil || err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
fetched, err := FetchSandbox(s.ID())
|
||||
assert.Nil(t, err, "%v", err)
|
||||
assert.True(t, fetched == s, "fetched sandboxed do not match")
|
||||
}
|
||||
|
||||
func TestFetchNonExistingSandbox(t *testing.T) {
|
||||
cleanUp()
|
||||
|
||||
_, err := FetchSandbox("some-non-existing-sandbox-name")
|
||||
assert.NotNil(t, err, "fetch non-existing sandbox should fail")
|
||||
}
|
||||
|
||||
@@ -54,6 +54,12 @@ func (impl *VCImpl) ListSandbox() ([]SandboxStatus, error) {
|
||||
return ListSandbox()
|
||||
}
|
||||
|
||||
// FetchSandbox will find out and connect to an existing sandbox and
|
||||
// return the sandbox structure.
|
||||
func (impl *VCImpl) FetchSandbox(sandboxID string) (VCSandbox, error) {
|
||||
return FetchSandbox(sandboxID)
|
||||
}
|
||||
|
||||
// StatusSandbox implements the VC function of the same name.
|
||||
func (impl *VCImpl) StatusSandbox(sandboxID string) (SandboxStatus, error) {
|
||||
return StatusSandbox(sandboxID)
|
||||
|
||||
@@ -17,6 +17,7 @@ type VC interface {
|
||||
|
||||
CreateSandbox(sandboxConfig SandboxConfig) (VCSandbox, error)
|
||||
DeleteSandbox(sandboxID string) (VCSandbox, error)
|
||||
FetchSandbox(sandboxID string) (VCSandbox, error)
|
||||
ListSandbox() ([]SandboxStatus, error)
|
||||
PauseSandbox(sandboxID string) (VCSandbox, error)
|
||||
ResumeSandbox(sandboxID string) (VCSandbox, error)
|
||||
|
||||
@@ -52,6 +52,15 @@ func (m *VCMock) DeleteSandbox(sandboxID string) (vc.VCSandbox, error) {
|
||||
return nil, fmt.Errorf("%s: %s (%+v): sandboxID: %v", mockErrorPrefix, getSelf(), m, sandboxID)
|
||||
}
|
||||
|
||||
// FetchSandbox implements the VC function of the same name.
|
||||
func (m *VCMock) FetchSandbox(sandboxID string) (vc.VCSandbox, error) {
|
||||
if m.FetchSandboxFunc != nil {
|
||||
return m.FetchSandboxFunc(sandboxID)
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("%s: %s (%+v): sandboxID: %v", mockErrorPrefix, getSelf(), m, sandboxID)
|
||||
}
|
||||
|
||||
// StartSandbox implements the VC function of the same name.
|
||||
func (m *VCMock) StartSandbox(sandboxID string) (vc.VCSandbox, error) {
|
||||
if m.StartSandboxFunc != nil {
|
||||
|
||||
@@ -568,3 +568,31 @@ func TestVCMockProcessListContainer(t *testing.T) {
|
||||
assert.Error(err)
|
||||
assert.True(IsMockError(err))
|
||||
}
|
||||
|
||||
func TestVCMockFetchSandbox(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
m := &VCMock{}
|
||||
config := &vc.SandboxConfig{}
|
||||
assert.Nil(m.FetchSandboxFunc)
|
||||
|
||||
_, err := m.FetchSandbox(config.ID)
|
||||
assert.Error(err)
|
||||
assert.True(IsMockError(err))
|
||||
|
||||
m.FetchSandboxFunc = func(id string) (vc.VCSandbox, error) {
|
||||
return &Sandbox{}, nil
|
||||
}
|
||||
|
||||
sandbox, err := m.FetchSandbox(config.ID)
|
||||
assert.NoError(err)
|
||||
assert.Equal(sandbox, &Sandbox{})
|
||||
|
||||
// reset
|
||||
m.FetchSandboxFunc = nil
|
||||
|
||||
_, err = m.FetchSandbox(config.ID)
|
||||
assert.Error(err)
|
||||
assert.True(IsMockError(err))
|
||||
|
||||
}
|
||||
|
||||
@@ -39,6 +39,7 @@ type VCMock struct {
|
||||
CreateSandboxFunc func(sandboxConfig vc.SandboxConfig) (vc.VCSandbox, error)
|
||||
DeleteSandboxFunc func(sandboxID string) (vc.VCSandbox, error)
|
||||
ListSandboxFunc func() ([]vc.SandboxStatus, error)
|
||||
FetchSandboxFunc func(sandboxID string) (vc.VCSandbox, error)
|
||||
PauseSandboxFunc func(sandboxID string) (vc.VCSandbox, error)
|
||||
ResumeSandboxFunc func(sandboxID string) (vc.VCSandbox, error)
|
||||
RunSandboxFunc func(sandboxConfig vc.SandboxConfig) (vc.VCSandbox, error)
|
||||
|
||||
Reference in New Issue
Block a user