github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/build/kaniko/args_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 kaniko
    18  
    19  import (
    20  	"fmt"
    21  	"testing"
    22  
    23  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
    24  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
    25  	"github.com/GoogleContainerTools/skaffold/testutil"
    26  )
    27  
    28  func TestArgs(t *testing.T) {
    29  	tests := []struct {
    30  		description  string
    31  		artifact     *latest.KanikoArtifact
    32  		expectedArgs []string
    33  		wantErr      bool
    34  	}{
    35  		{
    36  			description: "simple build",
    37  			artifact: &latest.KanikoArtifact{
    38  				DockerfilePath: "Dockerfile",
    39  			},
    40  			expectedArgs: []string{},
    41  			wantErr:      false,
    42  		},
    43  		{
    44  			description: "with BuildArgs",
    45  			artifact: &latest.KanikoArtifact{
    46  				DockerfilePath: "Dockerfile",
    47  				BuildArgs: map[string]*string{
    48  					"arg1": util.StringPtr("value1"),
    49  					"arg2": nil,
    50  				},
    51  			},
    52  			expectedArgs: []string{
    53  				BuildArgsFlag, "arg1=value1",
    54  				BuildArgsFlag, "arg2",
    55  			},
    56  			wantErr: false,
    57  		},
    58  		{
    59  			description: "with Cache",
    60  			artifact: &latest.KanikoArtifact{
    61  				DockerfilePath: "Dockerfile",
    62  				Cache:          &latest.KanikoCache{},
    63  			},
    64  			expectedArgs: []string{
    65  				CacheFlag,
    66  			},
    67  			wantErr: false,
    68  		},
    69  		{
    70  			description: "with Cache Options",
    71  			artifact: &latest.KanikoArtifact{
    72  				DockerfilePath: "Dockerfile",
    73  				Cache: &latest.KanikoCache{
    74  					Repo:     "gcr.io/ngnix",
    75  					HostPath: "/cache",
    76  					TTL:      "2",
    77  				},
    78  			},
    79  			expectedArgs: []string{
    80  				CacheFlag,
    81  				CacheRepoFlag, "gcr.io/ngnix",
    82  				CacheDirFlag, "/cache",
    83  				CacheTTLFlag, "2",
    84  			},
    85  			wantErr: false,
    86  		},
    87  		{
    88  			description: "with Cleanup",
    89  			artifact: &latest.KanikoArtifact{
    90  				DockerfilePath: "Dockerfile",
    91  				Cleanup:        true,
    92  			},
    93  			expectedArgs: []string{
    94  				CleanupFlag,
    95  			},
    96  			wantErr: false,
    97  		},
    98  		{
    99  			description: "with DigestFile",
   100  			artifact: &latest.KanikoArtifact{
   101  				DockerfilePath: "Dockerfile",
   102  				DigestFile:     "/tmp/digest",
   103  			},
   104  			expectedArgs: []string{
   105  				DigestFileFlag, "/tmp/digest",
   106  			},
   107  			wantErr: false,
   108  		},
   109  		{
   110  			description: "with Force",
   111  			artifact: &latest.KanikoArtifact{
   112  				DockerfilePath: "Dockerfile",
   113  				Force:          true,
   114  			},
   115  			expectedArgs: []string{
   116  				ForceFlag,
   117  			},
   118  			wantErr: false,
   119  		},
   120  		{
   121  			description: "with ImageNameWithDigestFile",
   122  			artifact: &latest.KanikoArtifact{
   123  				DockerfilePath:          "Dockerfile",
   124  				ImageNameWithDigestFile: "/tmp/imageName",
   125  			},
   126  			expectedArgs: []string{
   127  				ImageNameWithDigestFileFlag, "/tmp/imageName",
   128  			},
   129  			wantErr: false,
   130  		},
   131  		{
   132  			description: "with Insecure",
   133  			artifact: &latest.KanikoArtifact{
   134  				DockerfilePath: "Dockerfile",
   135  				Insecure:       true,
   136  			},
   137  			expectedArgs: []string{
   138  				InsecureFlag,
   139  			},
   140  			wantErr: false,
   141  		},
   142  		{
   143  			description: "with InsecurePull",
   144  			artifact: &latest.KanikoArtifact{
   145  				DockerfilePath: "Dockerfile",
   146  				InsecurePull:   true,
   147  			},
   148  			expectedArgs: []string{
   149  				InsecurePullFlag,
   150  			},
   151  			wantErr: false,
   152  		},
   153  		{
   154  			description: "with InsecureRegistry",
   155  			artifact: &latest.KanikoArtifact{
   156  				DockerfilePath: "Dockerfile",
   157  				InsecureRegistry: []string{
   158  					"s1.registry.url:5000",
   159  					"s2.registry.url:5000",
   160  				},
   161  			},
   162  			expectedArgs: []string{
   163  				InsecureRegistryFlag, "s1.registry.url:5000",
   164  				InsecureRegistryFlag, "s2.registry.url:5000",
   165  			},
   166  			wantErr: false,
   167  		},
   168  		{
   169  			description: "with LogFormat",
   170  			artifact: &latest.KanikoArtifact{
   171  				DockerfilePath: "Dockerfile",
   172  				LogFormat:      "json",
   173  			},
   174  			expectedArgs: []string{
   175  				LogFormatFlag, "json",
   176  			},
   177  			wantErr: false,
   178  		},
   179  		{
   180  			description: "with LogTimestamp",
   181  			artifact: &latest.KanikoArtifact{
   182  				DockerfilePath: "Dockerfile",
   183  				LogTimestamp:   true,
   184  			},
   185  			expectedArgs: []string{
   186  				LogTimestampFlag,
   187  			},
   188  			wantErr: false,
   189  		},
   190  		{
   191  			description: "with NoPush",
   192  			artifact: &latest.KanikoArtifact{
   193  				DockerfilePath: "Dockerfile",
   194  				NoPush:         true,
   195  			},
   196  			expectedArgs: []string{
   197  				NoPushFlag,
   198  			},
   199  			wantErr: false,
   200  		},
   201  		{
   202  			description: "with OCILayoutPath",
   203  			artifact: &latest.KanikoArtifact{
   204  				DockerfilePath: "Dockerfile",
   205  				OCILayoutPath:  "/tmp/builtImage",
   206  			},
   207  			expectedArgs: []string{
   208  				OCILayoutFlag, "/tmp/builtImage",
   209  			},
   210  			wantErr: false,
   211  		},
   212  		{
   213  			description: "with RegistryCertificate",
   214  			artifact: &latest.KanikoArtifact{
   215  				DockerfilePath: "Dockerfile",
   216  				RegistryCertificate: map[string]*string{
   217  					"s1.registry.url": util.StringPtr("/etc/certs/certificate1.cert"),
   218  					"s2.registry.url": util.StringPtr("/etc/certs/certificate2.cert"),
   219  				},
   220  			},
   221  			expectedArgs: []string{
   222  				RegistryCertificateFlag, "s1.registry.url=/etc/certs/certificate1.cert",
   223  				RegistryCertificateFlag, "s2.registry.url=/etc/certs/certificate2.cert",
   224  			},
   225  			wantErr: false,
   226  		},
   227  		{
   228  			description: "with RegistryMirror",
   229  			artifact: &latest.KanikoArtifact{
   230  				DockerfilePath: "Dockerfile",
   231  				RegistryMirror: "mirror.gcr.io",
   232  			},
   233  			expectedArgs: []string{
   234  				RegistryMirrorFlag, "mirror.gcr.io",
   235  			},
   236  			wantErr: false,
   237  		},
   238  		{
   239  			description: "with Reproducible",
   240  			artifact: &latest.KanikoArtifact{
   241  				DockerfilePath: "Dockerfile",
   242  				Reproducible:   true,
   243  			},
   244  			expectedArgs: []string{
   245  				ReproducibleFlag,
   246  			},
   247  			wantErr: false,
   248  		},
   249  		{
   250  			description: "with SingleSnapshot",
   251  			artifact: &latest.KanikoArtifact{
   252  				DockerfilePath: "Dockerfile",
   253  				SingleSnapshot: true,
   254  			},
   255  			expectedArgs: []string{
   256  				SingleSnapshotFlag,
   257  			},
   258  			wantErr: false,
   259  		},
   260  		{
   261  			description: "with SkipTLS",
   262  			artifact: &latest.KanikoArtifact{
   263  				DockerfilePath: "Dockerfile",
   264  				SkipTLS:        true,
   265  			},
   266  			expectedArgs: []string{
   267  				SkipTLSFlag,
   268  				SkipTLSVerifyRegistryFlag, "gcr.io",
   269  			},
   270  			wantErr: false,
   271  		},
   272  		{
   273  			description: "with SkipTLSVerifyPull",
   274  			artifact: &latest.KanikoArtifact{
   275  				DockerfilePath:    "Dockerfile",
   276  				SkipTLSVerifyPull: true,
   277  			},
   278  			expectedArgs: []string{
   279  				SkipTLSVerifyPullFlag,
   280  			},
   281  			wantErr: false,
   282  		},
   283  		{
   284  			description: "with SkipTLSVerifyRegistry",
   285  			artifact: &latest.KanikoArtifact{
   286  				DockerfilePath: "Dockerfile",
   287  				SkipTLSVerifyRegistry: []string{
   288  					"s1.registry.url:443",
   289  					"s2.registry.url:443",
   290  				},
   291  			},
   292  			expectedArgs: []string{
   293  				SkipTLSVerifyRegistryFlag, "s1.registry.url:443",
   294  				SkipTLSVerifyRegistryFlag, "s2.registry.url:443",
   295  			},
   296  			wantErr: false,
   297  		},
   298  		{
   299  			description: "with SkipUnusedStages",
   300  			artifact: &latest.KanikoArtifact{
   301  				DockerfilePath:   "Dockerfile",
   302  				SkipUnusedStages: true,
   303  			},
   304  			expectedArgs: []string{
   305  				SkipUnusedStagesFlag,
   306  			},
   307  			wantErr: false,
   308  		},
   309  		{
   310  			description: "with Target",
   311  			artifact: &latest.KanikoArtifact{
   312  				DockerfilePath: "Dockerfile",
   313  				Target:         "builder",
   314  			},
   315  			expectedArgs: []string{
   316  				TargetFlag, "builder",
   317  			},
   318  			wantErr: false,
   319  		},
   320  		{
   321  			description: "with SnapshotMode",
   322  			artifact: &latest.KanikoArtifact{
   323  				DockerfilePath: "Dockerfile",
   324  				SnapshotMode:   "redo",
   325  			},
   326  			expectedArgs: []string{
   327  				"--snapshotMode", "redo",
   328  			},
   329  			wantErr: false,
   330  		},
   331  		{
   332  			description: "with TarPath",
   333  			artifact: &latest.KanikoArtifact{
   334  				DockerfilePath: "Dockerfile",
   335  				TarPath:        "/workspace/tars",
   336  			},
   337  			expectedArgs: []string{
   338  				TarPathFlag, "/workspace/tars",
   339  			},
   340  			wantErr: false,
   341  		},
   342  		{
   343  			description: "with UseNewRun",
   344  			artifact: &latest.KanikoArtifact{
   345  				DockerfilePath: "Dockerfile",
   346  				UseNewRun:      true,
   347  			},
   348  			expectedArgs: []string{
   349  				UseNewRunFlag,
   350  			},
   351  			wantErr: false,
   352  		},
   353  		{
   354  			description: "with Verbosity",
   355  			artifact: &latest.KanikoArtifact{
   356  				DockerfilePath: "Dockerfile",
   357  				Verbosity:      "trace",
   358  			},
   359  			expectedArgs: []string{
   360  				VerbosityFlag, "trace",
   361  			},
   362  			wantErr: false,
   363  		},
   364  		{
   365  			description: "with WhitelistVarRun",
   366  			artifact: &latest.KanikoArtifact{
   367  				DockerfilePath:  "Dockerfile",
   368  				WhitelistVarRun: true,
   369  			},
   370  			expectedArgs: []string{
   371  				WhitelistVarRunFlag,
   372  			},
   373  			wantErr: false,
   374  		},
   375  		{
   376  			description: "with WhitelistVarRun",
   377  			artifact: &latest.KanikoArtifact{
   378  				DockerfilePath:  "Dockerfile",
   379  				WhitelistVarRun: true,
   380  			},
   381  			expectedArgs: []string{
   382  				WhitelistVarRunFlag,
   383  			},
   384  			wantErr: false,
   385  		},
   386  		{
   387  			description: "with Labels",
   388  			artifact: &latest.KanikoArtifact{
   389  				DockerfilePath: "Dockerfile",
   390  				Label: map[string]*string{
   391  					"label1": util.StringPtr("value1"),
   392  					"label2": nil,
   393  				},
   394  			},
   395  			expectedArgs: []string{
   396  				LabelFlag, "label1=value1",
   397  				LabelFlag, "label2",
   398  			},
   399  			wantErr: false,
   400  		},
   401  	}
   402  
   403  	defaultExpectedArgs := []string{
   404  		"--destination", "gcr.io/nginx",
   405  		"--dockerfile", "Dockerfile",
   406  		"--context", fmt.Sprintf("dir://%s", DefaultEmptyDirMountPath),
   407  	}
   408  
   409  	for _, test := range tests {
   410  		testutil.Run(t, test.description, func(t *testutil.T) {
   411  			got, err := Args(test.artifact, "gcr.io/nginx", fmt.Sprintf("dir://%s", DefaultEmptyDirMountPath))
   412  			if (err != nil) != test.wantErr {
   413  				t.Errorf("Args() error = %v, wantErr %v", err, test.wantErr)
   414  				return
   415  			}
   416  			t.CheckDeepEqual(got, append(defaultExpectedArgs, test.expectedArgs...))
   417  		})
   418  	}
   419  }
   420  
   421  func Test_artifactRegistry(t *testing.T) {
   422  	tests := []struct {
   423  		name    string
   424  		i       string
   425  		want    string
   426  		wantErr bool
   427  	}{
   428  		{
   429  			name:    "Regular",
   430  			i:       "gcr.io/nginx",
   431  			want:    "gcr.io",
   432  			wantErr: false,
   433  		},
   434  		{
   435  			name:    "with Project",
   436  			i:       "gcr.io/google_containers/nginx",
   437  			want:    "gcr.io",
   438  			wantErr: false,
   439  		},
   440  	}
   441  	for _, test := range tests {
   442  		testutil.Run(t, test.name, func(t *testutil.T) {
   443  			got, err := artifactRegistry(test.i)
   444  			if (err != nil) != test.wantErr {
   445  				t.Errorf("artifactRegistry() error = %v, wantErr %v", err, test.wantErr)
   446  				return
   447  			}
   448  			t.CheckDeepEqual(got, test.want)
   449  		})
   450  	}
   451  }