github.com/GoogleContainerTools/skaffold/v2@v2.13.2/integration/init_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  	"errors"
    21  	"fmt"
    22  	"os"
    23  	"os/exec"
    24  	"path/filepath"
    25  	"strings"
    26  	"testing"
    27  
    28  	"github.com/GoogleContainerTools/skaffold/v2/integration/skaffold"
    29  	"github.com/GoogleContainerTools/skaffold/v2/testutil"
    30  )
    31  
    32  func TestInit(t *testing.T) {
    33  	tests := []struct {
    34  		name string
    35  		dir  string
    36  		args []string
    37  	}{
    38  		/*
    39  			// Fix after https://github.com/GoogleContainerTools/skaffold/issues/6722
    40  				{
    41  					name: "compose",
    42  					dir:  "testdata/init/compose",
    43  					args: []string{"--compose-file", "docker-compose.yaml"},
    44  				},
    45  		*/
    46  		{
    47  			name: "helm init",
    48  			dir:  "testdata/init/helm-project",
    49  			args: []string{"--XXenableBuildpacksInit=false"},
    50  		},
    51  	}
    52  	for _, test := range tests {
    53  		testutil.Run(t, test.name, func(t *testutil.T) {
    54  			MarkIntegrationTest(t.T, CanRunWithoutGcp)
    55  			ns, _ := SetupNamespace(t.T)
    56  
    57  			initArgs := append([]string{"--force"}, test.args...)
    58  			skaffold.Init(initArgs...).InDir(test.dir).WithConfig("skaffold.yaml.out").RunOrFail(t.T)
    59  			checkGeneratedConfig(t, test.dir)
    60  
    61  			// Make sure the skaffold yaml and the kubernetes manifests created by kompose are ok
    62  			skaffold.Run().InDir(test.dir).WithConfig("skaffold.yaml.out").InNs(ns.Name).RunOrFail(t.T)
    63  			defer skaffold.Delete().InDir(test.dir).WithConfig("skaffold.yaml.out").InNs(ns.Name)
    64  		})
    65  	}
    66  }
    67  
    68  func TestInitManifestGeneration(t *testing.T) {
    69  	t.Skipf("Fix after https://github.com/GoogleContainerTools/skaffold/issues/6722")
    70  
    71  	MarkIntegrationTest(t, CanRunWithoutGcp)
    72  
    73  	tests := []struct {
    74  		name                  string
    75  		dir                   string
    76  		args                  []string
    77  		expectedManifestPaths []string
    78  	}{
    79  		{
    80  			name:                  "hello",
    81  			dir:                   "testdata/init/hello",
    82  			args:                  []string{"--generate-manifests"},
    83  			expectedManifestPaths: []string{"deployment.yaml"},
    84  		},
    85  		// TODO(nkubala): add this back when the --force flag is fixed
    86  		// {
    87  		// 	name:                  "microservices",
    88  		// 	dir:                   "testdata/init/microservices",
    89  		// 	args:                  []string{"--XXenableManifestGeneration"},
    90  		// 	expectedManifestPaths: []string{"leeroy-web/deployment.yaml", "leeroy-app/deployment.yaml"},
    91  		// },
    92  	}
    93  
    94  	for _, test := range tests {
    95  		testutil.Run(t, test.name, func(t *testutil.T) {
    96  			ns, _ := SetupNamespace(t.T)
    97  
    98  			initArgs := append([]string{"--force"}, test.args...)
    99  			skaffold.Init(initArgs...).InDir(test.dir).WithConfig("skaffold.yaml.out").RunOrFail(t.T)
   100  
   101  			checkGeneratedManifests(t, test.dir, test.expectedManifestPaths)
   102  
   103  			// Make sure the skaffold yaml and the kubernetes manifests created by kompose are ok
   104  			skaffold.Run().InDir(test.dir).WithConfig("skaffold.yaml.out").InNs(ns.Name).RunOrFail(t.T)
   105  			skaffold.Delete().InDir(test.dir).WithConfig("skaffold.yaml.out").InNs(ns.Name).RunOrFail(t.T)
   106  		})
   107  	}
   108  }
   109  
   110  func TestInitKustomize(t *testing.T) {
   111  	t.Skipf("Fix after https://github.com/GoogleContainerTools/skaffold/issues/6722")
   112  
   113  	MarkIntegrationTest(t, CanRunWithoutGcp)
   114  
   115  	testutil.Run(t, "kustomize init", func(t *testutil.T) {
   116  		dir := "examples/getting-started-kustomize"
   117  		ns, _ := SetupNamespace(t.T)
   118  
   119  		initArgs := []string{"--force"}
   120  		defer func() {
   121  			path := filepath.Join(dir, "skaffold.yaml.out")
   122  			_, err := os.Stat(path)
   123  			if os.IsNotExist(err) {
   124  				return
   125  			}
   126  			os.Remove(path)
   127  		}()
   128  		skaffold.Init(initArgs...).InDir(dir).WithConfig("skaffold.yaml.out").RunOrFail(t.T)
   129  
   130  		checkGeneratedConfig(t, dir)
   131  
   132  		skaffold.Run().InDir(dir).WithConfig("skaffold.yaml.out").InNs(ns.Name).RunOrFail(t.T)
   133  		skaffold.Delete().InDir(dir).WithConfig("skaffold.yaml.out").InNs(ns.Name).RunOrFail(t.T)
   134  	})
   135  }
   136  
   137  func TestInitWithCLIArtifact(t *testing.T) {
   138  	t.Skipf("Fix after https://github.com/GoogleContainerTools/skaffold/issues/6722")
   139  
   140  	MarkIntegrationTest(t, CanRunWithoutGcp)
   141  
   142  	testutil.Run(t, "init with cli artifact", func(t *testutil.T) {
   143  		dir := "testdata/init/hello-with-manifest"
   144  		ns, _ := SetupNamespace(t.T)
   145  
   146  		initArgs := append([]string{"--force"},
   147  			`--artifact={"builder":"Docker","payload":{"path":"../hello/Dockerfile"},"image":"dockerfile-image"}`)
   148  		skaffold.Init(initArgs...).InDir(dir).WithConfig("skaffold.yaml.out").RunOrFail(t.T)
   149  
   150  		checkGeneratedConfig(t, dir)
   151  
   152  		// Make sure the skaffold yaml is ok
   153  		skaffold.Run().InDir(dir).WithConfig("skaffold.yaml.out").InNs(ns.Name).RunOrFail(t.T)
   154  		skaffold.Delete().InDir(dir).WithConfig("skaffold.yaml.out").InNs(ns.Name).RunOrFail(t.T)
   155  	})
   156  }
   157  
   158  func TestInitWithCLIArtifactAndManifestGeneration(t *testing.T) {
   159  	MarkIntegrationTest(t, CanRunWithoutGcp)
   160  	t.Skipf("Fix after https://github.com/GoogleContainerTools/skaffold/issues/6722")
   161  
   162  	testutil.Run(t, "init with cli artifact and manifests", func(t *testutil.T) {
   163  		ns, _ := SetupNamespace(t.T)
   164  		dir := "testdata/init/hello"
   165  
   166  		initArgs := append([]string{"--force"},
   167  			`--artifact={"builder":"Docker","payload":{"path":"./Dockerfile"},"image":"dockerfile-image","manifest":{"generate":true,"port":8080}}`)
   168  		skaffold.Init(initArgs...).InDir(dir).WithConfig("skaffold.yaml.out").RunOrFail(t.T)
   169  
   170  		checkGeneratedManifests(t, dir, []string{"deployment.yaml"})
   171  
   172  		skaffold.Run().InDir(dir).WithConfig("skaffold.yaml.out").InNs(ns.Name).RunOrFail(t.T)
   173  	})
   174  }
   175  
   176  func checkGeneratedConfig(t *testutil.T, dir string) {
   177  	expectedOutput, err := os.ReadFile(filepath.Join(dir, "skaffold.yaml"))
   178  	t.CheckNoError(err)
   179  
   180  	output, err := os.ReadFile(filepath.Join(dir, "skaffold.yaml.out"))
   181  	t.CheckNoError(err)
   182  	t.CheckDeepEqual(string(expectedOutput), string(output), testutil.YamlObj(t.T))
   183  }
   184  
   185  func checkGeneratedManifests(t *testutil.T, dir string, manifestPaths []string) {
   186  	for _, path := range manifestPaths {
   187  		expectedOutput, err := os.ReadFile(filepath.Join(dir, strings.Join([]string{".", path, ".expected"}, "")))
   188  		t.CheckNoError(err)
   189  
   190  		output, err := os.ReadFile(filepath.Join(dir, path))
   191  		t.CheckNoError(err)
   192  		t.CheckDeepEqual(string(expectedOutput), string(output))
   193  	}
   194  }
   195  
   196  func TestInitFailures(t *testing.T) {
   197  	MarkIntegrationTest(t, CanRunWithoutGcp)
   198  
   199  	testutil.Run(t, "no builder", func(t *testutil.T) {
   200  		out, err := skaffold.Init().InDir("testdata/init/no-builder").RunWithCombinedOutput(t.T)
   201  
   202  		t.CheckContains("please provide at least one build config", string(out))
   203  		t.CheckDeepEqual(101, exitCode(err))
   204  	})
   205  }
   206  
   207  func exitCode(err error) int {
   208  	var exitErr *exec.ExitError
   209  	if ok := errors.As(err, &exitErr); ok {
   210  		return exitErr.ExitCode()
   211  	}
   212  
   213  	return 1
   214  }
   215  
   216  func TestInitWithDirWithoutReadPerms(t *testing.T) {
   217  	MarkIntegrationTest(t, CanRunWithoutGcp)
   218  	tests := []struct {
   219  		description string
   220  		shouldFail  bool
   221  		flags       []string
   222  		dir         string
   223  		dirToCreate string
   224  	}{
   225  		{
   226  			description: "without --skip-unreachable-dirs flag, should fail",
   227  			shouldFail:  true,
   228  			flags:       []string{"--analyze"},
   229  			dir:         "testdata/getting-started/",
   230  			dirToCreate: "dir1",
   231  		},
   232  		{
   233  			description: "with --skip-unreachable-dirs flag, shouldn't fail",
   234  			shouldFail:  false,
   235  			flags:       []string{"--analyze", "--skip-unreachable-dirs"},
   236  			dir:         "testdata/getting-started",
   237  			dirToCreate: "dir2",
   238  		},
   239  	}
   240  
   241  	for _, test := range tests {
   242  		testutil.Run(t, test.description, func(t *testutil.T) {
   243  			tmpDir := testutil.NewTempDir(t.T)
   244  			copyFiles(tmpDir.Root(), test.dir)
   245  
   246  			dirWithoutReadPerms := filepath.Join(tmpDir.Root(), test.dirToCreate)
   247  			os.MkdirAll(dirWithoutReadPerms, 0377)
   248  			defer os.Remove(dirWithoutReadPerms)
   249  
   250  			output, err := skaffold.Init(test.flags...).InDir(tmpDir.Root()).RunWithCombinedOutput(t.T)
   251  			t.CheckError(test.shouldFail, err)
   252  			if test.shouldFail {
   253  				t.CheckDeepEqual(fmt.Sprintf("open %v: permission denied\n", test.dirToCreate), string(output))
   254  			}
   255  		})
   256  	}
   257  }