/
RemoteModuleTest.java
579 lines (500 loc) · 24.2 KB
/
RemoteModuleTest.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
// Copyright 2020 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.remote;
import static com.google.common.truth.Truth.assertThat;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import build.bazel.remote.execution.v2.ActionCacheUpdateCapabilities;
import build.bazel.remote.execution.v2.CacheCapabilities;
import build.bazel.remote.execution.v2.CapabilitiesGrpc.CapabilitiesImplBase;
import build.bazel.remote.execution.v2.DigestFunction.Value;
import build.bazel.remote.execution.v2.ExecutionCapabilities;
import build.bazel.remote.execution.v2.GetCapabilitiesRequest;
import build.bazel.remote.execution.v2.ServerCapabilities;
import com.google.auth.Credentials;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.google.common.eventbus.EventBus;
import com.google.devtools.build.lib.analysis.BlazeDirectories;
import com.google.devtools.build.lib.analysis.ServerDirectories;
import com.google.devtools.build.lib.analysis.config.CoreOptions;
import com.google.devtools.build.lib.authandtls.AuthAndTLSOptions;
import com.google.devtools.build.lib.authandtls.BasicHttpAuthenticationEncoder;
import com.google.devtools.build.lib.events.Reporter;
import com.google.devtools.build.lib.exec.BinTools;
import com.google.devtools.build.lib.exec.ExecutionOptions;
import com.google.devtools.build.lib.pkgcache.PackageOptions;
import com.google.devtools.build.lib.remote.options.RemoteOptions;
import com.google.devtools.build.lib.runtime.BlazeRuntime;
import com.google.devtools.build.lib.runtime.BlazeServerStartupOptions;
import com.google.devtools.build.lib.runtime.BlazeWorkspace;
import com.google.devtools.build.lib.runtime.ClientOptions;
import com.google.devtools.build.lib.runtime.Command;
import com.google.devtools.build.lib.runtime.CommandEnvironment;
import com.google.devtools.build.lib.runtime.CommonCommandOptions;
import com.google.devtools.build.lib.runtime.commands.BuildCommand;
import com.google.devtools.build.lib.testutil.Scratch;
import com.google.devtools.build.lib.util.AbruptExitException;
import com.google.devtools.build.lib.vfs.DigestHashFunction;
import com.google.devtools.build.lib.vfs.FileSystem;
import com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem;
import com.google.devtools.common.options.Options;
import com.google.devtools.common.options.OptionsParser;
import com.google.devtools.common.options.OptionsParsingResult;
import io.grpc.BindableService;
import io.grpc.Server;
import io.grpc.ServerInterceptors;
import io.grpc.inprocess.InProcessChannelBuilder;
import io.grpc.inprocess.InProcessServerBuilder;
import io.grpc.stub.StreamObserver;
import io.grpc.util.MutableHandlerRegistry;
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
/** Tests for {@link RemoteModule}. */
@RunWith(JUnit4.class)
public final class RemoteModuleTest {
private static CommandEnvironment createTestCommandEnvironment(RemoteOptions remoteOptions)
throws IOException, AbruptExitException {
CoreOptions coreOptions = Options.getDefaults(CoreOptions.class);
CommonCommandOptions commonCommandOptions = Options.getDefaults(CommonCommandOptions.class);
PackageOptions packageOptions = Options.getDefaults(PackageOptions.class);
ClientOptions clientOptions = Options.getDefaults(ClientOptions.class);
ExecutionOptions executionOptions = Options.getDefaults(ExecutionOptions.class);
AuthAndTLSOptions authAndTLSOptions = Options.getDefaults(AuthAndTLSOptions.class);
OptionsParsingResult options = mock(OptionsParsingResult.class);
when(options.getOptions(CoreOptions.class)).thenReturn(coreOptions);
when(options.getOptions(CommonCommandOptions.class)).thenReturn(commonCommandOptions);
when(options.getOptions(PackageOptions.class)).thenReturn(packageOptions);
when(options.getOptions(ClientOptions.class)).thenReturn(clientOptions);
when(options.getOptions(RemoteOptions.class)).thenReturn(remoteOptions);
when(options.getOptions(AuthAndTLSOptions.class)).thenReturn(authAndTLSOptions);
when(options.getOptions(ExecutionOptions.class)).thenReturn(executionOptions);
String productName = "bazel";
Scratch scratch = new Scratch(new InMemoryFileSystem(DigestHashFunction.SHA256));
ServerDirectories serverDirectories =
new ServerDirectories(
scratch.dir("install"), scratch.dir("output"), scratch.dir("user_root"));
BlazeRuntime runtime =
new BlazeRuntime.Builder()
.setProductName(productName)
.setFileSystem(scratch.getFileSystem())
.setServerDirectories(serverDirectories)
.setStartupOptionsProvider(
OptionsParser.builder().optionsClasses(BlazeServerStartupOptions.class).build())
.build();
BlazeDirectories directories =
new BlazeDirectories(
serverDirectories,
scratch.dir("/workspace"),
scratch.dir("/system_javabase"),
productName);
BlazeWorkspace workspace = runtime.initWorkspace(directories, BinTools.empty(directories));
Command command = BuildCommand.class.getAnnotation(Command.class);
return workspace.initCommand(
command, options, new ArrayList<>(), 0, 0, ImmutableList.of(), s -> {});
}
static class CapabilitiesImpl extends CapabilitiesImplBase {
private int requestCount;
private final ServerCapabilities caps;
CapabilitiesImpl(ServerCapabilities caps) {
this.caps = caps;
}
@Override
public void getCapabilities(
GetCapabilitiesRequest request, StreamObserver<ServerCapabilities> responseObserver) {
++requestCount;
responseObserver.onNext(caps);
responseObserver.onCompleted();
}
int getRequestCount() {
return requestCount;
}
}
private static Server createFakeServer(String serverName, BindableService... services) {
MutableHandlerRegistry executionServerRegistry = new MutableHandlerRegistry();
for (BindableService service : services) {
executionServerRegistry.addService(ServerInterceptors.intercept(service));
}
return InProcessServerBuilder.forName(serverName)
.fallbackHandlerRegistry(executionServerRegistry)
.directExecutor()
.build();
}
@Test
public void testVerifyCapabilities_executionAndCacheForSingleEndpoint() throws Exception {
ServerCapabilities caps =
ServerCapabilities.newBuilder()
.setLowApiVersion(ApiVersion.current.toSemVer())
.setHighApiVersion(ApiVersion.current.toSemVer())
.setExecutionCapabilities(
ExecutionCapabilities.newBuilder()
.setExecEnabled(true)
.setDigestFunction(Value.SHA256)
.build())
.setCacheCapabilities(
CacheCapabilities.newBuilder().addDigestFunctions(Value.SHA256).build())
.build();
CapabilitiesImpl executionServerCapabilitiesImpl = new CapabilitiesImpl(caps);
String executionServerName = "execution-server";
Server executionServer = createFakeServer(executionServerName, executionServerCapabilitiesImpl);
executionServer.start();
try {
RemoteModule remoteModule = new RemoteModule();
remoteModule.setChannelFactory(
(target, proxy, options, interceptors) ->
InProcessChannelBuilder.forName(target).directExecutor().build());
RemoteOptions remoteOptions = Options.getDefaults(RemoteOptions.class);
remoteOptions.remoteExecutor = executionServerName;
CommandEnvironment env = createTestCommandEnvironment(remoteOptions);
remoteModule.beforeCommand(env);
assertThat(Thread.interrupted()).isFalse();
assertThat(executionServerCapabilitiesImpl.getRequestCount()).isEqualTo(1);
} finally {
executionServer.shutdownNow();
executionServer.awaitTermination();
}
}
@Test
public void testVerifyCapabilities_cacheOnlyEndpoint() throws Exception {
ServerCapabilities cacheOnlyCaps =
ServerCapabilities.newBuilder()
.setLowApiVersion(ApiVersion.current.toSemVer())
.setHighApiVersion(ApiVersion.current.toSemVer())
.setCacheCapabilities(
CacheCapabilities.newBuilder()
.addDigestFunctions(Value.SHA256)
.setActionCacheUpdateCapabilities(
ActionCacheUpdateCapabilities.newBuilder().setUpdateEnabled(true).build())
.build())
.build();
CapabilitiesImpl cacheServerCapabilitiesImpl = new CapabilitiesImpl(cacheOnlyCaps);
String cacheServerName = "cache-server";
Server cacheServer = createFakeServer(cacheServerName, cacheServerCapabilitiesImpl);
cacheServer.start();
try {
RemoteModule remoteModule = new RemoteModule();
remoteModule.setChannelFactory(
(target, proxy, options, interceptors) ->
InProcessChannelBuilder.forName(target).directExecutor().build());
RemoteOptions remoteOptions = Options.getDefaults(RemoteOptions.class);
remoteOptions.remoteCache = cacheServerName;
CommandEnvironment env = createTestCommandEnvironment(remoteOptions);
remoteModule.beforeCommand(env);
assertThat(Thread.interrupted()).isFalse();
assertThat(cacheServerCapabilitiesImpl.getRequestCount()).isEqualTo(1);
} finally {
cacheServer.shutdownNow();
cacheServer.awaitTermination();
}
}
@Test
public void testVerifyCapabilities_executionAndCacheForDifferentEndpoints() throws Exception {
ServerCapabilities caps =
ServerCapabilities.newBuilder()
.setLowApiVersion(ApiVersion.current.toSemVer())
.setHighApiVersion(ApiVersion.current.toSemVer())
.setExecutionCapabilities(
ExecutionCapabilities.newBuilder()
.setExecEnabled(true)
.setDigestFunction(Value.SHA256)
.build())
.setCacheCapabilities(
CacheCapabilities.newBuilder().addDigestFunctions(Value.SHA256).build())
.build();
CapabilitiesImpl executionServerCapabilitiesImpl = new CapabilitiesImpl(caps);
String executionServerName = "execution-server";
Server executionServer = createFakeServer(executionServerName, executionServerCapabilitiesImpl);
executionServer.start();
CapabilitiesImpl cacheServerCapabilitiesImpl = new CapabilitiesImpl(caps);
String cacheServerName = "cache-server";
Server cacheServer = createFakeServer(cacheServerName, cacheServerCapabilitiesImpl);
cacheServer.start();
try {
RemoteModule remoteModule = new RemoteModule();
remoteModule.setChannelFactory(
(target, proxy, options, interceptors) ->
InProcessChannelBuilder.forName(target).directExecutor().build());
RemoteOptions remoteOptions = Options.getDefaults(RemoteOptions.class);
remoteOptions.remoteExecutor = executionServerName;
remoteOptions.remoteCache = cacheServerName;
CommandEnvironment env = createTestCommandEnvironment(remoteOptions);
remoteModule.beforeCommand(env);
assertThat(Thread.interrupted()).isFalse();
assertThat(executionServerCapabilitiesImpl.getRequestCount()).isEqualTo(1);
assertThat(cacheServerCapabilitiesImpl.getRequestCount()).isEqualTo(1);
} finally {
executionServer.shutdownNow();
cacheServer.shutdownNow();
executionServer.awaitTermination();
cacheServer.awaitTermination();
}
}
@Test
public void testVerifyCapabilities_executionOnlyAndCacheOnlyEndpoints() throws Exception {
ServerCapabilities executionOnlyCaps =
ServerCapabilities.newBuilder()
.setLowApiVersion(ApiVersion.current.toSemVer())
.setHighApiVersion(ApiVersion.current.toSemVer())
.setExecutionCapabilities(
ExecutionCapabilities.newBuilder()
.setExecEnabled(true)
.setDigestFunction(Value.SHA256)
.build())
.build();
CapabilitiesImpl executionServerCapabilitiesImpl = new CapabilitiesImpl(executionOnlyCaps);
String executionServerName = "execution-server";
Server executionServer = createFakeServer(executionServerName, executionServerCapabilitiesImpl);
executionServer.start();
ServerCapabilities cacheOnlyCaps =
ServerCapabilities.newBuilder()
.setLowApiVersion(ApiVersion.current.toSemVer())
.setHighApiVersion(ApiVersion.current.toSemVer())
.setCacheCapabilities(
CacheCapabilities.newBuilder().addDigestFunctions(Value.SHA256).build())
.build();
CapabilitiesImpl cacheServerCapabilitiesImpl = new CapabilitiesImpl(cacheOnlyCaps);
String cacheServerName = "cache-server";
Server cacheServer = createFakeServer(cacheServerName, cacheServerCapabilitiesImpl);
cacheServer.start();
try {
RemoteModule remoteModule = new RemoteModule();
remoteModule.setChannelFactory(
(target, proxy, options, interceptors) ->
InProcessChannelBuilder.forName(target).directExecutor().build());
RemoteOptions remoteOptions = Options.getDefaults(RemoteOptions.class);
remoteOptions.remoteExecutor = executionServerName;
remoteOptions.remoteCache = cacheServerName;
CommandEnvironment env = createTestCommandEnvironment(remoteOptions);
remoteModule.beforeCommand(env);
assertThat(Thread.interrupted()).isFalse();
assertThat(executionServerCapabilitiesImpl.getRequestCount()).isEqualTo(1);
assertThat(cacheServerCapabilitiesImpl.getRequestCount()).isEqualTo(1);
} finally {
executionServer.shutdownNow();
cacheServer.shutdownNow();
executionServer.awaitTermination();
cacheServer.awaitTermination();
}
}
@Test
public void testLocalFallback_shouldErrorForRemoteCacheWithoutRequiredCapabilities()
throws Exception {
ServerCapabilities noneCaps =
ServerCapabilities.newBuilder()
.setLowApiVersion(ApiVersion.current.toSemVer())
.setHighApiVersion(ApiVersion.current.toSemVer())
.build();
CapabilitiesImpl cacheServerCapabilitiesImpl = new CapabilitiesImpl(noneCaps);
String cacheServerName = "cache-server";
Server cacheServer = createFakeServer(cacheServerName, cacheServerCapabilitiesImpl);
cacheServer.start();
try {
RemoteModule remoteModule = new RemoteModule();
RemoteOptions remoteOptions = Options.getDefaults(RemoteOptions.class);
remoteOptions.remoteCache = cacheServerName;
remoteOptions.remoteLocalFallback = true;
remoteModule.setChannelFactory(
(target, proxy, options, interceptors) ->
InProcessChannelBuilder.forName(target).directExecutor().build());
CommandEnvironment env = createTestCommandEnvironment(remoteOptions);
assertThrows(AbruptExitException.class, () -> remoteModule.beforeCommand(env));
} finally {
cacheServer.shutdownNow();
cacheServer.awaitTermination();
}
}
@Test
public void testLocalFallback_shouldErrorInaccessibleGrpcRemoteCacheIfFlagNotSet()
throws Exception {
String cacheServerName = "cache-server";
CapabilitiesImplBase cacheServerCapabilitiesImpl =
new CapabilitiesImplBase() {
@Override
public void getCapabilities(
GetCapabilitiesRequest request, StreamObserver<ServerCapabilities> responseObserver) {
responseObserver.onError(new UnsupportedOperationException());
}
};
Server cacheServer = createFakeServer(cacheServerName, cacheServerCapabilitiesImpl);
cacheServer.start();
try {
RemoteModule remoteModule = new RemoteModule();
RemoteOptions remoteOptions = Options.getDefaults(RemoteOptions.class);
remoteOptions.remoteCache = cacheServerName;
remoteOptions.remoteLocalFallback = false;
remoteModule.setChannelFactory(
(target, proxy, options, interceptors) ->
InProcessChannelBuilder.forName(target).directExecutor().build());
CommandEnvironment env = createTestCommandEnvironment(remoteOptions);
assertThrows(AbruptExitException.class, () -> remoteModule.beforeCommand(env));
} finally {
cacheServer.shutdownNow();
cacheServer.awaitTermination();
}
}
@Test
public void testLocalFallback_shouldIgnoreInaccessibleGrpcRemoteCache() throws Exception {
String cacheServerName = "cache-server";
CapabilitiesImplBase cacheServerCapabilitiesImpl =
new CapabilitiesImplBase() {
@Override
public void getCapabilities(
GetCapabilitiesRequest request, StreamObserver<ServerCapabilities> responseObserver) {
responseObserver.onError(new UnsupportedOperationException());
}
};
Server cacheServer = createFakeServer(cacheServerName, cacheServerCapabilitiesImpl);
cacheServer.start();
try {
RemoteModule remoteModule = new RemoteModule();
RemoteOptions remoteOptions = Options.getDefaults(RemoteOptions.class);
remoteOptions.remoteCache = cacheServerName;
remoteOptions.remoteLocalFallback = true;
remoteModule.setChannelFactory(
(target, proxy, options, interceptors) ->
InProcessChannelBuilder.forName(target).directExecutor().build());
CommandEnvironment env = createTestCommandEnvironment(remoteOptions);
remoteModule.beforeCommand(env);
assertThat(Thread.interrupted()).isFalse();
RemoteActionContextProvider actionContextProvider = remoteModule.getActionContextProvider();
assertThat(actionContextProvider).isNotNull();
assertThat(actionContextProvider.getRemoteCache()).isNull();
assertThat(actionContextProvider.getRemoteExecutionClient()).isNull();
} finally {
cacheServer.shutdownNow();
cacheServer.awaitTermination();
}
}
@Test
public void testLocalFallback_shouldIgnoreInaccessibleGrpcRemoteExecutor() throws Exception {
CapabilitiesImplBase executionServerCapabilitiesImpl =
new CapabilitiesImplBase() {
@Override
public void getCapabilities(
GetCapabilitiesRequest request, StreamObserver<ServerCapabilities> responseObserver) {
responseObserver.onError(new UnsupportedOperationException());
}
};
String executionServerName = "execution-server";
Server executionServer = createFakeServer(executionServerName, executionServerCapabilitiesImpl);
executionServer.start();
try {
RemoteModule remoteModule = new RemoteModule();
RemoteOptions remoteOptions = Options.getDefaults(RemoteOptions.class);
remoteOptions.remoteExecutor = executionServerName;
remoteOptions.remoteLocalFallback = true;
remoteModule.setChannelFactory(
(target, proxy, options, interceptors) ->
InProcessChannelBuilder.forName(target).directExecutor().build());
CommandEnvironment env = createTestCommandEnvironment(remoteOptions);
remoteModule.beforeCommand(env);
assertThat(Thread.interrupted()).isFalse();
RemoteActionContextProvider actionContextProvider = remoteModule.getActionContextProvider();
assertThat(actionContextProvider).isNotNull();
assertThat(actionContextProvider.getRemoteCache()).isNull();
assertThat(actionContextProvider.getRemoteExecutionClient()).isNull();
} finally {
executionServer.shutdownNow();
executionServer.awaitTermination();
}
}
@Test
public void testNetrc_emptyEnv_shouldIgnore() throws Exception {
Map<String, String> clientEnv = ImmutableMap.of();
FileSystem fileSystem = new InMemoryFileSystem(DigestHashFunction.SHA256);
Credentials credentials = RemoteModule.newCredentialsFromNetrc(clientEnv, fileSystem);
assertThat(credentials).isNull();
}
@Test
public void testNetrc_netrcNotExist_shouldIgnore() throws Exception {
String home = "/home/foo";
Map<String, String> clientEnv = ImmutableMap.of("HOME", home);
FileSystem fileSystem = new InMemoryFileSystem(DigestHashFunction.SHA256);
Credentials credentials = RemoteModule.newCredentialsFromNetrc(clientEnv, fileSystem);
assertThat(credentials).isNull();
}
@Test
public void testNetrc_netrcExist_shouldUse() throws Exception {
String home = "/home/foo";
Map<String, String> clientEnv = ImmutableMap.of("HOME", home);
FileSystem fileSystem = new InMemoryFileSystem(DigestHashFunction.SHA256);
Scratch scratch = new Scratch(fileSystem);
scratch.file(home + "/.netrc", "machine foo.example.org login foouser password foopass");
Credentials credentials = RemoteModule.newCredentialsFromNetrc(clientEnv, fileSystem);
assertThat(credentials).isNotNull();
assertRequestMetadata(
credentials.getRequestMetadata(URI.create("https://foo.example.org")),
"foouser",
"foopass");
}
@Test
public void testNetrc_netrcFromNetrcEnvExist_shouldUse() throws Exception {
String home = "/home/foo";
String netrc = "/.netrc";
Map<String, String> clientEnv = ImmutableMap.of("HOME", home, "NETRC", netrc);
FileSystem fileSystem = new InMemoryFileSystem(DigestHashFunction.SHA256);
Scratch scratch = new Scratch(fileSystem);
scratch.file(home + "/.netrc", "machine foo.example.org login foouser password foopass");
scratch.file(netrc, "machine foo.example.org login baruser password barpass");
Credentials credentials = RemoteModule.newCredentialsFromNetrc(clientEnv, fileSystem);
assertThat(credentials).isNotNull();
assertRequestMetadata(
credentials.getRequestMetadata(URI.create("https://foo.example.org")),
"baruser",
"barpass");
}
@Test
public void testNetrc_netrcFromNetrcEnvNotExist_shouldIgnore() throws Exception {
String home = "/home/foo";
String netrc = "/.netrc";
Map<String, String> clientEnv = ImmutableMap.of("HOME", home, "NETRC", netrc);
FileSystem fileSystem = new InMemoryFileSystem(DigestHashFunction.SHA256);
Scratch scratch = new Scratch(fileSystem);
scratch.file(home + "/.netrc", "machine foo.example.org login foouser password foopass");
Credentials credentials = RemoteModule.newCredentialsFromNetrc(clientEnv, fileSystem);
assertThat(credentials).isNull();
}
@Test
public void testNetrc_netrcWithoutRemoteCache() throws Exception {
String netrc = "/.netrc";
Map<String, String> clientEnv = ImmutableMap.of("NETRC", netrc);
FileSystem fileSystem = new InMemoryFileSystem(DigestHashFunction.SHA256);
Scratch scratch = new Scratch(fileSystem);
scratch.file(netrc, "machine foo.example.org login baruser password barpass");
AuthAndTLSOptions authAndTLSOptions = Options.getDefaults(AuthAndTLSOptions.class);
RemoteOptions remoteOptions = Options.getDefaults(RemoteOptions.class);
Reporter reporter = new Reporter(new EventBus());
Credentials credentials =
RemoteModule.newCredentials(
clientEnv, fileSystem, reporter, authAndTLSOptions, remoteOptions);
assertThat(credentials).isNotNull();
assertThat(credentials.getRequestMetadata(URI.create("https://foo.example.org"))).isNotEmpty();
assertThat(credentials.getRequestMetadata(URI.create("https://bar.example.org"))).isEmpty();
}
private static void assertRequestMetadata(
Map<String, List<String>> requestMetadata, String username, String password) {
assertThat(requestMetadata.keySet()).containsExactly("Authorization");
assertThat(Iterables.getOnlyElement(requestMetadata.values()))
.containsExactly(BasicHttpAuthenticationEncoder.encode(username, password, UTF_8));
}
}