github.com/GoogleContainerTools/skaffold/v2@v2.13.2/integration/build_cluster_test.go (about)

     1  /*
     2  Copyright 2019 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  	"fmt"
    21  	"os"
    22  	"os/exec"
    23  	"path"
    24  	"path/filepath"
    25  	"strings"
    26  	"testing"
    27  
    28  	v1 "k8s.io/api/core/v1"
    29  
    30  	"github.com/GoogleContainerTools/skaffold/v2/integration/skaffold"
    31  	"github.com/GoogleContainerTools/skaffold/v2/testutil"
    32  )
    33  
    34  // run on GCP as this test requires a load balancer
    35  func TestBuildKanikoInsecureRegistry(t *testing.T) {
    36  	// TODO: This test shall pass once render v2 is completed.
    37  	t.SkipNow()
    38  
    39  	MarkIntegrationTest(t, NeedsGcp)
    40  
    41  	ns, client := SetupNamespace(t)
    42  
    43  	dir := "testdata/kaniko-insecure-registry"
    44  	skaffold.Run("-p", "deploy-insecure-registry").InDir(dir).InNs(ns.Name).RunOrFailOutput(t)
    45  
    46  	ip := client.ExternalIP("registry")
    47  	registry := fmt.Sprintf("%s:5000", ip)
    48  
    49  	skaffold.Build("--insecure-registry", registry, "-p", "build-artifact").WithRepo(registry).InDir(dir).InNs(ns.Name).RunOrFailOutput(t)
    50  }
    51  
    52  func TestBuildKanikoWithExplicitRepo(t *testing.T) {
    53  	// TODO: This test shall pass once render v2 is completed.
    54  	t.SkipNow()
    55  
    56  	MarkIntegrationTest(t, NeedsGcp)
    57  
    58  	// Other integration tests run with the --default-repo option.
    59  	// This one explicitly specifies the full image name.
    60  	skaffold.Build().WithRepo("").InDir("testdata/kaniko-explicit-repo").RunOrFail(t)
    61  }
    62  
    63  // see integration/testdata/README.md for details
    64  func TestBuildInCluster(t *testing.T) {
    65  	// TODO: This test shall pass once render v2 is completed.
    66  	t.SkipNow()
    67  
    68  	MarkIntegrationTest(t, NeedsGcp)
    69  
    70  	testutil.Run(t, "", func(t *testutil.T) {
    71  		ns, client := SetupNamespace(t.T)
    72  
    73  		// this workaround is to ensure there is no overlap between testcases on kokoro
    74  		// see https://github.com/GoogleContainerTools/skaffold/issues/2781#issuecomment-527770537
    75  		project, err := filepath.Abs("testdata/skaffold-in-cluster")
    76  		t.CheckNoError(err)
    77  
    78  		// copy the skaffold binary to the test case folder
    79  		// this is geared towards the in-docker setup: the fresh built binary is here
    80  		// for manual testing, we can override this temporarily
    81  		skaffoldSrc, err := exec.LookPath("skaffold")
    82  		t.CheckNoError(err)
    83  
    84  		t.NewTempDir().Chdir()
    85  		copyDir(t, project, ".")
    86  		copyFile(t, skaffoldSrc, "skaffold")
    87  
    88  		// TODO: until https://github.com/GoogleContainerTools/skaffold/issues/2757 is resolved,
    89  		// this is the simplest way to override the build.cluster.namespace
    90  		replaceNamespace(t, "skaffold.yaml", ns)
    91  		replaceNamespace(t, "build-step/kustomization.yaml", ns)
    92  
    93  		// we have to copy the e2esecret from default ns -> temporary namespace for kaniko
    94  		client.CreateSecretFrom("default", "e2esecret")
    95  
    96  		skaffold.Run("-p", "create-build-step").InNs(ns.Name).RunOrFail(t.T)
    97  
    98  		client.WaitForPodsInPhase(v1.PodSucceeded, "skaffold-in-cluster")
    99  	})
   100  }
   101  
   102  func replaceNamespace(t *testutil.T, fileName string, ns *v1.Namespace) {
   103  	origSkaffoldYaml, err := os.ReadFile(fileName)
   104  	if err != nil {
   105  		t.Fatalf("failed reading %s: %s", fileName, err)
   106  	}
   107  
   108  	namespacedYaml := strings.ReplaceAll(string(origSkaffoldYaml), "VAR_CLUSTER_NAMESPACE", ns.Name)
   109  
   110  	if err := os.WriteFile(fileName, []byte(namespacedYaml), 0666); err != nil {
   111  		t.Fatalf("failed to write %s: %s", fileName, err)
   112  	}
   113  }
   114  
   115  func copyFile(t *testutil.T, src, dst string) {
   116  	content, err := os.ReadFile(src)
   117  	if err != nil {
   118  		t.Fatalf("can't read source file: %s: %s", src, err)
   119  	}
   120  
   121  	if err := os.WriteFile(dst, content, 0666); err != nil {
   122  		t.Fatalf("failed to copy file %s to %s: %s", src, dst, err)
   123  	}
   124  }
   125  
   126  func copyDir(t *testutil.T, src string, dst string) {
   127  	srcInfo, err := os.Stat(src)
   128  	if err != nil {
   129  		t.Fatalf("failed to copy dir %s->%s: %s ", src, dst, err)
   130  	}
   131  
   132  	if err = os.MkdirAll(dst, srcInfo.Mode()); err != nil {
   133  		t.Fatalf("failed to copy dir %s->%s: %s ", src, dst, err)
   134  	}
   135  
   136  	files, err := os.ReadDir(src)
   137  	if err != nil {
   138  		t.Fatalf("failed to copy dir %s->%s: %s ", src, dst, err)
   139  	}
   140  
   141  	for _, f := range files {
   142  		srcfp := path.Join(src, f.Name())
   143  		dstfp := path.Join(dst, f.Name())
   144  
   145  		if f.IsDir() {
   146  			copyDir(t, srcfp, dstfp)
   147  		} else {
   148  			copyFile(t, srcfp, dstfp)
   149  		}
   150  	}
   151  }