mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-07-31 07:19:06 +00:00
cli: Optimize container research
This commit will allow for better performance regarding the time spent to retrieve the sandbox ID related to a container ID. The way it works is by relying on a specific mapping between container IDs and sanbox IDs, meaning it allows to retrieve directly the sandbox ID related to a container ID from the CLI. This lowers complexity from O(n²) to O(1), because we don't need to call into ListPod() which was parsing all the pods and all the containers on the system everytime the CLI need to retrieve this mapping. This commit also updates the whole unit tests as a consequence. This is involving most of them since they were all relying on ListPod() before. Fixes #212 Signed-off-by: Sebastien Boeuf <sebastien.boeuf@intel.com>
This commit is contained in:
parent
f92d7dd1c1
commit
e6f066b828
@ -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
|
||||
}
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
|
@ -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("", "")
|
||||
@ -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)
|
||||
@ -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)
|
||||
@ -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)
|
||||
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@ -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
|
||||
}()
|
||||
|
||||
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@ -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
|
||||
}()
|
||||
|
||||
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@ -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
|
||||
}()
|
||||
|
||||
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@ -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
|
||||
}()
|
||||
|
||||
@ -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)
|
||||
@ -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)
|
||||
@ -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)
|
||||
@ -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)
|
||||
@ -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)
|
||||
@ -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
|
||||
}()
|
||||
|
||||
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
}
|
||||
|
||||
|
@ -54,18 +54,10 @@ func TestDeleteInvalidContainer(t *testing.T) {
|
||||
assert.Error(err)
|
||||
assert.False(vcmock.IsMockError(err))
|
||||
|
||||
// Mock Listsandbox error
|
||||
err = delete(testContainerID, false)
|
||||
assert.Error(err)
|
||||
assert.True(vcmock.IsMockError(err))
|
||||
|
||||
testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
|
||||
return []vc.SandboxStatus{}, nil
|
||||
}
|
||||
|
||||
defer func() {
|
||||
testingImpl.ListSandboxFunc = nil
|
||||
}()
|
||||
path, err := ioutil.TempDir("", "containers-mapping")
|
||||
assert.NoError(err)
|
||||
defer os.RemoveAll(path)
|
||||
ctrsMapTreePath = path
|
||||
|
||||
// Container missing in ListSandbox
|
||||
err = delete(testContainerID, false)
|
||||
@ -80,25 +72,22 @@ func TestDeleteMissingContainerTypeAnnotation(t *testing.T) {
|
||||
MockID: testSandboxID,
|
||||
}
|
||||
|
||||
testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
|
||||
return []vc.SandboxStatus{
|
||||
{
|
||||
ID: sandbox.ID(),
|
||||
ContainersStatus: []vc.ContainerStatus{
|
||||
{
|
||||
ID: sandbox.ID(),
|
||||
Annotations: map[string]string{},
|
||||
},
|
||||
},
|
||||
},
|
||||
path, err := createTempContainerIDMapping(sandbox.ID(), sandbox.ID())
|
||||
assert.NoError(err)
|
||||
defer os.RemoveAll(path)
|
||||
|
||||
testingImpl.StatusContainerFunc = func(sandboxID, containerID string) (vc.ContainerStatus, error) {
|
||||
return vc.ContainerStatus{
|
||||
ID: sandbox.ID(),
|
||||
Annotations: map[string]string{},
|
||||
}, nil
|
||||
}
|
||||
|
||||
defer func() {
|
||||
testingImpl.ListSandboxFunc = nil
|
||||
testingImpl.StatusContainerFunc = nil
|
||||
}()
|
||||
|
||||
err := delete(sandbox.ID(), false)
|
||||
err = delete(sandbox.ID(), false)
|
||||
assert.Error(err)
|
||||
assert.False(vcmock.IsMockError(err))
|
||||
}
|
||||
@ -110,27 +99,24 @@ func TestDeleteInvalidConfig(t *testing.T) {
|
||||
MockID: testSandboxID,
|
||||
}
|
||||
|
||||
testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
|
||||
return []vc.SandboxStatus{
|
||||
{
|
||||
ID: sandbox.ID(),
|
||||
ContainersStatus: []vc.ContainerStatus{
|
||||
{
|
||||
ID: sandbox.ID(),
|
||||
Annotations: map[string]string{
|
||||
vcAnnotations.ContainerTypeKey: string(vc.PodSandbox),
|
||||
},
|
||||
},
|
||||
},
|
||||
path, err := createTempContainerIDMapping(sandbox.ID(), sandbox.ID())
|
||||
assert.NoError(err)
|
||||
defer os.RemoveAll(path)
|
||||
|
||||
testingImpl.StatusContainerFunc = func(sandboxID, containerID string) (vc.ContainerStatus, error) {
|
||||
return vc.ContainerStatus{
|
||||
ID: sandbox.ID(),
|
||||
Annotations: map[string]string{
|
||||
vcAnnotations.ContainerTypeKey: string(vc.PodSandbox),
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
defer func() {
|
||||
testingImpl.ListSandboxFunc = nil
|
||||
testingImpl.StatusContainerFunc = nil
|
||||
}()
|
||||
|
||||
err := delete(sandbox.ID(), false)
|
||||
err = delete(sandbox.ID(), false)
|
||||
assert.Error(err)
|
||||
assert.False(vcmock.IsMockError(err))
|
||||
}
|
||||
@ -164,28 +150,25 @@ func TestDeleteSandbox(t *testing.T) {
|
||||
configJSON, err := readOCIConfigJSON(configPath)
|
||||
assert.NoError(err)
|
||||
|
||||
testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
|
||||
return []vc.SandboxStatus{
|
||||
{
|
||||
ID: sandbox.ID(),
|
||||
ContainersStatus: []vc.ContainerStatus{
|
||||
{
|
||||
ID: sandbox.ID(),
|
||||
Annotations: map[string]string{
|
||||
vcAnnotations.ContainerTypeKey: string(vc.PodSandbox),
|
||||
vcAnnotations.ConfigJSONKey: configJSON,
|
||||
},
|
||||
State: vc.State{
|
||||
State: "ready",
|
||||
},
|
||||
},
|
||||
},
|
||||
path, err := createTempContainerIDMapping(sandbox.ID(), sandbox.ID())
|
||||
assert.NoError(err)
|
||||
defer os.RemoveAll(path)
|
||||
|
||||
testingImpl.StatusContainerFunc = func(sandboxID, containerID string) (vc.ContainerStatus, error) {
|
||||
return vc.ContainerStatus{
|
||||
ID: sandbox.ID(),
|
||||
Annotations: map[string]string{
|
||||
vcAnnotations.ContainerTypeKey: string(vc.PodSandbox),
|
||||
vcAnnotations.ConfigJSONKey: configJSON,
|
||||
},
|
||||
State: vc.State{
|
||||
State: "ready",
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
defer func() {
|
||||
testingImpl.ListSandboxFunc = nil
|
||||
testingImpl.StatusContainerFunc = nil
|
||||
}()
|
||||
|
||||
err = delete(sandbox.ID(), false)
|
||||
@ -244,28 +227,25 @@ func TestDeleteInvalidContainerType(t *testing.T) {
|
||||
configJSON, err := readOCIConfigJSON(configPath)
|
||||
assert.NoError(err)
|
||||
|
||||
testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
|
||||
return []vc.SandboxStatus{
|
||||
{
|
||||
ID: sandbox.ID(),
|
||||
ContainersStatus: []vc.ContainerStatus{
|
||||
{
|
||||
ID: sandbox.ID(),
|
||||
Annotations: map[string]string{
|
||||
vcAnnotations.ContainerTypeKey: "InvalidType",
|
||||
vcAnnotations.ConfigJSONKey: configJSON,
|
||||
},
|
||||
State: vc.State{
|
||||
State: "created",
|
||||
},
|
||||
},
|
||||
},
|
||||
path, err := createTempContainerIDMapping(sandbox.ID(), sandbox.ID())
|
||||
assert.NoError(err)
|
||||
defer os.RemoveAll(path)
|
||||
|
||||
testingImpl.StatusContainerFunc = func(sandboxID, containerID string) (vc.ContainerStatus, error) {
|
||||
return vc.ContainerStatus{
|
||||
ID: sandbox.ID(),
|
||||
Annotations: map[string]string{
|
||||
vcAnnotations.ContainerTypeKey: "InvalidType",
|
||||
vcAnnotations.ConfigJSONKey: configJSON,
|
||||
},
|
||||
State: vc.State{
|
||||
State: "created",
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
defer func() {
|
||||
testingImpl.ListSandboxFunc = nil
|
||||
testingImpl.StatusContainerFunc = nil
|
||||
}()
|
||||
|
||||
// Delete an invalid container type
|
||||
@ -285,28 +265,25 @@ func TestDeleteSandboxRunning(t *testing.T) {
|
||||
configJSON, err := readOCIConfigJSON(configPath)
|
||||
assert.NoError(err)
|
||||
|
||||
testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
|
||||
return []vc.SandboxStatus{
|
||||
{
|
||||
ID: sandbox.ID(),
|
||||
ContainersStatus: []vc.ContainerStatus{
|
||||
{
|
||||
ID: sandbox.ID(),
|
||||
Annotations: map[string]string{
|
||||
vcAnnotations.ContainerTypeKey: string(vc.PodSandbox),
|
||||
vcAnnotations.ConfigJSONKey: configJSON,
|
||||
},
|
||||
State: vc.State{
|
||||
State: "running",
|
||||
},
|
||||
},
|
||||
},
|
||||
path, err := createTempContainerIDMapping(sandbox.ID(), sandbox.ID())
|
||||
assert.NoError(err)
|
||||
defer os.RemoveAll(path)
|
||||
|
||||
testingImpl.StatusContainerFunc = func(sandboxID, containerID string) (vc.ContainerStatus, error) {
|
||||
return vc.ContainerStatus{
|
||||
ID: sandbox.ID(),
|
||||
Annotations: map[string]string{
|
||||
vcAnnotations.ContainerTypeKey: string(vc.PodSandbox),
|
||||
vcAnnotations.ConfigJSONKey: configJSON,
|
||||
},
|
||||
State: vc.State{
|
||||
State: "running",
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
defer func() {
|
||||
testingImpl.ListSandboxFunc = nil
|
||||
testingImpl.StatusContainerFunc = nil
|
||||
}()
|
||||
|
||||
// Delete on a running sandbox should fail
|
||||
@ -367,28 +344,25 @@ func TestDeleteRunningContainer(t *testing.T) {
|
||||
configJSON, err := readOCIConfigJSON(configPath)
|
||||
assert.NoError(err)
|
||||
|
||||
testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
|
||||
return []vc.SandboxStatus{
|
||||
{
|
||||
ID: sandbox.ID(),
|
||||
ContainersStatus: []vc.ContainerStatus{
|
||||
{
|
||||
ID: sandbox.MockContainers[0].ID(),
|
||||
Annotations: map[string]string{
|
||||
vcAnnotations.ContainerTypeKey: string(vc.PodContainer),
|
||||
vcAnnotations.ConfigJSONKey: configJSON,
|
||||
},
|
||||
State: vc.State{
|
||||
State: "running",
|
||||
},
|
||||
},
|
||||
},
|
||||
path, err := createTempContainerIDMapping(sandbox.MockContainers[0].ID(), sandbox.MockContainers[0].ID())
|
||||
assert.NoError(err)
|
||||
defer os.RemoveAll(path)
|
||||
|
||||
testingImpl.StatusContainerFunc = func(sandboxID, containerID string) (vc.ContainerStatus, error) {
|
||||
return vc.ContainerStatus{
|
||||
ID: sandbox.MockContainers[0].ID(),
|
||||
Annotations: map[string]string{
|
||||
vcAnnotations.ContainerTypeKey: string(vc.PodContainer),
|
||||
vcAnnotations.ConfigJSONKey: configJSON,
|
||||
},
|
||||
State: vc.State{
|
||||
State: "running",
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
defer func() {
|
||||
testingImpl.ListSandboxFunc = nil
|
||||
testingImpl.StatusContainerFunc = nil
|
||||
}()
|
||||
|
||||
// Delete on a running container should fail.
|
||||
@ -396,6 +370,10 @@ func TestDeleteRunningContainer(t *testing.T) {
|
||||
assert.Error(err)
|
||||
assert.False(vcmock.IsMockError(err))
|
||||
|
||||
path, err = createTempContainerIDMapping(sandbox.MockContainers[0].ID(), sandbox.MockContainers[0].ID())
|
||||
assert.NoError(err)
|
||||
defer os.RemoveAll(path)
|
||||
|
||||
// force delete
|
||||
err = delete(sandbox.MockContainers[0].ID(), true)
|
||||
assert.Error(err)
|
||||
@ -406,6 +384,10 @@ func TestDeleteRunningContainer(t *testing.T) {
|
||||
testingImpl.StopContainerFunc = nil
|
||||
}()
|
||||
|
||||
path, err = createTempContainerIDMapping(sandbox.MockContainers[0].ID(), sandbox.MockContainers[0].ID())
|
||||
assert.NoError(err)
|
||||
defer os.RemoveAll(path)
|
||||
|
||||
err = delete(sandbox.MockContainers[0].ID(), true)
|
||||
assert.Error(err)
|
||||
assert.True(vcmock.IsMockError(err))
|
||||
@ -418,6 +400,10 @@ func TestDeleteRunningContainer(t *testing.T) {
|
||||
testingImpl.DeleteContainerFunc = nil
|
||||
}()
|
||||
|
||||
path, err = createTempContainerIDMapping(sandbox.MockContainers[0].ID(), sandbox.MockContainers[0].ID())
|
||||
assert.NoError(err)
|
||||
defer os.RemoveAll(path)
|
||||
|
||||
err = delete(sandbox.MockContainers[0].ID(), true)
|
||||
assert.Nil(err)
|
||||
}
|
||||
@ -440,34 +426,35 @@ func TestDeleteContainer(t *testing.T) {
|
||||
configJSON, err := readOCIConfigJSON(configPath)
|
||||
assert.NoError(err)
|
||||
|
||||
testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
|
||||
return []vc.SandboxStatus{
|
||||
{
|
||||
ID: sandbox.ID(),
|
||||
ContainersStatus: []vc.ContainerStatus{
|
||||
{
|
||||
ID: sandbox.MockContainers[0].ID(),
|
||||
Annotations: map[string]string{
|
||||
vcAnnotations.ContainerTypeKey: string(vc.PodContainer),
|
||||
vcAnnotations.ConfigJSONKey: configJSON,
|
||||
},
|
||||
State: vc.State{
|
||||
State: "ready",
|
||||
},
|
||||
},
|
||||
},
|
||||
path, err := createTempContainerIDMapping(sandbox.MockContainers[0].ID(), sandbox.MockContainers[0].ID())
|
||||
assert.NoError(err)
|
||||
defer os.RemoveAll(path)
|
||||
|
||||
testingImpl.StatusContainerFunc = func(sandboxID, containerID string) (vc.ContainerStatus, error) {
|
||||
return vc.ContainerStatus{
|
||||
ID: sandbox.MockContainers[0].ID(),
|
||||
Annotations: map[string]string{
|
||||
vcAnnotations.ContainerTypeKey: string(vc.PodContainer),
|
||||
vcAnnotations.ConfigJSONKey: configJSON,
|
||||
},
|
||||
State: vc.State{
|
||||
State: "ready",
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
defer func() {
|
||||
testingImpl.ListSandboxFunc = nil
|
||||
testingImpl.StatusContainerFunc = nil
|
||||
}()
|
||||
|
||||
err = delete(sandbox.MockContainers[0].ID(), false)
|
||||
assert.Error(err)
|
||||
assert.True(vcmock.IsMockError(err))
|
||||
|
||||
path, err = createTempContainerIDMapping(sandbox.MockContainers[0].ID(), sandbox.MockContainers[0].ID())
|
||||
assert.NoError(err)
|
||||
defer os.RemoveAll(path)
|
||||
|
||||
testingImpl.StopContainerFunc = testStopContainerFuncReturnNil
|
||||
defer func() {
|
||||
testingImpl.StopContainerFunc = nil
|
||||
@ -485,6 +472,10 @@ func TestDeleteContainer(t *testing.T) {
|
||||
testingImpl.DeleteContainerFunc = nil
|
||||
}()
|
||||
|
||||
path, err = createTempContainerIDMapping(sandbox.MockContainers[0].ID(), sandbox.MockContainers[0].ID())
|
||||
assert.NoError(err)
|
||||
defer os.RemoveAll(path)
|
||||
|
||||
err = delete(sandbox.MockContainers[0].ID(), false)
|
||||
assert.Nil(err)
|
||||
}
|
||||
@ -505,6 +496,10 @@ func TestDeleteCLIFunction(t *testing.T) {
|
||||
assert.Error(err)
|
||||
assert.False(vcmock.IsMockError(err))
|
||||
|
||||
path, err := createTempContainerIDMapping("xyz", "xyz")
|
||||
assert.NoError(err)
|
||||
defer os.RemoveAll(path)
|
||||
|
||||
flagSet = flag.NewFlagSet("container-id", flag.ContinueOnError)
|
||||
flagSet.Parse([]string{"xyz"})
|
||||
ctx = cli.NewContext(app, flagSet, nil)
|
||||
@ -532,22 +527,19 @@ func TestDeleteCLIFunctionSuccess(t *testing.T) {
|
||||
configJSON, err := readOCIConfigJSON(configPath)
|
||||
assert.NoError(err)
|
||||
|
||||
testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
|
||||
return []vc.SandboxStatus{
|
||||
{
|
||||
ID: sandbox.ID(),
|
||||
ContainersStatus: []vc.ContainerStatus{
|
||||
{
|
||||
ID: sandbox.ID(),
|
||||
Annotations: map[string]string{
|
||||
vcAnnotations.ContainerTypeKey: string(vc.PodSandbox),
|
||||
vcAnnotations.ConfigJSONKey: configJSON,
|
||||
},
|
||||
State: vc.State{
|
||||
State: "ready",
|
||||
},
|
||||
},
|
||||
},
|
||||
path, err := createTempContainerIDMapping(sandbox.ID(), sandbox.ID())
|
||||
assert.NoError(err)
|
||||
defer os.RemoveAll(path)
|
||||
|
||||
testingImpl.StatusContainerFunc = func(sandboxID, containerID string) (vc.ContainerStatus, error) {
|
||||
return vc.ContainerStatus{
|
||||
ID: sandbox.ID(),
|
||||
Annotations: map[string]string{
|
||||
vcAnnotations.ContainerTypeKey: string(vc.PodSandbox),
|
||||
vcAnnotations.ConfigJSONKey: configJSON,
|
||||
},
|
||||
State: vc.State{
|
||||
State: "ready",
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
@ -570,7 +562,7 @@ func TestDeleteCLIFunctionSuccess(t *testing.T) {
|
||||
}
|
||||
|
||||
defer func() {
|
||||
testingImpl.ListSandboxFunc = nil
|
||||
testingImpl.StatusContainerFunc = nil
|
||||
testingImpl.StopSandboxFunc = nil
|
||||
testingImpl.DeleteSandboxFunc = nil
|
||||
}()
|
||||
|
@ -36,6 +36,10 @@ func TestExecCLIFunction(t *testing.T) {
|
||||
assert.Error(err)
|
||||
assert.False(vcmock.IsMockError(err))
|
||||
|
||||
path, err := createTempContainerIDMapping("xyz", "xyz")
|
||||
assert.NoError(err)
|
||||
defer os.RemoveAll(path)
|
||||
|
||||
// pass container-id
|
||||
flagSet = flag.NewFlagSet("container-id", flag.ContinueOnError)
|
||||
flagSet.Parse([]string{"xyz"})
|
||||
@ -57,7 +61,11 @@ func TestExecuteErrors(t *testing.T) {
|
||||
assert.Error(err)
|
||||
assert.False(vcmock.IsMockError(err))
|
||||
|
||||
// ListSandbox error
|
||||
path, err := createTempContainerIDMapping(testContainerID, testSandboxID)
|
||||
assert.NoError(err)
|
||||
defer os.RemoveAll(path)
|
||||
|
||||
// StatusSandbox error
|
||||
flagSet.Parse([]string{testContainerID})
|
||||
err = execute(ctx)
|
||||
assert.Error(err)
|
||||
@ -68,12 +76,12 @@ func TestExecuteErrors(t *testing.T) {
|
||||
vcAnnotations.ContainerTypeKey: string(vc.PodSandbox),
|
||||
}
|
||||
|
||||
testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
|
||||
return newSingleContainerSandboxStatusList(testSandboxID, testContainerID, vc.State{}, vc.State{}, annotations), nil
|
||||
testingImpl.StatusContainerFunc = func(sandboxID, containerID string) (vc.ContainerStatus, error) {
|
||||
return newSingleContainerStatus(testContainerID, vc.State{}, annotations), nil
|
||||
}
|
||||
|
||||
defer func() {
|
||||
testingImpl.ListSandboxFunc = nil
|
||||
testingImpl.StatusContainerFunc = nil
|
||||
}()
|
||||
|
||||
err = execute(ctx)
|
||||
@ -91,8 +99,8 @@ func TestExecuteErrors(t *testing.T) {
|
||||
}
|
||||
|
||||
containerState := vc.State{}
|
||||
testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
|
||||
return newSingleContainerSandboxStatusList(testSandboxID, testContainerID, vc.State{}, containerState, annotations), nil
|
||||
testingImpl.StatusContainerFunc = func(sandboxID, containerID string) (vc.ContainerStatus, error) {
|
||||
return newSingleContainerStatus(testContainerID, containerState, annotations), nil
|
||||
}
|
||||
|
||||
err = execute(ctx)
|
||||
@ -103,8 +111,8 @@ func TestExecuteErrors(t *testing.T) {
|
||||
containerState = vc.State{
|
||||
State: vc.StatePaused,
|
||||
}
|
||||
testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
|
||||
return newSingleContainerSandboxStatusList(testSandboxID, testContainerID, vc.State{}, containerState, annotations), nil
|
||||
testingImpl.StatusContainerFunc = func(sandboxID, containerID string) (vc.ContainerStatus, error) {
|
||||
return newSingleContainerStatus(testContainerID, containerState, annotations), nil
|
||||
}
|
||||
|
||||
err = execute(ctx)
|
||||
@ -115,8 +123,8 @@ func TestExecuteErrors(t *testing.T) {
|
||||
containerState = vc.State{
|
||||
State: vc.StateStopped,
|
||||
}
|
||||
testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
|
||||
return newSingleContainerSandboxStatusList(testSandboxID, testContainerID, vc.State{}, containerState, annotations), nil
|
||||
testingImpl.StatusContainerFunc = func(sandboxID, containerID string) (vc.ContainerStatus, error) {
|
||||
return newSingleContainerStatus(testContainerID, containerState, annotations), nil
|
||||
}
|
||||
|
||||
err = execute(ctx)
|
||||
@ -152,12 +160,16 @@ func TestExecuteErrorReadingProcessJson(t *testing.T) {
|
||||
State: vc.StateRunning,
|
||||
}
|
||||
|
||||
testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
|
||||
return newSingleContainerSandboxStatusList(testSandboxID, testContainerID, state, state, annotations), nil
|
||||
path, err := createTempContainerIDMapping(testContainerID, testSandboxID)
|
||||
assert.NoError(err)
|
||||
defer os.RemoveAll(path)
|
||||
|
||||
testingImpl.StatusContainerFunc = func(sandboxID, containerID string) (vc.ContainerStatus, error) {
|
||||
return newSingleContainerStatus(testContainerID, state, annotations), nil
|
||||
}
|
||||
|
||||
defer func() {
|
||||
testingImpl.ListSandboxFunc = nil
|
||||
testingImpl.StatusContainerFunc = nil
|
||||
}()
|
||||
|
||||
// Note: flags can only be tested with the CLI command function
|
||||
@ -196,12 +208,16 @@ func TestExecuteErrorOpeningConsole(t *testing.T) {
|
||||
State: vc.StateRunning,
|
||||
}
|
||||
|
||||
testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
|
||||
return newSingleContainerSandboxStatusList(testSandboxID, testContainerID, state, state, annotations), nil
|
||||
path, err := createTempContainerIDMapping(testContainerID, testSandboxID)
|
||||
assert.NoError(err)
|
||||
defer os.RemoveAll(path)
|
||||
|
||||
testingImpl.StatusContainerFunc = func(sandboxID, containerID string) (vc.ContainerStatus, error) {
|
||||
return newSingleContainerStatus(testContainerID, state, annotations), nil
|
||||
}
|
||||
|
||||
defer func() {
|
||||
testingImpl.ListSandboxFunc = nil
|
||||
testingImpl.StatusContainerFunc = nil
|
||||
}()
|
||||
|
||||
// Note: flags can only be tested with the CLI command function
|
||||
@ -258,12 +274,16 @@ func TestExecuteWithFlags(t *testing.T) {
|
||||
State: vc.StateRunning,
|
||||
}
|
||||
|
||||
testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
|
||||
return newSingleContainerSandboxStatusList(testSandboxID, testContainerID, state, state, annotations), nil
|
||||
path, err := createTempContainerIDMapping(testContainerID, testSandboxID)
|
||||
assert.NoError(err)
|
||||
defer os.RemoveAll(path)
|
||||
|
||||
testingImpl.StatusContainerFunc = func(sandboxID, containerID string) (vc.ContainerStatus, error) {
|
||||
return newSingleContainerStatus(testContainerID, state, annotations), nil
|
||||
}
|
||||
|
||||
defer func() {
|
||||
testingImpl.ListSandboxFunc = nil
|
||||
testingImpl.StatusContainerFunc = nil
|
||||
}()
|
||||
|
||||
fn, ok := execCLICommand.Action.(func(context *cli.Context) error)
|
||||
@ -342,12 +362,16 @@ func TestExecuteWithFlagsDetached(t *testing.T) {
|
||||
State: vc.StateRunning,
|
||||
}
|
||||
|
||||
testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
|
||||
return newSingleContainerSandboxStatusList(testSandboxID, testContainerID, state, state, annotations), nil
|
||||
path, err := createTempContainerIDMapping(testContainerID, testSandboxID)
|
||||
assert.NoError(err)
|
||||
defer os.RemoveAll(path)
|
||||
|
||||
testingImpl.StatusContainerFunc = func(sandboxID, containerID string) (vc.ContainerStatus, error) {
|
||||
return newSingleContainerStatus(testContainerID, state, annotations), nil
|
||||
}
|
||||
|
||||
defer func() {
|
||||
testingImpl.ListSandboxFunc = nil
|
||||
testingImpl.StatusContainerFunc = nil
|
||||
}()
|
||||
|
||||
testingImpl.EnterContainerFunc = func(sandboxID, containerID string, cmd vc.Cmd) (vc.VCSandbox, vc.VCContainer, *vc.Process, error) {
|
||||
@ -416,12 +440,16 @@ func TestExecuteWithInvalidProcessJson(t *testing.T) {
|
||||
State: vc.StateRunning,
|
||||
}
|
||||
|
||||
testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
|
||||
return newSingleContainerSandboxStatusList(testSandboxID, testContainerID, state, state, annotations), nil
|
||||
path, err := createTempContainerIDMapping(testContainerID, testSandboxID)
|
||||
assert.NoError(err)
|
||||
defer os.RemoveAll(path)
|
||||
|
||||
testingImpl.StatusContainerFunc = func(sandboxID, containerID string) (vc.ContainerStatus, error) {
|
||||
return newSingleContainerStatus(testContainerID, state, annotations), nil
|
||||
}
|
||||
|
||||
defer func() {
|
||||
testingImpl.ListSandboxFunc = nil
|
||||
testingImpl.StatusContainerFunc = nil
|
||||
}()
|
||||
|
||||
fn, ok := execCLICommand.Action.(func(context *cli.Context) error)
|
||||
@ -463,12 +491,16 @@ func TestExecuteWithValidProcessJson(t *testing.T) {
|
||||
State: vc.StateRunning,
|
||||
}
|
||||
|
||||
testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
|
||||
return newSingleContainerSandboxStatusList(testSandboxID, testContainerID, state, state, annotations), nil
|
||||
path, err := createTempContainerIDMapping(testContainerID, testSandboxID)
|
||||
assert.NoError(err)
|
||||
defer os.RemoveAll(path)
|
||||
|
||||
testingImpl.StatusContainerFunc = func(sandboxID, containerID string) (vc.ContainerStatus, error) {
|
||||
return newSingleContainerStatus(testContainerID, state, annotations), nil
|
||||
}
|
||||
|
||||
defer func() {
|
||||
testingImpl.ListSandboxFunc = nil
|
||||
testingImpl.StatusContainerFunc = nil
|
||||
}()
|
||||
|
||||
processJSON := `{
|
||||
@ -555,12 +587,16 @@ func TestExecuteWithInvalidEnvironment(t *testing.T) {
|
||||
State: vc.StateRunning,
|
||||
}
|
||||
|
||||
testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
|
||||
return newSingleContainerSandboxStatusList(testSandboxID, testContainerID, state, state, annotations), nil
|
||||
path, err := createTempContainerIDMapping(testContainerID, testSandboxID)
|
||||
assert.NoError(err)
|
||||
defer os.RemoveAll(path)
|
||||
|
||||
testingImpl.StatusContainerFunc = func(sandboxID, containerID string) (vc.ContainerStatus, error) {
|
||||
return newSingleContainerStatus(testContainerID, state, annotations), nil
|
||||
}
|
||||
|
||||
defer func() {
|
||||
testingImpl.ListSandboxFunc = nil
|
||||
testingImpl.StatusContainerFunc = nil
|
||||
}()
|
||||
|
||||
processJSON := `{
|
||||
|
128
cli/kill_test.go
128
cli/kill_test.go
@ -8,6 +8,7 @@ package main
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"syscall"
|
||||
"testing"
|
||||
|
||||
@ -72,13 +73,19 @@ func testKillCLIFunctionTerminationSignalSuccessful(t *testing.T, sig string) {
|
||||
|
||||
testingImpl.KillContainerFunc = testKillContainerFuncReturnNil
|
||||
testingImpl.StopContainerFunc = testStopContainerFuncReturnNil
|
||||
testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
|
||||
return newSingleContainerSandboxStatusList(testSandboxID, testContainerID, state, state, annotations), nil
|
||||
|
||||
path, err := createTempContainerIDMapping(testContainerID, testSandboxID)
|
||||
assert.NoError(err)
|
||||
defer os.RemoveAll(path)
|
||||
|
||||
testingImpl.StatusContainerFunc = func(sandboxID, containerID string) (vc.ContainerStatus, error) {
|
||||
return newSingleContainerStatus(testContainerID, state, annotations), nil
|
||||
}
|
||||
|
||||
defer func() {
|
||||
testingImpl.KillContainerFunc = nil
|
||||
testingImpl.StopContainerFunc = nil
|
||||
testingImpl.ListSandboxFunc = nil
|
||||
testingImpl.StatusContainerFunc = nil
|
||||
}()
|
||||
|
||||
set := flag.NewFlagSet("", 0)
|
||||
@ -90,11 +97,12 @@ func testKillCLIFunctionTerminationSignalSuccessful(t *testing.T, sig string) {
|
||||
vcAnnotations.ContainerTypeKey: string(vc.PodSandbox),
|
||||
}
|
||||
|
||||
testingImpl.StatusContainerFunc = func(sandboxID, containerID string) (vc.ContainerStatus, error) {
|
||||
return newSingleContainerStatus(testContainerID, state, annotations), nil
|
||||
}
|
||||
|
||||
testingImpl.StopContainerFunc = nil
|
||||
testingImpl.StopSandboxFunc = testStopSandboxFuncReturnNil
|
||||
testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
|
||||
return newSingleContainerSandboxStatusList(testSandboxID, testContainerID, state, state, annotations), nil
|
||||
}
|
||||
defer func() {
|
||||
testingImpl.StopSandboxFunc = nil
|
||||
}()
|
||||
@ -118,12 +126,18 @@ func TestKillCLIFunctionNotTerminationSignalSuccessful(t *testing.T) {
|
||||
}
|
||||
|
||||
testingImpl.KillContainerFunc = testKillContainerFuncReturnNil
|
||||
testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
|
||||
return newSingleContainerSandboxStatusList(testSandboxID, testContainerID, state, state, map[string]string{}), nil
|
||||
|
||||
path, err := createTempContainerIDMapping(testContainerID, testSandboxID)
|
||||
assert.NoError(err)
|
||||
defer os.RemoveAll(path)
|
||||
|
||||
testingImpl.StatusContainerFunc = func(sandboxID, containerID string) (vc.ContainerStatus, error) {
|
||||
return newSingleContainerStatus(testContainerID, state, map[string]string{}), nil
|
||||
}
|
||||
|
||||
defer func() {
|
||||
testingImpl.KillContainerFunc = nil
|
||||
testingImpl.ListSandboxFunc = nil
|
||||
testingImpl.StatusContainerFunc = nil
|
||||
}()
|
||||
|
||||
set := flag.NewFlagSet("", 0)
|
||||
@ -145,13 +159,19 @@ func TestKillCLIFunctionNoSignalSuccessful(t *testing.T) {
|
||||
|
||||
testingImpl.KillContainerFunc = testKillContainerFuncReturnNil
|
||||
testingImpl.StopContainerFunc = testStopContainerFuncReturnNil
|
||||
testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
|
||||
return newSingleContainerSandboxStatusList(testSandboxID, testContainerID, state, state, annotations), nil
|
||||
|
||||
path, err := createTempContainerIDMapping(testContainerID, testSandboxID)
|
||||
assert.NoError(err)
|
||||
defer os.RemoveAll(path)
|
||||
|
||||
testingImpl.StatusContainerFunc = func(sandboxID, containerID string) (vc.ContainerStatus, error) {
|
||||
return newSingleContainerStatus(testContainerID, state, annotations), nil
|
||||
}
|
||||
|
||||
defer func() {
|
||||
testingImpl.KillContainerFunc = nil
|
||||
testingImpl.StopContainerFunc = nil
|
||||
testingImpl.ListSandboxFunc = nil
|
||||
testingImpl.StatusContainerFunc = nil
|
||||
}()
|
||||
|
||||
set := flag.NewFlagSet("", 0)
|
||||
@ -163,11 +183,12 @@ func TestKillCLIFunctionNoSignalSuccessful(t *testing.T) {
|
||||
vcAnnotations.ContainerTypeKey: string(vc.PodSandbox),
|
||||
}
|
||||
|
||||
testingImpl.StatusContainerFunc = func(sandboxID, containerID string) (vc.ContainerStatus, error) {
|
||||
return newSingleContainerStatus(testContainerID, state, annotations), nil
|
||||
}
|
||||
|
||||
testingImpl.StopContainerFunc = nil
|
||||
testingImpl.StopSandboxFunc = testStopSandboxFuncReturnNil
|
||||
testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
|
||||
return newSingleContainerSandboxStatusList(testSandboxID, testContainerID, state, state, annotations), nil
|
||||
}
|
||||
defer func() {
|
||||
testingImpl.StopSandboxFunc = nil
|
||||
}()
|
||||
@ -194,13 +215,19 @@ func TestKillCLIFunctionEnableAllSuccessful(t *testing.T) {
|
||||
return nil
|
||||
}
|
||||
testingImpl.StopContainerFunc = testStopContainerFuncReturnNil
|
||||
testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
|
||||
return newSingleContainerSandboxStatusList(testSandboxID, testContainerID, state, state, annotations), nil
|
||||
|
||||
path, err := createTempContainerIDMapping(testContainerID, testSandboxID)
|
||||
assert.NoError(err)
|
||||
defer os.RemoveAll(path)
|
||||
|
||||
testingImpl.StatusContainerFunc = func(sandboxID, containerID string) (vc.ContainerStatus, error) {
|
||||
return newSingleContainerStatus(testContainerID, state, annotations), nil
|
||||
}
|
||||
|
||||
defer func() {
|
||||
testingImpl.KillContainerFunc = nil
|
||||
testingImpl.StopContainerFunc = nil
|
||||
testingImpl.ListSandboxFunc = nil
|
||||
testingImpl.StatusContainerFunc = nil
|
||||
}()
|
||||
|
||||
set := flag.NewFlagSet("", 0)
|
||||
@ -213,11 +240,12 @@ func TestKillCLIFunctionEnableAllSuccessful(t *testing.T) {
|
||||
vcAnnotations.ContainerTypeKey: string(vc.PodSandbox),
|
||||
}
|
||||
|
||||
testingImpl.StatusContainerFunc = func(sandboxID, containerID string) (vc.ContainerStatus, error) {
|
||||
return newSingleContainerStatus(testContainerID, state, annotations), nil
|
||||
}
|
||||
|
||||
testingImpl.StopContainerFunc = nil
|
||||
testingImpl.StopSandboxFunc = testStopSandboxFuncReturnNil
|
||||
testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
|
||||
return newSingleContainerSandboxStatusList(testSandboxID, testContainerID, state, state, annotations), nil
|
||||
}
|
||||
defer func() {
|
||||
testingImpl.StopSandboxFunc = nil
|
||||
}()
|
||||
@ -237,12 +265,17 @@ func TestKillCLIFunctionContainerNotExistFailure(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
testingImpl.KillContainerFunc = testKillContainerFuncReturnNil
|
||||
testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
|
||||
return []vc.SandboxStatus{}, nil
|
||||
|
||||
path, err := createTempContainerIDMapping(testContainerID, testSandboxID)
|
||||
assert.NoError(err)
|
||||
defer os.RemoveAll(path)
|
||||
|
||||
testingImpl.StatusContainerFunc = func(sandboxID, containerID string) (vc.ContainerStatus, error) {
|
||||
return vc.ContainerStatus{}, nil
|
||||
}
|
||||
|
||||
defer func() {
|
||||
testingImpl.KillContainerFunc = nil
|
||||
testingImpl.ListSandboxFunc = nil
|
||||
}()
|
||||
|
||||
set := flag.NewFlagSet("", 0)
|
||||
@ -259,12 +292,18 @@ func TestKillCLIFunctionInvalidSignalFailure(t *testing.T) {
|
||||
}
|
||||
|
||||
testingImpl.KillContainerFunc = testKillContainerFuncReturnNil
|
||||
testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
|
||||
return newSingleContainerSandboxStatusList(testSandboxID, testContainerID, state, state, map[string]string{}), nil
|
||||
|
||||
path, err := createTempContainerIDMapping(testContainerID, testSandboxID)
|
||||
assert.NoError(err)
|
||||
defer os.RemoveAll(path)
|
||||
|
||||
testingImpl.StatusContainerFunc = func(sandboxID, containerID string) (vc.ContainerStatus, error) {
|
||||
return newSingleContainerStatus(testContainerID, state, map[string]string{}), nil
|
||||
}
|
||||
|
||||
defer func() {
|
||||
testingImpl.KillContainerFunc = nil
|
||||
testingImpl.ListSandboxFunc = nil
|
||||
testingImpl.StatusContainerFunc = nil
|
||||
}()
|
||||
|
||||
set := flag.NewFlagSet("", 0)
|
||||
@ -281,12 +320,18 @@ func TestKillCLIFunctionInvalidStatePausedFailure(t *testing.T) {
|
||||
}
|
||||
|
||||
testingImpl.KillContainerFunc = testKillContainerFuncReturnNil
|
||||
testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
|
||||
return newSingleContainerSandboxStatusList(testSandboxID, testContainerID, state, state, map[string]string{}), nil
|
||||
|
||||
path, err := createTempContainerIDMapping(testContainerID, testSandboxID)
|
||||
assert.NoError(err)
|
||||
defer os.RemoveAll(path)
|
||||
|
||||
testingImpl.StatusContainerFunc = func(sandboxID, containerID string) (vc.ContainerStatus, error) {
|
||||
return newSingleContainerStatus(testContainerID, state, map[string]string{}), nil
|
||||
}
|
||||
|
||||
defer func() {
|
||||
testingImpl.KillContainerFunc = nil
|
||||
testingImpl.ListSandboxFunc = nil
|
||||
testingImpl.StatusContainerFunc = nil
|
||||
}()
|
||||
|
||||
set := flag.NewFlagSet("", 0)
|
||||
@ -303,12 +348,18 @@ func TestKillCLIFunctionInvalidStateStoppedFailure(t *testing.T) {
|
||||
}
|
||||
|
||||
testingImpl.KillContainerFunc = testKillContainerFuncReturnNil
|
||||
testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
|
||||
return newSingleContainerSandboxStatusList(testSandboxID, testContainerID, state, state, map[string]string{}), nil
|
||||
|
||||
path, err := createTempContainerIDMapping(testContainerID, testSandboxID)
|
||||
assert.NoError(err)
|
||||
defer os.RemoveAll(path)
|
||||
|
||||
testingImpl.StatusContainerFunc = func(sandboxID, containerID string) (vc.ContainerStatus, error) {
|
||||
return newSingleContainerStatus(testContainerID, state, map[string]string{}), nil
|
||||
}
|
||||
|
||||
defer func() {
|
||||
testingImpl.KillContainerFunc = nil
|
||||
testingImpl.ListSandboxFunc = nil
|
||||
testingImpl.StatusContainerFunc = nil
|
||||
}()
|
||||
|
||||
set := flag.NewFlagSet("", 0)
|
||||
@ -324,11 +375,16 @@ func TestKillCLIFunctionKillContainerFailure(t *testing.T) {
|
||||
State: vc.StateRunning,
|
||||
}
|
||||
|
||||
testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
|
||||
return newSingleContainerSandboxStatusList(testSandboxID, testContainerID, state, state, map[string]string{}), nil
|
||||
path, err := createTempContainerIDMapping(testContainerID, testSandboxID)
|
||||
assert.NoError(err)
|
||||
defer os.RemoveAll(path)
|
||||
|
||||
testingImpl.StatusContainerFunc = func(sandboxID, containerID string) (vc.ContainerStatus, error) {
|
||||
return newSingleContainerStatus(testContainerID, state, map[string]string{}), nil
|
||||
}
|
||||
|
||||
defer func() {
|
||||
testingImpl.ListSandboxFunc = nil
|
||||
testingImpl.StatusContainerFunc = nil
|
||||
}()
|
||||
|
||||
set := flag.NewFlagSet("", 0)
|
||||
|
@ -438,19 +438,11 @@ func writeOCIConfigFile(spec oci.CompatOCISpec, configPath string) error {
|
||||
return ioutil.WriteFile(configPath, bytes, testFileMode)
|
||||
}
|
||||
|
||||
func newSingleContainerSandboxStatusList(sandboxID, containerID string, sandboxState, containerState vc.State, annotations map[string]string) []vc.SandboxStatus {
|
||||
return []vc.SandboxStatus{
|
||||
{
|
||||
ID: sandboxID,
|
||||
State: sandboxState,
|
||||
ContainersStatus: []vc.ContainerStatus{
|
||||
{
|
||||
ID: containerID,
|
||||
State: containerState,
|
||||
Annotations: annotations,
|
||||
},
|
||||
},
|
||||
},
|
||||
func newSingleContainerStatus(containerID string, containerState vc.State, annotations map[string]string) vc.ContainerStatus {
|
||||
return vc.ContainerStatus{
|
||||
ID: containerID,
|
||||
State: containerState,
|
||||
Annotations: annotations,
|
||||
}
|
||||
}
|
||||
|
||||
@ -1102,3 +1094,18 @@ func TestMainResetCLIGlobals(t *testing.T) {
|
||||
assert.NotNil(cli.VersionPrinter)
|
||||
assert.NotNil(savedCLIVersionPrinter)
|
||||
}
|
||||
|
||||
func createTempContainerIDMapping(containerID, sandboxID string) (string, error) {
|
||||
tmpDir, err := ioutil.TempDir("", "containers-mapping")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
ctrsMapTreePath = tmpDir
|
||||
|
||||
path := filepath.Join(ctrsMapTreePath, containerID, sandboxID)
|
||||
if err := os.MkdirAll(path, 0750); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return tmpDir, nil
|
||||
}
|
||||
|
99
cli/oci.go
99
cli/oci.go
@ -9,6 +9,7 @@ import (
|
||||
"bufio"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@ -24,10 +25,11 @@ import (
|
||||
|
||||
// Contants related to cgroup memory directory
|
||||
const (
|
||||
cgroupsTasksFile = "tasks"
|
||||
cgroupsProcsFile = "cgroup.procs"
|
||||
cgroupsDirMode = os.FileMode(0750)
|
||||
cgroupsFileMode = os.FileMode(0640)
|
||||
cgroupsTasksFile = "tasks"
|
||||
cgroupsProcsFile = "cgroup.procs"
|
||||
cgroupsDirMode = os.FileMode(0750)
|
||||
cgroupsFileMode = os.FileMode(0640)
|
||||
ctrsMappingDirMode = os.FileMode(0750)
|
||||
|
||||
// Filesystem type corresponding to CGROUP_SUPER_MAGIC as listed
|
||||
// here: http://man7.org/linux/man-pages/man2/statfs.2.html
|
||||
@ -40,6 +42,8 @@ var cgroupsDirPath string
|
||||
|
||||
var procMountInfo = "/proc/self/mountinfo"
|
||||
|
||||
var ctrsMapTreePath = "/var/run/kata-containers/containers-mapping"
|
||||
|
||||
// getContainerInfo returns the container status and its sandbox ID.
|
||||
func getContainerInfo(containerID string) (vc.ContainerStatus, string, error) {
|
||||
// container ID MUST be provided.
|
||||
@ -47,23 +51,23 @@ func getContainerInfo(containerID string) (vc.ContainerStatus, string, error) {
|
||||
return vc.ContainerStatus{}, "", fmt.Errorf("Missing container ID")
|
||||
}
|
||||
|
||||
sandboxStatusList, err := vci.ListSandbox()
|
||||
sandboxID, err := fetchContainerIDMapping(containerID)
|
||||
if err != nil {
|
||||
return vc.ContainerStatus{}, "", err
|
||||
}
|
||||
if sandboxID == "" {
|
||||
// Not finding a container should not trigger an error as
|
||||
// getContainerInfo is used for checking the existence and
|
||||
// the absence of a container ID.
|
||||
return vc.ContainerStatus{}, "", nil
|
||||
}
|
||||
|
||||
ctrStatus, err := vci.StatusContainer(sandboxID, containerID)
|
||||
if err != nil {
|
||||
return vc.ContainerStatus{}, "", err
|
||||
}
|
||||
|
||||
for _, sandboxStatus := range sandboxStatusList {
|
||||
for _, containerStatus := range sandboxStatus.ContainersStatus {
|
||||
if containerStatus.ID == containerID {
|
||||
return containerStatus, sandboxStatus.ID, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Not finding a container should not trigger an error as
|
||||
// getContainerInfo is used for checking the existence and
|
||||
// the absence of a container ID.
|
||||
return vc.ContainerStatus{}, "", nil
|
||||
return ctrStatus, sandboxID, nil
|
||||
}
|
||||
|
||||
func getExistingContainerInfo(containerID string) (vc.ContainerStatus, string, error) {
|
||||
@ -339,3 +343,64 @@ func getCgroupsDirPath(mountInfoFile string) (string, error) {
|
||||
|
||||
return cgroupRootPath, nil
|
||||
}
|
||||
|
||||
// This function assumes it should find only one file inside the container
|
||||
// ID directory. If there are several files, we could not determine which
|
||||
// file name corresponds to the sandbox ID associated, and this would throw
|
||||
// an error.
|
||||
func fetchContainerIDMapping(containerID string) (string, error) {
|
||||
if containerID == "" {
|
||||
return "", fmt.Errorf("Missing container ID")
|
||||
}
|
||||
|
||||
dirPath := filepath.Join(ctrsMapTreePath, containerID)
|
||||
|
||||
files, err := ioutil.ReadDir(dirPath)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
return "", err
|
||||
}
|
||||
|
||||
if len(files) != 1 {
|
||||
return "", fmt.Errorf("Too many files (%d) in %q", len(files), dirPath)
|
||||
}
|
||||
|
||||
return files[0].Name(), nil
|
||||
}
|
||||
|
||||
func addContainerIDMapping(containerID, sandboxID string) error {
|
||||
if containerID == "" {
|
||||
return fmt.Errorf("Missing container ID")
|
||||
}
|
||||
|
||||
if sandboxID == "" {
|
||||
return fmt.Errorf("Missing sandbox ID")
|
||||
}
|
||||
|
||||
parentPath := filepath.Join(ctrsMapTreePath, containerID)
|
||||
|
||||
if err := os.RemoveAll(parentPath); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
path := filepath.Join(parentPath, sandboxID)
|
||||
|
||||
if err := os.MkdirAll(path, ctrsMappingDirMode); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func delContainerIDMapping(containerID string) error {
|
||||
if containerID == "" {
|
||||
return fmt.Errorf("Missing container ID")
|
||||
}
|
||||
|
||||
path := filepath.Join(ctrsMapTreePath, containerID)
|
||||
|
||||
return os.RemoveAll(path)
|
||||
}
|
||||
|
247
cli/oci_test.go
247
cli/oci_test.go
@ -87,17 +87,16 @@ func TestGetContainerInfo(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
|
||||
return []vc.SandboxStatus{
|
||||
{
|
||||
ID: sandbox.ID(),
|
||||
ContainersStatus: []vc.ContainerStatus{containerStatus},
|
||||
},
|
||||
}, nil
|
||||
path, err := createTempContainerIDMapping(containerID, testSandboxID)
|
||||
assert.NoError(err)
|
||||
defer os.RemoveAll(path)
|
||||
|
||||
testingImpl.StatusContainerFunc = func(sandboxID, containerID string) (vc.ContainerStatus, error) {
|
||||
return containerStatus, nil
|
||||
}
|
||||
|
||||
defer func() {
|
||||
testingImpl.ListSandboxFunc = nil
|
||||
testingImpl.StatusContainerFunc = nil
|
||||
}()
|
||||
|
||||
status, sandboxID, err := getContainerInfo(testContainerID)
|
||||
@ -106,40 +105,6 @@ func TestGetContainerInfo(t *testing.T) {
|
||||
assert.Equal(status, containerStatus)
|
||||
}
|
||||
|
||||
func TestGetContainerInfoMismatch(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
sandbox := &vcmock.Sandbox{
|
||||
MockID: testSandboxID,
|
||||
}
|
||||
|
||||
containerID := testContainerID + testContainerID
|
||||
|
||||
containerStatus := vc.ContainerStatus{
|
||||
ID: containerID,
|
||||
Annotations: map[string]string{
|
||||
vcAnnotations.ContainerTypeKey: string(vc.PodSandbox),
|
||||
},
|
||||
}
|
||||
|
||||
testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
|
||||
return []vc.SandboxStatus{
|
||||
{
|
||||
ID: sandbox.ID(),
|
||||
ContainersStatus: []vc.ContainerStatus{containerStatus},
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
defer func() {
|
||||
testingImpl.ListSandboxFunc = nil
|
||||
}()
|
||||
|
||||
_, sandboxID, err := getContainerInfo(testContainerID)
|
||||
assert.NoError(err)
|
||||
assert.Equal(sandboxID, "")
|
||||
}
|
||||
|
||||
func TestValidCreateParamsContainerIDEmptyFailure(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
_, err := validCreateParams("", "")
|
||||
@ -159,75 +124,16 @@ func TestGetExistingContainerInfoContainerIDEmptyFailure(t *testing.T) {
|
||||
func TestValidCreateParamsContainerIDNotUnique(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
containerID := testContainerID + testContainerID
|
||||
testSandboxID2 := testSandboxID + "2"
|
||||
|
||||
sandbox := &vcmock.Sandbox{
|
||||
MockID: testSandboxID,
|
||||
}
|
||||
path, err := createTempContainerIDMapping(testContainerID, testSandboxID)
|
||||
assert.NoError(err)
|
||||
defer os.RemoveAll(path)
|
||||
err = os.MkdirAll(filepath.Join(ctrsMapTreePath, testContainerID, testSandboxID2), 0750)
|
||||
assert.NoError(err)
|
||||
|
||||
testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
|
||||
return []vc.SandboxStatus{
|
||||
{
|
||||
ID: sandbox.ID(),
|
||||
ContainersStatus: []vc.ContainerStatus{
|
||||
// 2 containers with same ID
|
||||
{
|
||||
ID: containerID,
|
||||
Annotations: map[string]string{
|
||||
vcAnnotations.ContainerTypeKey: string(vc.PodSandbox),
|
||||
},
|
||||
},
|
||||
{
|
||||
ID: containerID,
|
||||
Annotations: map[string]string{
|
||||
vcAnnotations.ContainerTypeKey: string(vc.PodSandbox),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
_, err = validCreateParams(testContainerID, "")
|
||||
|
||||
defer func() {
|
||||
testingImpl.ListSandboxFunc = nil
|
||||
}()
|
||||
|
||||
_, err := validCreateParams(testContainerID, "")
|
||||
|
||||
assert.Error(err)
|
||||
assert.False(vcmock.IsMockError(err))
|
||||
}
|
||||
|
||||
func TestValidCreateParamsContainerIDNotUnique2(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
containerID := testContainerID + testContainerID
|
||||
|
||||
sandbox := &vcmock.Sandbox{
|
||||
MockID: testSandboxID,
|
||||
}
|
||||
|
||||
testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
|
||||
return []vc.SandboxStatus{
|
||||
{
|
||||
ID: sandbox.ID(),
|
||||
ContainersStatus: []vc.ContainerStatus{
|
||||
{
|
||||
ID: containerID,
|
||||
Annotations: map[string]string{
|
||||
vcAnnotations.ContainerTypeKey: string(vc.PodSandbox),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
defer func() {
|
||||
testingImpl.ListSandboxFunc = nil
|
||||
}()
|
||||
|
||||
_, err := validCreateParams(testContainerID, "")
|
||||
assert.Error(err)
|
||||
assert.False(vcmock.IsMockError(err))
|
||||
}
|
||||
@ -241,13 +147,10 @@ func TestValidCreateParamsInvalidBundle(t *testing.T) {
|
||||
|
||||
bundlePath := filepath.Join(tmpdir, "bundle")
|
||||
|
||||
testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
|
||||
return []vc.SandboxStatus{}, nil
|
||||
}
|
||||
|
||||
defer func() {
|
||||
testingImpl.ListSandboxFunc = nil
|
||||
}()
|
||||
path, err := ioutil.TempDir("", "containers-mapping")
|
||||
assert.NoError(err)
|
||||
defer os.RemoveAll(path)
|
||||
ctrsMapTreePath = path
|
||||
|
||||
_, err = validCreateParams(testContainerID, bundlePath)
|
||||
// bundle is ENOENT
|
||||
@ -266,13 +169,10 @@ func TestValidCreateParamsBundleIsAFile(t *testing.T) {
|
||||
err = createEmptyFile(bundlePath)
|
||||
assert.NoError(err)
|
||||
|
||||
testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
|
||||
return []vc.SandboxStatus{}, nil
|
||||
}
|
||||
|
||||
defer func() {
|
||||
testingImpl.ListSandboxFunc = nil
|
||||
}()
|
||||
path, err := ioutil.TempDir("", "containers-mapping")
|
||||
assert.NoError(err)
|
||||
defer os.RemoveAll(path)
|
||||
ctrsMapTreePath = path
|
||||
|
||||
_, err = validCreateParams(testContainerID, bundlePath)
|
||||
// bundle exists as a file, not a directory
|
||||
@ -612,3 +512,106 @@ func TestGetCgroupsDirPath(t *testing.T) {
|
||||
assert.Equal(d.expectedResult, path)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFetchContainerIDMappingContainerIDEmptyFailure(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
sandboxID, err := fetchContainerIDMapping("")
|
||||
assert.Error(err)
|
||||
assert.Empty(sandboxID)
|
||||
}
|
||||
|
||||
func TestFetchContainerIDMappingEmptyMappingSuccess(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
path, err := ioutil.TempDir("", "containers-mapping")
|
||||
assert.NoError(err)
|
||||
defer os.RemoveAll(path)
|
||||
ctrsMapTreePath = path
|
||||
|
||||
sandboxID, err := fetchContainerIDMapping(testContainerID)
|
||||
assert.NoError(err)
|
||||
assert.Empty(sandboxID)
|
||||
}
|
||||
|
||||
func TestFetchContainerIDMappingTooManyFilesFailure(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
path, err := createTempContainerIDMapping(testContainerID, testSandboxID)
|
||||
assert.NoError(err)
|
||||
defer os.RemoveAll(path)
|
||||
err = os.MkdirAll(filepath.Join(ctrsMapTreePath, testContainerID, testSandboxID+"2"), ctrsMappingDirMode)
|
||||
assert.NoError(err)
|
||||
|
||||
sandboxID, err := fetchContainerIDMapping(testContainerID)
|
||||
assert.Error(err)
|
||||
assert.Empty(sandboxID)
|
||||
}
|
||||
|
||||
func TestFetchContainerIDMappingSuccess(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
path, err := createTempContainerIDMapping(testContainerID, testSandboxID)
|
||||
assert.NoError(err)
|
||||
defer os.RemoveAll(path)
|
||||
|
||||
sandboxID, err := fetchContainerIDMapping(testContainerID)
|
||||
assert.NoError(err)
|
||||
assert.Equal(sandboxID, testSandboxID)
|
||||
}
|
||||
|
||||
func TestAddContainerIDMappingContainerIDEmptyFailure(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
err := addContainerIDMapping("", testSandboxID)
|
||||
assert.Error(err)
|
||||
}
|
||||
|
||||
func TestAddContainerIDMappingSandboxIDEmptyFailure(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
err := addContainerIDMapping(testContainerID, "")
|
||||
assert.Error(err)
|
||||
}
|
||||
|
||||
func TestAddContainerIDMappingSuccess(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
path, err := ioutil.TempDir("", "containers-mapping")
|
||||
assert.NoError(err)
|
||||
defer os.RemoveAll(path)
|
||||
ctrsMapTreePath = path
|
||||
|
||||
_, err = os.Stat(filepath.Join(ctrsMapTreePath, testContainerID, testSandboxID))
|
||||
assert.True(os.IsNotExist(err))
|
||||
|
||||
err = addContainerIDMapping(testContainerID, testSandboxID)
|
||||
assert.NoError(err)
|
||||
|
||||
_, err = os.Stat(filepath.Join(ctrsMapTreePath, testContainerID, testSandboxID))
|
||||
assert.NoError(err)
|
||||
}
|
||||
|
||||
func TestDelContainerIDMappingContainerIDEmptyFailure(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
err := delContainerIDMapping("")
|
||||
assert.Error(err)
|
||||
}
|
||||
|
||||
func TestDelContainerIDMappingSuccess(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
path, err := createTempContainerIDMapping(testContainerID, testSandboxID)
|
||||
assert.NoError(err)
|
||||
defer os.RemoveAll(path)
|
||||
|
||||
_, err = os.Stat(filepath.Join(ctrsMapTreePath, testContainerID, testSandboxID))
|
||||
assert.NoError(err)
|
||||
|
||||
err = delContainerIDMapping(testContainerID)
|
||||
assert.NoError(err)
|
||||
|
||||
_, err = os.Stat(filepath.Join(ctrsMapTreePath, testContainerID, testSandboxID))
|
||||
assert.True(os.IsNotExist(err))
|
||||
}
|
||||
|
@ -7,6 +7,8 @@ package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
vc "github.com/kata-containers/runtime/virtcontainers"
|
||||
@ -32,12 +34,18 @@ func TestPauseCLIFunctionSuccessful(t *testing.T) {
|
||||
}
|
||||
|
||||
testingImpl.PauseSandboxFunc = testPauseSandboxFuncReturnNil
|
||||
testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
|
||||
return newSingleContainerSandboxStatusList(testSandboxID, testContainerID, state, state, map[string]string{}), nil
|
||||
|
||||
path, err := createTempContainerIDMapping(testContainerID, testSandboxID)
|
||||
assert.NoError(err)
|
||||
defer os.RemoveAll(path)
|
||||
|
||||
testingImpl.StatusContainerFunc = func(sandboxID, containerID string) (vc.ContainerStatus, error) {
|
||||
return newSingleContainerStatus(testContainerID, state, map[string]string{}), nil
|
||||
}
|
||||
|
||||
defer func() {
|
||||
testingImpl.PauseSandboxFunc = nil
|
||||
testingImpl.ListSandboxFunc = nil
|
||||
testingImpl.StatusContainerFunc = nil
|
||||
}()
|
||||
|
||||
set := flag.NewFlagSet("", 0)
|
||||
@ -50,12 +58,14 @@ func TestPauseCLIFunctionContainerNotExistFailure(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
testingImpl.PauseSandboxFunc = testPauseSandboxFuncReturnNil
|
||||
testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
|
||||
return []vc.SandboxStatus{}, nil
|
||||
}
|
||||
|
||||
path, err := ioutil.TempDir("", "containers-mapping")
|
||||
assert.NoError(err)
|
||||
defer os.RemoveAll(path)
|
||||
ctrsMapTreePath = path
|
||||
|
||||
defer func() {
|
||||
testingImpl.PauseSandboxFunc = nil
|
||||
testingImpl.ListSandboxFunc = nil
|
||||
}()
|
||||
|
||||
set := flag.NewFlagSet("", 0)
|
||||
@ -71,11 +81,16 @@ func TestPauseCLIFunctionPauseSandboxFailure(t *testing.T) {
|
||||
State: vc.StateRunning,
|
||||
}
|
||||
|
||||
testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
|
||||
return newSingleContainerSandboxStatusList(testSandboxID, testContainerID, state, state, map[string]string{}), nil
|
||||
path, err := createTempContainerIDMapping(testContainerID, testSandboxID)
|
||||
assert.NoError(err)
|
||||
defer os.RemoveAll(path)
|
||||
|
||||
testingImpl.StatusContainerFunc = func(sandboxID, containerID string) (vc.ContainerStatus, error) {
|
||||
return newSingleContainerStatus(testContainerID, state, map[string]string{}), nil
|
||||
}
|
||||
|
||||
defer func() {
|
||||
testingImpl.ListSandboxFunc = nil
|
||||
testingImpl.StatusContainerFunc = nil
|
||||
}()
|
||||
|
||||
set := flag.NewFlagSet("", 0)
|
||||
@ -92,12 +107,18 @@ func TestResumeCLIFunctionSuccessful(t *testing.T) {
|
||||
}
|
||||
|
||||
testingImpl.ResumeSandboxFunc = testResumeSandboxFuncReturnNil
|
||||
testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
|
||||
return newSingleContainerSandboxStatusList(testSandboxID, testContainerID, state, state, map[string]string{}), nil
|
||||
|
||||
path, err := createTempContainerIDMapping(testContainerID, testSandboxID)
|
||||
assert.NoError(err)
|
||||
defer os.RemoveAll(path)
|
||||
|
||||
testingImpl.StatusContainerFunc = func(sandboxID, containerID string) (vc.ContainerStatus, error) {
|
||||
return newSingleContainerStatus(testContainerID, state, map[string]string{}), nil
|
||||
}
|
||||
|
||||
defer func() {
|
||||
testingImpl.ResumeSandboxFunc = nil
|
||||
testingImpl.ListSandboxFunc = nil
|
||||
testingImpl.StatusContainerFunc = nil
|
||||
}()
|
||||
|
||||
set := flag.NewFlagSet("", 0)
|
||||
@ -110,12 +131,13 @@ func TestResumeCLIFunctionContainerNotExistFailure(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
testingImpl.ResumeSandboxFunc = testResumeSandboxFuncReturnNil
|
||||
testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
|
||||
return []vc.SandboxStatus{}, nil
|
||||
}
|
||||
|
||||
path, err := createTempContainerIDMapping(testContainerID, testSandboxID)
|
||||
assert.NoError(err)
|
||||
defer os.RemoveAll(path)
|
||||
|
||||
defer func() {
|
||||
testingImpl.ResumeSandboxFunc = nil
|
||||
testingImpl.ListSandboxFunc = nil
|
||||
}()
|
||||
|
||||
set := flag.NewFlagSet("", 0)
|
||||
@ -131,11 +153,16 @@ func TestResumeCLIFunctionPauseSandboxFailure(t *testing.T) {
|
||||
State: vc.StateRunning,
|
||||
}
|
||||
|
||||
testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
|
||||
return newSingleContainerSandboxStatusList(testSandboxID, testContainerID, state, state, map[string]string{}), nil
|
||||
path, err := createTempContainerIDMapping(testContainerID, testSandboxID)
|
||||
assert.NoError(err)
|
||||
defer os.RemoveAll(path)
|
||||
|
||||
testingImpl.StatusContainerFunc = func(sandboxID, containerID string) (vc.ContainerStatus, error) {
|
||||
return newSingleContainerStatus(testContainerID, state, map[string]string{}), nil
|
||||
}
|
||||
|
||||
defer func() {
|
||||
testingImpl.ListSandboxFunc = nil
|
||||
testingImpl.StatusContainerFunc = nil
|
||||
}()
|
||||
|
||||
set := flag.NewFlagSet("", 0)
|
||||
|
@ -7,6 +7,7 @@ package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
vc "github.com/kata-containers/runtime/virtcontainers"
|
||||
@ -47,29 +48,25 @@ func TestPSFailure(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
|
||||
// return a sandboxStatus with the container status
|
||||
return []vc.SandboxStatus{
|
||||
{
|
||||
ID: sandbox.ID(),
|
||||
ContainersStatus: []vc.ContainerStatus{
|
||||
{
|
||||
ID: sandbox.ID(),
|
||||
Annotations: map[string]string{
|
||||
vcAnnotations.ContainerTypeKey: string(vc.PodContainer),
|
||||
},
|
||||
},
|
||||
},
|
||||
path, err := createTempContainerIDMapping(sandbox.ID(), sandbox.ID())
|
||||
assert.NoError(err)
|
||||
defer os.RemoveAll(path)
|
||||
|
||||
testingImpl.StatusContainerFunc = func(sandboxID, containerID string) (vc.ContainerStatus, error) {
|
||||
return vc.ContainerStatus{
|
||||
ID: sandbox.ID(),
|
||||
Annotations: map[string]string{
|
||||
vcAnnotations.ContainerTypeKey: string(vc.PodContainer),
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
defer func() {
|
||||
testingImpl.ListSandboxFunc = nil
|
||||
testingImpl.StatusContainerFunc = nil
|
||||
}()
|
||||
|
||||
// inexistent container
|
||||
err := ps("xyz123abc", "json", []string{"-ef"})
|
||||
err = ps("xyz123abc", "json", []string{"-ef"})
|
||||
assert.Error(err)
|
||||
|
||||
// container is not running
|
||||
@ -91,22 +88,18 @@ func TestPSSuccessful(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
|
||||
// return a sandboxStatus with the container status
|
||||
return []vc.SandboxStatus{
|
||||
{
|
||||
ID: sandbox.ID(),
|
||||
ContainersStatus: []vc.ContainerStatus{
|
||||
{
|
||||
State: vc.State{
|
||||
State: vc.StateRunning,
|
||||
},
|
||||
ID: sandbox.ID(),
|
||||
Annotations: map[string]string{
|
||||
vcAnnotations.ContainerTypeKey: string(vc.PodContainer),
|
||||
},
|
||||
},
|
||||
},
|
||||
path, err := createTempContainerIDMapping(sandbox.ID(), sandbox.ID())
|
||||
assert.NoError(err)
|
||||
defer os.RemoveAll(path)
|
||||
|
||||
testingImpl.StatusContainerFunc = func(sandboxID, containerID string) (vc.ContainerStatus, error) {
|
||||
return vc.ContainerStatus{
|
||||
State: vc.State{
|
||||
State: vc.StateRunning,
|
||||
},
|
||||
ID: sandbox.ID(),
|
||||
Annotations: map[string]string{
|
||||
vcAnnotations.ContainerTypeKey: string(vc.PodContainer),
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
@ -116,10 +109,10 @@ func TestPSSuccessful(t *testing.T) {
|
||||
}
|
||||
|
||||
defer func() {
|
||||
testingImpl.ListSandboxFunc = nil
|
||||
testingImpl.StatusContainerFunc = nil
|
||||
testingImpl.ProcessListContainerFunc = nil
|
||||
}()
|
||||
|
||||
err := ps(sandbox.ID(), "json", []string{})
|
||||
err = ps(sandbox.ID(), "json", []string{})
|
||||
assert.NoError(err)
|
||||
}
|
||||
|
190
cli/run_test.go
190
cli/run_test.go
@ -74,14 +74,14 @@ func TestRunInvalidArgs(t *testing.T) {
|
||||
return sandbox, nil
|
||||
}
|
||||
|
||||
testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
|
||||
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.StartSandboxFunc = nil
|
||||
testingImpl.ListSandboxFunc = nil
|
||||
}()
|
||||
|
||||
// temporal dir to place container files
|
||||
@ -237,25 +237,23 @@ func TestRunContainerSuccessful(t *testing.T) {
|
||||
return d.sandbox, nil
|
||||
}
|
||||
|
||||
testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
|
||||
path, err := ioutil.TempDir("", "containers-mapping")
|
||||
assert.NoError(err)
|
||||
defer os.RemoveAll(path)
|
||||
ctrsMapTreePath = path
|
||||
|
||||
testingImpl.StatusContainerFunc = func(sandboxID, containerID string) (vc.ContainerStatus, error) {
|
||||
// return an empty list on create
|
||||
if !flagCreate {
|
||||
return []vc.SandboxStatus{}, nil
|
||||
return vc.ContainerStatus{}, nil
|
||||
}
|
||||
|
||||
// return a sandboxStatus with the container status
|
||||
return []vc.SandboxStatus{
|
||||
{
|
||||
ID: d.sandbox.ID(),
|
||||
ContainersStatus: []vc.ContainerStatus{
|
||||
{
|
||||
ID: d.sandbox.ID(),
|
||||
Annotations: map[string]string{
|
||||
vcAnnotations.ContainerTypeKey: string(vc.PodContainer),
|
||||
vcAnnotations.ConfigJSONKey: d.configJSON,
|
||||
},
|
||||
},
|
||||
},
|
||||
return vc.ContainerStatus{
|
||||
ID: d.sandbox.ID(),
|
||||
Annotations: map[string]string{
|
||||
vcAnnotations.ContainerTypeKey: string(vc.PodContainer),
|
||||
vcAnnotations.ConfigJSONKey: d.configJSON,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
@ -279,13 +277,13 @@ func TestRunContainerSuccessful(t *testing.T) {
|
||||
defer func() {
|
||||
testingImpl.CreateSandboxFunc = nil
|
||||
testingImpl.StartSandboxFunc = nil
|
||||
testingImpl.ListSandboxFunc = nil
|
||||
testingImpl.StatusContainerFunc = nil
|
||||
testingImpl.StartContainerFunc = nil
|
||||
testingImpl.DeleteSandboxFunc = nil
|
||||
testingImpl.DeleteContainerFunc = nil
|
||||
}()
|
||||
|
||||
err := run(d.sandbox.ID(), d.bundlePath, d.consolePath, "", d.pidFilePath, false, d.runtimeConfig)
|
||||
err = run(d.sandbox.ID(), d.bundlePath, d.consolePath, "", d.pidFilePath, false, d.runtimeConfig)
|
||||
|
||||
// should return ExitError with the message and exit code
|
||||
e, ok := err.(*cli.ExitError)
|
||||
@ -313,25 +311,23 @@ func TestRunContainerDetachSuccessful(t *testing.T) {
|
||||
return d.sandbox, nil
|
||||
}
|
||||
|
||||
testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
|
||||
path, err := ioutil.TempDir("", "containers-mapping")
|
||||
assert.NoError(err)
|
||||
defer os.RemoveAll(path)
|
||||
ctrsMapTreePath = path
|
||||
|
||||
testingImpl.StatusContainerFunc = func(sandboxID, containerID string) (vc.ContainerStatus, error) {
|
||||
// return an empty list on create
|
||||
if !flagCreate {
|
||||
return []vc.SandboxStatus{}, nil
|
||||
return vc.ContainerStatus{}, nil
|
||||
}
|
||||
|
||||
// return a sandboxStatus with the container status
|
||||
return []vc.SandboxStatus{
|
||||
{
|
||||
ID: d.sandbox.ID(),
|
||||
ContainersStatus: []vc.ContainerStatus{
|
||||
{
|
||||
ID: d.sandbox.ID(),
|
||||
Annotations: map[string]string{
|
||||
vcAnnotations.ContainerTypeKey: string(vc.PodContainer),
|
||||
vcAnnotations.ConfigJSONKey: d.configJSON,
|
||||
},
|
||||
},
|
||||
},
|
||||
return vc.ContainerStatus{
|
||||
ID: d.sandbox.ID(),
|
||||
Annotations: map[string]string{
|
||||
vcAnnotations.ContainerTypeKey: string(vc.PodContainer),
|
||||
vcAnnotations.ConfigJSONKey: d.configJSON,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
@ -355,13 +351,13 @@ func TestRunContainerDetachSuccessful(t *testing.T) {
|
||||
defer func() {
|
||||
testingImpl.CreateSandboxFunc = nil
|
||||
testingImpl.StartSandboxFunc = nil
|
||||
testingImpl.ListSandboxFunc = nil
|
||||
testingImpl.StatusContainerFunc = nil
|
||||
testingImpl.StartContainerFunc = nil
|
||||
testingImpl.DeleteSandboxFunc = nil
|
||||
testingImpl.DeleteContainerFunc = nil
|
||||
}()
|
||||
|
||||
err := run(d.sandbox.ID(), d.bundlePath, d.consolePath, "", d.pidFilePath, true, d.runtimeConfig)
|
||||
err = run(d.sandbox.ID(), d.bundlePath, d.consolePath, "", d.pidFilePath, true, d.runtimeConfig)
|
||||
|
||||
// should not return ExitError
|
||||
assert.NoError(err)
|
||||
@ -386,25 +382,23 @@ func TestRunContainerDeleteFail(t *testing.T) {
|
||||
return d.sandbox, nil
|
||||
}
|
||||
|
||||
testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
|
||||
path, err := ioutil.TempDir("", "containers-mapping")
|
||||
assert.NoError(err)
|
||||
defer os.RemoveAll(path)
|
||||
ctrsMapTreePath = path
|
||||
|
||||
testingImpl.StatusContainerFunc = func(sandboxID, containerID string) (vc.ContainerStatus, error) {
|
||||
// return an empty list on create
|
||||
if !flagCreate {
|
||||
return []vc.SandboxStatus{}, nil
|
||||
return vc.ContainerStatus{}, nil
|
||||
}
|
||||
|
||||
// return a sandboxStatus with the container status
|
||||
return []vc.SandboxStatus{
|
||||
{
|
||||
ID: d.sandbox.ID(),
|
||||
ContainersStatus: []vc.ContainerStatus{
|
||||
{
|
||||
ID: d.sandbox.ID(),
|
||||
Annotations: map[string]string{
|
||||
vcAnnotations.ContainerTypeKey: string(vc.PodContainer),
|
||||
vcAnnotations.ConfigJSONKey: d.configJSON,
|
||||
},
|
||||
},
|
||||
},
|
||||
return vc.ContainerStatus{
|
||||
ID: d.sandbox.ID(),
|
||||
Annotations: map[string]string{
|
||||
vcAnnotations.ContainerTypeKey: string(vc.PodContainer),
|
||||
vcAnnotations.ConfigJSONKey: d.configJSON,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
@ -430,13 +424,13 @@ func TestRunContainerDeleteFail(t *testing.T) {
|
||||
defer func() {
|
||||
testingImpl.CreateSandboxFunc = nil
|
||||
testingImpl.StartSandboxFunc = nil
|
||||
testingImpl.ListSandboxFunc = nil
|
||||
testingImpl.StatusContainerFunc = nil
|
||||
testingImpl.StartContainerFunc = nil
|
||||
testingImpl.DeleteSandboxFunc = nil
|
||||
testingImpl.DeleteContainerFunc = nil
|
||||
}()
|
||||
|
||||
err := run(d.sandbox.ID(), d.bundlePath, d.consolePath, "", d.pidFilePath, false, d.runtimeConfig)
|
||||
err = run(d.sandbox.ID(), d.bundlePath, d.consolePath, "", d.pidFilePath, false, d.runtimeConfig)
|
||||
|
||||
// should not return ExitError
|
||||
err, ok := err.(*cli.ExitError)
|
||||
@ -462,25 +456,23 @@ func TestRunContainerWaitFail(t *testing.T) {
|
||||
return d.sandbox, nil
|
||||
}
|
||||
|
||||
testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
|
||||
path, err := ioutil.TempDir("", "containers-mapping")
|
||||
assert.NoError(err)
|
||||
defer os.RemoveAll(path)
|
||||
ctrsMapTreePath = path
|
||||
|
||||
testingImpl.StatusContainerFunc = func(sandboxID, containerID string) (vc.ContainerStatus, error) {
|
||||
// return an empty list on create
|
||||
if !flagCreate {
|
||||
return []vc.SandboxStatus{}, nil
|
||||
return vc.ContainerStatus{}, nil
|
||||
}
|
||||
|
||||
// return a sandboxStatus with the container status
|
||||
return []vc.SandboxStatus{
|
||||
{
|
||||
ID: d.sandbox.ID(),
|
||||
ContainersStatus: []vc.ContainerStatus{
|
||||
{
|
||||
ID: d.sandbox.ID(),
|
||||
Annotations: map[string]string{
|
||||
vcAnnotations.ContainerTypeKey: string(vc.PodContainer),
|
||||
vcAnnotations.ConfigJSONKey: d.configJSON,
|
||||
},
|
||||
},
|
||||
},
|
||||
return vc.ContainerStatus{
|
||||
ID: d.sandbox.ID(),
|
||||
Annotations: map[string]string{
|
||||
vcAnnotations.ContainerTypeKey: string(vc.PodContainer),
|
||||
vcAnnotations.ConfigJSONKey: d.configJSON,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
@ -509,13 +501,13 @@ func TestRunContainerWaitFail(t *testing.T) {
|
||||
defer func() {
|
||||
testingImpl.CreateSandboxFunc = nil
|
||||
testingImpl.StartSandboxFunc = nil
|
||||
testingImpl.ListSandboxFunc = nil
|
||||
testingImpl.StatusContainerFunc = nil
|
||||
testingImpl.StartContainerFunc = nil
|
||||
testingImpl.DeleteSandboxFunc = nil
|
||||
testingImpl.DeleteContainerFunc = nil
|
||||
}()
|
||||
|
||||
err := run(d.sandbox.ID(), d.bundlePath, d.consolePath, "", d.pidFilePath, false, d.runtimeConfig)
|
||||
err = run(d.sandbox.ID(), d.bundlePath, d.consolePath, "", d.pidFilePath, false, d.runtimeConfig)
|
||||
|
||||
// should not return ExitError
|
||||
err, ok := err.(*cli.ExitError)
|
||||
@ -546,25 +538,23 @@ func TestRunContainerStartFail(t *testing.T) {
|
||||
return nil, fmt.Errorf("StartSandbox")
|
||||
}
|
||||
|
||||
testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
|
||||
path, err := ioutil.TempDir("", "containers-mapping")
|
||||
assert.NoError(err)
|
||||
defer os.RemoveAll(path)
|
||||
ctrsMapTreePath = path
|
||||
|
||||
testingImpl.StatusContainerFunc = func(sandboxID, containerID string) (vc.ContainerStatus, error) {
|
||||
// return an empty list on create
|
||||
if !flagCreate {
|
||||
return []vc.SandboxStatus{}, nil
|
||||
return vc.ContainerStatus{}, nil
|
||||
}
|
||||
|
||||
// return a sandboxStatus with the container status
|
||||
return []vc.SandboxStatus{
|
||||
{
|
||||
ID: d.sandbox.ID(),
|
||||
ContainersStatus: []vc.ContainerStatus{
|
||||
{
|
||||
ID: d.sandbox.ID(),
|
||||
Annotations: map[string]string{
|
||||
vcAnnotations.ContainerTypeKey: string(vc.PodContainer),
|
||||
vcAnnotations.ConfigJSONKey: d.configJSON,
|
||||
},
|
||||
},
|
||||
},
|
||||
return vc.ContainerStatus{
|
||||
ID: d.sandbox.ID(),
|
||||
Annotations: map[string]string{
|
||||
vcAnnotations.ContainerTypeKey: string(vc.PodContainer),
|
||||
vcAnnotations.ConfigJSONKey: d.configJSON,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
@ -572,7 +562,7 @@ func TestRunContainerStartFail(t *testing.T) {
|
||||
defer func() {
|
||||
testingImpl.CreateSandboxFunc = nil
|
||||
testingImpl.StartSandboxFunc = nil
|
||||
testingImpl.ListSandboxFunc = nil
|
||||
testingImpl.StatusContainerFunc = nil
|
||||
}()
|
||||
|
||||
err = run(d.sandbox.ID(), d.bundlePath, d.consolePath, "", d.pidFilePath, false, d.runtimeConfig)
|
||||
@ -582,11 +572,9 @@ func TestRunContainerStartFail(t *testing.T) {
|
||||
assert.False(ok, "error should not be a cli.ExitError: %s", err)
|
||||
}
|
||||
|
||||
func TestRunContainerStartFailNoContainers(t *testing.T) {
|
||||
func TestRunContainerStartFailExistingContainer(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
listCallCount := 0
|
||||
|
||||
d := testRunContainerSetup(t)
|
||||
defer os.RemoveAll(d.tmpDir)
|
||||
|
||||
@ -601,24 +589,16 @@ func TestRunContainerStartFailNoContainers(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
|
||||
listCallCount++
|
||||
path, err := createTempContainerIDMapping(testContainerID, sandbox.ID())
|
||||
assert.NoError(err)
|
||||
defer os.RemoveAll(path)
|
||||
|
||||
if listCallCount == 1 {
|
||||
return []vc.SandboxStatus{}, nil
|
||||
}
|
||||
|
||||
return []vc.SandboxStatus{
|
||||
{
|
||||
ID: sandbox.ID(),
|
||||
ContainersStatus: []vc.ContainerStatus{
|
||||
{
|
||||
ID: testContainerID,
|
||||
Annotations: map[string]string{
|
||||
vcAnnotations.ContainerTypeKey: string(vc.PodSandbox),
|
||||
},
|
||||
},
|
||||
},
|
||||
testingImpl.StatusContainerFunc = func(sandboxID, containerID string) (vc.ContainerStatus, error) {
|
||||
// return the container status
|
||||
return vc.ContainerStatus{
|
||||
ID: testContainerID,
|
||||
Annotations: map[string]string{
|
||||
vcAnnotations.ContainerTypeKey: string(vc.PodContainer),
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
@ -635,12 +615,12 @@ func TestRunContainerStartFailNoContainers(t *testing.T) {
|
||||
}
|
||||
|
||||
defer func() {
|
||||
testingImpl.ListSandboxFunc = nil
|
||||
testingImpl.StatusContainerFunc = nil
|
||||
testingImpl.CreateSandboxFunc = nil
|
||||
testingImpl.StartSandboxFunc = nil
|
||||
}()
|
||||
|
||||
err := run(d.sandbox.ID(), d.bundlePath, d.consolePath, "", d.pidFilePath, false, d.runtimeConfig)
|
||||
err = run(d.sandbox.ID(), d.bundlePath, d.consolePath, "", d.pidFilePath, false, d.runtimeConfig)
|
||||
assert.Error(err)
|
||||
assert.False(vcmock.IsMockError(err))
|
||||
}
|
||||
|
@ -7,6 +7,8 @@ package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
vc "github.com/kata-containers/runtime/virtcontainers"
|
||||
@ -24,20 +26,21 @@ func TestStartInvalidArgs(t *testing.T) {
|
||||
assert.Error(err)
|
||||
assert.False(vcmock.IsMockError(err))
|
||||
|
||||
// Mock Listsandbox error
|
||||
path, err := createTempContainerIDMapping(testContainerID, testSandboxID)
|
||||
assert.NoError(err)
|
||||
defer os.RemoveAll(path)
|
||||
|
||||
// Mock StatusContainer error
|
||||
_, err = start(testContainerID)
|
||||
assert.Error(err)
|
||||
assert.True(vcmock.IsMockError(err))
|
||||
|
||||
testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
|
||||
return []vc.SandboxStatus{}, nil
|
||||
}
|
||||
path, err = ioutil.TempDir("", "containers-mapping")
|
||||
assert.NoError(err)
|
||||
defer os.RemoveAll(path)
|
||||
ctrsMapTreePath = path
|
||||
|
||||
defer func() {
|
||||
testingImpl.ListSandboxFunc = nil
|
||||
}()
|
||||
|
||||
// Container missing in ListSandbox
|
||||
// Container missing in container mapping
|
||||
_, err = start(testContainerID)
|
||||
assert.Error(err)
|
||||
assert.False(vcmock.IsMockError(err))
|
||||
@ -50,27 +53,24 @@ func TestStartSandbox(t *testing.T) {
|
||||
MockID: testSandboxID,
|
||||
}
|
||||
|
||||
testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
|
||||
return []vc.SandboxStatus{
|
||||
{
|
||||
ID: sandbox.ID(),
|
||||
ContainersStatus: []vc.ContainerStatus{
|
||||
{
|
||||
ID: sandbox.ID(),
|
||||
Annotations: map[string]string{
|
||||
vcAnnotations.ContainerTypeKey: string(vc.PodSandbox),
|
||||
},
|
||||
},
|
||||
},
|
||||
path, err := createTempContainerIDMapping(sandbox.ID(), sandbox.ID())
|
||||
assert.NoError(err)
|
||||
defer os.RemoveAll(path)
|
||||
|
||||
testingImpl.StatusContainerFunc = func(sandboxID, containerID string) (vc.ContainerStatus, error) {
|
||||
return vc.ContainerStatus{
|
||||
ID: sandbox.ID(),
|
||||
Annotations: map[string]string{
|
||||
vcAnnotations.ContainerTypeKey: string(vc.PodSandbox),
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
defer func() {
|
||||
testingImpl.ListSandboxFunc = nil
|
||||
testingImpl.StatusContainerFunc = nil
|
||||
}()
|
||||
|
||||
_, err := start(sandbox.ID())
|
||||
_, err = start(sandbox.ID())
|
||||
assert.Error(err)
|
||||
assert.True(vcmock.IsMockError(err))
|
||||
|
||||
@ -93,25 +93,22 @@ func TestStartMissingAnnotation(t *testing.T) {
|
||||
MockID: testSandboxID,
|
||||
}
|
||||
|
||||
testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
|
||||
return []vc.SandboxStatus{
|
||||
{
|
||||
ID: sandbox.ID(),
|
||||
ContainersStatus: []vc.ContainerStatus{
|
||||
{
|
||||
ID: sandbox.ID(),
|
||||
Annotations: map[string]string{},
|
||||
},
|
||||
},
|
||||
},
|
||||
path, err := createTempContainerIDMapping(sandbox.ID(), sandbox.ID())
|
||||
assert.NoError(err)
|
||||
defer os.RemoveAll(path)
|
||||
|
||||
testingImpl.StatusContainerFunc = func(sandboxID, containerID string) (vc.ContainerStatus, error) {
|
||||
return vc.ContainerStatus{
|
||||
ID: sandbox.ID(),
|
||||
Annotations: map[string]string{},
|
||||
}, nil
|
||||
}
|
||||
|
||||
defer func() {
|
||||
testingImpl.ListSandboxFunc = nil
|
||||
testingImpl.StatusContainerFunc = nil
|
||||
}()
|
||||
|
||||
_, err := start(sandbox.ID())
|
||||
_, err = start(sandbox.ID())
|
||||
assert.Error(err)
|
||||
assert.False(vcmock.IsMockError(err))
|
||||
}
|
||||
@ -130,27 +127,24 @@ func TestStartContainerSucessFailure(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
|
||||
return []vc.SandboxStatus{
|
||||
{
|
||||
ID: sandbox.ID(),
|
||||
ContainersStatus: []vc.ContainerStatus{
|
||||
{
|
||||
ID: testContainerID,
|
||||
Annotations: map[string]string{
|
||||
vcAnnotations.ContainerTypeKey: string(vc.PodContainer),
|
||||
},
|
||||
},
|
||||
},
|
||||
path, err := createTempContainerIDMapping(testContainerID, sandbox.ID())
|
||||
assert.NoError(err)
|
||||
defer os.RemoveAll(path)
|
||||
|
||||
testingImpl.StatusContainerFunc = func(sandboxID, containerID string) (vc.ContainerStatus, error) {
|
||||
return vc.ContainerStatus{
|
||||
ID: testContainerID,
|
||||
Annotations: map[string]string{
|
||||
vcAnnotations.ContainerTypeKey: string(vc.PodContainer),
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
defer func() {
|
||||
testingImpl.ListSandboxFunc = nil
|
||||
testingImpl.StatusContainerFunc = nil
|
||||
}()
|
||||
|
||||
_, err := start(testContainerID)
|
||||
_, err = start(testContainerID)
|
||||
assert.Error(err)
|
||||
assert.True(vcmock.IsMockError(err))
|
||||
|
||||
@ -182,6 +176,10 @@ func TestStartCLIFunction(t *testing.T) {
|
||||
assert.Error(err)
|
||||
assert.False(vcmock.IsMockError(err))
|
||||
|
||||
path, err := createTempContainerIDMapping("xyz", "xyz")
|
||||
assert.NoError(err)
|
||||
defer os.RemoveAll(path)
|
||||
|
||||
flagSet = flag.NewFlagSet("container-id", flag.ContinueOnError)
|
||||
flagSet.Parse([]string{"xyz"})
|
||||
ctx = cli.NewContext(app, flagSet, nil)
|
||||
@ -205,18 +203,15 @@ func TestStartCLIFunctionSuccess(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
|
||||
return []vc.SandboxStatus{
|
||||
{
|
||||
ID: sandbox.ID(),
|
||||
ContainersStatus: []vc.ContainerStatus{
|
||||
{
|
||||
ID: testContainerID,
|
||||
Annotations: map[string]string{
|
||||
vcAnnotations.ContainerTypeKey: string(vc.PodContainer),
|
||||
},
|
||||
},
|
||||
},
|
||||
path, err := createTempContainerIDMapping(testContainerID, testSandboxID)
|
||||
assert.NoError(err)
|
||||
defer os.RemoveAll(path)
|
||||
|
||||
testingImpl.StatusContainerFunc = func(sandboxID, containerID string) (vc.ContainerStatus, error) {
|
||||
return vc.ContainerStatus{
|
||||
ID: testContainerID,
|
||||
Annotations: map[string]string{
|
||||
vcAnnotations.ContainerTypeKey: string(vc.PodContainer),
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
@ -226,7 +221,7 @@ func TestStartCLIFunctionSuccess(t *testing.T) {
|
||||
}
|
||||
|
||||
defer func() {
|
||||
testingImpl.ListSandboxFunc = nil
|
||||
testingImpl.StatusContainerFunc = nil
|
||||
testingImpl.StartContainerFunc = nil
|
||||
}()
|
||||
|
||||
@ -240,6 +235,6 @@ func TestStartCLIFunctionSuccess(t *testing.T) {
|
||||
ctx := cli.NewContext(app, flagSet, nil)
|
||||
assert.NotNil(ctx)
|
||||
|
||||
err := fn(ctx)
|
||||
err = fn(ctx)
|
||||
assert.NoError(err)
|
||||
}
|
||||
|
@ -7,6 +7,8 @@ package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
vc "github.com/kata-containers/runtime/virtcontainers"
|
||||
@ -51,31 +53,32 @@ func TestStateSuccessful(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
testingImpl.ListSandboxFunc = func() ([]vc.SandboxStatus, error) {
|
||||
// return a sandboxStatus with the container status
|
||||
return []vc.SandboxStatus{
|
||||
{
|
||||
ID: sandbox.ID(),
|
||||
ContainersStatus: []vc.ContainerStatus{
|
||||
{
|
||||
ID: sandbox.ID(),
|
||||
Annotations: map[string]string{
|
||||
vcAnnotations.ContainerTypeKey: string(vc.PodContainer),
|
||||
},
|
||||
},
|
||||
},
|
||||
path, err := ioutil.TempDir("", "containers-mapping")
|
||||
assert.NoError(err)
|
||||
defer os.RemoveAll(path)
|
||||
ctrsMapTreePath = path
|
||||
|
||||
// trying with an inexistent id
|
||||
err = state("123456789")
|
||||
assert.Error(err)
|
||||
|
||||
path, err = createTempContainerIDMapping(testContainerID, sandbox.ID())
|
||||
assert.NoError(err)
|
||||
defer os.RemoveAll(path)
|
||||
|
||||
testingImpl.StatusContainerFunc = func(sandboxID, containerID string) (vc.ContainerStatus, error) {
|
||||
return vc.ContainerStatus{
|
||||
ID: testContainerID,
|
||||
Annotations: map[string]string{
|
||||
vcAnnotations.ContainerTypeKey: string(vc.PodContainer),
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
defer func() {
|
||||
testingImpl.ListSandboxFunc = nil
|
||||
testingImpl.StatusContainerFunc = nil
|
||||
}()
|
||||
|
||||
// trying with an inexistent id
|
||||
err := state("123456789")
|
||||
assert.Error(err)
|
||||
|
||||
err = state(sandbox.ID())
|
||||
err = state(testContainerID)
|
||||
assert.NoError(err)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user