|
| 1 | +package containerd_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + "github.com/stretchr/testify/mock" |
| 9 | + v1 "k8s.io/cri-api/pkg/apis/runtime/v1" |
| 10 | + |
| 11 | + "github.com/aws/eks-hybrid/internal/containerd" |
| 12 | + "github.com/aws/eks-hybrid/internal/containerd/mocks" |
| 13 | +) |
| 14 | + |
| 15 | +func TestClient_RemovePods(t *testing.T) { |
| 16 | + t.Run("no pods or containers", func(t *testing.T) { |
| 17 | + m := new(mocks.MockRuntimeService) |
| 18 | + m.On("ListPodSandbox", mock.Anything).Return([]*v1.PodSandbox{}, nil) |
| 19 | + m.On("ListContainers", (*v1.ContainerFilter)(nil)).Return([]*v1.Container{}, nil) |
| 20 | + c := &containerd.Client{Runtime: m} |
| 21 | + err := c.RemovePods() |
| 22 | + assert.NoError(t, err) |
| 23 | + m.AssertExpectations(t) |
| 24 | + }) |
| 25 | + |
| 26 | + t.Run("error listing pods", func(t *testing.T) { |
| 27 | + m := new(mocks.MockRuntimeService) |
| 28 | + m.On("ListPodSandbox", mock.Anything).Return(nil, errors.New("fail list pods")) |
| 29 | + c := &containerd.Client{Runtime: m} |
| 30 | + err := c.RemovePods() |
| 31 | + assert.ErrorContains(t, err, "fail list pods") |
| 32 | + m.AssertExpectations(t) |
| 33 | + }) |
| 34 | + |
| 35 | + t.Run("error stopping/removing pod", func(t *testing.T) { |
| 36 | + m := new(mocks.MockRuntimeService) |
| 37 | + pod := &v1.PodSandbox{Metadata: &v1.PodSandboxMetadata{Name: "pod1"}, Id: "pod1id"} |
| 38 | + m.On("ListPodSandbox", mock.Anything).Return([]*v1.PodSandbox{pod}, nil) |
| 39 | + m.On("StopPodSandbox", "pod1id").Return(errors.New("fail stop pod")).Times(3) |
| 40 | + m.On("ListContainers", (*v1.ContainerFilter)(nil)).Return([]*v1.Container{}, nil) |
| 41 | + c := &containerd.Client{Runtime: m} |
| 42 | + err := c.RemovePods() |
| 43 | + assert.NoError(t, err, "RemovePods should ignore pod stop errors") |
| 44 | + m.AssertExpectations(t) |
| 45 | + }) |
| 46 | + |
| 47 | + t.Run("error listing containers", func(t *testing.T) { |
| 48 | + m := new(mocks.MockRuntimeService) |
| 49 | + m.On("ListPodSandbox", mock.Anything).Return([]*v1.PodSandbox{}, nil) |
| 50 | + m.On("ListContainers", (*v1.ContainerFilter)(nil)).Return(nil, errors.New("fail list containers")) |
| 51 | + c := &containerd.Client{Runtime: m} |
| 52 | + err := c.RemovePods() |
| 53 | + assert.ErrorContains(t, err, "fail list containers") |
| 54 | + m.AssertExpectations(t) |
| 55 | + }) |
| 56 | + |
| 57 | + t.Run("error stopping/removing container", func(t *testing.T) { |
| 58 | + m := new(mocks.MockRuntimeService) |
| 59 | + container := &v1.Container{Metadata: &v1.ContainerMetadata{Name: "c1"}, Id: "cid1"} |
| 60 | + status := &v1.ContainerStatus{State: v1.ContainerState_CONTAINER_RUNNING} |
| 61 | + m.On("ListPodSandbox", mock.Anything).Return([]*v1.PodSandbox{}, nil) |
| 62 | + m.On("ListContainers", (*v1.ContainerFilter)(nil)).Return([]*v1.Container{container}, nil) |
| 63 | + m.On("ContainerStatus", "cid1").Return(status, nil) |
| 64 | + m.On("StopContainer", "cid1", int64(0)).Return(errors.New("fail stop container")).Times(3) |
| 65 | + c := &containerd.Client{Runtime: m} |
| 66 | + err := c.RemovePods() |
| 67 | + assert.NoError(t, err, "RemovePods should ignore container stop errors") |
| 68 | + m.AssertExpectations(t) |
| 69 | + }) |
| 70 | + |
| 71 | + t.Run("all success", func(t *testing.T) { |
| 72 | + m := new(mocks.MockRuntimeService) |
| 73 | + pod := &v1.PodSandbox{Metadata: &v1.PodSandboxMetadata{Name: "pod1"}, Id: "pod1id"} |
| 74 | + container := &v1.Container{Metadata: &v1.ContainerMetadata{Name: "c1"}, Id: "cid1"} |
| 75 | + status := &v1.ContainerStatus{State: v1.ContainerState_CONTAINER_EXITED} |
| 76 | + m.On("ListPodSandbox", mock.Anything).Return([]*v1.PodSandbox{pod}, nil) |
| 77 | + m.On("StopPodSandbox", "pod1id").Return(nil) |
| 78 | + m.On("RemovePodSandbox", "pod1id").Return(nil) |
| 79 | + m.On("ListContainers", (*v1.ContainerFilter)(nil)).Return([]*v1.Container{container}, nil) |
| 80 | + m.On("ContainerStatus", "cid1").Return(status, nil) |
| 81 | + m.On("RemoveContainer", "cid1").Return(nil) |
| 82 | + c := &containerd.Client{Runtime: m} |
| 83 | + err := c.RemovePods() |
| 84 | + assert.NoError(t, err) |
| 85 | + m.AssertExpectations(t) |
| 86 | + }) |
| 87 | +} |
0 commit comments