mirror of
https://github.com/aljazceru/kata-containers.git
synced 2025-12-26 18:44:47 +01:00
We can now remove all the sandbox shared types and convert the rest of the code to using the new internal types package. This commit includes virtcontainers, cli and containerd-shim changes in one atomic change in order to not break bisect'ibility. Fixes: #1095 Signed-off-by: Samuel Ortiz <sameo@linux.intel.com>
204 lines
4.6 KiB
Go
204 lines
4.6 KiB
Go
// Copyright (c) 2017 Intel Corporation
|
|
// Copyright (c) 2018 HyperHQ Inc.
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
|
|
package containerdshim
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/containerd/containerd/namespaces"
|
|
taskAPI "github.com/containerd/containerd/runtime/v2/task"
|
|
|
|
vc "github.com/kata-containers/runtime/virtcontainers"
|
|
"github.com/kata-containers/runtime/virtcontainers/pkg/vcmock"
|
|
"github.com/kata-containers/runtime/virtcontainers/types"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestPauseContainerSuccess(t *testing.T) {
|
|
assert := assert.New(t)
|
|
var err error
|
|
|
|
sandbox := &vcmock.Sandbox{
|
|
MockID: testSandboxID,
|
|
}
|
|
|
|
testingImpl.PauseContainerFunc = func(ctx context.Context, sandboxID, containerID string) error {
|
|
return nil
|
|
}
|
|
defer func() {
|
|
testingImpl.PauseContainerFunc = nil
|
|
}()
|
|
|
|
testingImpl.StatusContainerFunc = func(ctx context.Context, sandboxID, containerID string) (vc.ContainerStatus, error) {
|
|
return vc.ContainerStatus{
|
|
ID: testContainerID,
|
|
Annotations: make(map[string]string),
|
|
State: types.State{
|
|
State: types.StateRunning,
|
|
},
|
|
}, nil
|
|
}
|
|
defer func() {
|
|
testingImpl.StatusContainerFunc = nil
|
|
}()
|
|
|
|
s := &service{
|
|
id: testSandboxID,
|
|
sandbox: sandbox,
|
|
containers: make(map[string]*container),
|
|
}
|
|
|
|
reqCreate := &taskAPI.CreateTaskRequest{
|
|
ID: testContainerID,
|
|
}
|
|
s.containers[testContainerID], err = newContainer(s, reqCreate, "", nil)
|
|
assert.NoError(err)
|
|
|
|
reqPause := &taskAPI.PauseRequest{
|
|
ID: testContainerID,
|
|
}
|
|
ctx := namespaces.WithNamespace(context.Background(), "UnitTest")
|
|
|
|
_, err = s.Pause(ctx, reqPause)
|
|
assert.NoError(err)
|
|
}
|
|
|
|
func TestPauseContainerFail(t *testing.T) {
|
|
assert := assert.New(t)
|
|
|
|
sandbox := &vcmock.Sandbox{
|
|
MockID: testSandboxID,
|
|
}
|
|
|
|
testingImpl.PauseContainerFunc = func(ctx context.Context, sandboxID, containerID string) error {
|
|
return nil
|
|
}
|
|
defer func() {
|
|
testingImpl.PauseContainerFunc = nil
|
|
}()
|
|
|
|
testingImpl.StatusContainerFunc = func(ctx context.Context, sandboxID, containerID string) (vc.ContainerStatus, error) {
|
|
return vc.ContainerStatus{
|
|
ID: testContainerID,
|
|
Annotations: make(map[string]string),
|
|
State: types.State{
|
|
State: types.StateRunning,
|
|
},
|
|
}, nil
|
|
}
|
|
defer func() {
|
|
testingImpl.StatusContainerFunc = nil
|
|
}()
|
|
|
|
s := &service{
|
|
id: testSandboxID,
|
|
sandbox: sandbox,
|
|
containers: make(map[string]*container),
|
|
}
|
|
|
|
reqPause := &taskAPI.PauseRequest{
|
|
ID: testContainerID,
|
|
}
|
|
ctx := namespaces.WithNamespace(context.Background(), "UnitTest")
|
|
|
|
_, err := s.Pause(ctx, reqPause)
|
|
assert.Error(err)
|
|
}
|
|
|
|
func TestResumeContainerSuccess(t *testing.T) {
|
|
assert := assert.New(t)
|
|
var err error
|
|
|
|
sandbox := &vcmock.Sandbox{
|
|
MockID: testSandboxID,
|
|
}
|
|
|
|
testingImpl.ResumeContainerFunc = func(ctx context.Context, sandboxID, containerID string) error {
|
|
return nil
|
|
}
|
|
defer func() {
|
|
testingImpl.ResumeContainerFunc = nil
|
|
}()
|
|
|
|
testingImpl.StatusContainerFunc = func(ctx context.Context, sandboxID, containerID string) (vc.ContainerStatus, error) {
|
|
return vc.ContainerStatus{
|
|
ID: testContainerID,
|
|
Annotations: make(map[string]string),
|
|
State: types.State{
|
|
State: types.StateRunning,
|
|
},
|
|
}, nil
|
|
}
|
|
|
|
defer func() {
|
|
testingImpl.StatusContainerFunc = nil
|
|
}()
|
|
|
|
s := &service{
|
|
id: testSandboxID,
|
|
sandbox: sandbox,
|
|
containers: make(map[string]*container),
|
|
}
|
|
|
|
reqCreate := &taskAPI.CreateTaskRequest{
|
|
ID: testContainerID,
|
|
}
|
|
s.containers[testContainerID], err = newContainer(s, reqCreate, "", nil)
|
|
assert.NoError(err)
|
|
|
|
reqResume := &taskAPI.ResumeRequest{
|
|
ID: testContainerID,
|
|
}
|
|
ctx := namespaces.WithNamespace(context.Background(), "UnitTest")
|
|
|
|
_, err = s.Resume(ctx, reqResume)
|
|
assert.NoError(err)
|
|
}
|
|
|
|
func TestResumeContainerFail(t *testing.T) {
|
|
assert := assert.New(t)
|
|
|
|
sandbox := &vcmock.Sandbox{
|
|
MockID: testSandboxID,
|
|
}
|
|
|
|
testingImpl.ResumeContainerFunc = func(ctx context.Context, sandboxID, containerID string) error {
|
|
return nil
|
|
}
|
|
defer func() {
|
|
testingImpl.ResumeContainerFunc = nil
|
|
}()
|
|
testingImpl.StatusContainerFunc = func(ctx context.Context, sandboxID, containerID string) (vc.ContainerStatus, error) {
|
|
return vc.ContainerStatus{
|
|
ID: testContainerID,
|
|
Annotations: make(map[string]string),
|
|
State: types.State{
|
|
State: types.StateRunning,
|
|
},
|
|
}, nil
|
|
}
|
|
defer func() {
|
|
testingImpl.StatusContainerFunc = nil
|
|
}()
|
|
|
|
s := &service{
|
|
id: testSandboxID,
|
|
sandbox: sandbox,
|
|
containers: make(map[string]*container),
|
|
}
|
|
|
|
reqResume := &taskAPI.ResumeRequest{
|
|
ID: testContainerID,
|
|
}
|
|
ctx := namespaces.WithNamespace(context.Background(), "UnitTest")
|
|
|
|
_, err := s.Resume(ctx, reqResume)
|
|
assert.Error(err)
|
|
}
|