|
| 1 | +/* |
| 2 | +Copyright 2021 The Skaffold Authors |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package integration |
| 18 | + |
| 19 | +import ( |
| 20 | + "bytes" |
| 21 | + "context" |
| 22 | + "fmt" |
| 23 | + "io/ioutil" |
| 24 | + stdlog "log" |
| 25 | + "net/http/httptest" |
| 26 | + "path/filepath" |
| 27 | + "runtime" |
| 28 | + "strings" |
| 29 | + "testing" |
| 30 | + |
| 31 | + "github.com/google/go-cmp/cmp" |
| 32 | + "github.com/google/go-containerregistry/pkg/crane" |
| 33 | + "github.com/google/go-containerregistry/pkg/registry" |
| 34 | + "github.com/google/go-containerregistry/pkg/v1/random" |
| 35 | + |
| 36 | + "github.com/GoogleContainerTools/skaffold/pkg/skaffold/build/ko" |
| 37 | + "github.com/GoogleContainerTools/skaffold/pkg/skaffold/config" |
| 38 | + latestV1 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest/v1" |
| 39 | +) |
| 40 | + |
| 41 | +func TestBuildAndPushKoImageProgrammatically(t *testing.T) { |
| 42 | + MarkIntegrationTest(t, CanRunWithoutGcp) |
| 43 | + |
| 44 | + // Start a local registry server. |
| 45 | + // This registry hosts the base image, and it is the target registry for the built image. |
| 46 | + baseimageNamespace := "baseimage" |
| 47 | + registryServer, err := registryServerWithImage(baseimageNamespace) |
| 48 | + if err != nil { |
| 49 | + t.Fatalf("could not create test registry server: %v", err) |
| 50 | + } |
| 51 | + defer registryServer.Close() |
| 52 | + registryAddr := registryServer.Listener.Addr().String() |
| 53 | + baseImage := fmt.Sprintf("%s/%s", registryAddr, baseimageNamespace) |
| 54 | + |
| 55 | + // Get the directory of the basic ko sample app from the `examples` directory. |
| 56 | + exampleAppDir, err := koExampleAppDir() |
| 57 | + if err != nil { |
| 58 | + t.Fatalf("could not get ko example app dir: %+v", err) |
| 59 | + } |
| 60 | + |
| 61 | + // Build the artifact |
| 62 | + b := ko.NewArtifactBuilder(nil, true, config.RunModes.Build, nil) |
| 63 | + var imageFullNameBuffer bytes.Buffer |
| 64 | + artifact := &latestV1.Artifact{ |
| 65 | + ArtifactType: latestV1.ArtifactType{ |
| 66 | + KoArtifact: &latestV1.KoArtifact{ |
| 67 | + BaseImage: baseImage, |
| 68 | + }, |
| 69 | + }, |
| 70 | + Workspace: exampleAppDir, |
| 71 | + } |
| 72 | + imageName := fmt.Sprintf("%s/%s", registryAddr, "skaffold-ko") |
| 73 | + digest, err := b.Build(context.Background(), &imageFullNameBuffer, artifact, imageName) |
| 74 | + if err != nil { |
| 75 | + t.Fatalf("b.Build(): %+v", err) |
| 76 | + } |
| 77 | + |
| 78 | + wantImageFullName := fmt.Sprintf("%s@%s", imageName, digest) |
| 79 | + gotImageFullName := strings.TrimSuffix(imageFullNameBuffer.String(), "\n") |
| 80 | + if diff := cmp.Diff(wantImageFullName, gotImageFullName); diff != "" { |
| 81 | + t.Errorf("image name mismatch (-want +got):\n%s", diff) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +// registryServerWithImage starts a local registry and pushes a random image. |
| 86 | +// Use this to speed up tests, by not having to reach out to a real registry. |
| 87 | +// The registry uses a NOP logger to avoid spamming test logs. |
| 88 | +// Remember to call `defer Close()` on the returned `httptest.Server`. |
| 89 | +func registryServerWithImage(namespace string) (*httptest.Server, error) { |
| 90 | + nopLog := stdlog.New(ioutil.Discard, "", 0) |
| 91 | + r := registry.New(registry.Logger(nopLog)) |
| 92 | + s := httptest.NewServer(r) |
| 93 | + imageName := fmt.Sprintf("%s/%s", s.Listener.Addr().String(), namespace) |
| 94 | + image, err := random.Image(1024, 1) |
| 95 | + if err != nil { |
| 96 | + return nil, fmt.Errorf("random.Image(): %+v", err) |
| 97 | + } |
| 98 | + err = crane.Push(image, imageName) |
| 99 | + if err != nil { |
| 100 | + return nil, fmt.Errorf("crane.Push(): %+v", err) |
| 101 | + } |
| 102 | + return s, nil |
| 103 | +} |
| 104 | + |
| 105 | +// koExampleAppDir returns the directory path of the basic ko builder sample app. |
| 106 | +func koExampleAppDir() (string, error) { |
| 107 | + _, filename, _, ok := runtime.Caller(0) |
| 108 | + if !ok { |
| 109 | + return "", fmt.Errorf("could not get current filename") |
| 110 | + } |
| 111 | + basepath := filepath.Dir(filename) |
| 112 | + exampleDir, err := filepath.Abs(filepath.Join(basepath, "examples", "ko")) |
| 113 | + if err != nil { |
| 114 | + return "", fmt.Errorf("could not get absolute path of example from basepath %q: %w", basepath, err) |
| 115 | + } |
| 116 | + return exampleDir, nil |
| 117 | +} |
0 commit comments