github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/initializer/transparent_test.go (about)

     1  /*
     2  Copyright 2020 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 initializer
    18  
    19  import (
    20  	"context"
    21  	"io"
    22  	"os"
    23  	"testing"
    24  
    25  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config"
    26  	initconfig "github.com/GoogleContainerTools/skaffold/pkg/skaffold/initializer/config"
    27  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
    28  	"github.com/GoogleContainerTools/skaffold/testutil"
    29  )
    30  
    31  func TestTransparentInit(t *testing.T) {
    32  	tests := []struct {
    33  		name             string
    34  		dir              string
    35  		config           initconfig.Config
    36  		expectedError    string
    37  		expectedExitCode int
    38  		doneResponse     bool
    39  	}{
    40  		//TODO: mocked kompose test
    41  		{
    42  			name: "getting-started",
    43  			dir:  "testdata/init/hello",
    44  			config: initconfig.Config{
    45  				Force: true,
    46  				Opts: config.SkaffoldOptions{
    47  					ConfigurationFile: "skaffold.yaml.out",
    48  				},
    49  			},
    50  		},
    51  		{
    52  			name: "ignore existing tags",
    53  			dir:  "testdata/init/ignore-tags",
    54  			config: initconfig.Config{
    55  				Force: true,
    56  				Opts: config.SkaffoldOptions{
    57  					ConfigurationFile: "skaffold.yaml.out",
    58  				},
    59  			},
    60  		},
    61  		{
    62  			name: "microservices (backwards compatibility)",
    63  			dir:  "testdata/init/microservices",
    64  			config: initconfig.Config{
    65  				Force: true,
    66  				CliArtifacts: []string{
    67  					"leeroy-app/Dockerfile=gcr.io/k8s-skaffold/leeroy-app",
    68  					"leeroy-web/Dockerfile=gcr.io/k8s-skaffold/leeroy-web",
    69  				},
    70  				Opts: config.SkaffoldOptions{
    71  					ConfigurationFile: "skaffold.yaml.out",
    72  				},
    73  			},
    74  		},
    75  		{
    76  			name: "error writing config file",
    77  			dir:  "testdata/init/hello",
    78  			config: initconfig.Config{
    79  				Force: true,
    80  				Opts: config.SkaffoldOptions{
    81  					// erroneous config file as . is a directory
    82  					ConfigurationFile: ".",
    83  				},
    84  			},
    85  			expectedError:    "writing config to file: open .: is a directory",
    86  			expectedExitCode: 1,
    87  		},
    88  		{
    89  			name: "error no builders",
    90  			dir:  "testdata/init/no-builder",
    91  			config: initconfig.Config{
    92  				Force: true,
    93  				Opts: config.SkaffoldOptions{
    94  					ConfigurationFile: "skaffold.yaml.out",
    95  				},
    96  			},
    97  			expectedError:    "please provide at least one build config",
    98  			expectedExitCode: 101,
    99  		},
   100  		{
   101  			name: "error no manifests",
   102  			dir:  "testdata/init/hello-no-manifest",
   103  			config: initconfig.Config{
   104  				Force: true,
   105  				Opts: config.SkaffoldOptions{
   106  					ConfigurationFile: "skaffold.yaml.out",
   107  				},
   108  			},
   109  			expectedError:    "one or more valid Kubernetes manifests are required to run skaffold",
   110  			expectedExitCode: 102,
   111  		},
   112  		{
   113  			name: "builder/image ambiguity",
   114  			dir:  "testdata/init/microservices",
   115  			config: initconfig.Config{
   116  				Force: true,
   117  				Opts: config.SkaffoldOptions{
   118  					ConfigurationFile: "skaffold.yaml.out",
   119  				},
   120  			},
   121  			expectedError:    "unable to automatically resolve builder/image pairs",
   122  			expectedExitCode: 104,
   123  		},
   124  		{
   125  			name: "kustomize",
   126  			dir:  "testdata/init/getting-started-kustomize",
   127  			config: initconfig.Config{
   128  				Force: true,
   129  				Opts: config.SkaffoldOptions{
   130  					ConfigurationFile: "skaffold.yaml.out",
   131  				},
   132  			},
   133  		},
   134  		{
   135  			name: "helm fails",
   136  			dir:  "testdata/init/helm-deployment",
   137  			config: initconfig.Config{
   138  				Opts: config.SkaffoldOptions{
   139  					ConfigurationFile: "skaffold.yaml.out",
   140  				},
   141  			},
   142  			expectedError: `Projects set up to deploy with helm must be manually configured.
   143  
   144  See https://skaffold.dev/docs/pipeline-stages/deployers/helm/ for a detailed guide on setting your project up with skaffold.`,
   145  			expectedExitCode: 1,
   146  		},
   147  		{
   148  			name: "user selects 'no'",
   149  			dir:  "testdata/init/hello",
   150  			config: initconfig.Config{
   151  				Opts: config.SkaffoldOptions{
   152  					ConfigurationFile: "skaffold.yaml.out",
   153  				},
   154  			},
   155  			doneResponse: true,
   156  		},
   157  	}
   158  	for _, test := range tests {
   159  		testutil.Run(t, test.name, func(t *testutil.T) {
   160  			t.Chdir(test.dir)
   161  
   162  			t.Override(&confirmInitOptions, func(_ io.Writer, _ *latest.SkaffoldConfig) (bool, error) {
   163  				return test.doneResponse, nil
   164  			})
   165  
   166  			got, err := Transparent(context.TODO(), os.Stdout, test.config)
   167  
   168  			switch {
   169  			case test.expectedError != "":
   170  				t.CheckErrorContains(test.expectedError, err)
   171  				t.CheckDeepEqual(exitCode(err), test.expectedExitCode)
   172  			case test.doneResponse == true:
   173  				t.CheckErrorAndDeepEqual(false, err, (*latest.SkaffoldConfig)(nil), got)
   174  			default:
   175  				t.CheckNoError(err)
   176  				checkGeneratedConfig(t, ".")
   177  			}
   178  		})
   179  	}
   180  }
   181  
   182  func TestValidCmd(t *testing.T) {
   183  	tests := []struct {
   184  		name     string
   185  		cmd      string
   186  		expected bool
   187  	}{
   188  		{
   189  			name:     "valid string",
   190  			cmd:      "dev",
   191  			expected: true,
   192  		},
   193  		{
   194  			name:     "invalid",
   195  			cmd:      "build",
   196  			expected: false,
   197  		},
   198  	}
   199  	for _, test := range tests {
   200  		testutil.Run(t, test.name, func(t *testutil.T) {
   201  			config := config.SkaffoldOptions{
   202  				Command: test.cmd,
   203  			}
   204  			valid := ValidCmd(config)
   205  
   206  			t.CheckDeepEqual(test.expected, valid)
   207  		})
   208  	}
   209  }