diff --git a/CHANGELOG.md b/CHANGELOG.md index 1892acdf..910f304e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - [#518](https://github.com/spegel-org/spegel/pull/518) Extend tests for image. +- [#519](https://github.com/spegel-org/spegel/pull/519) Extend tests for containerd. ### Deprecated diff --git a/pkg/oci/containerd.go b/pkg/oci/containerd.go index 2c98d9bc..776a6be3 100644 --- a/pkg/oci/containerd.go +++ b/pkg/oci/containerd.go @@ -423,9 +423,12 @@ func getContentFilter(labels map[string]string) (string, error) { } func getEventImage(e typeurl.Any) (string, EventType, error) { + if e == nil { + return "", "", errors.New("any cannot be nil") + } evt, err := typeurl.UnmarshalAny(e) if err != nil { - return "", UnknownEvent, fmt.Errorf("failed to unmarshalany: %w", err) + return "", "", fmt.Errorf("failed to unmarshal any: %w", err) } switch e := evt.(type) { case *eventtypes.ImageCreate: @@ -435,7 +438,7 @@ func getEventImage(e typeurl.Any) (string, EventType, error) { case *eventtypes.ImageDelete: return e.Name, DeleteEvent, nil default: - return "", UnknownEvent, errors.New("unsupported event") + return "", "", errors.New("unsupported event type") } } diff --git a/pkg/oci/containerd_test.go b/pkg/oci/containerd_test.go index d89b06d7..7a6fcd05 100644 --- a/pkg/oci/containerd_test.go +++ b/pkg/oci/containerd_test.go @@ -7,11 +7,28 @@ import ( "net/url" "testing" + eventtypes "github.com/containerd/containerd/api/events" + "github.com/containerd/typeurl/v2" + ocispec "github.com/opencontainers/image-spec/specs-go/v1" "github.com/spf13/afero" "github.com/stretchr/testify/require" runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1" ) +func TestNewContainerd(t *testing.T) { + t.Parallel() + + c, err := NewContainerd("socket", "namespace", "foo", nil) + require.NoError(t, err) + require.Empty(t, c.contentPath) + require.Nil(t, c.client) + require.Equal(t, "foo", c.registryConfigPath) + + c, err = NewContainerd("socket", "namespace", "foo", nil, WithContentPath("local")) + require.NoError(t, err) + require.Equal(t, "local", c.contentPath) +} + func TestVerifyStatusResponse(t *testing.T) { t.Parallel() @@ -56,7 +73,6 @@ func TestVerifyStatusResponse(t *testing.T) { expectedErrMsg: "Containerd discard unpacked layers cannot be enabled", }, } - for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { t.Parallel() @@ -98,7 +114,6 @@ func TestCreateFilter(t *testing.T) { expectedEventFilter: `topic~="/images/create|/images/update|/images/delete",event.name~="^(docker\\.io|gcr\\.io)/"`, }, } - for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { t.Parallel() @@ -110,6 +125,112 @@ func TestCreateFilter(t *testing.T) { } } +func TestGetEventImage(t *testing.T) { + t.Parallel() + + tests := []struct { + name string + data interface{} + expectedErr string + expectedName string + expectedEventType EventType + }{ + { + name: "type url is nil", + data: nil, + expectedErr: "any cannot be nil", + }, + { + name: "unknown event", + data: &eventtypes.ContainerCreate{}, + expectedErr: "unsupported event type", + }, + { + name: "create event", + data: &eventtypes.ImageCreate{ + Name: "create", + }, + expectedName: "create", + expectedEventType: CreateEvent, + }, + { + name: "update event", + data: &eventtypes.ImageUpdate{ + Name: "update", + }, + expectedName: "update", + expectedEventType: UpdateEvent, + }, + { + name: "delete event", + data: &eventtypes.ImageDelete{ + Name: "delete", + }, + expectedName: "delete", + expectedEventType: DeleteEvent, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + var e typeurl.Any + var err error + if tt.data != nil { + e, err = typeurl.MarshalAny(tt.data) + require.NoError(t, err) + } + + name, event, err := getEventImage(e) + if tt.expectedErr != "" { + require.EqualError(t, err, tt.expectedErr) + return + } + require.NoError(t, err) + require.Equal(t, tt.expectedName, name) + require.Equal(t, tt.expectedEventType, event) + }) + } +} + +func TestIsImageConfig(t *testing.T) { + t.Parallel() + + tests := []struct { + name string + cImg ocispec.Image + expected bool + }{ + { + name: "image with required fields set", + cImg: ocispec.Image{ + Platform: ocispec.Platform{ + Architecture: "dummy", + OS: "dummy", + }, + RootFS: ocispec.RootFS{ + Type: "dummy", + }, + }, + expected: true, + }, + { + name: "image with other fields setk", + cImg: ocispec.Image{}, + expected: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + ok := isImageConfig(tt.cImg) + require.Equal(t, tt.expected, ok) + }) + + } +} + func TestMirrorConfiguration(t *testing.T) { t.Parallel() diff --git a/pkg/oci/image.go b/pkg/oci/image.go index c85ee12f..efd29c75 100644 --- a/pkg/oci/image.go +++ b/pkg/oci/image.go @@ -21,10 +21,9 @@ type Image struct { type EventType string const ( - CreateEvent EventType = "CREATE" - UpdateEvent EventType = "UPDATE" - DeleteEvent EventType = "DELETE" - UnknownEvent EventType = "" + CreateEvent EventType = "CREATE" + UpdateEvent EventType = "UPDATE" + DeleteEvent EventType = "DELETE" ) type ImageEvent struct {