Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit test for findRunImage #3560

Merged
merged 1 commit into from
Jan 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/skaffold/build/buildpacks/lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (b *Builder) build(ctx context.Context, out io.Writer, a *latest.Artifact,
// If ForcePull is true: we will let `pack` always pull.
// Ideally, we add a `--pullIfNotPresent` option to upstream `pack`.
var err error
runImage, err = b.findRunImage(ctx, artifact, builderImage)
runImage, err = b.findRunImage(ctx, artifact)
if err != nil {
return "", err
}
Expand Down
10 changes: 6 additions & 4 deletions pkg/skaffold/build/buildpacks/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"context"
"encoding/json"

"github.com/pkg/errors"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
)

Expand All @@ -36,20 +38,20 @@ type metadata struct {
}

// TODO(dgageot): mirrors
func (b *Builder) findRunImage(ctx context.Context, a *latest.BuildpackArtifact, builder string) (string, error) {
func (b *Builder) findRunImage(ctx context.Context, a *latest.BuildpackArtifact) (string, error) {
if a.RunImage != "" {
return a.RunImage, nil
}

cfg, err := b.localDocker.ConfigFile(ctx, builder)
cfg, err := b.localDocker.ConfigFile(ctx, a.Builder)
if err != nil {
return "", err
return "", errors.Wrapf(err, "unable to find image %q", a.Builder)
}

var m metadata
label := cfg.Config.Labels["io.buildpacks.builder.metadata"]
if err := json.Unmarshal([]byte(label), &m); err != nil {
return "", err
return "", errors.Wrapf(err, "unable to decode image labels for %q", a.Builder)
}

return m.Stack.RunImage.Image, nil
Expand Down
112 changes: 112 additions & 0 deletions pkg/skaffold/build/buildpacks/metadata_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
Copyright 2019 The Skaffold Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package buildpacks

import (
"context"
"errors"
"testing"

v1 "github.com/google/go-containerregistry/pkg/v1"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
"github.com/GoogleContainerTools/skaffold/testutil"
)

type fakeDocker struct {
docker.LocalDaemon

configFiles map[string]*v1.ConfigFile
}

func (f *fakeDocker) ConfigFile(ctx context.Context, image string) (*v1.ConfigFile, error) {
if configFile, present := f.configFiles[image]; present {
return configFile, nil
}
return nil, errors.New("Not found")
}

func TestFindRunImage(t *testing.T) {
tests := []struct {
description string
artifact *latest.BuildpackArtifact
expectedRunImage string
expectedError string
}{
{
description: "user specified run image",
artifact: &latest.BuildpackArtifact{
RunImage: "custom-image/run",
},
expectedRunImage: "custom-image/run",
},
{
description: "default run image",
artifact: &latest.BuildpackArtifact{
Builder: "image/build",
},
expectedRunImage: "image/run",
},
{
description: "unable to find image",
artifact: &latest.BuildpackArtifact{
Builder: "unknown",
},
expectedError: `unable to find image "unknown"`,
},
{
description: "invalid-labels",
artifact: &latest.BuildpackArtifact{
Builder: "invalid-labels",
},
expectedError: `unable to decode image labels for "invalid-labels"`,
},
}
for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
localDocker := &fakeDocker{
configFiles: map[string]*v1.ConfigFile{
"image/build": {
Config: v1.Config{
Labels: map[string]string{
"io.buildpacks.builder.metadata": `{"stack":{"runImage":{"image": "image/run"}}}`,
},
},
},
"invalid-labels": {
Config: v1.Config{
Labels: map[string]string{
"io.buildpacks.builder.metadata": "invalid",
},
},
},
},
}

builder := NewArtifactBuilder(localDocker, false)
runImage, err := builder.findRunImage(context.Background(), test.artifact)

if test.expectedError == "" {
t.CheckNoError(err)
t.CheckDeepEqual(test.expectedRunImage, runImage)
} else {
t.CheckErrorContains(test.expectedError, err)
}
})
}
}