Skip to content

Commit

Permalink
test(imagesset): adds tests for AddImages
Browse files Browse the repository at this point in the history
  • Loading branch information
vitorfhc committed Mar 28, 2022
1 parent 0d2eef0 commit 4d75a60
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
13 changes: 13 additions & 0 deletions pkg/docker/imagesset.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package docker

import "errors"

// ImagesSet is a set of images. It maps image IDs to images.
type ImagesSet struct {
size int
mapping map[string]*Image
Expand All @@ -13,6 +14,8 @@ func init() {
globalImagesSet = NewImagesSet()
}

// NewImagesSet returns a new instance of ImagesSet.
// You may pass initial images to the constructor.
func NewImagesSet(images ...*Image) *ImagesSet {
set := &ImagesSet{
mapping: make(map[string]*Image),
Expand All @@ -23,6 +26,8 @@ func NewImagesSet(images ...*Image) *ImagesSet {
return set
}

// AddImages adds images to the set.
// It returns an error if there are any ID collisions.
func (is *ImagesSet) AddImages(imgs ...*Image) error {
for _, img := range imgs {
_, ok := is.mapping[img.Config.ID]
Expand All @@ -35,27 +40,35 @@ func (is *ImagesSet) AddImages(imgs ...*Image) error {
return nil
}

// FindImage searches for an image ID and returns (*Image, true)
// if found. If not found it returns (nil, false).
func (is *ImagesSet) FindImage(id string) (*Image, bool) {
img, ok := is.mapping[id]
return img, ok
}

// IterateImages iterates over all images in the set.
func (is *ImagesSet) IterateImages(f func(string, *Image)) {
for id, img := range is.mapping {
f(id, img)
}
}

// AddImages adds images to the global set.
// It returns an error if there are any ID collisions.
func AddImages(imgs ...*Image) error {
is := globalImagesSet
return is.AddImages(imgs...)
}

// FindImage searches in the global set for an image ID and returns (*Image, true)
// if found. If not found it returns (nil, false).
func FindImage(id string) (*Image, bool) {
is := globalImagesSet
return is.FindImage(id)
}

// IterateImages iterates over all images in the global set.
func IterateImages(f func(string, *Image)) {
is := globalImagesSet
is.IterateImages(f)
Expand Down
47 changes: 47 additions & 0 deletions pkg/docker/imagesset_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package docker

import "testing"

func TestImagesSet_AddImages(t *testing.T) {
tests := []struct {
name string
imagesAdded []map[string]interface{}
imagesToAdd []map[string]interface{}
wantErr bool
}{
{
name: "adds images without error",
imagesAdded: []map[string]interface{}{},
imagesToAdd: []map[string]interface{}{
{"id": "1"},
},
wantErr: false,
},
{
name: "adds duplicated images",
imagesAdded: []map[string]interface{}{
{"id": "1"},
},
imagesToAdd: []map[string]interface{}{
{"id": "1"},
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
imagesAdded := make([]*Image, len(tt.imagesAdded))
for i, img := range tt.imagesAdded {
imagesAdded[i], _ = NewImage(img)
}
imagesToAdd := make([]*Image, len(tt.imagesToAdd))
for i, img := range tt.imagesToAdd {
imagesToAdd[i], _ = NewImage(img)
}
is := NewImagesSet(imagesAdded...)
if err := is.AddImages(imagesToAdd...); (err != nil) != tt.wantErr {
t.Errorf("ImagesSet.AddImages() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

0 comments on commit 4d75a60

Please sign in to comment.