|
| 1 | +// Copyright 2020 The Bazel Authors. All rights reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package com.google.devtools.build.lib.sandbox; |
| 16 | + |
| 17 | +import static com.google.common.collect.ImmutableMap.toImmutableMap; |
| 18 | +import static com.google.common.truth.Truth.assertThat; |
| 19 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 20 | + |
| 21 | +import com.google.common.collect.ImmutableList; |
| 22 | +import com.google.common.collect.ImmutableMap; |
| 23 | +import com.google.devtools.build.lib.actions.ActionInput; |
| 24 | +import com.google.devtools.build.lib.actions.Artifact.ArtifactExpander; |
| 25 | +import com.google.devtools.build.lib.actions.CommandLines.ParamFileActionInput; |
| 26 | +import com.google.devtools.build.lib.actions.ParameterFile.ParameterFileType; |
| 27 | +import com.google.devtools.build.lib.actions.Spawn; |
| 28 | +import com.google.devtools.build.lib.actions.cache.VirtualActionInput; |
| 29 | +import com.google.devtools.build.lib.actions.util.ActionsTestUtil; |
| 30 | +import com.google.devtools.build.lib.exec.BinTools; |
| 31 | +import com.google.devtools.build.lib.exec.util.SpawnBuilder; |
| 32 | +import com.google.devtools.build.lib.sandbox.SandboxHelpers.SandboxInputs; |
| 33 | +import com.google.devtools.build.lib.testutil.Scratch; |
| 34 | +import com.google.devtools.build.lib.vfs.Dirent; |
| 35 | +import com.google.devtools.build.lib.vfs.FileSystemUtils; |
| 36 | +import com.google.devtools.build.lib.vfs.Path; |
| 37 | +import com.google.devtools.build.lib.vfs.PathFragment; |
| 38 | +import com.google.devtools.build.lib.vfs.Symlinks; |
| 39 | +import java.io.IOException; |
| 40 | +import java.util.Arrays; |
| 41 | +import java.util.function.Function; |
| 42 | +import org.junit.Before; |
| 43 | +import org.junit.Test; |
| 44 | +import org.junit.runner.RunWith; |
| 45 | +import org.junit.runners.JUnit4; |
| 46 | + |
| 47 | +/** Tests for {@link SandboxHelpers}. */ |
| 48 | +@RunWith(JUnit4.class) |
| 49 | +public class SandboxHelpersTest { |
| 50 | + |
| 51 | + private static final ArtifactExpander EMPTY_EXPANDER = (ignored1, ignored2) -> {}; |
| 52 | + private static final Spawn SPAWN = new SpawnBuilder().build(); |
| 53 | + |
| 54 | + private final Scratch scratch = new Scratch(); |
| 55 | + private Path execRoot; |
| 56 | + |
| 57 | + @Before |
| 58 | + public void createExecRoot() throws IOException { |
| 59 | + execRoot = scratch.dir("/execRoot"); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void processInputFiles_materializesParamFile() throws Exception { |
| 64 | + SandboxHelpers sandboxHelpers = new SandboxHelpers(/*delayVirtualInputMaterialization=*/ false); |
| 65 | + ParamFileActionInput paramFile = |
| 66 | + new ParamFileActionInput( |
| 67 | + PathFragment.create("paramFile"), |
| 68 | + ImmutableList.of("-a", "-b"), |
| 69 | + ParameterFileType.UNQUOTED, |
| 70 | + UTF_8); |
| 71 | + |
| 72 | + SandboxInputs inputs = |
| 73 | + sandboxHelpers.processInputFiles(inputMap(paramFile), SPAWN, EMPTY_EXPANDER, execRoot); |
| 74 | + |
| 75 | + assertThat(inputs.getFiles()) |
| 76 | + .containsExactly(PathFragment.create("paramFile"), execRoot.getChild("paramFile")); |
| 77 | + assertThat(inputs.getSymlinks()).isEmpty(); |
| 78 | + assertThat(FileSystemUtils.readLines(execRoot.getChild("paramFile"), UTF_8)) |
| 79 | + .containsExactly("-a", "-b") |
| 80 | + .inOrder(); |
| 81 | + assertThat(execRoot.getChild("paramFile").isExecutable()).isTrue(); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void processInputFiles_materializesBinToolsFile() throws Exception { |
| 86 | + SandboxHelpers sandboxHelpers = new SandboxHelpers(/*delayVirtualInputMaterialization=*/ false); |
| 87 | + BinTools.PathActionInput tool = |
| 88 | + new BinTools.PathActionInput( |
| 89 | + scratch.file("tool", "#!/bin/bash", "echo hello"), |
| 90 | + PathFragment.create("_bin/say_hello")); |
| 91 | + |
| 92 | + SandboxInputs inputs = |
| 93 | + sandboxHelpers.processInputFiles(inputMap(tool), SPAWN, EMPTY_EXPANDER, execRoot); |
| 94 | + |
| 95 | + assertThat(inputs.getFiles()) |
| 96 | + .containsExactly( |
| 97 | + PathFragment.create("_bin/say_hello"), execRoot.getRelative("_bin/say_hello")); |
| 98 | + assertThat(inputs.getSymlinks()).isEmpty(); |
| 99 | + assertThat(FileSystemUtils.readLines(execRoot.getRelative("_bin/say_hello"), UTF_8)) |
| 100 | + .containsExactly("#!/bin/bash", "echo hello") |
| 101 | + .inOrder(); |
| 102 | + assertThat(execRoot.getRelative("_bin/say_hello").isExecutable()).isTrue(); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + public void processInputFiles_delayVirtualInputMaterialization_doesNotStoreVirtualInput() |
| 107 | + throws Exception { |
| 108 | + SandboxHelpers sandboxHelpers = new SandboxHelpers(/*delayVirtualInputMaterialization=*/ true); |
| 109 | + ParamFileActionInput paramFile = |
| 110 | + new ParamFileActionInput( |
| 111 | + PathFragment.create("paramFile"), |
| 112 | + ImmutableList.of("-a", "-b"), |
| 113 | + ParameterFileType.UNQUOTED, |
| 114 | + UTF_8); |
| 115 | + |
| 116 | + SandboxInputs inputs = |
| 117 | + sandboxHelpers.processInputFiles(inputMap(paramFile), SPAWN, EMPTY_EXPANDER, execRoot); |
| 118 | + |
| 119 | + assertThat(inputs.getFiles()).isEmpty(); |
| 120 | + assertThat(inputs.getSymlinks()).isEmpty(); |
| 121 | + assertThat(execRoot.getChild("paramFile").exists()).isFalse(); |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + public void sandboxInputMaterializeVirtualInputs_delayMaterialization_writesCorrectFiles() |
| 126 | + throws Exception { |
| 127 | + SandboxHelpers sandboxHelpers = new SandboxHelpers(/*delayVirtualInputMaterialization=*/ true); |
| 128 | + ParamFileActionInput paramFile = |
| 129 | + new ParamFileActionInput( |
| 130 | + PathFragment.create("paramFile"), |
| 131 | + ImmutableList.of("-a", "-b"), |
| 132 | + ParameterFileType.UNQUOTED, |
| 133 | + UTF_8); |
| 134 | + BinTools.PathActionInput tool = |
| 135 | + new BinTools.PathActionInput( |
| 136 | + scratch.file("tool", "tool_code"), PathFragment.create("tools/tool")); |
| 137 | + SandboxInputs inputs = |
| 138 | + sandboxHelpers.processInputFiles( |
| 139 | + inputMap(paramFile, tool), SPAWN, EMPTY_EXPANDER, execRoot); |
| 140 | + |
| 141 | + inputs.materializeVirtualInputs(scratch.dir("/sandbox")); |
| 142 | + |
| 143 | + Path sandboxParamFile = scratch.resolve("/sandbox/paramFile"); |
| 144 | + assertThat(FileSystemUtils.readLines(sandboxParamFile, UTF_8)) |
| 145 | + .containsExactly("-a", "-b") |
| 146 | + .inOrder(); |
| 147 | + assertThat(sandboxParamFile.isExecutable()).isTrue(); |
| 148 | + Path sandboxToolFile = scratch.resolve("/sandbox/tools/tool"); |
| 149 | + assertThat(FileSystemUtils.readLines(sandboxToolFile, UTF_8)).containsExactly("tool_code"); |
| 150 | + assertThat(sandboxToolFile.isExecutable()).isTrue(); |
| 151 | + } |
| 152 | + |
| 153 | + private static ImmutableMap<PathFragment, ActionInput> inputMap(ActionInput... inputs) { |
| 154 | + return Arrays.stream(inputs) |
| 155 | + .collect(toImmutableMap(ActionInput::getExecPath, Function.identity())); |
| 156 | + } |
| 157 | + |
| 158 | + @Test |
| 159 | + public void atomicallyWriteVirtualInput_writesParamFile() throws Exception { |
| 160 | + ParamFileActionInput paramFile = |
| 161 | + new ParamFileActionInput( |
| 162 | + PathFragment.create("paramFile"), |
| 163 | + ImmutableList.of("-a", "-b"), |
| 164 | + ParameterFileType.UNQUOTED, |
| 165 | + UTF_8); |
| 166 | + |
| 167 | + SandboxHelpers.atomicallyWriteVirtualInput( |
| 168 | + paramFile, scratch.resolve("/outputs/paramFile"), "-1234"); |
| 169 | + |
| 170 | + assertThat(scratch.resolve("/outputs").readdir(Symlinks.NOFOLLOW)) |
| 171 | + .containsExactly(new Dirent("paramFile", Dirent.Type.FILE)); |
| 172 | + Path outputFile = scratch.resolve("/outputs/paramFile"); |
| 173 | + assertThat(FileSystemUtils.readLines(outputFile, UTF_8)).containsExactly("-a", "-b").inOrder(); |
| 174 | + assertThat(outputFile.isExecutable()).isTrue(); |
| 175 | + } |
| 176 | + |
| 177 | + @Test |
| 178 | + public void atomicallyWriteVirtualInput_writesBinToolsFile() throws Exception { |
| 179 | + BinTools.PathActionInput tool = |
| 180 | + new BinTools.PathActionInput( |
| 181 | + scratch.file("tool", "tool_code"), PathFragment.create("tools/tool")); |
| 182 | + |
| 183 | + SandboxHelpers.atomicallyWriteVirtualInput(tool, scratch.resolve("/outputs/tool"), "-1234"); |
| 184 | + |
| 185 | + assertThat(scratch.resolve("/outputs").readdir(Symlinks.NOFOLLOW)) |
| 186 | + .containsExactly(new Dirent("tool", Dirent.Type.FILE)); |
| 187 | + Path outputFile = scratch.resolve("/outputs/tool"); |
| 188 | + assertThat(FileSystemUtils.readLines(outputFile, UTF_8)).containsExactly("tool_code"); |
| 189 | + assertThat(outputFile.isExecutable()).isTrue(); |
| 190 | + } |
| 191 | + |
| 192 | + @Test |
| 193 | + public void atomicallyWriteVirtualInput_writesArbitraryVirtualInput() throws Exception { |
| 194 | + VirtualActionInput input = ActionsTestUtil.createVirtualActionInput("file", "hello"); |
| 195 | + |
| 196 | + SandboxHelpers.atomicallyWriteVirtualInput(input, scratch.resolve("/outputs/file"), "-1234"); |
| 197 | + |
| 198 | + assertThat(scratch.resolve("/outputs").readdir(Symlinks.NOFOLLOW)) |
| 199 | + .containsExactly(new Dirent("file", Dirent.Type.FILE)); |
| 200 | + Path outputFile = scratch.resolve("/outputs/file"); |
| 201 | + assertThat(FileSystemUtils.readLines(outputFile, UTF_8)).containsExactly("hello"); |
| 202 | + assertThat(outputFile.isExecutable()).isTrue(); |
| 203 | + } |
| 204 | +} |
0 commit comments