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 startcontainer API
Browse files Browse the repository at this point in the history
And make VC.StartContainer a wrapper of it.

Signed-off-by: Peng Tao <[email protected]>
  • Loading branch information
bergwolf committed Apr 24, 2018
1 parent d9144c8 commit 4b30446
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 9 deletions.
11 changes: 2 additions & 9 deletions virtcontainers/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,19 +355,12 @@ func StartContainer(sandboxID, containerID string) (VCContainer, error) {
}
defer unlockSandbox(lockFile)

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

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

// Start it.
err = c.start()
c, err := s.StartContainer(containerID)
if err != nil {
return nil, err
}
Expand Down
1 change: 1 addition & 0 deletions virtcontainers/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type VCSandbox interface {
Delete() error
CreateContainer(contConfig ContainerConfig) (VCContainer, error)
DeleteContainer(contID string) (VCContainer, error)
StartContainer(containerID string) (VCContainer, 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 @@ -79,3 +79,8 @@ func (p *Sandbox) CreateContainer(conf vc.ContainerConfig) (vc.VCContainer, erro
func (p *Sandbox) DeleteContainer(contID string) (vc.VCContainer, error) {
return &Container{}, nil
}

// StartContainer implements the VCSandbox function of the same name.
func (p *Sandbox) StartContainer(contID string) (vc.VCContainer, error) {
return &Container{}, nil
}
17 changes: 17 additions & 0 deletions virtcontainers/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,23 @@ func (s *Sandbox) CreateContainer(contConfig ContainerConfig) (VCContainer, erro
return c, nil
}

// StartContainer starts a container in the sandbox
func (s *Sandbox) StartContainer(containerID string) (VCContainer, error) {
// Fetch the container.
c, err := s.findContainer(containerID)
if err != nil {
return nil, err
}

// Start it.
err = c.start()
if err != nil {
return nil, err
}

return c, nil
}

// DeleteContainer deletes a container from the sandbox
func (s *Sandbox) DeleteContainer(containerID string) (VCContainer, error) {
if containerID == "" {
Expand Down
20 changes: 20 additions & 0 deletions virtcontainers/sandbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1316,3 +1316,23 @@ func TestDeleteContainer(t *testing.T) {
_, err = s.DeleteContainer(contID)
assert.Nil(t, err, "Failed to delete container %s in sandbox %s: %v", contID, s.ID(), err)
}

func TestStartContainer(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"
_, err = s.StartContainer(contID)
assert.NotNil(t, err, "Starting non-existing container should fail")

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

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.StartContainer(contID)
assert.Nil(t, err, "Start container failed: %v", err)
}

0 comments on commit 4b30446

Please sign in to comment.