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

Commit

Permalink
Merge pull request #121 from sboeuf/exec_process
Browse files Browse the repository at this point in the history
exec: Allow to exec a process on a ready container
  • Loading branch information
grahamwhaley authored Mar 29, 2018
2 parents 9a7813e + aa469f4 commit 6ac1958
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 10 deletions.
8 changes: 5 additions & 3 deletions cli/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,11 @@ func execute(context *cli.Context) error {

params.cID = status.ID

// container MUST be running
if status.State.State != vc.StateRunning {
return fmt.Errorf("Container %s is not running", params.cID)
// container MUST be ready or running.
if status.State.State != vc.StateReady &&
status.State.State != vc.StateRunning {
return fmt.Errorf("Container %s is not ready or running",
params.cID)
}

envVars, err := oci.EnvVars(params.ociProcess.Env)
Expand Down
31 changes: 26 additions & 5 deletions cli/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func TestExecuteErrors(t *testing.T) {
assert.Error(err)
assert.False(vcmock.IsMockError(err))

// Container not running
// Container state undefined
configPath := testConfigSetup(t)
configJSON, err := readOCIConfigJSON(configPath)
assert.NoError(err)
Expand All @@ -99,13 +99,34 @@ func TestExecuteErrors(t *testing.T) {
vcAnnotations.ConfigJSONKey: configJSON,
}

containerState := vc.State{}
testingImpl.ListPodFunc = func() ([]vc.PodStatus, error) {
return newSingleContainerPodStatusList(testPodID, testContainerID, vc.State{}, vc.State{}, annotations), nil
return newSingleContainerPodStatusList(testPodID, testContainerID, vc.State{}, containerState, annotations), nil
}

defer func() {
testingImpl.ListPodFunc = nil
}()
err = execute(ctx)
assert.Error(err)
assert.False(vcmock.IsMockError(err))

// Container paused
containerState = vc.State{
State: vc.StatePaused,
}
testingImpl.ListPodFunc = func() ([]vc.PodStatus, error) {
return newSingleContainerPodStatusList(testPodID, testContainerID, vc.State{}, containerState, annotations), nil
}

err = execute(ctx)
assert.Error(err)
assert.False(vcmock.IsMockError(err))

// Container stopped
containerState = vc.State{
State: vc.StateStopped,
}
testingImpl.ListPodFunc = func() ([]vc.PodStatus, error) {
return newSingleContainerPodStatusList(testPodID, testContainerID, vc.State{}, containerState, annotations), nil
}

err = execute(ctx)
assert.Error(err)
Expand Down
6 changes: 4 additions & 2 deletions virtcontainers/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -627,8 +627,10 @@ func (c *Container) enter(cmd Cmd) (*Process, error) {
return nil, err
}

if c.state.State != StateRunning {
return nil, fmt.Errorf("Container not running, impossible to enter")
if c.state.State != StateReady &&
c.state.State != StateRunning {
return nil, fmt.Errorf("Container not ready or running, " +
"impossible to enter")
}

process, err := c.pod.agent.exec(c.pod, *c, cmd)
Expand Down
26 changes: 26 additions & 0 deletions virtcontainers/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,3 +336,29 @@ func TestContainerRemoveResources(t *testing.T) {
err = c.removeResources()
assert.Nil(err)
}

func TestContainerEnterErrorsOnContainerStates(t *testing.T) {
assert := assert.New(t)
c := &Container{
pod: &Pod{
state: State{
State: StateRunning,
},
},
}
cmd := Cmd{}

// Container state undefined
_, err := c.enter(cmd)
assert.Error(err)

// Container paused
c.state.State = StatePaused
_, err = c.enter(cmd)
assert.Error(err)

// Container stopped
c.state.State = StateStopped
_, err = c.enter(cmd)
assert.Error(err)
}

0 comments on commit 6ac1958

Please sign in to comment.