github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/build/ko/builder_test.go (about)

     1  /*
     2  Copyright 2021 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 ko
    18  
    19  import (
    20  	"fmt"
    21  	"os"
    22  	"path/filepath"
    23  	"testing"
    24  
    25  	"github.com/google/go-cmp/cmp/cmpopts"
    26  	"github.com/google/ko/pkg/build"
    27  	"github.com/google/ko/pkg/commands/options"
    28  	specs "github.com/opencontainers/image-spec/specs-go/v1"
    29  
    30  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config"
    31  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/platform"
    32  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
    33  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/version"
    34  	"github.com/GoogleContainerTools/skaffold/testutil"
    35  )
    36  
    37  const (
    38  	testKoBuildOptionsEnvVar = "TEST_KO_BUILDER_IMAGE_LABEL_ENV_VAR"
    39  )
    40  
    41  func TestBuildOptions(t *testing.T) {
    42  	tests := []struct {
    43  		description string
    44  		artifact    latest.Artifact
    45  		platforms   platform.Matcher
    46  		envVarValue string
    47  		runMode     config.RunMode
    48  		wantBo      options.BuildOptions
    49  	}{
    50  		{
    51  			description: "all zero value",
    52  			artifact: latest.Artifact{
    53  				ArtifactType: latest.ArtifactType{
    54  					KoArtifact: &latest.KoArtifact{},
    55  				},
    56  			},
    57  			wantBo: options.BuildOptions{
    58  				ConcurrentBuilds: 1,
    59  				SBOM:             "none",
    60  				Trimpath:         true,
    61  				UserAgent:        version.UserAgentWithClient(),
    62  			},
    63  		},
    64  		{
    65  			description: "all options",
    66  			artifact: latest.Artifact{
    67  				ArtifactType: latest.ArtifactType{
    68  					KoArtifact: &latest.KoArtifact{
    69  						BaseImage: "gcr.io/distroless/base:nonroot",
    70  						Dir:       "gomoddir",
    71  						Env: []string{
    72  							"FOO=BAR",
    73  							fmt.Sprintf("frob={{.%s}}", testKoBuildOptionsEnvVar),
    74  						},
    75  						Flags: []string{
    76  							"-v",
    77  							fmt.Sprintf("-flag-{{.%s}}", testKoBuildOptionsEnvVar),
    78  							fmt.Sprintf("-flag2-{{.Env.%s}}", testKoBuildOptionsEnvVar),
    79  						},
    80  						Labels: map[string]string{
    81  							"foo":  "bar",
    82  							"frob": fmt.Sprintf("{{.%s}}", testKoBuildOptionsEnvVar),
    83  						},
    84  						Ldflags: []string{
    85  							"-s",
    86  							fmt.Sprintf("-ldflag-{{.%s}}", testKoBuildOptionsEnvVar),
    87  							fmt.Sprintf("-ldflag2-{{.Env.%s}}", testKoBuildOptionsEnvVar),
    88  						},
    89  						Main: "cmd/app",
    90  					},
    91  				},
    92  				ImageName: "ko://example.com/foo",
    93  				Workspace: "workdir",
    94  			},
    95  			platforms: platform.Matcher{Platforms: []specs.Platform{{OS: "linux", Architecture: "amd64"}, {OS: "linux", Architecture: "arm64"}}},
    96  
    97  			envVarValue: "baz",
    98  			runMode:     config.RunModes.Debug,
    99  			wantBo: options.BuildOptions{
   100  				BaseImage: "gcr.io/distroless/base:nonroot",
   101  				BuildConfigs: map[string]build.Config{
   102  					"example.com/foo": {
   103  						ID:      "ko://example.com/foo",
   104  						Dir:     ".",
   105  						Env:     []string{"FOO=BAR", "frob=baz"},
   106  						Flags:   build.FlagArray{"-v", "-flag-baz", "-flag2-baz"},
   107  						Ldflags: build.StringArray{"-s", "-ldflag-baz", "-ldflag2-baz"},
   108  						Main:    "cmd/app",
   109  					},
   110  				},
   111  				ConcurrentBuilds:     1,
   112  				DisableOptimizations: true,
   113  				Labels:               []string{"foo=bar", "frob=baz"},
   114  				Platforms:            []string{"linux/amd64", "linux/arm64"},
   115  				SBOM:                 "none",
   116  				Trimpath:             false,
   117  				UserAgent:            version.UserAgentWithClient(),
   118  				WorkingDirectory:     "workdir" + string(filepath.Separator) + "gomoddir",
   119  			},
   120  		},
   121  		{
   122  			description: "compatibility with ko envvar expansion syntax for flags and ldflags",
   123  			artifact: latest.Artifact{
   124  				ArtifactType: latest.ArtifactType{
   125  					KoArtifact: &latest.KoArtifact{
   126  						Flags: []string{
   127  							"-v",
   128  							fmt.Sprintf("-flag-{{.Env.%s}}", testKoBuildOptionsEnvVar),
   129  						},
   130  						Ldflags: []string{
   131  							"-s",
   132  							fmt.Sprintf("-ldflag-{{.Env.%s}}", testKoBuildOptionsEnvVar),
   133  						},
   134  					},
   135  				},
   136  				ImageName: "ko://example.com/foo",
   137  			},
   138  			envVarValue: "xyzzy",
   139  			wantBo: options.BuildOptions{
   140  				BuildConfigs: map[string]build.Config{
   141  					"example.com/foo": {
   142  						ID:      "ko://example.com/foo",
   143  						Dir:     ".",
   144  						Flags:   build.FlagArray{"-v", "-flag-xyzzy"},
   145  						Ldflags: build.StringArray{"-s", "-ldflag-xyzzy"},
   146  					},
   147  				},
   148  				ConcurrentBuilds: 1,
   149  				SBOM:             "none",
   150  				Trimpath:         true,
   151  				UserAgent:        version.UserAgentWithClient(),
   152  			},
   153  		},
   154  	}
   155  	for _, test := range tests {
   156  		testutil.Run(t, test.description, func(t *testutil.T) {
   157  			os.Setenv(testKoBuildOptionsEnvVar, test.envVarValue)
   158  			gotBo, err := buildOptions(&test.artifact, test.runMode, test.platforms)
   159  			defer os.Unsetenv(testKoBuildOptionsEnvVar)
   160  			t.CheckErrorAndFailNow(false, err)
   161  			t.CheckDeepEqual(test.wantBo, *gotBo,
   162  				cmpopts.EquateEmpty(),
   163  				cmpopts.SortSlices(func(x, y string) bool { return x < y }),
   164  			)
   165  		})
   166  	}
   167  }