image: add test
Signed-off-by: Yoan Blanc <yoan@dosimple.ch>
This commit is contained in:
parent
fcb055e581
commit
ce8298bb14
|
|
@ -118,11 +118,9 @@ func remoteImage(image string, opts *config.KanikoOptions) (v1.Image, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if !strings.ContainsRune(image, '/') {
|
ref, err = normalizeReference(ref, image)
|
||||||
ref, err = name.ParseReference("library/"+image, name.WeakValidation)
|
if err != nil {
|
||||||
if err != nil {
|
return nil, err
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
toSet = true
|
toSet = true
|
||||||
|
|
@ -142,16 +140,31 @@ func remoteImage(image string, opts *config.KanikoOptions) (v1.Image, error) {
|
||||||
tag.Repository.Registry = newReg
|
tag.Repository.Registry = newReg
|
||||||
ref = tag
|
ref = tag
|
||||||
}
|
}
|
||||||
|
|
||||||
if digest, ok := ref.(name.Digest); ok {
|
if digest, ok := ref.(name.Digest); ok {
|
||||||
digest.Repository.Registry = newReg
|
digest.Repository.Registry = newReg
|
||||||
ref = digest
|
ref = digest
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logrus.Infof("Retrieving image %s", ref)
|
||||||
|
|
||||||
rOpts := remoteOptions(registryName, opts)
|
rOpts := remoteOptions(registryName, opts)
|
||||||
return remote.Image(ref, rOpts...)
|
return remote.Image(ref, rOpts...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// normalizeReference adds the library/ prefix to images without it.
|
||||||
|
//
|
||||||
|
// It is mostly useful when using a registry mirror that is not able to perform
|
||||||
|
// this fix automatically.
|
||||||
|
func normalizeReference(ref name.Reference, image string) (name.Reference, error) {
|
||||||
|
if !strings.ContainsRune(image, '/') {
|
||||||
|
return name.ParseReference("library/"+image, name.WeakValidation)
|
||||||
|
}
|
||||||
|
|
||||||
|
return ref, nil
|
||||||
|
}
|
||||||
|
|
||||||
func remoteOptions(registryName string, opts *config.KanikoOptions) []remote.Option {
|
func remoteOptions(registryName string, opts *config.KanikoOptions) []remote.Option {
|
||||||
tr := util.MakeTransport(opts, registryName)
|
tr := util.MakeTransport(opts, registryName)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/google/go-containerregistry/pkg/name"
|
||||||
v1 "github.com/google/go-containerregistry/pkg/v1"
|
v1 "github.com/google/go-containerregistry/pkg/v1"
|
||||||
"github.com/google/go-containerregistry/pkg/v1/empty"
|
"github.com/google/go-containerregistry/pkg/v1/empty"
|
||||||
"github.com/moby/buildkit/frontend/dockerfile/instructions"
|
"github.com/moby/buildkit/frontend/dockerfile/instructions"
|
||||||
|
|
@ -109,6 +110,25 @@ func Test_ScratchImageFromMirror(t *testing.T) {
|
||||||
testutil.CheckErrorAndDeepEqual(t, false, err, expected, actual)
|
testutil.CheckErrorAndDeepEqual(t, false, err, expected, actual)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_normalizeReference(t *testing.T) {
|
||||||
|
image := "debian"
|
||||||
|
expected := "index.docker.io/library/debian:latest"
|
||||||
|
|
||||||
|
ref, err := name.ParseReference(image)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
ref2, err := normalizeReference(ref, image)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if ref2.Name() != ref.Name() || ref2.Name() != expected {
|
||||||
|
t.Errorf("%s should have been normalized to %s, got %s", ref2.Name(), expected, ref.Name())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// parse parses the contents of a Dockerfile and returns a list of commands
|
// parse parses the contents of a Dockerfile and returns a list of commands
|
||||||
func parse(s string) ([]instructions.Stage, error) {
|
func parse(s string) ([]instructions.Stage, error) {
|
||||||
p, err := parser.Parse(bytes.NewReader([]byte(s)))
|
p, err := parser.Parse(bytes.NewReader([]byte(s)))
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue