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

Commit

Permalink
api: add sandbox EnterContainer API
Browse files Browse the repository at this point in the history
And make VC EnterContainer a wrapper of it.

Signed-off-by: Peng Tao <[email protected]>
  • Loading branch information
bergwolf committed Apr 24, 2018
1 parent 488c3ee commit 29ce01f
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 10 deletions.
13 changes: 3 additions & 10 deletions virtcontainers/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,24 +422,17 @@ func EnterContainer(sandboxID, containerID string, cmd Cmd) (VCSandbox, VCContai
}
defer unlockSandbox(lockFile)

p, err := fetchSandbox(sandboxID)
if err != nil {
return nil, nil, nil, err
}

// Fetch the container.
c, err := p.findContainer(containerID)
s, err := fetchSandbox(sandboxID)
if err != nil {
return nil, nil, nil, err
}

// Enter it.
process, err := c.enter(cmd)
c, process, err := s.EnterContainer(containerID, cmd)
if err != nil {
return nil, nil, nil, err
}

return p, c, process, nil
return s, c, process, nil
}

// StatusContainer is the virtcontainers container status entry point.
Expand Down
1 change: 1 addition & 0 deletions virtcontainers/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type VCSandbox interface {
DeleteContainer(contID string) (VCContainer, error)
StartContainer(containerID string) (VCContainer, error)
StatusContainer(containerID string) (ContainerStatus, error)
EnterContainer(containerID string, cmd Cmd) (VCContainer, *Process, error)
}

// VCContainer is the Container interface
Expand Down
5 changes: 5 additions & 0 deletions virtcontainers/pkg/vcmock/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,8 @@ func (p *Sandbox) StatusContainer(contID string) (vc.ContainerStatus, error) {
func (p *Sandbox) Status() vc.SandboxStatus {
return vc.SandboxStatus{}
}

// EnterContainer implements the VCSandbox function of the same name.
func (p *Sandbox) EnterContainer(containerID string, cmd vc.Cmd) (vc.VCContainer, *vc.Process, error) {
return &Container{}, &vc.Process{}, nil
}
18 changes: 18 additions & 0 deletions virtcontainers/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -995,6 +995,24 @@ func (s *Sandbox) StatusContainer(containerID string) (ContainerStatus, error) {
return ContainerStatus{}, errNoSuchContainer
}

// EnterContainer is the virtcontainers container command execution entry point.
// EnterContainer enters an already running container and runs a given command.
func (s *Sandbox) EnterContainer(containerID string, cmd Cmd) (VCContainer, *Process, error) {
// Fetch the container.
c, err := s.findContainer(containerID)
if err != nil {
return nil, nil, err
}

// Enter it.
process, err := c.enter(cmd)
if err != nil {
return nil, nil, err
}

return c, process, nil
}

// createContainers registers all containers to the proxy, create the
// containers in the guest and starts one shim per container.
func (s *Sandbox) createContainers() error {
Expand Down
24 changes: 24 additions & 0 deletions virtcontainers/sandbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1364,3 +1364,27 @@ func TestStatusSandbox(t *testing.T) {

s.Status()
}

func TestEnterContainer(t *testing.T) {
s, err := testCreateSandbox(t, testSandboxID, MockHypervisor, newHypervisorConfig(nil, nil), NoopAgentType, NoopNetworkModel, NetworkConfig{}, nil, nil)
assert.Nil(t, err, "VirtContainers should not allow empty sandboxes")
defer cleanUp()

contID := "999"
cmd := Cmd{}
_, _, err = s.EnterContainer(contID, cmd)
assert.NotNil(t, err, "Entering non-existing container should fail")

contConfig := newTestContainerConfigNoop(contID)
_, err = s.CreateContainer(contConfig)
assert.Nil(t, err, "Failed to create container %+v in sandbox %+v: %v", contConfig, s, err)

_, _, err = s.EnterContainer(contID, cmd)
assert.NotNil(t, err, "Entering non-running container should fail")

err = s.start()
assert.Nil(t, err, "Failed to start sandbox: %v", err)

_, _, err = s.EnterContainer(contID, cmd)
assert.Nil(t, err, "Enter container failed: %v", err)
}

0 comments on commit 29ce01f

Please sign in to comment.