mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-10-21 20:08:54 +00:00
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>
119 lines
2.7 KiB
Go
119 lines
2.7 KiB
Go
// Copyright (c) 2017 Intel Corporation
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
|
|
package main
|
|
|
|
import (
|
|
"flag"
|
|
"os"
|
|
"testing"
|
|
|
|
vc "github.com/kata-containers/runtime/virtcontainers"
|
|
vcAnnotations "github.com/kata-containers/runtime/virtcontainers/pkg/annotations"
|
|
"github.com/kata-containers/runtime/virtcontainers/pkg/vcmock"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/urfave/cli"
|
|
)
|
|
|
|
func TestPSCLIAction(t *testing.T) {
|
|
assert := assert.New(t)
|
|
|
|
flagSet := flag.NewFlagSet("flag", flag.ContinueOnError)
|
|
flagSet.Parse([]string{"runtime"})
|
|
|
|
// create a new fake context
|
|
ctx := cli.NewContext(&cli.App{Metadata: map[string]interface{}{}}, flagSet, nil)
|
|
|
|
// get Action function
|
|
actionFunc, ok := psCLICommand.Action.(func(ctx *cli.Context) error)
|
|
assert.True(ok)
|
|
|
|
err := actionFunc(ctx)
|
|
assert.Error(err, "Missing container ID")
|
|
}
|
|
|
|
func TestPSFailure(t *testing.T) {
|
|
assert := assert.New(t)
|
|
|
|
sandbox := &vcmock.Sandbox{
|
|
MockID: testContainerID,
|
|
}
|
|
|
|
sandbox.MockContainers = []*vcmock.Container{
|
|
{
|
|
MockID: sandbox.ID(),
|
|
MockSandbox: sandbox,
|
|
},
|
|
}
|
|
|
|
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.StatusContainerFunc = nil
|
|
}()
|
|
|
|
// inexistent container
|
|
err = ps("xyz123abc", "json", []string{"-ef"})
|
|
assert.Error(err)
|
|
|
|
// container is not running
|
|
err = ps(sandbox.ID(), "json", []string{"-ef"})
|
|
assert.Error(err)
|
|
}
|
|
|
|
func TestPSSuccessful(t *testing.T) {
|
|
assert := assert.New(t)
|
|
|
|
sandbox := &vcmock.Sandbox{
|
|
MockID: testContainerID,
|
|
}
|
|
|
|
sandbox.MockContainers = []*vcmock.Container{
|
|
{
|
|
MockID: sandbox.ID(),
|
|
MockSandbox: sandbox,
|
|
},
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
testingImpl.ProcessListContainerFunc = func(sandboxID, containerID string, options vc.ProcessListOptions) (vc.ProcessList, error) {
|
|
return []byte("echo,sleep,grep"), nil
|
|
}
|
|
|
|
defer func() {
|
|
testingImpl.StatusContainerFunc = nil
|
|
testingImpl.ProcessListContainerFunc = nil
|
|
}()
|
|
|
|
err = ps(sandbox.ID(), "json", []string{})
|
|
assert.NoError(err)
|
|
}
|