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:
Peng Tao
2018-04-19 18:53:28 +08:00
parent de32be7eed
commit d189be8579
7 changed files with 85 additions and 0 deletions

View File

@@ -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.

View File

@@ -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")
}

View File

@@ -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)

View File

@@ -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)

View File

@@ -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 {

View File

@@ -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))
}

View File

@@ -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)