Skip to content

Commit

Permalink
[FIX]: import all requested images (#701, @mszostok)
Browse files Browse the repository at this point in the history
  • Loading branch information
mszostok authored Aug 18, 2021
1 parent 5d0d0ac commit 0c02607
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
10 changes: 7 additions & 3 deletions pkg/tools/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,11 @@ func ImageImportIntoClusterMulti(ctx context.Context, runtime runtimes.Runtime,

}

func findImages(ctx context.Context, runtime runtimes.Runtime, requestedImages []string) (imagesFromRuntime, imagesFromTar []string, err error) {
type runtimeImageGetter interface {
GetImages(context.Context) ([]string, error)
}

func findImages(ctx context.Context, runtime runtimeImageGetter, requestedImages []string) (imagesFromRuntime, imagesFromTar []string, err error) {
runtimeImages, err := runtime.GetImages(ctx)
if err != nil {
log.Errorln("Failed to fetch list of existing images from runtime")
Expand All @@ -189,14 +193,14 @@ func findImages(ctx context.Context, runtime runtimes.Runtime, requestedImages [
if isFile(requestedImage) {
imagesFromTar = append(imagesFromTar, requestedImage)
log.Debugf("Selected image '%s' is a file", requestedImage)
break
continue
}

runtimeImage, found := findRuntimeImage(requestedImage, runtimeImages)
if found {
imagesFromRuntime = append(imagesFromRuntime, runtimeImage)
log.Debugf("Selected image '%s' (found as '%s') in runtime", requestedImage, runtimeImage)
break
continue
}

log.Warnf("Image '%s' is not a file and couldn't be found in the container runtime", requestedImage)
Expand Down
46 changes: 46 additions & 0 deletions pkg/tools/tools_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ THE SOFTWARE.
package tools

import (
"context"
"io/ioutil"
"os"
"testing"

"github.com/go-test/deep"
)

func Test_findRuntimeImage(T *testing.T) {
Expand Down Expand Up @@ -170,3 +175,44 @@ func Test_findRuntimeImage(T *testing.T) {
})
}
}

func Test_findImages(t *testing.T) {
// given
tarImage, err := ioutil.TempFile("", "images.tgz")
if err != nil {
t.Fatal("Failed to create temporary file")
}
defer os.Remove(tarImage.Name())

tarImages := []string{tarImage.Name()}
runtimeImages := []string{
"alpine:version",
"busybox:latest",
}
runtime := &FakeRuntimeImageGetter{runtimeImages: runtimeImages}

requestedImages := append(runtimeImages, tarImages...)

// when
foundRuntimeImages, foundTarImages, err := findImages(context.Background(), runtime, requestedImages)

// then
if err != nil {
t.Errorf("Got unexpected error %v", err)
}

if diff := deep.Equal(foundRuntimeImages, runtimeImages); diff != nil {
t.Errorf("Found runtime images\n%+v\ndoes not match expected runtime images\n%+v\nDiff:\n%+v", foundRuntimeImages, runtimeImages, diff)
}
if diff := deep.Equal(foundTarImages, tarImages); diff != nil {
t.Errorf("Found tar images\n%+v\ndoes not match expected tar images\n%+v\nDiff:\n%+v", foundTarImages, runtimeImages, diff)
}
}

type FakeRuntimeImageGetter struct {
runtimeImages []string
}

func (f *FakeRuntimeImageGetter) GetImages(_ context.Context) ([]string, error) {
return f.runtimeImages, nil
}

0 comments on commit 0c02607

Please sign in to comment.