blob: 032f17668d923cd0a52a3f0874148e81c21fdfac [file] [log] [blame]
Chi Wang0cb411b2020-09-03 01:49:50 -07001// 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.
14package com.google.devtools.build.lib.remote;
15
16import static com.google.common.truth.Truth.assertThat;
Chi Wangae28ab72020-10-11 21:05:27 -070017import static java.nio.charset.StandardCharsets.UTF_8;
Chi Wang39512a42020-09-14 07:13:20 -070018import static org.junit.Assert.assertThrows;
Chi Wang0cb411b2020-09-03 01:49:50 -070019import static org.mockito.Mockito.mock;
20import static org.mockito.Mockito.when;
21
22import build.bazel.remote.execution.v2.ActionCacheUpdateCapabilities;
23import build.bazel.remote.execution.v2.CacheCapabilities;
24import build.bazel.remote.execution.v2.CapabilitiesGrpc.CapabilitiesImplBase;
25import build.bazel.remote.execution.v2.DigestFunction.Value;
26import build.bazel.remote.execution.v2.ExecutionCapabilities;
27import build.bazel.remote.execution.v2.GetCapabilitiesRequest;
28import build.bazel.remote.execution.v2.ServerCapabilities;
Chi Wangae28ab72020-10-11 21:05:27 -070029import com.google.auth.Credentials;
jhorvitz4182af42020-12-28 09:57:34 -080030import com.google.common.collect.ImmutableList;
Chi Wangae28ab72020-10-11 21:05:27 -070031import com.google.common.collect.ImmutableMap;
32import com.google.common.collect.Iterables;
Googlerc5ce7032020-11-12 01:26:47 -080033import com.google.common.eventbus.EventBus;
Chi Wang0cb411b2020-09-03 01:49:50 -070034import com.google.devtools.build.lib.analysis.BlazeDirectories;
35import com.google.devtools.build.lib.analysis.ServerDirectories;
Chi Wang0cb411b2020-09-03 01:49:50 -070036import com.google.devtools.build.lib.analysis.config.CoreOptions;
37import com.google.devtools.build.lib.authandtls.AuthAndTLSOptions;
Chi Wangae28ab72020-10-11 21:05:27 -070038import com.google.devtools.build.lib.authandtls.BasicHttpAuthenticationEncoder;
Googlerc5ce7032020-11-12 01:26:47 -080039import com.google.devtools.build.lib.events.Reporter;
Chi Wang0cb411b2020-09-03 01:49:50 -070040import com.google.devtools.build.lib.exec.BinTools;
Chi Wang39512a42020-09-14 07:13:20 -070041import com.google.devtools.build.lib.exec.ExecutionOptions;
Chi Wang0cb411b2020-09-03 01:49:50 -070042import com.google.devtools.build.lib.pkgcache.PackageOptions;
43import com.google.devtools.build.lib.remote.options.RemoteOptions;
Chi Wang0cb411b2020-09-03 01:49:50 -070044import com.google.devtools.build.lib.runtime.BlazeRuntime;
45import com.google.devtools.build.lib.runtime.BlazeServerStartupOptions;
46import com.google.devtools.build.lib.runtime.BlazeWorkspace;
47import com.google.devtools.build.lib.runtime.ClientOptions;
48import com.google.devtools.build.lib.runtime.Command;
49import com.google.devtools.build.lib.runtime.CommandEnvironment;
50import com.google.devtools.build.lib.runtime.CommonCommandOptions;
51import com.google.devtools.build.lib.runtime.commands.BuildCommand;
52import com.google.devtools.build.lib.testutil.Scratch;
53import com.google.devtools.build.lib.util.AbruptExitException;
janakr97c0bd12020-09-08 13:19:03 -070054import com.google.devtools.build.lib.vfs.DigestHashFunction;
Chi Wangae28ab72020-10-11 21:05:27 -070055import com.google.devtools.build.lib.vfs.FileSystem;
janakr97c0bd12020-09-08 13:19:03 -070056import com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem;
Chi Wang0cb411b2020-09-03 01:49:50 -070057import com.google.devtools.common.options.Options;
58import com.google.devtools.common.options.OptionsParser;
59import com.google.devtools.common.options.OptionsParsingResult;
60import io.grpc.BindableService;
61import io.grpc.Server;
62import io.grpc.ServerInterceptors;
63import io.grpc.inprocess.InProcessChannelBuilder;
64import io.grpc.inprocess.InProcessServerBuilder;
65import io.grpc.stub.StreamObserver;
66import io.grpc.util.MutableHandlerRegistry;
67import java.io.IOException;
Chi Wangae28ab72020-10-11 21:05:27 -070068import java.net.URI;
Chi Wang0cb411b2020-09-03 01:49:50 -070069import java.util.ArrayList;
Chi Wangae28ab72020-10-11 21:05:27 -070070import java.util.List;
71import java.util.Map;
Chi Wang0cb411b2020-09-03 01:49:50 -070072import org.junit.Test;
73import org.junit.runner.RunWith;
74import org.junit.runners.JUnit4;
75
76/** Tests for {@link RemoteModule}. */
77@RunWith(JUnit4.class)
jhorvitz4182af42020-12-28 09:57:34 -080078public final class RemoteModuleTest {
Chi Wang0cb411b2020-09-03 01:49:50 -070079
80 private static CommandEnvironment createTestCommandEnvironment(RemoteOptions remoteOptions)
81 throws IOException, AbruptExitException {
82 CoreOptions coreOptions = Options.getDefaults(CoreOptions.class);
83 CommonCommandOptions commonCommandOptions = Options.getDefaults(CommonCommandOptions.class);
84 PackageOptions packageOptions = Options.getDefaults(PackageOptions.class);
85 ClientOptions clientOptions = Options.getDefaults(ClientOptions.class);
Chi Wang39512a42020-09-14 07:13:20 -070086 ExecutionOptions executionOptions = Options.getDefaults(ExecutionOptions.class);
Chi Wang0cb411b2020-09-03 01:49:50 -070087
88 AuthAndTLSOptions authAndTLSOptions = Options.getDefaults(AuthAndTLSOptions.class);
89
90 OptionsParsingResult options = mock(OptionsParsingResult.class);
91 when(options.getOptions(CoreOptions.class)).thenReturn(coreOptions);
92 when(options.getOptions(CommonCommandOptions.class)).thenReturn(commonCommandOptions);
93 when(options.getOptions(PackageOptions.class)).thenReturn(packageOptions);
94 when(options.getOptions(ClientOptions.class)).thenReturn(clientOptions);
95 when(options.getOptions(RemoteOptions.class)).thenReturn(remoteOptions);
96 when(options.getOptions(AuthAndTLSOptions.class)).thenReturn(authAndTLSOptions);
Chi Wang39512a42020-09-14 07:13:20 -070097 when(options.getOptions(ExecutionOptions.class)).thenReturn(executionOptions);
Chi Wang0cb411b2020-09-03 01:49:50 -070098
99 String productName = "bazel";
janakr97c0bd12020-09-08 13:19:03 -0700100 Scratch scratch = new Scratch(new InMemoryFileSystem(DigestHashFunction.SHA256));
Chi Wang0cb411b2020-09-03 01:49:50 -0700101 ServerDirectories serverDirectories =
102 new ServerDirectories(
103 scratch.dir("install"), scratch.dir("output"), scratch.dir("user_root"));
104 BlazeRuntime runtime =
105 new BlazeRuntime.Builder()
106 .setProductName(productName)
107 .setFileSystem(scratch.getFileSystem())
108 .setServerDirectories(serverDirectories)
109 .setStartupOptionsProvider(
110 OptionsParser.builder().optionsClasses(BlazeServerStartupOptions.class).build())
Chi Wang0cb411b2020-09-03 01:49:50 -0700111 .build();
112
113 BlazeDirectories directories =
114 new BlazeDirectories(
115 serverDirectories,
116 scratch.dir("/workspace"),
117 scratch.dir("/system_javabase"),
118 productName);
119 BlazeWorkspace workspace = runtime.initWorkspace(directories, BinTools.empty(directories));
120 Command command = BuildCommand.class.getAnnotation(Command.class);
janakr8e881f72021-07-07 22:57:33 -0700121 return workspace.initCommand(
122 command, options, new ArrayList<>(), 0, 0, ImmutableList.of(), s -> {});
Chi Wang0cb411b2020-09-03 01:49:50 -0700123 }
124
125 static class CapabilitiesImpl extends CapabilitiesImplBase {
126
127 private int requestCount;
128 private final ServerCapabilities caps;
129
130 CapabilitiesImpl(ServerCapabilities caps) {
131 this.caps = caps;
132 }
133
134 @Override
135 public void getCapabilities(
136 GetCapabilitiesRequest request, StreamObserver<ServerCapabilities> responseObserver) {
137 ++requestCount;
138 responseObserver.onNext(caps);
139 responseObserver.onCompleted();
140 }
141
jhorvitz4182af42020-12-28 09:57:34 -0800142 int getRequestCount() {
Chi Wang0cb411b2020-09-03 01:49:50 -0700143 return requestCount;
144 }
145 }
146
147 private static Server createFakeServer(String serverName, BindableService... services) {
148 MutableHandlerRegistry executionServerRegistry = new MutableHandlerRegistry();
149 for (BindableService service : services) {
150 executionServerRegistry.addService(ServerInterceptors.intercept(service));
151 }
152 return InProcessServerBuilder.forName(serverName)
153 .fallbackHandlerRegistry(executionServerRegistry)
154 .directExecutor()
155 .build();
156 }
157
158 @Test
159 public void testVerifyCapabilities_executionAndCacheForSingleEndpoint() throws Exception {
160 ServerCapabilities caps =
161 ServerCapabilities.newBuilder()
162 .setLowApiVersion(ApiVersion.current.toSemVer())
163 .setHighApiVersion(ApiVersion.current.toSemVer())
164 .setExecutionCapabilities(
165 ExecutionCapabilities.newBuilder()
166 .setExecEnabled(true)
167 .setDigestFunction(Value.SHA256)
168 .build())
169 .setCacheCapabilities(
Googler7f40eb92021-08-12 01:42:29 -0700170 CacheCapabilities.newBuilder().addDigestFunctions(Value.SHA256).build())
Chi Wang0cb411b2020-09-03 01:49:50 -0700171 .build();
172 CapabilitiesImpl executionServerCapabilitiesImpl = new CapabilitiesImpl(caps);
173 String executionServerName = "execution-server";
174 Server executionServer = createFakeServer(executionServerName, executionServerCapabilitiesImpl);
175 executionServer.start();
176
177 try {
178 RemoteModule remoteModule = new RemoteModule();
179 remoteModule.setChannelFactory(
180 (target, proxy, options, interceptors) ->
181 InProcessChannelBuilder.forName(target).directExecutor().build());
182
183 RemoteOptions remoteOptions = Options.getDefaults(RemoteOptions.class);
184 remoteOptions.remoteExecutor = executionServerName;
185
186 CommandEnvironment env = createTestCommandEnvironment(remoteOptions);
187
188 remoteModule.beforeCommand(env);
189
190 assertThat(Thread.interrupted()).isFalse();
191 assertThat(executionServerCapabilitiesImpl.getRequestCount()).isEqualTo(1);
192 } finally {
193 executionServer.shutdownNow();
194 executionServer.awaitTermination();
195 }
196 }
197
198 @Test
199 public void testVerifyCapabilities_cacheOnlyEndpoint() throws Exception {
200 ServerCapabilities cacheOnlyCaps =
201 ServerCapabilities.newBuilder()
202 .setLowApiVersion(ApiVersion.current.toSemVer())
203 .setHighApiVersion(ApiVersion.current.toSemVer())
204 .setCacheCapabilities(
205 CacheCapabilities.newBuilder()
Googler7f40eb92021-08-12 01:42:29 -0700206 .addDigestFunctions(Value.SHA256)
Chi Wang0cb411b2020-09-03 01:49:50 -0700207 .setActionCacheUpdateCapabilities(
208 ActionCacheUpdateCapabilities.newBuilder().setUpdateEnabled(true).build())
209 .build())
210 .build();
211 CapabilitiesImpl cacheServerCapabilitiesImpl = new CapabilitiesImpl(cacheOnlyCaps);
212 String cacheServerName = "cache-server";
213 Server cacheServer = createFakeServer(cacheServerName, cacheServerCapabilitiesImpl);
214 cacheServer.start();
215
216 try {
217 RemoteModule remoteModule = new RemoteModule();
218 remoteModule.setChannelFactory(
219 (target, proxy, options, interceptors) ->
220 InProcessChannelBuilder.forName(target).directExecutor().build());
221
222 RemoteOptions remoteOptions = Options.getDefaults(RemoteOptions.class);
223 remoteOptions.remoteCache = cacheServerName;
224
225 CommandEnvironment env = createTestCommandEnvironment(remoteOptions);
226
227 remoteModule.beforeCommand(env);
228
229 assertThat(Thread.interrupted()).isFalse();
230 assertThat(cacheServerCapabilitiesImpl.getRequestCount()).isEqualTo(1);
231 } finally {
232 cacheServer.shutdownNow();
233 cacheServer.awaitTermination();
234 }
235 }
236
237 @Test
238 public void testVerifyCapabilities_executionAndCacheForDifferentEndpoints() throws Exception {
239 ServerCapabilities caps =
240 ServerCapabilities.newBuilder()
241 .setLowApiVersion(ApiVersion.current.toSemVer())
242 .setHighApiVersion(ApiVersion.current.toSemVer())
243 .setExecutionCapabilities(
244 ExecutionCapabilities.newBuilder()
245 .setExecEnabled(true)
246 .setDigestFunction(Value.SHA256)
247 .build())
248 .setCacheCapabilities(
Googler7f40eb92021-08-12 01:42:29 -0700249 CacheCapabilities.newBuilder().addDigestFunctions(Value.SHA256).build())
Chi Wang0cb411b2020-09-03 01:49:50 -0700250 .build();
251 CapabilitiesImpl executionServerCapabilitiesImpl = new CapabilitiesImpl(caps);
252 String executionServerName = "execution-server";
253 Server executionServer = createFakeServer(executionServerName, executionServerCapabilitiesImpl);
254 executionServer.start();
255
256 CapabilitiesImpl cacheServerCapabilitiesImpl = new CapabilitiesImpl(caps);
257 String cacheServerName = "cache-server";
258 Server cacheServer = createFakeServer(cacheServerName, cacheServerCapabilitiesImpl);
259 cacheServer.start();
260
261 try {
262 RemoteModule remoteModule = new RemoteModule();
263 remoteModule.setChannelFactory(
264 (target, proxy, options, interceptors) ->
265 InProcessChannelBuilder.forName(target).directExecutor().build());
266
267 RemoteOptions remoteOptions = Options.getDefaults(RemoteOptions.class);
268 remoteOptions.remoteExecutor = executionServerName;
269 remoteOptions.remoteCache = cacheServerName;
270
271 CommandEnvironment env = createTestCommandEnvironment(remoteOptions);
272
273 remoteModule.beforeCommand(env);
274
275 assertThat(Thread.interrupted()).isFalse();
276 assertThat(executionServerCapabilitiesImpl.getRequestCount()).isEqualTo(1);
277 assertThat(cacheServerCapabilitiesImpl.getRequestCount()).isEqualTo(1);
278 } finally {
279 executionServer.shutdownNow();
280 cacheServer.shutdownNow();
281
282 executionServer.awaitTermination();
283 cacheServer.awaitTermination();
284 }
285 }
286
287 @Test
288 public void testVerifyCapabilities_executionOnlyAndCacheOnlyEndpoints() throws Exception {
289 ServerCapabilities executionOnlyCaps =
290 ServerCapabilities.newBuilder()
291 .setLowApiVersion(ApiVersion.current.toSemVer())
292 .setHighApiVersion(ApiVersion.current.toSemVer())
293 .setExecutionCapabilities(
294 ExecutionCapabilities.newBuilder()
295 .setExecEnabled(true)
296 .setDigestFunction(Value.SHA256)
297 .build())
298 .build();
299 CapabilitiesImpl executionServerCapabilitiesImpl = new CapabilitiesImpl(executionOnlyCaps);
300 String executionServerName = "execution-server";
301 Server executionServer = createFakeServer(executionServerName, executionServerCapabilitiesImpl);
302 executionServer.start();
303
304 ServerCapabilities cacheOnlyCaps =
305 ServerCapabilities.newBuilder()
Chi Wang39512a42020-09-14 07:13:20 -0700306 .setLowApiVersion(ApiVersion.current.toSemVer())
307 .setHighApiVersion(ApiVersion.current.toSemVer())
Chi Wang0cb411b2020-09-03 01:49:50 -0700308 .setCacheCapabilities(
Googler7f40eb92021-08-12 01:42:29 -0700309 CacheCapabilities.newBuilder().addDigestFunctions(Value.SHA256).build())
Chi Wang0cb411b2020-09-03 01:49:50 -0700310 .build();
311 CapabilitiesImpl cacheServerCapabilitiesImpl = new CapabilitiesImpl(cacheOnlyCaps);
312 String cacheServerName = "cache-server";
313 Server cacheServer = createFakeServer(cacheServerName, cacheServerCapabilitiesImpl);
314 cacheServer.start();
315
316 try {
317 RemoteModule remoteModule = new RemoteModule();
318 remoteModule.setChannelFactory(
319 (target, proxy, options, interceptors) ->
320 InProcessChannelBuilder.forName(target).directExecutor().build());
321
322 RemoteOptions remoteOptions = Options.getDefaults(RemoteOptions.class);
323 remoteOptions.remoteExecutor = executionServerName;
324 remoteOptions.remoteCache = cacheServerName;
325
326 CommandEnvironment env = createTestCommandEnvironment(remoteOptions);
327
328 remoteModule.beforeCommand(env);
329
330 assertThat(Thread.interrupted()).isFalse();
331 assertThat(executionServerCapabilitiesImpl.getRequestCount()).isEqualTo(1);
332 assertThat(cacheServerCapabilitiesImpl.getRequestCount()).isEqualTo(1);
333 } finally {
334 executionServer.shutdownNow();
335 cacheServer.shutdownNow();
336
337 executionServer.awaitTermination();
338 cacheServer.awaitTermination();
339 }
340 }
Chi Wang39512a42020-09-14 07:13:20 -0700341
342 @Test
343 public void testLocalFallback_shouldErrorForRemoteCacheWithoutRequiredCapabilities()
344 throws Exception {
345 ServerCapabilities noneCaps =
346 ServerCapabilities.newBuilder()
347 .setLowApiVersion(ApiVersion.current.toSemVer())
348 .setHighApiVersion(ApiVersion.current.toSemVer())
349 .build();
350 CapabilitiesImpl cacheServerCapabilitiesImpl = new CapabilitiesImpl(noneCaps);
351 String cacheServerName = "cache-server";
352 Server cacheServer = createFakeServer(cacheServerName, cacheServerCapabilitiesImpl);
353 cacheServer.start();
354
355 try {
356 RemoteModule remoteModule = new RemoteModule();
357 RemoteOptions remoteOptions = Options.getDefaults(RemoteOptions.class);
358 remoteOptions.remoteCache = cacheServerName;
359 remoteOptions.remoteLocalFallback = true;
360 remoteModule.setChannelFactory(
361 (target, proxy, options, interceptors) ->
362 InProcessChannelBuilder.forName(target).directExecutor().build());
363
364 CommandEnvironment env = createTestCommandEnvironment(remoteOptions);
365
jhorvitz4182af42020-12-28 09:57:34 -0800366 assertThrows(AbruptExitException.class, () -> remoteModule.beforeCommand(env));
Chi Wang39512a42020-09-14 07:13:20 -0700367 } finally {
368 cacheServer.shutdownNow();
369 cacheServer.awaitTermination();
370 }
371 }
372
373 @Test
374 public void testLocalFallback_shouldErrorInaccessibleGrpcRemoteCacheIfFlagNotSet()
375 throws Exception {
376 String cacheServerName = "cache-server";
377 CapabilitiesImplBase cacheServerCapabilitiesImpl =
378 new CapabilitiesImplBase() {
379 @Override
380 public void getCapabilities(
381 GetCapabilitiesRequest request, StreamObserver<ServerCapabilities> responseObserver) {
382 responseObserver.onError(new UnsupportedOperationException());
383 }
384 };
385 Server cacheServer = createFakeServer(cacheServerName, cacheServerCapabilitiesImpl);
386 cacheServer.start();
387
388 try {
389 RemoteModule remoteModule = new RemoteModule();
390 RemoteOptions remoteOptions = Options.getDefaults(RemoteOptions.class);
391 remoteOptions.remoteCache = cacheServerName;
392 remoteOptions.remoteLocalFallback = false;
393 remoteModule.setChannelFactory(
394 (target, proxy, options, interceptors) ->
395 InProcessChannelBuilder.forName(target).directExecutor().build());
396
397 CommandEnvironment env = createTestCommandEnvironment(remoteOptions);
398
jhorvitz4182af42020-12-28 09:57:34 -0800399 assertThrows(AbruptExitException.class, () -> remoteModule.beforeCommand(env));
Chi Wang39512a42020-09-14 07:13:20 -0700400 } finally {
401 cacheServer.shutdownNow();
402 cacheServer.awaitTermination();
403 }
404 }
405
406 @Test
407 public void testLocalFallback_shouldIgnoreInaccessibleGrpcRemoteCache() throws Exception {
408 String cacheServerName = "cache-server";
409 CapabilitiesImplBase cacheServerCapabilitiesImpl =
410 new CapabilitiesImplBase() {
411 @Override
412 public void getCapabilities(
413 GetCapabilitiesRequest request, StreamObserver<ServerCapabilities> responseObserver) {
414 responseObserver.onError(new UnsupportedOperationException());
415 }
416 };
417 Server cacheServer = createFakeServer(cacheServerName, cacheServerCapabilitiesImpl);
418 cacheServer.start();
419
420 try {
421 RemoteModule remoteModule = new RemoteModule();
422 RemoteOptions remoteOptions = Options.getDefaults(RemoteOptions.class);
423 remoteOptions.remoteCache = cacheServerName;
424 remoteOptions.remoteLocalFallback = true;
425 remoteModule.setChannelFactory(
426 (target, proxy, options, interceptors) ->
427 InProcessChannelBuilder.forName(target).directExecutor().build());
428
429 CommandEnvironment env = createTestCommandEnvironment(remoteOptions);
430
431 remoteModule.beforeCommand(env);
432
433 assertThat(Thread.interrupted()).isFalse();
Chi Wang7643d3a2021-06-09 00:29:55 -0700434 RemoteActionContextProvider actionContextProvider = remoteModule.getActionContextProvider();
435 assertThat(actionContextProvider).isNotNull();
436 assertThat(actionContextProvider.getRemoteCache()).isNull();
437 assertThat(actionContextProvider.getRemoteExecutionClient()).isNull();
Chi Wang39512a42020-09-14 07:13:20 -0700438 } finally {
439 cacheServer.shutdownNow();
440 cacheServer.awaitTermination();
441 }
442 }
Chi Wangae28ab72020-10-11 21:05:27 -0700443
444 @Test
Chi Wang7643d3a2021-06-09 00:29:55 -0700445 public void testLocalFallback_shouldIgnoreInaccessibleGrpcRemoteExecutor() throws Exception {
446 CapabilitiesImplBase executionServerCapabilitiesImpl =
447 new CapabilitiesImplBase() {
448 @Override
449 public void getCapabilities(
450 GetCapabilitiesRequest request, StreamObserver<ServerCapabilities> responseObserver) {
451 responseObserver.onError(new UnsupportedOperationException());
452 }
453 };
454 String executionServerName = "execution-server";
455 Server executionServer = createFakeServer(executionServerName, executionServerCapabilitiesImpl);
456 executionServer.start();
457
458 try {
459 RemoteModule remoteModule = new RemoteModule();
460 RemoteOptions remoteOptions = Options.getDefaults(RemoteOptions.class);
461 remoteOptions.remoteExecutor = executionServerName;
462 remoteOptions.remoteLocalFallback = true;
463 remoteModule.setChannelFactory(
464 (target, proxy, options, interceptors) ->
465 InProcessChannelBuilder.forName(target).directExecutor().build());
466
467 CommandEnvironment env = createTestCommandEnvironment(remoteOptions);
468
469 remoteModule.beforeCommand(env);
470
471 assertThat(Thread.interrupted()).isFalse();
472 RemoteActionContextProvider actionContextProvider = remoteModule.getActionContextProvider();
473 assertThat(actionContextProvider).isNotNull();
474 assertThat(actionContextProvider.getRemoteCache()).isNull();
475 assertThat(actionContextProvider.getRemoteExecutionClient()).isNull();
476 } finally {
477 executionServer.shutdownNow();
478 executionServer.awaitTermination();
479 }
480 }
481
482 @Test
Chi Wangae28ab72020-10-11 21:05:27 -0700483 public void testNetrc_emptyEnv_shouldIgnore() throws Exception {
484 Map<String, String> clientEnv = ImmutableMap.of();
485 FileSystem fileSystem = new InMemoryFileSystem(DigestHashFunction.SHA256);
486
487 Credentials credentials = RemoteModule.newCredentialsFromNetrc(clientEnv, fileSystem);
488
489 assertThat(credentials).isNull();
490 }
491
492 @Test
493 public void testNetrc_netrcNotExist_shouldIgnore() throws Exception {
494 String home = "/home/foo";
495 Map<String, String> clientEnv = ImmutableMap.of("HOME", home);
496 FileSystem fileSystem = new InMemoryFileSystem(DigestHashFunction.SHA256);
497
498 Credentials credentials = RemoteModule.newCredentialsFromNetrc(clientEnv, fileSystem);
499
500 assertThat(credentials).isNull();
501 }
502
503 @Test
504 public void testNetrc_netrcExist_shouldUse() throws Exception {
505 String home = "/home/foo";
506 Map<String, String> clientEnv = ImmutableMap.of("HOME", home);
507 FileSystem fileSystem = new InMemoryFileSystem(DigestHashFunction.SHA256);
508 Scratch scratch = new Scratch(fileSystem);
509 scratch.file(home + "/.netrc", "machine foo.example.org login foouser password foopass");
510
511 Credentials credentials = RemoteModule.newCredentialsFromNetrc(clientEnv, fileSystem);
512
513 assertThat(credentials).isNotNull();
514 assertRequestMetadata(
515 credentials.getRequestMetadata(URI.create("https://foo.example.org")),
516 "foouser",
517 "foopass");
518 }
519
520 @Test
521 public void testNetrc_netrcFromNetrcEnvExist_shouldUse() throws Exception {
522 String home = "/home/foo";
523 String netrc = "/.netrc";
524 Map<String, String> clientEnv = ImmutableMap.of("HOME", home, "NETRC", netrc);
525 FileSystem fileSystem = new InMemoryFileSystem(DigestHashFunction.SHA256);
526 Scratch scratch = new Scratch(fileSystem);
527 scratch.file(home + "/.netrc", "machine foo.example.org login foouser password foopass");
528 scratch.file(netrc, "machine foo.example.org login baruser password barpass");
529
530 Credentials credentials = RemoteModule.newCredentialsFromNetrc(clientEnv, fileSystem);
531
532 assertThat(credentials).isNotNull();
533 assertRequestMetadata(
534 credentials.getRequestMetadata(URI.create("https://foo.example.org")),
535 "baruser",
536 "barpass");
537 }
538
539 @Test
540 public void testNetrc_netrcFromNetrcEnvNotExist_shouldIgnore() throws Exception {
541 String home = "/home/foo";
542 String netrc = "/.netrc";
543 Map<String, String> clientEnv = ImmutableMap.of("HOME", home, "NETRC", netrc);
544 FileSystem fileSystem = new InMemoryFileSystem(DigestHashFunction.SHA256);
545 Scratch scratch = new Scratch(fileSystem);
546 scratch.file(home + "/.netrc", "machine foo.example.org login foouser password foopass");
547
548 Credentials credentials = RemoteModule.newCredentialsFromNetrc(clientEnv, fileSystem);
549
550 assertThat(credentials).isNull();
551 }
552
Googlerc5ce7032020-11-12 01:26:47 -0800553 @Test
554 public void testNetrc_netrcWithoutRemoteCache() throws Exception {
555 String netrc = "/.netrc";
556 Map<String, String> clientEnv = ImmutableMap.of("NETRC", netrc);
557 FileSystem fileSystem = new InMemoryFileSystem(DigestHashFunction.SHA256);
558 Scratch scratch = new Scratch(fileSystem);
559 scratch.file(netrc, "machine foo.example.org login baruser password barpass");
560 AuthAndTLSOptions authAndTLSOptions = Options.getDefaults(AuthAndTLSOptions.class);
561 RemoteOptions remoteOptions = Options.getDefaults(RemoteOptions.class);
562 Reporter reporter = new Reporter(new EventBus());
563
564 Credentials credentials =
565 RemoteModule.newCredentials(
566 clientEnv, fileSystem, reporter, authAndTLSOptions, remoteOptions);
567
568 assertThat(credentials).isNotNull();
569 }
570
Chi Wangae28ab72020-10-11 21:05:27 -0700571 private static void assertRequestMetadata(
572 Map<String, List<String>> requestMetadata, String username, String password) {
573 assertThat(requestMetadata.keySet()).containsExactly("Authorization");
574 assertThat(Iterables.getOnlyElement(requestMetadata.values()))
575 .containsExactly(BasicHttpAuthenticationEncoder.encode(username, password, UTF_8));
576 }
Chi Wang0cb411b2020-09-03 01:49:50 -0700577}