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 #256 from sboeuf/improve_container_search_CLI
Browse files Browse the repository at this point in the history
cli: Optimize container research
  • Loading branch information
Eric Ernst authored Apr 30, 2018
2 parents f92d7dd + e6f066b commit ff9b2bd
Show file tree
Hide file tree
Showing 14 changed files with 836 additions and 694 deletions.
8 changes: 8 additions & 0 deletions cli/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,10 @@ func createSandbox(ociSpec oci.CompatOCISpec, runtimeConfig oci.RuntimeConfig,
return vc.Process{}, fmt.Errorf("BUG: Container list from sandbox is wrong, expecting only one container, found %d containers", len(containers))
}

if err := addContainerIDMapping(containerID, sandbox.ID()); err != nil {
return vc.Process{}, err
}

return containers[0].Process(), nil
}

Expand All @@ -260,6 +264,10 @@ func createContainer(ociSpec oci.CompatOCISpec, containerID, bundlePath,
return vc.Process{}, err
}

if err := addContainerIDMapping(containerID, sandboxID); err != nil {
return vc.Process{}, err
}

return c.Process(), nil
}

Expand Down
161 changes: 67 additions & 94 deletions cli/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,14 +305,13 @@ func TestCreateInvalidArgs(t *testing.T) {
return sandbox, nil
}

testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
// No pre-existing sandboxes
return []vc.SandboxStatus{}, nil
}
path, err := ioutil.TempDir("", "containers-mapping")
assert.NoError(err)
defer os.RemoveAll(path)
ctrsMapTreePath = path

defer func() {
testingImpl.CreateSandboxFunc = nil
testingImpl.ListSandboxFunc = nil
}()

tmpdir, err := ioutil.TempDir("", "")
Expand Down Expand Up @@ -355,14 +354,10 @@ func TestCreateInvalidArgs(t *testing.T) {
func TestCreateInvalidConfigJSON(t *testing.T) {
assert := assert.New(t)

testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
// No pre-existing sandboxes
return []vc.SandboxStatus{}, nil
}

defer func() {
testingImpl.ListSandboxFunc = nil
}()
path, err := ioutil.TempDir("", "containers-mapping")
assert.NoError(err)
defer os.RemoveAll(path)
ctrsMapTreePath = path

tmpdir, err := ioutil.TempDir("", "")
assert.NoError(err)
Expand Down Expand Up @@ -393,20 +388,17 @@ func TestCreateInvalidConfigJSON(t *testing.T) {
err := create(testContainerID, bundlePath, testConsole, pidFilePath, true, runtimeConfig)
assert.Errorf(err, "%+v", detach)
assert.False(vcmock.IsMockError(err))
os.RemoveAll(path)
}
}

func TestCreateInvalidContainerType(t *testing.T) {
assert := assert.New(t)

testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
// No pre-existing sandboxes
return []vc.SandboxStatus{}, nil
}

defer func() {
testingImpl.ListSandboxFunc = nil
}()
path, err := ioutil.TempDir("", "containers-mapping")
assert.NoError(err)
defer os.RemoveAll(path)
ctrsMapTreePath = path

tmpdir, err := ioutil.TempDir("", "")
assert.NoError(err)
Expand Down Expand Up @@ -440,20 +432,17 @@ func TestCreateInvalidContainerType(t *testing.T) {
err := create(testContainerID, bundlePath, testConsole, pidFilePath, true, runtimeConfig)
assert.Errorf(err, "%+v", detach)
assert.False(vcmock.IsMockError(err))
os.RemoveAll(path)
}
}

func TestCreateContainerInvalid(t *testing.T) {
assert := assert.New(t)

testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
// No pre-existing sandboxes
return []vc.SandboxStatus{}, nil
}

defer func() {
testingImpl.ListSandboxFunc = nil
}()
path, err := ioutil.TempDir("", "containers-mapping")
assert.NoError(err)
defer os.RemoveAll(path)
ctrsMapTreePath = path

tmpdir, err := ioutil.TempDir("", "")
assert.NoError(err)
Expand Down Expand Up @@ -488,6 +477,7 @@ func TestCreateContainerInvalid(t *testing.T) {
err := create(testContainerID, bundlePath, testConsole, pidFilePath, true, runtimeConfig)
assert.Errorf(err, "%+v", detach)
assert.False(vcmock.IsMockError(err))
os.RemoveAll(path)
}
}

Expand All @@ -501,17 +491,16 @@ func TestCreateProcessCgroupsPathSuccessful(t *testing.T) {
},
}

testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
// No pre-existing sandboxes
return []vc.SandboxStatus{}, nil
}
path, err := ioutil.TempDir("", "containers-mapping")
assert.NoError(err)
defer os.RemoveAll(path)
ctrsMapTreePath = path

testingImpl.CreateSandboxFunc = func(sandboxConfig vc.SandboxConfig) (vc.VCSandbox, error) {
return sandbox, nil
}

defer func() {
testingImpl.ListSandboxFunc = nil
testingImpl.CreateSandboxFunc = nil
}()

Expand Down Expand Up @@ -578,6 +567,7 @@ func TestCreateProcessCgroupsPathSuccessful(t *testing.T) {
for _, detach := range []bool{true, false} {
err := create(testContainerID, bundlePath, testConsole, pidFilePath, detach, runtimeConfig)
assert.NoError(err, "detached: %+v", detach)
os.RemoveAll(path)
}
}

Expand All @@ -596,17 +586,16 @@ func TestCreateCreateCgroupsFilesFail(t *testing.T) {
},
}

testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
// No pre-existing sandboxes
return []vc.SandboxStatus{}, nil
}
path, err := ioutil.TempDir("", "containers-mapping")
assert.NoError(err)
defer os.RemoveAll(path)
ctrsMapTreePath = path

testingImpl.CreateSandboxFunc = func(sandboxConfig vc.SandboxConfig) (vc.VCSandbox, error) {
return sandbox, nil
}

defer func() {
testingImpl.ListSandboxFunc = nil
testingImpl.CreateSandboxFunc = nil
}()

Expand Down Expand Up @@ -663,6 +652,7 @@ func TestCreateCreateCgroupsFilesFail(t *testing.T) {
err := create(testContainerID, bundlePath, testConsole, pidFilePath, true, runtimeConfig)
assert.Errorf(err, "%+v", detach)
assert.False(vcmock.IsMockError(err))
os.RemoveAll(path)
}
}

Expand All @@ -681,17 +671,16 @@ func TestCreateCreateCreatePidFileFail(t *testing.T) {
},
}

testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
// No pre-existing sandboxes
return []vc.SandboxStatus{}, nil
}
path, err := ioutil.TempDir("", "containers-mapping")
assert.NoError(err)
defer os.RemoveAll(path)
ctrsMapTreePath = path

testingImpl.CreateSandboxFunc = func(sandboxConfig vc.SandboxConfig) (vc.VCSandbox, error) {
return sandbox, nil
}

defer func() {
testingImpl.ListSandboxFunc = nil
testingImpl.CreateSandboxFunc = nil
}()

Expand Down Expand Up @@ -739,6 +728,7 @@ func TestCreateCreateCreatePidFileFail(t *testing.T) {
err := create(testContainerID, bundlePath, testConsole, pidFilePath, true, runtimeConfig)
assert.Errorf(err, "%+v", detach)
assert.False(vcmock.IsMockError(err))
os.RemoveAll(path)
}
}

Expand All @@ -752,17 +742,16 @@ func TestCreate(t *testing.T) {
},
}

testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
// No pre-existing sandboxes
return []vc.SandboxStatus{}, nil
}
path, err := ioutil.TempDir("", "containers-mapping")
assert.NoError(err)
defer os.RemoveAll(path)
ctrsMapTreePath = path

testingImpl.CreateSandboxFunc = func(sandboxConfig vc.SandboxConfig) (vc.VCSandbox, error) {
return sandbox, nil
}

defer func() {
testingImpl.ListSandboxFunc = nil
testingImpl.CreateSandboxFunc = nil
}()

Expand Down Expand Up @@ -804,20 +793,17 @@ func TestCreate(t *testing.T) {
for detach := range []bool{true, false} {
err := create(testContainerID, bundlePath, testConsole, pidFilePath, true, runtimeConfig)
assert.NoError(err, "%+v", detach)
os.RemoveAll(path)
}
}

func TestCreateInvalidKernelParams(t *testing.T) {
assert := assert.New(t)

testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
// No pre-existing sandboxes
return []vc.SandboxStatus{}, nil
}

defer func() {
testingImpl.ListSandboxFunc = nil
}()
path, err := ioutil.TempDir("", "containers-mapping")
assert.NoError(err)
defer os.RemoveAll(path)
ctrsMapTreePath = path

tmpdir, err := ioutil.TempDir("", "")
assert.NoError(err)
Expand Down Expand Up @@ -865,20 +851,17 @@ func TestCreateInvalidKernelParams(t *testing.T) {
err := create(testContainerID, bundlePath, testConsole, pidFilePath, true, runtimeConfig)
assert.Errorf(err, "%+v", detach)
assert.False(vcmock.IsMockError(err))
os.RemoveAll(path)
}
}

func TestCreateSandboxConfigFail(t *testing.T) {
assert := assert.New(t)

testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
// No pre-existing sandboxes
return []vc.SandboxStatus{}, nil
}

defer func() {
testingImpl.ListSandboxFunc = nil
}()
path, err := ioutil.TempDir("", "containers-mapping")
assert.NoError(err)
defer os.RemoveAll(path)
ctrsMapTreePath = path

tmpdir, err := ioutil.TempDir("", "")
assert.NoError(err)
Expand Down Expand Up @@ -918,14 +901,10 @@ func TestCreateSandboxConfigFail(t *testing.T) {
func TestCreateCreateSandboxFail(t *testing.T) {
assert := assert.New(t)

testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
// No pre-existing sandboxes
return []vc.SandboxStatus{}, nil
}

defer func() {
testingImpl.ListSandboxFunc = nil
}()
path, err := ioutil.TempDir("", "containers-mapping")
assert.NoError(err)
defer os.RemoveAll(path)
ctrsMapTreePath = path

tmpdir, err := ioutil.TempDir("", "")
assert.NoError(err)
Expand Down Expand Up @@ -953,14 +932,10 @@ func TestCreateCreateSandboxFail(t *testing.T) {
func TestCreateCreateContainerContainerConfigFail(t *testing.T) {
assert := assert.New(t)

testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
// No pre-existing sandboxes
return []vc.SandboxStatus{}, nil
}

defer func() {
testingImpl.ListSandboxFunc = nil
}()
path, err := ioutil.TempDir("", "containers-mapping")
assert.NoError(err)
defer os.RemoveAll(path)
ctrsMapTreePath = path

tmpdir, err := ioutil.TempDir("", "")
assert.NoError(err)
Expand Down Expand Up @@ -991,20 +966,17 @@ func TestCreateCreateContainerContainerConfigFail(t *testing.T) {
assert.Error(err)
assert.False(vcmock.IsMockError(err))
assert.True(strings.Contains(err.Error(), containerType))
os.RemoveAll(path)
}
}

func TestCreateCreateContainerFail(t *testing.T) {
assert := assert.New(t)

testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
// No pre-existing sandboxes
return []vc.SandboxStatus{}, nil
}

defer func() {
testingImpl.ListSandboxFunc = nil
}()
path, err := ioutil.TempDir("", "containers-mapping")
assert.NoError(err)
defer os.RemoveAll(path)
ctrsMapTreePath = path

tmpdir, err := ioutil.TempDir("", "")
assert.NoError(err)
Expand Down Expand Up @@ -1034,23 +1006,23 @@ func TestCreateCreateContainerFail(t *testing.T) {
_, err = createContainer(spec, testContainerID, bundlePath, testConsole, disableOutput)
assert.Error(err)
assert.True(vcmock.IsMockError(err))
os.RemoveAll(path)
}
}

func TestCreateCreateContainer(t *testing.T) {
assert := assert.New(t)

testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
// No pre-existing sandboxes
return []vc.SandboxStatus{}, nil
}
path, err := ioutil.TempDir("", "containers-mapping")
assert.NoError(err)
defer os.RemoveAll(path)
ctrsMapTreePath = path

testingImpl.CreateContainerFunc = func(sandboxID string, containerConfig vc.ContainerConfig) (vc.VCSandbox, vc.VCContainer, error) {
return &vcmock.Sandbox{}, &vcmock.Container{}, nil
}

defer func() {
testingImpl.ListSandboxFunc = nil
testingImpl.CreateContainerFunc = nil
}()

Expand Down Expand Up @@ -1081,6 +1053,7 @@ func TestCreateCreateContainer(t *testing.T) {
for _, disableOutput := range []bool{true, false} {
_, err = createContainer(spec, testContainerID, bundlePath, testConsole, disableOutput)
assert.NoError(err)
os.RemoveAll(path)
}
}

Expand Down
4 changes: 4 additions & 0 deletions cli/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ func delete(containerID string, force bool) error {
return err
}

if err := delContainerIDMapping(containerID); err != nil {
return err
}

return removeCgroupsPath(containerID, cgroupsPathList)
}

Expand Down
Loading

0 comments on commit ff9b2bd

Please sign in to comment.