2021-05-26 04:44:52 +10:00
|
|
|
/*
|
|
|
|
Copyright 2021 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-08-28 02:52:01 +10:00
|
|
|
"fmt"
|
2021-05-26 04:44:52 +10:00
|
|
|
"testing"
|
|
|
|
|
2021-08-28 02:52:01 +10:00
|
|
|
"github.com/google/go-containerregistry/pkg/crane"
|
2021-07-02 17:40:56 +02:00
|
|
|
"github.com/google/ko/pkg/build"
|
2021-05-26 04:44:52 +10:00
|
|
|
"github.com/google/ko/pkg/commands/options"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestOverrideDefaultBaseImageUsingBuildOption(t *testing.T) {
|
2021-08-28 02:52:01 +10:00
|
|
|
namespace := "base"
|
|
|
|
s, err := registryServerWithImage(namespace)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not create test registry server: %v", err)
|
|
|
|
}
|
|
|
|
defer s.Close()
|
|
|
|
baseImage := fmt.Sprintf("%s/%s", s.Listener.Addr().String(), namespace)
|
|
|
|
wantDigest, err := crane.Digest(baseImage)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("crane.Digest(%s): %v", baseImage, err)
|
|
|
|
}
|
|
|
|
wantImage := fmt.Sprintf("%s@%s", baseImage, wantDigest)
|
2021-05-26 04:44:52 +10:00
|
|
|
bo := &options.BuildOptions{
|
|
|
|
BaseImage: wantImage,
|
|
|
|
}
|
|
|
|
|
|
|
|
baseFn := getBaseImage("all", bo)
|
2021-07-20 15:06:08 -05:00
|
|
|
_, res, err := baseFn(context.Background(), "ko://example.com/helloworld")
|
2021-05-26 04:44:52 +10:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("getBaseImage(): %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
digest, err := res.Digest()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("res.Digest(): %v", err)
|
|
|
|
}
|
|
|
|
gotDigest := digest.String()
|
|
|
|
if gotDigest != wantDigest {
|
|
|
|
t.Errorf("got digest %s, wanted %s", gotDigest, wantDigest)
|
|
|
|
}
|
|
|
|
}
|
2021-07-02 17:40:56 +02:00
|
|
|
|
|
|
|
func TestCreateBuildConfigs(t *testing.T) {
|
|
|
|
compare := func(expected string, actual string) {
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("test case failed: expected '%#v', but actual value is '%#v'", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
buildConfigs := []build.Config{
|
|
|
|
{ID: "defaults"},
|
|
|
|
{ID: "OnlyMain", Main: "test"},
|
|
|
|
{ID: "OnlyMainWithFile", Main: "test/main.go"},
|
|
|
|
{ID: "OnlyDir", Dir: "test"},
|
|
|
|
{ID: "DirAndMain", Dir: "test", Main: "main.go"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, b := range buildConfigs {
|
|
|
|
for importPath, buildCfg := range createBuildConfigs("../..", []build.Config{b}) {
|
|
|
|
switch buildCfg.ID {
|
|
|
|
case "defaults":
|
|
|
|
compare("github.com/google/ko", importPath)
|
|
|
|
|
|
|
|
case "OnlyMain", "OnlyMainWithFile", "OnlyDir", "DirAndMain":
|
|
|
|
compare("github.com/google/ko/test", importPath)
|
|
|
|
|
|
|
|
default:
|
|
|
|
t.Fatalf("unknown test case: %s", buildCfg.ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|