Temporarily remove unit test relying on fake containerd services.
Signed-off-by: Lantao Liu <lantaol@google.com>
This commit is contained in:
@@ -17,20 +17,11 @@ limitations under the License.
|
||||
package server
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/containerd/containerd/api/services/containers"
|
||||
snapshotapi "github.com/containerd/containerd/api/services/snapshot"
|
||||
"github.com/containerd/containerd/api/types/mount"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"golang.org/x/net/context"
|
||||
"k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime"
|
||||
|
||||
ostesting "github.com/kubernetes-incubator/cri-containerd/pkg/os/testing"
|
||||
servertesting "github.com/kubernetes-incubator/cri-containerd/pkg/server/testing"
|
||||
"github.com/kubernetes-incubator/cri-containerd/pkg/store"
|
||||
containerstore "github.com/kubernetes-incubator/cri-containerd/pkg/store/container"
|
||||
)
|
||||
|
||||
@@ -83,146 +74,3 @@ func TestSetContainerRemoving(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemoveContainer(t *testing.T) {
|
||||
testID := "test-id"
|
||||
testName := "test-name"
|
||||
testContainerStatus := &containerstore.Status{
|
||||
CreatedAt: time.Now().UnixNano(),
|
||||
StartedAt: time.Now().UnixNano(),
|
||||
FinishedAt: time.Now().UnixNano(),
|
||||
}
|
||||
|
||||
for desc, test := range map[string]struct {
|
||||
status *containerstore.Status
|
||||
removeSnapshotErr error
|
||||
deleteContainerErr error
|
||||
removeDirErr error
|
||||
expectErr bool
|
||||
expectUnsetRemoving bool
|
||||
}{
|
||||
"should return error when container is still running": {
|
||||
status: &containerstore.Status{
|
||||
CreatedAt: time.Now().UnixNano(),
|
||||
StartedAt: time.Now().UnixNano(),
|
||||
},
|
||||
expectErr: true,
|
||||
},
|
||||
"should return error when there is ongoing removing": {
|
||||
status: &containerstore.Status{
|
||||
CreatedAt: time.Now().UnixNano(),
|
||||
StartedAt: time.Now().UnixNano(),
|
||||
FinishedAt: time.Now().UnixNano(),
|
||||
Removing: true,
|
||||
},
|
||||
expectErr: true,
|
||||
},
|
||||
"should not return error if container does not exist": {
|
||||
status: nil,
|
||||
removeSnapshotErr: servertesting.SnapshotNotExistError,
|
||||
deleteContainerErr: servertesting.ContainerNotExistError,
|
||||
expectErr: false,
|
||||
},
|
||||
"should not return error if snapshot does not exist": {
|
||||
status: testContainerStatus,
|
||||
removeSnapshotErr: servertesting.SnapshotNotExistError,
|
||||
expectErr: false,
|
||||
},
|
||||
"should return error if remove snapshot fails": {
|
||||
status: testContainerStatus,
|
||||
removeSnapshotErr: errors.New("random error"),
|
||||
expectErr: true,
|
||||
},
|
||||
"should not return error if containerd container does not exist": {
|
||||
status: testContainerStatus,
|
||||
deleteContainerErr: servertesting.ContainerNotExistError,
|
||||
expectErr: false,
|
||||
},
|
||||
"should return error if delete containerd container fails": {
|
||||
status: testContainerStatus,
|
||||
deleteContainerErr: errors.New("random error"),
|
||||
expectErr: true,
|
||||
},
|
||||
"should return error if remove container root fails": {
|
||||
status: testContainerStatus,
|
||||
removeDirErr: errors.New("random error"),
|
||||
expectErr: true,
|
||||
expectUnsetRemoving: true,
|
||||
},
|
||||
"should be able to remove container successfully": {
|
||||
status: testContainerStatus,
|
||||
expectErr: false,
|
||||
},
|
||||
} {
|
||||
t.Logf("TestCase %q", desc)
|
||||
c := newTestCRIContainerdService()
|
||||
fake := c.containerService.(*servertesting.FakeContainersClient)
|
||||
fakeSnapshotClient := WithFakeSnapshotClient(c)
|
||||
fakeOS := c.os.(*ostesting.FakeOS)
|
||||
if test.status != nil {
|
||||
assert.NoError(t, c.containerNameIndex.Reserve(testName, testID))
|
||||
container, err := containerstore.NewContainer(
|
||||
containerstore.Metadata{ID: testID},
|
||||
*test.status,
|
||||
)
|
||||
assert.NoError(t, err)
|
||||
assert.NoError(t, c.containerStore.Add(container))
|
||||
}
|
||||
fakeOS.RemoveAllFn = func(path string) error {
|
||||
assert.Equal(t, getContainerRootDir(c.rootDir, testID), path)
|
||||
return test.removeDirErr
|
||||
}
|
||||
if test.removeSnapshotErr == nil {
|
||||
fakeSnapshotClient.SetFakeMounts(testID, []*mount.Mount{
|
||||
{
|
||||
Type: "bind",
|
||||
Source: "/test/source",
|
||||
Target: "/test/target",
|
||||
},
|
||||
})
|
||||
} else {
|
||||
fakeSnapshotClient.InjectError("remove", test.removeSnapshotErr)
|
||||
}
|
||||
if test.deleteContainerErr == nil {
|
||||
_, err := fake.Create(context.Background(), &containers.CreateContainerRequest{
|
||||
Container: containers.Container{ID: testID},
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
} else {
|
||||
fake.InjectError("delete", test.deleteContainerErr)
|
||||
}
|
||||
resp, err := c.RemoveContainer(context.Background(), &runtime.RemoveContainerRequest{
|
||||
ContainerId: testID,
|
||||
})
|
||||
if test.expectErr {
|
||||
assert.Error(t, err)
|
||||
assert.Nil(t, resp)
|
||||
if !test.expectUnsetRemoving {
|
||||
continue
|
||||
}
|
||||
container, err := c.containerStore.Get(testID)
|
||||
assert.NoError(t, err)
|
||||
// Also covers resetContainerRemoving.
|
||||
assert.False(t, container.Status.Get().Removing, "removing state should be unset")
|
||||
continue
|
||||
}
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, resp)
|
||||
_, err = c.containerStore.Get(testID)
|
||||
assert.Equal(t, store.ErrNotExist, err)
|
||||
assert.NoError(t, c.containerNameIndex.Reserve(testName, testID),
|
||||
"container name should be released")
|
||||
mountsResp, err := fakeSnapshotClient.Mounts(context.Background(), &snapshotapi.MountsRequest{Key: testID})
|
||||
assert.Equal(t, servertesting.SnapshotNotExistError, err, "snapshot should be removed")
|
||||
assert.Nil(t, mountsResp)
|
||||
getResp, err := fake.Get(context.Background(), &containers.GetContainerRequest{ID: testID})
|
||||
assert.Equal(t, servertesting.ContainerNotExistError, err, "containerd container should be removed")
|
||||
assert.Nil(t, getResp)
|
||||
|
||||
resp, err = c.RemoveContainer(context.Background(), &runtime.RemoveContainerRequest{
|
||||
ContainerId: testID,
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, resp, "remove should be idempotent")
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user