forked from bazelbuild/bazel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LocalSpawnRunnerTest.java
1108 lines (953 loc) · 44.7 KB
/
LocalSpawnRunnerTest.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2017 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.exec.local;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.util.concurrent.Futures.immediateVoidFuture;
import static com.google.devtools.build.lib.testing.common.DirectoryListingHelper.file;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertThrows;
import static org.junit.Assume.assumeTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.matches;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.io.ByteStreams;
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.ArtifactExpander;
import com.google.devtools.build.lib.actions.CommandLines.ParamFileActionInput;
import com.google.devtools.build.lib.actions.ExecutionRequirements;
import com.google.devtools.build.lib.actions.InputMetadataProvider;
import com.google.devtools.build.lib.actions.LocalHostCapacity;
import com.google.devtools.build.lib.actions.ParameterFile.ParameterFileType;
import com.google.devtools.build.lib.actions.ResourceManager;
import com.google.devtools.build.lib.actions.ResourceSet;
import com.google.devtools.build.lib.actions.Spawn;
import com.google.devtools.build.lib.actions.SpawnResult;
import com.google.devtools.build.lib.actions.SpawnResult.Status;
import com.google.devtools.build.lib.actions.cache.VirtualActionInput;
import com.google.devtools.build.lib.actions.util.ActionsTestUtil;
import com.google.devtools.build.lib.exec.BinTools;
import com.google.devtools.build.lib.exec.RunfilesTreeUpdater;
import com.google.devtools.build.lib.exec.SpawnExecutingEvent;
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.exec.SpawnSchedulingEvent;
import com.google.devtools.build.lib.exec.util.SpawnBuilder;
import com.google.devtools.build.lib.runtime.ProcessWrapper;
import com.google.devtools.build.lib.shell.JavaSubprocessFactory;
import com.google.devtools.build.lib.shell.Subprocess;
import com.google.devtools.build.lib.shell.SubprocessBuilder;
import com.google.devtools.build.lib.shell.SubprocessBuilder.StreamAction;
import com.google.devtools.build.lib.shell.SubprocessFactory;
import com.google.devtools.build.lib.testing.common.DirectoryListingHelper;
import com.google.devtools.build.lib.testutil.BlazeTestUtils;
import com.google.devtools.build.lib.testutil.TestConstants;
import com.google.devtools.build.lib.testutil.TestUtils;
import com.google.devtools.build.lib.unix.UnixFileSystem;
import com.google.devtools.build.lib.util.NetUtil;
import com.google.devtools.build.lib.util.OS;
import com.google.devtools.build.lib.util.io.FileOutErr;
import com.google.devtools.build.lib.vfs.DigestHashFunction;
import com.google.devtools.build.lib.vfs.FileSystem;
import com.google.devtools.build.lib.vfs.FileSystemUtils;
import com.google.devtools.build.lib.vfs.JavaIoFileSystem;
import com.google.devtools.build.lib.vfs.Path;
import com.google.devtools.build.lib.vfs.PathFragment;
import com.google.devtools.build.lib.vfs.SyscallCache;
import com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem;
import com.google.devtools.common.options.Converters.RegexPatternConverter;
import com.google.devtools.common.options.Options;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.Semaphore;
import java.util.concurrent.ThreadLocalRandom;
import java.util.logging.Filter;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import javax.annotation.Nullable;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
/** Unit tests for {@link LocalSpawnRunner}. */
@RunWith(JUnit4.class)
public class LocalSpawnRunnerTest {
private static class TestedLocalSpawnRunner extends LocalSpawnRunner {
public TestedLocalSpawnRunner(
Path execRoot,
LocalExecutionOptions localExecutionOptions,
ResourceManager resourceManager,
ProcessWrapper processWrapper,
LocalEnvProvider localEnvProvider) {
super(
execRoot,
localExecutionOptions,
resourceManager,
localEnvProvider,
/*binTools=*/ null,
processWrapper,
SyscallCache.NO_CACHE,
Mockito.mock(RunfilesTreeUpdater.class));
}
// Rigged to act on supplied filesystem (e.g. InMemoryFileSystem) for testing purposes
// TODO(b/70572634): Update FileSystem abstraction to support createTempDirectory() from
// the java.nio.file.Files package.
@Override
protected Path createActionTemp(Path execRoot) throws IOException {
Path tempDirPath;
do {
String idStr =
Long.toHexString(Thread.currentThread().getId())
+ "_"
+ Long.toHexString(ThreadLocalRandom.current().nextLong());
tempDirPath = execRoot.getRelative("tmp" + idStr);
} while (tempDirPath.exists());
if (!tempDirPath.createDirectory()) {
throw new IOException(String.format("Could not create temp directory '%s'", tempDirPath));
}
return tempDirPath;
}
}
private static class FinishedSubprocess implements Subprocess {
private final int exitCode;
public FinishedSubprocess(int exitCode) {
this.exitCode = exitCode;
}
@Override
public boolean destroy() {
return false;
}
@Override
public int exitValue() {
return exitCode;
}
@Override
public boolean finished() {
return true;
}
@Override
public boolean isAlive() {
return false;
}
@Override
public boolean timedout() {
return false;
}
@Override
public void waitFor() throws InterruptedException {
// Do nothing.
}
@Override
public OutputStream getOutputStream() {
return ByteStreams.nullOutputStream();
}
@Override
public InputStream getInputStream() {
return new ByteArrayInputStream(new byte[0]);
}
@Override
public InputStream getErrorStream() {
return new ByteArrayInputStream(new byte[0]);
}
@Override
public void close() {
// Do nothing.
}
@Override
public long getProcessId() {
return 0;
}
}
private static final Spawn SIMPLE_SPAWN =
new SpawnBuilder("/bin/echo", "Hi!").withEnvironment("VARIABLE", "value").build();
private static final class SubprocessInterceptor implements SubprocessFactory {
@Override
public Subprocess create(SubprocessBuilder params) throws IOException {
throw new UnsupportedOperationException();
}
}
private final class SpawnExecutionContextForTesting implements SpawnExecutionContext {
private final List<ProgressStatus> reportedStatus = new ArrayList<>();
private final TreeMap<PathFragment, ActionInput> inputMapping = new TreeMap<>();
private long timeoutMillis;
private boolean prefetchCalled;
private boolean lockOutputFilesCalled;
private FileOutErr fileOutErr;
public SpawnExecutionContextForTesting(FileOutErr fileOutErr) {
this.fileOutErr = fileOutErr;
}
@Override
public int getId() {
return 0;
}
@Override
public ListenableFuture<Void> prefetchInputs() {
prefetchCalled = true;
return immediateVoidFuture();
}
@Override
public void lockOutputFiles(int exitCode, String errorMessage, FileOutErr outErr)
throws InterruptedException {
lockOutputFilesCalled = true;
}
@Override
public boolean speculating() {
return false;
}
@Override
public InputMetadataProvider getInputMetadataProvider() {
return mockFileCache;
}
@Override
public ArtifactExpander getArtifactExpander() {
throw new UnsupportedOperationException();
}
@Override
public SpawnInputExpander getSpawnInputExpander() {
throw new UnsupportedOperationException();
}
@Override
public Duration getTimeout() {
return Duration.ofMillis(timeoutMillis);
}
@Override
public FileOutErr getFileOutErr() {
return fileOutErr;
}
@Override
public SortedMap<PathFragment, ActionInput> getInputMapping(
PathFragment baseDirectory, boolean willAccessRepeatedly) {
return inputMapping;
}
@Override
public void report(ProgressStatus progress) {
reportedStatus.add(progress);
}
@Override
public boolean isRewindingEnabled() {
return false;
}
@Override
public void checkForLostInputs() {}
@Override
public <T extends ActionContext> T getContext(Class<T> identifyingType) {
throw new UnsupportedOperationException();
}
@Nullable
@Override
public FileSystem getActionFileSystem() {
return null;
}
}
private final InputMetadataProvider mockFileCache = mock(InputMetadataProvider.class);
private final ResourceManager resourceManager = ResourceManager.instanceForTestingOnly();
private Logger logger;
private static ImmutableMap<String, String> keepLocalEnvUnchanged(
Map<String, String> env, BinTools binTools, String fallbackTmpDir) {
return ImmutableMap.copyOf(env);
}
@Before
public final void suppressLogging() {
logger = Logger.getLogger(TestedLocalSpawnRunner.class.getName());
logger.setFilter(
new Filter() {
@Override
public boolean isLoggable(LogRecord record) {
return false;
}
});
}
private FileSystem setupEnvironmentForFakeExecution() throws InterruptedException, IOException {
// Prevent any subprocess execution at all.
SubprocessBuilder.setDefaultSubprocessFactory(new SubprocessInterceptor());
resourceManager.setAvailableResources(
ResourceSet.create(/*memoryMb=*/ 1, /*cpuUsage=*/ 1, /*localTestCount=*/ 1));
return new InMemoryFileSystem(DigestHashFunction.SHA256);
}
private static ProcessWrapper makeProcessWrapper(FileSystem fs, LocalExecutionOptions options) {
return new ProcessWrapper(
fs.getPath("/process-wrapper"),
options.getLocalSigkillGraceSeconds(),
/*gracefulSigterm=*/ false);
}
/**
* Enables real execution by default.
*
* <p>Tests should call setupEnvironmentForFakeExecution() if they do not want real execution.
*/
@Before
public final void setupEnvironmentForRealExecution() throws InterruptedException, IOException {
SubprocessBuilder.setDefaultSubprocessFactory(JavaSubprocessFactory.INSTANCE);
resourceManager.setAvailableResources(LocalHostCapacity.getLocalHostCapacity());
}
@Test
public void vanillaZeroExit() throws Exception {
// TODO(#3536): Make this test work on Windows.
// The Command API implicitly absolutizes the path, and we get weird paths on Windows:
// T:\execroot\execroot\_bin\process-wrapper
assumeTrue(OS.getCurrent() != OS.WINDOWS);
FileSystem fs = setupEnvironmentForFakeExecution();
SubprocessFactory factory = mock(SubprocessFactory.class);
ArgumentCaptor<SubprocessBuilder> captor = ArgumentCaptor.forClass(SubprocessBuilder.class);
when(factory.create(captor.capture())).thenReturn(new FinishedSubprocess(0));
SubprocessBuilder.setDefaultSubprocessFactory(factory);
LocalExecutionOptions options = Options.getDefaults(LocalExecutionOptions.class);
options.localSigkillGraceSeconds = 456;
options.collectLocalExecutionStatistics = false;
LocalSpawnRunner runner =
new TestedLocalSpawnRunner(
fs.getPath("/execroot"),
options,
resourceManager,
makeProcessWrapper(fs, options),
LocalSpawnRunnerTest::keepLocalEnvUnchanged);
FileOutErr fileOutErr = new FileOutErr(fs.getPath("/out/stdout"), fs.getPath("/out/stderr"));
SpawnExecutionContextForTesting policy = new SpawnExecutionContextForTesting(fileOutErr);
policy.timeoutMillis = 123 * 1000L;
assertThat(fs.getPath("/execroot").createDirectory()).isTrue();
SpawnResult result = runner.exec(SIMPLE_SPAWN, policy);
verify(factory).create(any(SubprocessBuilder.class));
assertThat(result.status()).isEqualTo(SpawnResult.Status.SUCCESS);
assertThat(result.exitCode()).isEqualTo(0);
assertThat(result.setupSuccess()).isTrue();
assertThat(result.getExecutorHostName()).isEqualTo(NetUtil.getCachedShortHostName());
assertThat(captor.getValue().getArgv())
.containsExactlyElementsIn(
ImmutableList.of(
"/process-wrapper", "--timeout=123", "--kill_delay=456", "/bin/echo", "Hi!"));
assertThat(captor.getValue().getEnv()).containsExactly("VARIABLE", "value");
assertThat(captor.getValue().getTimeoutMillis()).isEqualTo(0);
assertThat(captor.getValue().getStdout()).isEqualTo(StreamAction.REDIRECT);
assertThat(captor.getValue().getStdoutFile()).isEqualTo(new File("/out/stdout"));
assertThat(captor.getValue().getStderr()).isEqualTo(StreamAction.REDIRECT);
assertThat(captor.getValue().getStderrFile()).isEqualTo(new File("/out/stderr"));
assertThat(policy.lockOutputFilesCalled).isTrue();
assertThat(policy.reportedStatus)
.containsExactly(SpawnSchedulingEvent.create("local"), SpawnExecutingEvent.create("local"))
.inOrder();
}
@Test
public void testParamFiles() throws Exception {
// TODO(#3536): Make this test work on Windows.
// The Command API implicitly absolutizes the path, and we get weird paths on Windows:
// T:\execroot\execroot\_bin\process-wrapper
assumeTrue(OS.getCurrent() != OS.WINDOWS);
FileSystem fs = setupEnvironmentForFakeExecution();
SubprocessFactory factory = mock(SubprocessFactory.class);
when(factory.create(any())).thenReturn(new FinishedSubprocess(0));
SubprocessBuilder.setDefaultSubprocessFactory(factory);
LocalExecutionOptions options = Options.getDefaults(LocalExecutionOptions.class);
options.localSigkillGraceSeconds = 456;
options.collectLocalExecutionStatistics = false;
Path execRoot = fs.getPath("/execroot");
LocalSpawnRunner runner =
new TestedLocalSpawnRunner(
execRoot,
options,
resourceManager,
makeProcessWrapper(fs, options),
LocalSpawnRunnerTest::keepLocalEnvUnchanged);
ParamFileActionInput paramFileActionInput =
new ParamFileActionInput(
PathFragment.create("some/dir/params"),
ImmutableList.of("--foo", "--bar"),
ParameterFileType.UNQUOTED,
UTF_8);
Spawn spawn =
new SpawnBuilder("/bin/echo", "Hi!")
.withInput(paramFileActionInput)
.withEnvironment("VARIABLE", "value")
.build();
FileOutErr fileOutErr = new FileOutErr(fs.getPath("/out/stdout"), fs.getPath("/out/stderr"));
SpawnExecutionContextForTesting policy = new SpawnExecutionContextForTesting(fileOutErr);
policy.timeoutMillis = 123 * 1000L;
assertThat(fs.getPath("/execroot").createDirectory()).isTrue();
SpawnResult result = runner.exec(spawn, policy);
assertThat(result.status()).isEqualTo(SpawnResult.Status.SUCCESS);
assertThat(result.exitCode()).isEqualTo(0);
assertThat(result.setupSuccess()).isTrue();
assertThat(result.getExecutorHostName()).isEqualTo(NetUtil.getCachedShortHostName());
Path paramFile = execRoot.getRelative("some/dir/params");
assertThat(paramFile.exists()).isTrue();
try (InputStream inputStream = paramFile.getInputStream()) {
assertThat(new String(ByteStreams.toByteArray(inputStream), UTF_8).split("\n"))
.asList()
.containsExactly("--foo", "--bar");
}
}
@Test
public void exec_materializesVirtualInputAsExecutable() throws Exception {
FileSystem fs = setupEnvironmentForFakeExecution();
SubprocessFactory factory = mock(SubprocessFactory.class);
when(factory.create(any())).thenReturn(new FinishedSubprocess(0));
SubprocessBuilder.setDefaultSubprocessFactory(factory);
Path execRoot = fs.getPath("/execroot");
LocalExecutionOptions options = Options.getDefaults(LocalExecutionOptions.class);
options.collectLocalExecutionStatistics = false;
LocalSpawnRunner runner =
new TestedLocalSpawnRunner(
execRoot,
options,
resourceManager,
makeProcessWrapper(fs, options),
LocalSpawnRunnerTest::keepLocalEnvUnchanged);
VirtualActionInput virtualInput = ActionsTestUtil.createVirtualActionInput("input1", "hello");
Spawn spawn = new SpawnBuilder("/bin/true").withInput(virtualInput).build();
FileOutErr fileOutErr = new FileOutErr(fs.getPath("/out/stdout"), fs.getPath("/out/stderr"));
SpawnExecutionContext spawnExecutionContext = new SpawnExecutionContextForTesting(fileOutErr);
SpawnResult result = runner.exec(spawn, spawnExecutionContext);
assertThat(result.status()).isEqualTo(Status.SUCCESS);
assertThat(DirectoryListingHelper.leafDirectoryEntries(execRoot))
.containsExactly(file("input1"));
Path inputPath = execRoot.getRelative(virtualInput.getExecPath());
assertThat(inputPath.isExecutable()).isTrue();
assertThat(FileSystemUtils.readLinesAsLatin1(inputPath)).containsExactly("hello");
}
@Test
public void noProcessWrapper() throws Exception {
// TODO(#3536): Make this test work on Windows.
// The Command API implicitly absolutizes the path, and we get weird paths on Windows:
// T:\execroot\bin\echo
assumeTrue(OS.getCurrent() != OS.WINDOWS);
FileSystem fs = setupEnvironmentForFakeExecution();
SubprocessFactory factory = mock(SubprocessFactory.class);
ArgumentCaptor<SubprocessBuilder> captor = ArgumentCaptor.forClass(SubprocessBuilder.class);
when(factory.create(captor.capture())).thenReturn(new FinishedSubprocess(0));
SubprocessBuilder.setDefaultSubprocessFactory(factory);
LocalExecutionOptions options = Options.getDefaults(LocalExecutionOptions.class);
options.localSigkillGraceSeconds = 456;
LocalSpawnRunner runner =
new TestedLocalSpawnRunner(
fs.getPath("/execroot"),
options,
resourceManager,
/*processWrapper=*/ null,
LocalSpawnRunnerTest::keepLocalEnvUnchanged);
FileOutErr fileOutErr = new FileOutErr(fs.getPath("/out/stdout"), fs.getPath("/out/stderr"));
SpawnExecutionContextForTesting policy = new SpawnExecutionContextForTesting(fileOutErr);
policy.timeoutMillis = 123 * 1000L;
assertThat(fs.getPath("/execroot").createDirectory()).isTrue();
SpawnResult result = runner.exec(SIMPLE_SPAWN, policy);
verify(factory).create(any());
assertThat(result.status()).isEqualTo(SpawnResult.Status.SUCCESS);
assertThat(result.exitCode()).isEqualTo(0);
assertThat(result.setupSuccess()).isTrue();
assertThat(result.getExecutorHostName()).isEqualTo(NetUtil.getCachedShortHostName());
assertThat(captor.getValue().getArgv())
.containsExactlyElementsIn(ImmutableList.of("/bin/echo", "Hi!"));
assertThat(captor.getValue().getEnv()).containsExactly("VARIABLE", "value");
// Without the process wrapper, we use the Command API to enforce the timeout.
assertThat(captor.getValue().getTimeoutMillis()).isEqualTo(policy.timeoutMillis);
assertThat(policy.lockOutputFilesCalled).isTrue();
}
@Test
public void nonZeroExit() throws Exception {
// TODO(#3536): Make this test work on Windows.
// The Command API implicitly absolutizes the path, and we get weird paths on Windows:
// T:\execroot\execroot\_bin\process-wrapper
assumeTrue(OS.getCurrent() != OS.WINDOWS);
FileSystem fs = setupEnvironmentForFakeExecution();
SubprocessFactory factory = mock(SubprocessFactory.class);
ArgumentCaptor<SubprocessBuilder> captor = ArgumentCaptor.forClass(SubprocessBuilder.class);
when(factory.create(captor.capture())).thenReturn(new FinishedSubprocess(3));
SubprocessBuilder.setDefaultSubprocessFactory(factory);
LocalExecutionOptions options = Options.getDefaults(LocalExecutionOptions.class);
options.collectLocalExecutionStatistics = false;
LocalSpawnRunner runner =
new TestedLocalSpawnRunner(
fs.getPath("/execroot"),
options,
resourceManager,
makeProcessWrapper(fs, options),
LocalSpawnRunnerTest::keepLocalEnvUnchanged);
assertThat(fs.getPath("/execroot").createDirectory()).isTrue();
FileOutErr fileOutErr = new FileOutErr(fs.getPath("/out/stdout"), fs.getPath("/out/stderr"));
SpawnExecutionContextForTesting policy = new SpawnExecutionContextForTesting(fileOutErr);
SpawnResult result = runner.exec(SIMPLE_SPAWN, policy);
verify(factory).create(any(SubprocessBuilder.class));
assertThat(result.status()).isEqualTo(SpawnResult.Status.NON_ZERO_EXIT);
assertThat(result.exitCode()).isEqualTo(3);
assertThat(result.setupSuccess()).isTrue();
assertThat(result.getExecutorHostName()).isEqualTo(NetUtil.getCachedShortHostName());
assertThat(captor.getValue().getArgv())
.containsExactlyElementsIn(
ImmutableList.of(
"/process-wrapper", "--timeout=0", "--kill_delay=15", "/bin/echo", "Hi!"));
assertThat(captor.getValue().getEnv()).containsExactly("VARIABLE", "value");
assertThat(captor.getValue().getStdout()).isEqualTo(StreamAction.REDIRECT);
assertThat(captor.getValue().getStdoutFile()).isEqualTo(new File("/out/stdout"));
assertThat(captor.getValue().getStderr()).isEqualTo(StreamAction.REDIRECT);
assertThat(captor.getValue().getStderrFile()).isEqualTo(new File("/out/stderr"));
assertThat(policy.lockOutputFilesCalled).isTrue();
}
@Test
public void processStartupThrows() throws Exception {
FileSystem fs = setupEnvironmentForFakeExecution();
SubprocessFactory factory = mock(SubprocessFactory.class);
ArgumentCaptor<SubprocessBuilder> captor = ArgumentCaptor.forClass(SubprocessBuilder.class);
when(factory.create(captor.capture())).thenThrow(new IOException("I'm sorry, Dave"));
SubprocessBuilder.setDefaultSubprocessFactory(factory);
LocalExecutionOptions options = Options.getDefaults(LocalExecutionOptions.class);
LocalSpawnRunner runner =
new TestedLocalSpawnRunner(
fs.getPath("/execroot"),
options,
resourceManager,
makeProcessWrapper(fs, options),
LocalSpawnRunnerTest::keepLocalEnvUnchanged);
assertThat(fs.getPath("/out").createDirectory()).isTrue();
assertThat(fs.getPath("/execroot").createDirectory()).isTrue();
FileOutErr fileOutErr = new FileOutErr(fs.getPath("/out/stdout"), fs.getPath("/out/stderr"));
SpawnExecutionContextForTesting policy = new SpawnExecutionContextForTesting(fileOutErr);
SpawnResult result = runner.exec(SIMPLE_SPAWN, policy);
verify(factory).create(any(SubprocessBuilder.class));
assertThat(result.status()).isEqualTo(SpawnResult.Status.EXECUTION_FAILED);
assertThat(result.exitCode()).isEqualTo(-1);
assertThat(result.setupSuccess()).isFalse();
assertThat(result.getWallTimeInMs()).isEqualTo(0);
assertThat(result.getUserTimeInMs()).isEqualTo(0);
assertThat(result.getSystemTimeInMs()).isEqualTo(0);
assertThat(result.getExecutorHostName()).isEqualTo(NetUtil.getCachedShortHostName());
assertThat(FileSystemUtils.readContent(fs.getPath("/out/stderr"), UTF_8))
.isEqualTo("Action failed to execute: java.io.IOException: I'm sorry, Dave\n");
assertThat(policy.lockOutputFilesCalled).isTrue();
}
@Test
public void disallowLocalExecution() throws Exception {
FileSystem fs = setupEnvironmentForFakeExecution();
LocalExecutionOptions options = Options.getDefaults(LocalExecutionOptions.class);
options.allowedLocalAction = new RegexPatternConverter().convert("none");
LocalSpawnRunner runner =
new TestedLocalSpawnRunner(
fs.getPath("/execroot"),
options,
resourceManager,
makeProcessWrapper(fs, options),
LocalSpawnRunnerTest::keepLocalEnvUnchanged);
assertThat(fs.getPath("/execroot").createDirectory()).isTrue();
FileOutErr fileOutErr = new FileOutErr();
SpawnExecutionContextForTesting policy = new SpawnExecutionContextForTesting(fileOutErr);
SpawnResult reply = runner.exec(SIMPLE_SPAWN, policy);
assertThat(reply.status()).isEqualTo(SpawnResult.Status.EXECUTION_DENIED);
assertThat(reply.exitCode()).isEqualTo(-1);
assertThat(reply.setupSuccess()).isFalse();
assertThat(reply.getWallTimeInMs()).isEqualTo(0);
assertThat(reply.getUserTimeInMs()).isEqualTo(0);
assertThat(reply.getSystemTimeInMs()).isEqualTo(0);
assertThat(reply.getExecutorHostName()).isEqualTo(NetUtil.getCachedShortHostName());
// TODO(ulfjack): Maybe we should only lock after checking?
assertThat(policy.lockOutputFilesCalled).isTrue();
}
@Test
public void interruptedException() throws Exception {
FileSystem fs = setupEnvironmentForFakeExecution();
SubprocessFactory factory = mock(SubprocessFactory.class);
ArgumentCaptor<SubprocessBuilder> captor = ArgumentCaptor.forClass(SubprocessBuilder.class);
when(factory.create(captor.capture()))
.thenReturn(
new FinishedSubprocess(3) {
private boolean destroyed;
@Override
public boolean destroy() {
destroyed = true;
return true;
}
@Override
public void waitFor() throws InterruptedException {
if (!destroyed) {
throw new InterruptedException();
}
}
});
SubprocessBuilder.setDefaultSubprocessFactory(factory);
LocalExecutionOptions options = Options.getDefaults(LocalExecutionOptions.class);
LocalSpawnRunner runner =
new TestedLocalSpawnRunner(
fs.getPath("/execroot"),
options,
resourceManager,
makeProcessWrapper(fs, options),
LocalSpawnRunnerTest::keepLocalEnvUnchanged);
FileOutErr fileOutErr = new FileOutErr(fs.getPath("/out/stdout"), fs.getPath("/out/stderr"));
SpawnExecutionContextForTesting policy = new SpawnExecutionContextForTesting(fileOutErr);
assertThat(fs.getPath("/execroot").createDirectory()).isTrue();
assertThrows(InterruptedException.class, () -> runner.exec(SIMPLE_SPAWN, policy));
Thread.interrupted();
assertThat(policy.lockOutputFilesCalled).isTrue();
}
@Test
public void interruptWaitsForProcessExit() throws Exception {
assumeTrue(OS.getCurrent() != OS.WINDOWS);
Path tempDir = TestUtils.createUniqueTmpDir(new JavaIoFileSystem(DigestHashFunction.SHA256));
LocalSpawnRunner runner =
new LocalSpawnRunner(
tempDir,
Options.getDefaults(LocalExecutionOptions.class),
resourceManager,
LocalEnvProvider.forCurrentOs(ImmutableMap.of()),
/*binTools=*/ null,
/*processWrapper=*/ null,
SyscallCache.NO_CACHE,
Mockito.mock(RunfilesTreeUpdater.class));
FileOutErr fileOutErr =
new FileOutErr(tempDir.getRelative("stdout"), tempDir.getRelative("stderr"));
SpawnExecutionContextForTesting policy = new SpawnExecutionContextForTesting(fileOutErr);
// This test to exercise a race condition by attempting an operation multiple times. We can get
// false positives (the test passing without us catching a problem), so try a few times. When
// implementing this fix on 2019-09-11, this specific configuration was sufficient to catch the
// previously-existent bug.
int tries = 10;
int delaySeconds = 1;
Path content = tempDir.getChild("content");
Path started = tempDir.getChild("started");
// Start a subprocess that blocks until it is killed, and when it is, writes some output to
// a temporary file after some delay.
String script =
"trap 'sleep "
+ delaySeconds
+ "; echo foo >"
+ content.getPathString()
+ "; exit 1' TERM; "
+ "touch "
+ started.getPathString()
+ "; "
+ "while :; do "
+ " echo 'waiting to be killed'; "
+ " sleep 1; "
+ "done";
Spawn spawn = new SpawnBuilder("/bin/sh", "-c", script).build();
ExecutorService executor = Executors.newSingleThreadExecutor();
try {
for (int i = 0; i < tries; i++) {
content.delete();
started.delete();
Semaphore interruptCaught = new Semaphore(0);
Future<?> future =
executor.submit(
() -> {
try {
runner.exec(spawn, policy);
} catch (InterruptedException e) {
interruptCaught.release();
} catch (Throwable t) {
throw new IllegalStateException(t);
}
});
// Wait until we know the subprocess has started so that delivering a termination signal
// to it triggers the delayed write to the file.
while (!started.exists()) {
Thread.sleep(1);
}
future.cancel(true);
interruptCaught.acquireUninterruptibly();
// At this point, the subprocess must have fully stopped so write some content to the file
// and expect that these contents remain unmodified.
FileSystemUtils.writeContent(content, StandardCharsets.UTF_8, "bar");
// Wait for longer than the spawn takes to exit before we check the file contents to ensure
// that we properly awaited for termination of the subprocess.
Thread.sleep(delaySeconds * 2 * 1000);
assertThat(FileSystemUtils.readContent(content, StandardCharsets.UTF_8)).isEqualTo("bar");
}
} finally {
executor.shutdown();
}
}
@Test
public void checkPrefetchCalled() throws Exception {
FileSystem fs = setupEnvironmentForFakeExecution();
SubprocessFactory factory = mock(SubprocessFactory.class);
when(factory.create(any())).thenReturn(new FinishedSubprocess(0));
SubprocessBuilder.setDefaultSubprocessFactory(factory);
LocalExecutionOptions options = Options.getDefaults(LocalExecutionOptions.class);
options.collectLocalExecutionStatistics = false;
LocalSpawnRunner runner =
new TestedLocalSpawnRunner(
fs.getPath("/execroot"),
options,
resourceManager,
makeProcessWrapper(fs, options),
LocalSpawnRunnerTest::keepLocalEnvUnchanged);
FileOutErr fileOutErr = new FileOutErr(fs.getPath("/out/stdout"), fs.getPath("/out/stderr"));
SpawnExecutionContextForTesting policy = new SpawnExecutionContextForTesting(fileOutErr);
policy.timeoutMillis = 123 * 1000L;
assertThat(fs.getPath("/execroot").createDirectory()).isTrue();
runner.exec(SIMPLE_SPAWN, policy);
assertThat(policy.prefetchCalled).isTrue();
}
@Test
public void checkNoPrefetchCalled() throws Exception {
FileSystem fs = setupEnvironmentForFakeExecution();
SubprocessFactory factory = mock(SubprocessFactory.class);
when(factory.create(any())).thenReturn(new FinishedSubprocess(0));
SubprocessBuilder.setDefaultSubprocessFactory(factory);
LocalExecutionOptions options = Options.getDefaults(LocalExecutionOptions.class);
options.collectLocalExecutionStatistics = false;
LocalSpawnRunner runner =
new TestedLocalSpawnRunner(
fs.getPath("/execroot"),
options,
resourceManager,
makeProcessWrapper(fs, options),
LocalSpawnRunnerTest::keepLocalEnvUnchanged);
FileOutErr fileOutErr = new FileOutErr(fs.getPath("/out/stdout"), fs.getPath("/out/stderr"));
SpawnExecutionContextForTesting policy = new SpawnExecutionContextForTesting(fileOutErr);
policy.timeoutMillis = 123 * 1000L;
Spawn spawn =
new SpawnBuilder("/bin/echo", "Hi!")
.withExecutionInfo(ExecutionRequirements.DISABLE_LOCAL_PREFETCH, "")
.build();
assertThat(fs.getPath("/execroot").createDirectory()).isTrue();
runner.exec(spawn, policy);
assertThat(policy.prefetchCalled).isFalse();
}
@Test
public void checkLocalEnvProviderCalled() throws Exception {
FileSystem fs = setupEnvironmentForFakeExecution();
SubprocessFactory factory = mock(SubprocessFactory.class);
when(factory.create(any())).thenReturn(new FinishedSubprocess(0));
SubprocessBuilder.setDefaultSubprocessFactory(factory);
LocalEnvProvider localEnvProvider = mock(LocalEnvProvider.class);
LocalExecutionOptions options = Options.getDefaults(LocalExecutionOptions.class);
options.collectLocalExecutionStatistics = false;
LocalSpawnRunner runner =
new TestedLocalSpawnRunner(
fs.getPath("/execroot"),
options,
resourceManager,
makeProcessWrapper(fs, options),
localEnvProvider);
FileOutErr fileOutErr = new FileOutErr(fs.getPath("/out/stdout"), fs.getPath("/out/stderr"));
SpawnExecutionContextForTesting policy = new SpawnExecutionContextForTesting(fileOutErr);
policy.timeoutMillis = 123 * 1000L;
assertThat(fs.getPath("/execroot").createDirectory()).isTrue();
runner.exec(SIMPLE_SPAWN, policy);
verify(localEnvProvider)
.rewriteLocalEnv(any(), any(), matches("^/execroot/tmp[0-9a-fA-F]+_[0-9a-fA-F]+/work$"));
}
/**
* Copies the {@code process-wrapper} tool into the path under the temporary execRoot where the
* {@link LocalSpawnRunner} expects to find it.
*/
private void copyProcessWrapperIntoExecRoot(Path wrapperPath) throws IOException {
File realProcessWrapperFile =
new File(
PathFragment.create(BlazeTestUtils.runfilesDir())
.getRelative(TestConstants.PROCESS_WRAPPER_PATH)
.getPathString());
assertThat(realProcessWrapperFile.exists()).isTrue();
wrapperPath.createDirectoryAndParents();
File wrapperFile = wrapperPath.getPathFile();
wrapperPath.delete();
Files.copy(realProcessWrapperFile, wrapperFile);
assertThat(wrapperPath.exists()).isTrue();
wrapperPath.setExecutable(true);
}
/**
* Copies the {@code spend_cpu_time} test util into the temporary execRoot so that the {@link
* LocalSpawnRunner} can execute it.
*/
private Path copyCpuTimeSpenderIntoExecRoot(Path execRoot) throws IOException {
File realCpuTimeSpenderFile =
new File(
PathFragment.create(BlazeTestUtils.runfilesDir())
.getRelative(TestConstants.CPU_TIME_SPENDER_PATH)
.getPathString());
assertThat(realCpuTimeSpenderFile.exists()).isTrue();
Path execRootCpuTimeSpenderPath = execRoot.getRelative("spend-cpu-time");
File execRootCpuTimeSpenderFile = execRootCpuTimeSpenderPath.getPathFile();
assertThat(execRootCpuTimeSpenderPath.exists()).isFalse();
Files.copy(realCpuTimeSpenderFile, execRootCpuTimeSpenderFile);
assertThat(execRootCpuTimeSpenderPath.exists()).isTrue();
execRootCpuTimeSpenderPath.setExecutable(true);
return execRootCpuTimeSpenderPath;
}
private Path getTemporaryRoot(FileSystem fs, String name) throws IOException {
Path tempDirPath = TestUtils.createUniqueTmpDir(fs);
assertThat(tempDirPath.exists()).isTrue();
Path root = tempDirPath.getRelative(name);
assertThat(root.createDirectory()).isTrue();
assertThat(root.exists()).isTrue();
return root;
}
/**
* Returns an execRoot {@link Path} inside a new temporary directory.
*
* <p>The temporary directory will be automatically deleted on exit.
*/
private Path getTemporaryExecRoot(FileSystem fs) throws IOException {
return getTemporaryRoot(fs, "execRoot");
}
private Path getTemporaryEmbeddedBin(FileSystem fs) throws IOException {
return getTemporaryRoot(fs, "embedded_bin");
}
@Test
public void hasExecutionStatistics_whenOptionIsEnabled() throws Exception {
// TODO(b/62588075) Currently no process-wrapper or execution statistics support in Windows.
assumeTrue(OS.getCurrent() != OS.WINDOWS);
FileSystem fs = new UnixFileSystem(DigestHashFunction.SHA256, /*hashAttributeName=*/ "");
LocalExecutionOptions options = Options.getDefaults(LocalExecutionOptions.class);
options.collectLocalExecutionStatistics = true;
int minimumWallTimeToSpendInMs = 10 * 1000;
int minimumUserTimeToSpendInMs = minimumWallTimeToSpendInMs;
// Under normal loads we should be able to use a much lower bound for maxUserTime, but be
// generous here in case of hardware issues.
int maximumUserTimeToSpendInMs = minimumUserTimeToSpendInMs + 20 * 1000;
int minimumSystemTimeToSpendInMs = 0;
// Under normal loads we should be able to use a much lower bound for maxSysTime, but be
// generous here in case of hardware issues.
int maximumSystemTimeToSpendInMs = minimumSystemTimeToSpendInMs + 20 * 1000;
Path execRoot = getTemporaryExecRoot(fs);
Path embeddedBinaries = getTemporaryEmbeddedBin(fs);
BinTools binTools =
BinTools.forEmbeddedBin(embeddedBinaries, ImmutableList.of("process-wrapper"));
Path processWrapperPath = binTools.getEmbeddedPath("process-wrapper");
copyProcessWrapperIntoExecRoot(processWrapperPath);
Path cpuTimeSpenderPath = copyCpuTimeSpenderIntoExecRoot(execRoot);
LocalSpawnRunner runner =
new LocalSpawnRunner(
execRoot,
options,
resourceManager,
LocalSpawnRunnerTest::keepLocalEnvUnchanged,
binTools,
new ProcessWrapper(
processWrapperPath, /*killDelay=*/ Duration.ZERO, /*gracefulSigterm=*/ false),
SyscallCache.NO_CACHE,
Mockito.mock(RunfilesTreeUpdater.class));
Spawn spawn =
new SpawnBuilder(
cpuTimeSpenderPath.getPathString(),
String.valueOf(minimumUserTimeToSpendInMs / 1000L),
String.valueOf(minimumSystemTimeToSpendInMs / 1000L))
.build();
FileOutErr fileOutErr = new FileOutErr(fs.getPath("/dev/null"), fs.getPath("/dev/null"));
SpawnExecutionContextForTesting policy = new SpawnExecutionContextForTesting(fileOutErr);
SpawnResult spawnResult = runner.exec(spawn, policy);
assertThat(spawnResult.status()).isEqualTo(SpawnResult.Status.SUCCESS);
assertThat(spawnResult.exitCode()).isEqualTo(0);
assertThat(spawnResult.setupSuccess()).isTrue();
assertThat(spawnResult.getExecutorHostName()).isEqualTo(NetUtil.getCachedShortHostName());
assertThat(spawnResult.getWallTimeInMs()).isNotNull();