-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
SpawnRunnerTestUtil.java
233 lines (196 loc) · 8.19 KB
/
SpawnRunnerTestUtil.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// Copyright 2022 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.devtools.build.lib.sandbox;
import static com.google.common.util.concurrent.Futures.immediateVoidFuture;
import static org.mockito.Mockito.mock;
import com.google.common.base.Preconditions;
import com.google.common.io.Files;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.devtools.build.lib.actions.ActionContext;
import com.google.devtools.build.lib.actions.ActionInput;
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.actions.Artifact.ArtifactExpander;
import com.google.devtools.build.lib.actions.InputMetadataProvider;
import com.google.devtools.build.lib.actions.Spawn;
import com.google.devtools.build.lib.exec.SpawnInputExpander;
import com.google.devtools.build.lib.exec.SpawnRunner.ProgressStatus;
import com.google.devtools.build.lib.exec.SpawnRunner.SpawnExecutionContext;
import com.google.devtools.build.lib.testutil.BlazeTestUtils;
import com.google.devtools.build.lib.testutil.TestConstants;
import com.google.devtools.build.lib.util.io.FileOutErr;
import com.google.devtools.build.lib.vfs.FileSystem;
import com.google.devtools.build.lib.vfs.Path;
import com.google.devtools.build.lib.vfs.PathFragment;
import java.io.File;
import java.io.IOException;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;
import javax.annotation.Nullable;
// TODO(b/62588075): Use this class for the LocalSpawnRunnerTest as well.
/**
* Utilities to help test SpawnRunners.
*
* <p>For example, to make embedded tools available for tests, or to use a rigged {@link
* SpawnExecutionContext} for testing purposes.
*/
public final class SpawnRunnerTestUtil {
private SpawnRunnerTestUtil() {}
/** A rigged spawn execution policy that can be used for testing purposes. */
public static final class SpawnExecutionContextForTesting implements SpawnExecutionContext {
private final List<ProgressStatus> reportedStatus = new ArrayList<>();
private final Spawn spawn;
private final Duration timeout;
private final FileOutErr fileOutErr;
/** An object that can expand middleman artifacts. */
private final ArtifactExpander artifactExpander =
new ArtifactExpander() {
@Override
public void expand(Artifact artifact, Collection<? super Artifact> output) {
// Do nothing.
}
};
/**
* Creates a new spawn execution policy for testing purposes.
*
* @param fileOutErr the {@link FileOutErr} object to use. After a {@link Spawn} is executed,
* its stdout and stderr can be available here, if the spawn runner uses the fileOutErr
* returned by {@link #getFileOutErr()} on the spawn execution policy
* @param timeout the timeout to use. Spawn runners may request this via {@link #getTimeout()}
*/
public SpawnExecutionContextForTesting(Spawn spawn, FileOutErr fileOutErr, Duration timeout) {
this.spawn = spawn;
this.fileOutErr = fileOutErr;
this.timeout = timeout;
}
@Override
public int getId() {
return 0;
}
@Override
public ListenableFuture<Void> prefetchInputs() {
return immediateVoidFuture();
}
@Override
public void lockOutputFiles(int exitCode, String errorMessage, FileOutErr outErr)
throws InterruptedException {}
@Override
public boolean speculating() {
return false;
}
@Override
public InputMetadataProvider getInputMetadataProvider() {
return mock(InputMetadataProvider.class);
}
@Override
public ArtifactExpander getArtifactExpander() {
return artifactExpander;
}
@Override
public SpawnInputExpander getSpawnInputExpander() {
throw new UnsupportedOperationException();
}
@Override
public Duration getTimeout() {
return timeout;
}
@Override
public FileOutErr getFileOutErr() {
return fileOutErr;
}
@Override
public SortedMap<PathFragment, ActionInput> getInputMapping(
PathFragment baseDirectory, boolean willAccessRepeatedly) {
TreeMap<PathFragment, ActionInput> inputMapping = new TreeMap<>();
for (ActionInput actionInput : spawn.getInputFiles().toList()) {
inputMapping.put(baseDirectory.getRelative(actionInput.getExecPath()), actionInput);
}
return inputMapping;
}
@Override
public void report(ProgressStatus progress) {
reportedStatus.add(progress);
}
@Override
public <T extends ActionContext> T getContext(Class<T> identifyingType) {
throw new UnsupportedOperationException();
}
@Override
public boolean isRewindingEnabled() {
return false;
}
@Override
public void checkForLostInputs() {}
@Nullable
@Override
public FileSystem getActionFileSystem() {
return null;
}
}
/**
* Copies a file into a specific path.
*
* @param sourceFile the file to copy
* @param destinationDirectoryPath the directory to copy the sourceFile into
*/
public static Path copyFileToPath(File sourceFile, Path destinationDirectoryPath)
throws IOException {
Preconditions.checkArgument(sourceFile.exists(), "source file to copy does not exist");
Preconditions.checkArgument(
destinationDirectoryPath.exists(), "destination directory to copy to does not exist");
Path destinationFilePath = destinationDirectoryPath.getRelative(sourceFile.getName());
File destinationFile = destinationFilePath.getPathFile();
Preconditions.checkState(!destinationFilePath.exists(), "destination file already exists");
Files.copy(sourceFile, destinationFile);
return destinationFilePath;
}
private static Path copyToolIntoPath(String sourceToolRelativePath, Path destinationDirectoryPath)
throws IOException {
File sourceToolFile =
new File(
PathFragment.create(BlazeTestUtils.runfilesDir())
.getRelative(sourceToolRelativePath)
.getPathString());
Preconditions.checkState(sourceToolFile.exists(), "tool not found");
Path binDirectoryPath = destinationDirectoryPath.getRelative("_bin");
binDirectoryPath.createDirectory();
Path destinationToolPath = copyFileToPath(sourceToolFile, binDirectoryPath);
destinationToolPath.setExecutable(true);
return destinationToolPath;
}
/** Copies the {@code process-wrapper} tool a path where a runner expects to find it. */
public static Path copyProcessWrapperIntoPath(Path destinationDirectoryPath) throws IOException {
return copyToolIntoPath(TestConstants.PROCESS_WRAPPER_PATH, destinationDirectoryPath);
}
/** Copies the {@code linux-sandbox} tool into a path where a runner expects to find it. */
public static Path copyLinuxSandboxIntoPath(Path destinationDirectoryPath) throws IOException {
return copyToolIntoPath(TestConstants.LINUX_SANDBOX_PATH, destinationDirectoryPath);
}
/** Copies the {@code spend_cpu_time} test util into a path where a runner expects to find it. */
public static Path copyCpuTimeSpenderIntoPath(Path destinationDirectoryPath) throws IOException {
File realCpuTimeSpenderFile =
new File(
PathFragment.create(BlazeTestUtils.runfilesDir())
.getRelative(TestConstants.CPU_TIME_SPENDER_PATH)
.getPathString());
Preconditions.checkState(realCpuTimeSpenderFile.exists(), "spend_cpu_time not found");
Path destinationCpuTimeSpenderPath =
copyFileToPath(realCpuTimeSpenderFile, destinationDirectoryPath);
destinationCpuTimeSpenderPath.setExecutable(true);
return destinationCpuTimeSpenderPath;
}
}