Files
kaniko/pkg/executor/push_test.go
Joël Pepper 14ea7c4071 Fix Integration tests (#2425)
* fix(ci): Bump golangci-lint to 1.51.1

* chore(lint): fix gofmt and goimport issues

* chore(lint): fix linter issues

- Adapted error comparison according to linter recommendation
- Disabled noctx linting for http request where canceling makes no sense
- Disabled nilerror linting where nil error is returned on purpose
- Disabled makezero linter where slice is explicitly deepcopied

* chore(ci): Update go version in tests workflows

* fix(ci): Allow boilerplate years from 2000-2099

Previously the regex only allowed the copyright notice to contain the
years 2018,2019,2020,2021, or 2022. This commit widens to regex to
20\d\d allowing any year in the range [2000-2099]

* feat(ci): Replace minikube with k3s for intregration tests

The existing setup for minikube is very complicated, replicating most of
the setup steps for a full kubernetes cluster in an only partially
supported minikube configuration (driver=none). Furthermore the existing
setup has been broken for sometime, likely, at least in part due to the
changes to CNI and CRI in recent kubernetes versions.

Since what we actually need is only a running Kubernetes cluster on the
node and access to a registry on localhost:5000, we can switch the
extremely complicated minikube setup for a lightweight cluster using
k3s. Minikube came with a default addon for running a registry on every
node, but the same is not the case for k3s, instead we make use of the
package helm controller and its HelmChart CR to deploy twuni/docker-registry.helm
and expose it on localhost using the integrated LoadBalancer controller.

* fix(test-684): pin base container version

The dockerfile for the regression test connected to issue 684 used a
rolling tag as base image, making it flaky and fail since it was
introduced.

This commit pins the base image to the digest of bionic-20200219, which,
based on the date of the commit that introduced to the dockerfile would
be the most newest ubuntu build and likely what the "rolling" tag
resolved to back then. Since this also an image from the pre-oci days of
ubuntu, this circumvents a bug in container-diff as well
(https://github.com/GoogleContainerTools/container-diff/issues/389)
2023-03-21 12:30:54 -04:00

378 lines
11 KiB
Go

/*
Copyright 2018 Google LLC
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 executor
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"testing"
"github.com/GoogleContainerTools/kaniko/pkg/config"
"github.com/GoogleContainerTools/kaniko/pkg/util"
"github.com/GoogleContainerTools/kaniko/testutil"
"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/name"
"github.com/google/go-containerregistry/pkg/v1/layout"
"github.com/google/go-containerregistry/pkg/v1/random"
"github.com/google/go-containerregistry/pkg/v1/validate"
"github.com/spf13/afero"
)
func mustTag(t *testing.T, s string) name.Tag {
tag, err := name.NewTag(s, name.StrictValidation)
if err != nil {
t.Fatalf("NewTag: %v", err)
}
return tag
}
func TestWriteImageOutputs(t *testing.T) {
img, err := random.Image(1024, 3)
if err != nil {
t.Fatalf("random.Image: %v", err)
}
d, err := img.Digest()
if err != nil {
t.Fatalf("Digest: %v", err)
}
for _, c := range []struct {
desc, env string
tags []name.Tag
want string
}{{
desc: "env unset, no output",
env: "",
}, {
desc: "env set, one tag",
env: "/foo",
tags: []name.Tag{mustTag(t, "gcr.io/foo/bar:latest")},
want: fmt.Sprintf(`{"name":"gcr.io/foo/bar:latest","digest":%q}
`, d),
}, {
desc: "env set, two tags",
env: "/foo",
tags: []name.Tag{
mustTag(t, "gcr.io/foo/bar:latest"),
mustTag(t, "gcr.io/baz/qux:latest"),
},
want: fmt.Sprintf(`{"name":"gcr.io/foo/bar:latest","digest":%q}
{"name":"gcr.io/baz/qux:latest","digest":%q}
`, d, d),
}} {
t.Run(c.desc, func(t *testing.T) {
fs = afero.NewMemMapFs()
if c.want == "" {
fs = afero.NewReadOnlyFs(fs) // No files should be written.
}
os.Setenv("BUILDER_OUTPUT", c.env)
if err := writeImageOutputs(img, c.tags); err != nil {
t.Fatalf("writeImageOutputs: %v", err)
}
if c.want == "" {
return
}
b, err := afero.ReadFile(fs, filepath.Join(c.env, "images"))
if err != nil {
t.Fatalf("ReadFile: %v", err)
}
if got := string(b); got != c.want {
t.Fatalf(" got: %s\nwant: %s", got, c.want)
}
})
}
}
func TestHeaderAdded(t *testing.T) {
tests := []struct {
name string
upstream string
expected string
}{{
name: "upstream env variable set",
upstream: "skaffold-v0.25.45",
expected: "kaniko/unset,skaffold-v0.25.45",
}, {
name: "upstream env variable not set",
expected: "kaniko/unset",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
rt := &withUserAgent{t: &mockRoundTripper{}}
if test.upstream != "" {
os.Setenv("UPSTREAM_CLIENT_TYPE", test.upstream)
defer func() { os.Unsetenv("UPSTREAM_CLIENT_TYPE") }()
}
req, err := http.NewRequest("GET", "dummy", nil) //nolint:noctx
if err != nil {
t.Fatalf("culd not create a req due to %s", err)
}
resp, err := rt.RoundTrip(req)
testutil.CheckError(t, false, err)
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
testutil.CheckErrorAndDeepEqual(t, false, err, test.expected, string(body))
})
}
}
type mockRoundTripper struct {
}
func (m *mockRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) {
ua := r.UserAgent()
return &http.Response{Body: ioutil.NopCloser(bytes.NewBufferString(ua))}, nil
}
func TestOCILayoutPath(t *testing.T) {
tmpDir := t.TempDir()
image, err := random.Image(1024, 4)
if err != nil {
t.Fatalf("could not create image: %s", err)
}
digest, err := image.Digest()
if err != nil {
t.Fatalf("could not get image digest: %s", err)
}
want, err := image.Manifest()
if err != nil {
t.Fatalf("could not get image manifest: %s", err)
}
opts := config.KanikoOptions{
NoPush: true,
OCILayoutPath: tmpDir,
}
if err := DoPush(image, &opts); err != nil {
t.Fatalf("could not push image: %s", err)
}
layoutIndex, err := layout.ImageIndexFromPath(tmpDir)
if err != nil {
t.Fatalf("could not get index from layout: %s", err)
}
testutil.CheckError(t, false, validate.Index(layoutIndex))
layoutImage, err := layoutIndex.Image(digest)
if err != nil {
t.Fatalf("could not get image from layout: %s", err)
}
got, err := layoutImage.Manifest()
testutil.CheckErrorAndDeepEqual(t, false, err, want, got)
}
func TestImageNameDigestFile(t *testing.T) {
image, err := random.Image(1024, 4)
if err != nil {
t.Fatalf("could not create image: %s", err)
}
digest, err := image.Digest()
if err != nil {
t.Fatalf("could not get image digest: %s", err)
}
opts := config.KanikoOptions{
NoPush: true,
Destinations: []string{"gcr.io/foo/bar:latest", "bob/image"},
ImageNameDigestFile: "tmpFile",
}
defer os.Remove("tmpFile")
if err := DoPush(image, &opts); err != nil {
t.Fatalf("could not push image: %s", err)
}
want := []byte("gcr.io/foo/bar@" + digest.String() + "\nindex.docker.io/bob/image@" + digest.String() + "\n")
got, err := ioutil.ReadFile("tmpFile")
testutil.CheckErrorAndDeepEqual(t, false, err, want, got)
}
func TestImageNameTagDigestFile(t *testing.T) {
image, err := random.Image(1024, 4)
if err != nil {
t.Fatalf("could not create image: %s", err)
}
digest, err := image.Digest()
if err != nil {
t.Fatalf("could not get image digest: %s", err)
}
opts := config.KanikoOptions{
NoPush: true,
Destinations: []string{"gcr.io/foo/bar:123", "bob/image"},
ImageNameTagDigestFile: "tmpFile",
}
defer os.Remove("tmpFile")
if err := DoPush(image, &opts); err != nil {
t.Fatalf("could not push image: %s", err)
}
want := []byte("gcr.io/foo/bar:123@" + digest.String() + "\nindex.docker.io/bob/image:latest@" + digest.String() + "\n")
got, err := ioutil.ReadFile("tmpFile")
testutil.CheckErrorAndDeepEqual(t, false, err, want, got)
}
var checkPushPermsCallCount = 0
func resetCalledCount() {
checkPushPermsCallCount = 0
}
func fakeCheckPushPermission(ref name.Reference, kc authn.Keychain, t http.RoundTripper) error {
checkPushPermsCallCount++
return nil
}
func TestCheckPushPermissions(t *testing.T) {
tests := []struct {
description string
cacheRepo string
checkPushPermsExpectedCallCount int
destinations []string
existingConfig bool
noPush bool
noPushCache bool
}{
{description: "a gcr image without config", destinations: []string{"gcr.io/test-image"}, checkPushPermsExpectedCallCount: 1},
{description: "a gcr image with config", destinations: []string{"gcr.io/test-image"}, existingConfig: true, checkPushPermsExpectedCallCount: 1},
{description: "a pkg.dev image without config", destinations: []string{"us-docker.pkg.dev/test-image"}, checkPushPermsExpectedCallCount: 1},
{description: "a pkg.dev image with config", destinations: []string{"us-docker.pkg.dev/test-image"}, existingConfig: true, checkPushPermsExpectedCallCount: 1},
{description: "localhost registry without config", destinations: []string{"localhost:5000/test-image"}, checkPushPermsExpectedCallCount: 1},
{description: "localhost registry with config", destinations: []string{"localhost:5000/test-image"}, existingConfig: true, checkPushPermsExpectedCallCount: 1},
{description: "any other registry", destinations: []string{"notgcr.io/test-image"}, checkPushPermsExpectedCallCount: 1},
{
description: "multiple destinations pushed to different registry",
destinations: []string{
"us-central1-docker.pkg.dev/prj/test-image",
"us-west-docker.pkg.dev/prj/test-image",
},
checkPushPermsExpectedCallCount: 2,
},
{
description: "same image names with different tags",
destinations: []string{
"us-central1-docker.pkg.dev/prj/test-image:tag1",
"us-central1-docker.pkg.dev/prj/test-image:tag2",
},
checkPushPermsExpectedCallCount: 1,
},
{
description: "same destination image multiple times",
destinations: []string{
"us-central1-docker.pkg.dev/prj/test-image",
"us-central1-docker.pkg.dev/prj/test-image",
},
checkPushPermsExpectedCallCount: 1,
},
{
description: "no push and no push cache",
destinations: []string{"us-central1-docker.pkg.dev/prj/test-image"},
checkPushPermsExpectedCallCount: 0,
noPush: true,
noPushCache: true,
},
{
description: "no push and push cache",
destinations: []string{"us-central1-docker.pkg.dev/prj/test-image"},
cacheRepo: "us-central1-docker.pkg.dev/prj/cache-image",
checkPushPermsExpectedCallCount: 1,
noPush: true,
},
{
description: "no push and cache repo is OCI image layout",
destinations: []string{"us-central1-docker.pkg.dev/prj/test-image"},
cacheRepo: "oci:/some-layout-path",
checkPushPermsExpectedCallCount: 0,
noPush: true,
},
}
checkRemotePushPermission = fakeCheckPushPermission
for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
resetCalledCount()
fs = afero.NewMemMapFs()
opts := config.KanikoOptions{
CacheRepo: test.cacheRepo,
Destinations: test.destinations,
NoPush: test.noPush,
NoPushCache: test.noPushCache,
}
if test.existingConfig {
afero.WriteFile(fs, util.DockerConfLocation(), []byte(""), os.FileMode(0644))
defer fs.Remove(util.DockerConfLocation())
}
CheckPushPermissions(&opts)
if checkPushPermsCallCount != test.checkPushPermsExpectedCallCount {
t.Errorf("expected check push permissions call count to be %d but it was %d", test.checkPushPermsExpectedCallCount, checkPushPermsCallCount)
}
})
}
}
func TestHelperProcess(t *testing.T) {
if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" {
return
}
fmt.Fprintf(os.Stdout, "fake result")
os.Exit(0)
}
func TestWriteDigestFile(t *testing.T) {
tmpDir := t.TempDir()
t.Run("parent directory does not exist", func(t *testing.T) {
err := writeDigestFile(tmpDir+"/test/df", []byte("test"))
if err != nil {
t.Errorf("expected file to be written successfully, but got error: %v", err)
}
})
t.Run("parent directory exists", func(t *testing.T) {
err := writeDigestFile(tmpDir+"/df", []byte("test"))
if err != nil {
t.Errorf("expected file to be written successfully, but got error: %v", err)
}
})
}