github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/build/bazel/build_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 bazel
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"io/ioutil"
    23  	"path/filepath"
    24  	"testing"
    25  
    26  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker"
    27  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/platform"
    28  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
    29  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
    30  	"github.com/GoogleContainerTools/skaffold/testutil"
    31  )
    32  
    33  func TestBuildBazel(t *testing.T) {
    34  	testutil.Run(t, "", func(t *testutil.T) {
    35  		t.NewTempDir().Mkdir("bin").Chdir()
    36  		t.Override(&util.DefaultExecCommand, testutil.CmdRun("bazel build //:app.tar --color=no").AndRunOut(
    37  			"bazel cquery //:app.tar --output starlark --starlark:expr target.files.to_list()[0].path",
    38  			"bin/app.tar").AndRunOut("bazel info execution_root", ""))
    39  		testutil.CreateFakeImageTar("bazel:app", "bin/app.tar")
    40  
    41  		artifact := &latest.Artifact{
    42  			Workspace: ".",
    43  			ArtifactType: latest.ArtifactType{
    44  				BazelArtifact: &latest.BazelArtifact{
    45  					BuildTarget: "//:app.tar",
    46  				},
    47  			},
    48  		}
    49  
    50  		builder := NewArtifactBuilder(fakeLocalDaemon(), &mockConfig{}, false)
    51  		_, err := builder.Build(context.Background(), ioutil.Discard, artifact, "img:tag", platform.Matcher{})
    52  
    53  		t.CheckNoError(err)
    54  	})
    55  }
    56  
    57  func TestBazelTarPathPrependExecutionRoot(t *testing.T) {
    58  	testutil.Run(t, "", func(t *testutil.T) {
    59  		t.Override(&util.DefaultExecCommand, testutil.CmdRun("bazel build //:app.tar --color=no").AndRunOut(
    60  			"bazel cquery //:app.tar --output starlark --starlark:expr target.files.to_list()[0].path",
    61  			"app.tar").AndRunOut("bazel info execution_root", ".."))
    62  		testutil.CreateFakeImageTar("bazel:app", "../app.tar")
    63  
    64  		artifact := &latest.Artifact{
    65  			Workspace: "..",
    66  			ArtifactType: latest.ArtifactType{
    67  				BazelArtifact: &latest.BazelArtifact{
    68  					BuildTarget: "//:app.tar",
    69  				},
    70  			},
    71  		}
    72  
    73  		builder := NewArtifactBuilder(fakeLocalDaemon(), &mockConfig{}, false)
    74  		_, err := builder.Build(context.Background(), ioutil.Discard, artifact, "img:tag", platform.Matcher{})
    75  
    76  		t.CheckNoError(err)
    77  	})
    78  }
    79  
    80  func TestBuildBazelFailInvalidTarget(t *testing.T) {
    81  	testutil.Run(t, "", func(t *testutil.T) {
    82  		artifact := &latest.Artifact{
    83  			ArtifactType: latest.ArtifactType{
    84  				BazelArtifact: &latest.BazelArtifact{
    85  					BuildTarget: "//:invalid-target",
    86  				},
    87  			},
    88  		}
    89  
    90  		builder := NewArtifactBuilder(nil, &mockConfig{}, false)
    91  		_, err := builder.Build(context.Background(), ioutil.Discard, artifact, "img:tag", platform.Matcher{})
    92  
    93  		t.CheckErrorContains("the bazel build target should end with .tar", err)
    94  	})
    95  }
    96  
    97  func TestBazelTarPath(t *testing.T) {
    98  	testutil.Run(t, "EmptyExecutionRoot", func(t *testutil.T) {
    99  		osSpecificPath := filepath.Join("absolute", "path", "bin")
   100  		t.Override(&util.DefaultExecCommand, testutil.CmdRunOut(
   101  			"bazel cquery //:skaffold_example.tar --output starlark --starlark:expr target.files.to_list()[0].path --arg1 --arg2",
   102  			fmt.Sprintf("%s\n", osSpecificPath),
   103  		).AndRunOut("bazel info execution_root", ""))
   104  
   105  		bazelBin, err := bazelTarPath(context.Background(), ".", &latest.BazelArtifact{
   106  			BuildArgs:   []string{"--arg1", "--arg2"},
   107  			BuildTarget: "//:skaffold_example.tar",
   108  		})
   109  
   110  		t.CheckNoError(err)
   111  		t.CheckDeepEqual(osSpecificPath, bazelBin)
   112  	})
   113  	testutil.Run(t, "AbsoluteExecutionRoot", func(t *testutil.T) {
   114  		osSpecificPath := filepath.Join("var", "tmp", "bazel-execution-roots", "abcdefg", "execroot", "workspace_name")
   115  		t.Override(&util.DefaultExecCommand, testutil.CmdRunOut(
   116  			"bazel cquery //:skaffold_example.tar --output starlark --starlark:expr target.files.to_list()[0].path --arg1 --arg2",
   117  			"bazel-bin/darwin-fastbuild-ST-confighash/path/to/bin\n",
   118  		).AndRunOut("bazel info execution_root", osSpecificPath))
   119  
   120  		bazelBin, err := bazelTarPath(context.Background(), ".", &latest.BazelArtifact{
   121  			BuildArgs:   []string{"--arg1", "--arg2"},
   122  			BuildTarget: "//:skaffold_example.tar",
   123  		})
   124  
   125  		t.CheckNoError(err)
   126  		expected := filepath.Join(osSpecificPath, "bazel-bin", "darwin-fastbuild-ST-confighash", "path", "to", "bin")
   127  		t.CheckDeepEqual(expected, bazelBin)
   128  	})
   129  }
   130  
   131  func TestBuildImageTag(t *testing.T) {
   132  	buildTarget := "//:skaffold_example.tar"
   133  
   134  	imageTag := buildImageTag(buildTarget)
   135  
   136  	testutil.CheckDeepEqual(t, "bazel:skaffold_example", imageTag)
   137  }
   138  
   139  func fakeLocalDaemon() docker.LocalDaemon {
   140  	return docker.NewLocalDaemon(&testutil.FakeAPIClient{}, nil, false, nil)
   141  }
   142  
   143  type mockConfig struct {
   144  	docker.Config
   145  }
   146  
   147  func (c *mockConfig) GetInsecureRegistries() map[string]bool { return nil }