github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/runner/v1/new_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 v1
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config"
    23  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/runner/runcontext"
    24  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
    25  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
    26  	"github.com/GoogleContainerTools/skaffold/testutil"
    27  )
    28  
    29  func TestIsImageLocal(t *testing.T) {
    30  	tests := []struct {
    31  		description       string
    32  		pushImagesFlagVal *bool
    33  		localBuildConfig  *bool
    34  		expected          bool
    35  	}{
    36  		{
    37  			description:       "skaffold build --push=nil, pipeline.Build.LocalBuild.Push=nil",
    38  			pushImagesFlagVal: nil,
    39  			localBuildConfig:  nil,
    40  			expected:          false,
    41  		},
    42  		{
    43  			description:       "skaffold build --push=nil, pipeline.Build.LocalBuild.Push=false",
    44  			pushImagesFlagVal: nil,
    45  			localBuildConfig:  util.BoolPtr(false),
    46  			expected:          true,
    47  		},
    48  		{
    49  			description:       "skaffold build --push=nil, pipeline.Build.LocalBuild.Push=true",
    50  			pushImagesFlagVal: nil,
    51  			localBuildConfig:  util.BoolPtr(true),
    52  			expected:          false,
    53  		},
    54  		{
    55  			description:       "skaffold build --push=false, pipeline.Build.LocalBuild.Push=nil",
    56  			pushImagesFlagVal: util.BoolPtr(false),
    57  			localBuildConfig:  nil,
    58  			expected:          true,
    59  		},
    60  		{
    61  			description:       "skaffold build --push=false, pipeline.Build.LocalBuild.Push=false",
    62  			pushImagesFlagVal: util.BoolPtr(false),
    63  			localBuildConfig:  util.BoolPtr(false),
    64  			expected:          true,
    65  		},
    66  		{
    67  			description:       "skaffold build --push=false, pipeline.Build.LocalBuild.Push=true",
    68  			pushImagesFlagVal: util.BoolPtr(false),
    69  			localBuildConfig:  util.BoolPtr(true),
    70  			expected:          true,
    71  		},
    72  		{
    73  			description:       "skaffold build --push=true, pipeline.Build.LocalBuild.Push=nil",
    74  			pushImagesFlagVal: util.BoolPtr(true),
    75  			localBuildConfig:  nil,
    76  			expected:          false,
    77  		},
    78  		{
    79  			description:       "skaffold build --push=true, pipeline.Build.LocalBuild.Push=nil",
    80  			pushImagesFlagVal: util.BoolPtr(true),
    81  			localBuildConfig:  util.BoolPtr(false),
    82  			expected:          false,
    83  		},
    84  		{
    85  			description:       "skaffold build --push=true, pipeline.Build.LocalBuild.Push=nil",
    86  			pushImagesFlagVal: util.BoolPtr(true),
    87  			localBuildConfig:  util.BoolPtr(true),
    88  			expected:          false,
    89  		},
    90  	}
    91  	imageName := "testImage"
    92  	for _, test := range tests {
    93  		testutil.Run(t, test.description, func(t *testutil.T) {
    94  			rctx := &runcontext.RunContext{
    95  				Cluster: config.Cluster{
    96  					PushImages: true,
    97  				},
    98  				Opts: config.SkaffoldOptions{
    99  					PushImages: config.NewBoolOrUndefined(test.pushImagesFlagVal),
   100  				},
   101  				Pipelines: runcontext.NewPipelines([]latest.Pipeline{{
   102  					Build: latest.BuildConfig{
   103  						Artifacts: []*latest.Artifact{
   104  							{ImageName: imageName},
   105  						},
   106  						BuildType: latest.BuildType{
   107  							LocalBuild: &latest.LocalBuild{
   108  								Push: test.localBuildConfig,
   109  							},
   110  						},
   111  					},
   112  				}})}
   113  			output, _ := isImageLocal(rctx, imageName)
   114  			if output != test.expected {
   115  				t.Errorf("isImageLocal output was %t, expected: %t", output, test.expected)
   116  			}
   117  		})
   118  	}
   119  }