2019-03-14 20:23:47 +02:00
|
|
|
// Copyright 2018 Google LLC All Rights Reserved.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2019-11-05 22:24:08 +02:00
|
|
|
package testing
|
2019-03-14 20:23:47 +02:00
|
|
|
|
|
|
|
import (
|
2019-11-09 19:23:09 +02:00
|
|
|
"context"
|
2019-03-14 20:23:47 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/google/go-containerregistry/pkg/name"
|
|
|
|
v1 "github.com/google/go-containerregistry/pkg/v1"
|
|
|
|
"github.com/google/go-containerregistry/pkg/v1/random"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestFixedPublish(t *testing.T) {
|
|
|
|
hex1 := "deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef"
|
|
|
|
hex2 := "baadf00dbaadf00dbaadf00dbaadf00dbaadf00dbaadf00dbaadf00dbaadf00d"
|
2019-11-05 22:24:08 +02:00
|
|
|
fixedBaseRepo, _ := name.NewRepository("gcr.io/asdf")
|
|
|
|
f := NewFixedPublish(fixedBaseRepo, map[string]v1.Hash{
|
2019-03-14 20:23:47 +02:00
|
|
|
"foo": {
|
|
|
|
Algorithm: "sha256",
|
|
|
|
Hex: hex1,
|
|
|
|
},
|
|
|
|
"bar": {
|
|
|
|
Algorithm: "sha256",
|
|
|
|
Hex: hex2,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
fooDigest, err := f.Publish(nil, "foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Publish(foo) = %v", err)
|
|
|
|
}
|
|
|
|
if got, want := fooDigest.String(), "gcr.io/asdf/foo@sha256:"+hex1; got != want {
|
|
|
|
t.Errorf("Publish(foo) = %q, want %q", got, want)
|
|
|
|
}
|
|
|
|
|
|
|
|
barDigest, err := f.Publish(nil, "bar")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Publish(bar) = %v", err)
|
|
|
|
}
|
|
|
|
if got, want := barDigest.String(), "gcr.io/asdf/bar@sha256:"+hex2; got != want {
|
|
|
|
t.Errorf("Publish(bar) = %q, want %q", got, want)
|
|
|
|
}
|
|
|
|
|
|
|
|
d, err := f.Publish(nil, "baz")
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Publish(baz) = %v, want error", d)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFixedBuild(t *testing.T) {
|
2019-11-05 22:24:08 +02:00
|
|
|
testImage, _ := random.Image(1024, 5)
|
|
|
|
f := NewFixedBuild(map[string]v1.Image{
|
2019-03-14 20:23:47 +02:00
|
|
|
"asdf": testImage,
|
|
|
|
})
|
|
|
|
|
|
|
|
if got, want := f.IsSupportedReference("asdf"), true; got != want {
|
|
|
|
t.Errorf("IsSupportedReference(asdf) = %v, want %v", got, want)
|
|
|
|
}
|
2019-11-09 19:23:09 +02:00
|
|
|
if got, err := f.Build(context.Background(), "asdf"); err != nil {
|
2019-03-14 20:23:47 +02:00
|
|
|
t.Errorf("Build(asdf) = %v, want %v", err, testImage)
|
|
|
|
} else if got != testImage {
|
|
|
|
t.Errorf("Build(asdf) = %v, want %v", got, testImage)
|
|
|
|
}
|
|
|
|
|
|
|
|
if got, want := f.IsSupportedReference("blah"), false; got != want {
|
|
|
|
t.Errorf("IsSupportedReference(blah) = %v, want %v", got, want)
|
|
|
|
}
|
2019-11-09 19:23:09 +02:00
|
|
|
if got, err := f.Build(context.Background(), "blah"); err == nil {
|
2019-03-14 20:23:47 +02:00
|
|
|
t.Errorf("Build(blah) = %v, want error", got)
|
|
|
|
}
|
|
|
|
}
|