Skip to content

Commit

Permalink
Extend tests for containerd
Browse files Browse the repository at this point in the history
  • Loading branch information
phillebaba committed Jun 20, 2024
1 parent da65403 commit 8342cd5
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 5 additions & 2 deletions pkg/oci/containerd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Check warning on line 431 in pkg/oci/containerd.go

View check run for this annotation

Codecov / codecov/patch

pkg/oci/containerd.go#L431

Added line #L431 was not covered by tests
}
switch e := evt.(type) {
case *eventtypes.ImageCreate:
Expand All @@ -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")
}
}

Expand Down
125 changes: 123 additions & 2 deletions pkg/oci/containerd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand All @@ -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()

Expand Down
7 changes: 3 additions & 4 deletions pkg/oci/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 8342cd5

Please sign in to comment.