Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
api: add FetchSandbox
Browse files Browse the repository at this point in the history
It finds out and existing sandbox and returns it.

Signed-off-by: Peng Tao <[email protected]>
  • Loading branch information
bergwolf committed Apr 24, 2018
1 parent de32be7 commit d189be8
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 0 deletions.
18 changes: 18 additions & 0 deletions virtcontainers/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
22 changes: 22 additions & 0 deletions virtcontainers/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
6 changes: 6 additions & 0 deletions virtcontainers/implementation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions virtcontainers/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions virtcontainers/pkg/vcmock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
28 changes: 28 additions & 0 deletions virtcontainers/pkg/vcmock/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))

}
1 change: 1 addition & 0 deletions virtcontainers/pkg/vcmock/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit d189be8

Please sign in to comment.