Skip to content

Commit

Permalink
[eclipse-kanto#53] Unit test the resources clean up implementation
Browse files Browse the repository at this point in the history
Aligned the containerd SPI API.

Signed-off-by: Konstantina Gramatova <[email protected]>
  • Loading branch information
konstantina-gramatova committed Sep 19, 2022
1 parent 0b706c0 commit 0dd0b8a
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 deletions.
4 changes: 2 additions & 2 deletions containerm/ctr/ctrd_spi.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ type containerdSpi interface {
UnpackImage(ctx context.Context, image containerd.Image, opts ...containerd.UnpackOpt) error
// DeleteImage removes the contents of the provided image from the disk
DeleteImage(ctx context.Context, imageRef string) error
// ListImages returns all locally existing images
ListImages(ctx context.Context) ([]containerd.Image, error)
// ListImages returns all locally existing images matching the provided filters or all if no filters are provided
ListImages(ctx context.Context, filters ...string) ([]containerd.Image, error)

// Wrapper section for managing the file system of the container and its snapshots
// GetSnapshotID generates a new ID for the snapshot to be used for this container
Expand Down
4 changes: 2 additions & 2 deletions containerm/ctr/ctrd_spi_images.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ func (spi *ctrdSpi) UnpackImage(ctx context.Context, image containerd.Image, opt
}

// ListImages returns all locally existing images
func (spi *ctrdSpi) ListImages(ctx context.Context) ([]containerd.Image, error) {
func (spi *ctrdSpi) ListImages(ctx context.Context, filters ...string) ([]containerd.Image, error) {
ctx = spi.setContext(ctx, false)
return spi.client.ListImages(ctx)
return spi.client.ListImages(ctx, filters...)
}

// DeleteImage removes the contents of the provided image from the disk
Expand Down
7 changes: 4 additions & 3 deletions containerm/ctr/ctrd_spi_images_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ func TestUnpackImage(t *testing.T) {
func TestListImages(t *testing.T) {
const (
testNamespace = "test-ns"
testFilter = "name=test.img/ref:latest"
)

testCases := map[string]struct {
Expand All @@ -197,14 +198,14 @@ func TestListImages(t *testing.T) {
"test_no_err": {
mockExec: func(ctx context.Context, ctrdWrapperMock *ctrdMocks.MockcontainerClientWrapper, imageMock *containerdMocks.MockImage) ([]containerd.Image, error) {
res := []containerd.Image{imageMock}
ctrdWrapperMock.EXPECT().ListImages(ctx).Return(res, nil)
ctrdWrapperMock.EXPECT().ListImages(ctx, testFilter).Return(res, nil)
return res, nil
},
},
"test_err": {
mockExec: func(ctx context.Context, ctrdWrapperMock *ctrdMocks.MockcontainerClientWrapper, imageMock *containerdMocks.MockImage) ([]containerd.Image, error) {
err := log.NewError("test pull image error")
ctrdWrapperMock.EXPECT().ListImages(ctx).Return(nil, err)
ctrdWrapperMock.EXPECT().ListImages(ctx, testFilter).Return(nil, err)
return nil, err
},
},
Expand All @@ -230,7 +231,7 @@ func TestListImages(t *testing.T) {
expectedImgs, expectedErr := testData.mockExec(namespaces.WithNamespace(ctx, testNamespace), mockCtrdWrapper, mockImg)

// test
imgs, actualErr := testSpi.ListImages(ctx)
imgs, actualErr := testSpi.ListImages(ctx, testFilter)
testutil.AssertError(t, expectedErr, actualErr)
testutil.AssertEqual(t, expectedImgs, imgs)
})
Expand Down
13 changes: 9 additions & 4 deletions containerm/pkg/testutil/mocks/ctrd/mock_ctrd_spi.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0dd0b8a

Please sign in to comment.