Chi Wang | 0cb411b | 2020-09-03 01:49:50 -0700 | [diff] [blame] | 1 | // Copyright 2020 The Bazel Authors. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | package com.google.devtools.build.lib.remote; |
| 15 | |
| 16 | import static com.google.common.truth.Truth.assertThat; |
Chi Wang | ae28ab7 | 2020-10-11 21:05:27 -0700 | [diff] [blame] | 17 | import static java.nio.charset.StandardCharsets.UTF_8; |
Chi Wang | 39512a4 | 2020-09-14 07:13:20 -0700 | [diff] [blame] | 18 | import static org.junit.Assert.assertThrows; |
Chi Wang | 0cb411b | 2020-09-03 01:49:50 -0700 | [diff] [blame] | 19 | import static org.mockito.Mockito.mock; |
| 20 | import static org.mockito.Mockito.when; |
| 21 | |
| 22 | import build.bazel.remote.execution.v2.ActionCacheUpdateCapabilities; |
| 23 | import build.bazel.remote.execution.v2.CacheCapabilities; |
| 24 | import build.bazel.remote.execution.v2.CapabilitiesGrpc.CapabilitiesImplBase; |
| 25 | import build.bazel.remote.execution.v2.DigestFunction.Value; |
| 26 | import build.bazel.remote.execution.v2.ExecutionCapabilities; |
| 27 | import build.bazel.remote.execution.v2.GetCapabilitiesRequest; |
| 28 | import build.bazel.remote.execution.v2.ServerCapabilities; |
Chi Wang | ae28ab7 | 2020-10-11 21:05:27 -0700 | [diff] [blame] | 29 | import com.google.auth.Credentials; |
jhorvitz | 4182af4 | 2020-12-28 09:57:34 -0800 | [diff] [blame] | 30 | import com.google.common.collect.ImmutableList; |
Chi Wang | ae28ab7 | 2020-10-11 21:05:27 -0700 | [diff] [blame] | 31 | import com.google.common.collect.ImmutableMap; |
| 32 | import com.google.common.collect.Iterables; |
Googler | c5ce703 | 2020-11-12 01:26:47 -0800 | [diff] [blame] | 33 | import com.google.common.eventbus.EventBus; |
Chi Wang | 0cb411b | 2020-09-03 01:49:50 -0700 | [diff] [blame] | 34 | import com.google.devtools.build.lib.analysis.BlazeDirectories; |
| 35 | import com.google.devtools.build.lib.analysis.ServerDirectories; |
Chi Wang | 0cb411b | 2020-09-03 01:49:50 -0700 | [diff] [blame] | 36 | import com.google.devtools.build.lib.analysis.config.CoreOptions; |
| 37 | import com.google.devtools.build.lib.authandtls.AuthAndTLSOptions; |
Chi Wang | ae28ab7 | 2020-10-11 21:05:27 -0700 | [diff] [blame] | 38 | import com.google.devtools.build.lib.authandtls.BasicHttpAuthenticationEncoder; |
Googler | c5ce703 | 2020-11-12 01:26:47 -0800 | [diff] [blame] | 39 | import com.google.devtools.build.lib.events.Reporter; |
Chi Wang | 0cb411b | 2020-09-03 01:49:50 -0700 | [diff] [blame] | 40 | import com.google.devtools.build.lib.exec.BinTools; |
Chi Wang | 39512a4 | 2020-09-14 07:13:20 -0700 | [diff] [blame] | 41 | import com.google.devtools.build.lib.exec.ExecutionOptions; |
Chi Wang | 0cb411b | 2020-09-03 01:49:50 -0700 | [diff] [blame] | 42 | import com.google.devtools.build.lib.pkgcache.PackageOptions; |
| 43 | import com.google.devtools.build.lib.remote.options.RemoteOptions; |
Chi Wang | 0cb411b | 2020-09-03 01:49:50 -0700 | [diff] [blame] | 44 | import com.google.devtools.build.lib.runtime.BlazeRuntime; |
| 45 | import com.google.devtools.build.lib.runtime.BlazeServerStartupOptions; |
| 46 | import com.google.devtools.build.lib.runtime.BlazeWorkspace; |
| 47 | import com.google.devtools.build.lib.runtime.ClientOptions; |
| 48 | import com.google.devtools.build.lib.runtime.Command; |
| 49 | import com.google.devtools.build.lib.runtime.CommandEnvironment; |
| 50 | import com.google.devtools.build.lib.runtime.CommonCommandOptions; |
| 51 | import com.google.devtools.build.lib.runtime.commands.BuildCommand; |
| 52 | import com.google.devtools.build.lib.testutil.Scratch; |
| 53 | import com.google.devtools.build.lib.util.AbruptExitException; |
janakr | 97c0bd1 | 2020-09-08 13:19:03 -0700 | [diff] [blame] | 54 | import com.google.devtools.build.lib.vfs.DigestHashFunction; |
Chi Wang | ae28ab7 | 2020-10-11 21:05:27 -0700 | [diff] [blame] | 55 | import com.google.devtools.build.lib.vfs.FileSystem; |
janakr | 97c0bd1 | 2020-09-08 13:19:03 -0700 | [diff] [blame] | 56 | import com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem; |
Chi Wang | 0cb411b | 2020-09-03 01:49:50 -0700 | [diff] [blame] | 57 | import com.google.devtools.common.options.Options; |
| 58 | import com.google.devtools.common.options.OptionsParser; |
| 59 | import com.google.devtools.common.options.OptionsParsingResult; |
| 60 | import io.grpc.BindableService; |
| 61 | import io.grpc.Server; |
| 62 | import io.grpc.ServerInterceptors; |
| 63 | import io.grpc.inprocess.InProcessChannelBuilder; |
| 64 | import io.grpc.inprocess.InProcessServerBuilder; |
| 65 | import io.grpc.stub.StreamObserver; |
| 66 | import io.grpc.util.MutableHandlerRegistry; |
| 67 | import java.io.IOException; |
Chi Wang | ae28ab7 | 2020-10-11 21:05:27 -0700 | [diff] [blame] | 68 | import java.net.URI; |
Chi Wang | 0cb411b | 2020-09-03 01:49:50 -0700 | [diff] [blame] | 69 | import java.util.ArrayList; |
Chi Wang | ae28ab7 | 2020-10-11 21:05:27 -0700 | [diff] [blame] | 70 | import java.util.List; |
| 71 | import java.util.Map; |
Chi Wang | 0cb411b | 2020-09-03 01:49:50 -0700 | [diff] [blame] | 72 | import org.junit.Test; |
| 73 | import org.junit.runner.RunWith; |
| 74 | import org.junit.runners.JUnit4; |
| 75 | |
| 76 | /** Tests for {@link RemoteModule}. */ |
| 77 | @RunWith(JUnit4.class) |
jhorvitz | 4182af4 | 2020-12-28 09:57:34 -0800 | [diff] [blame] | 78 | public final class RemoteModuleTest { |
Chi Wang | 0cb411b | 2020-09-03 01:49:50 -0700 | [diff] [blame] | 79 | |
| 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 Wang | 39512a4 | 2020-09-14 07:13:20 -0700 | [diff] [blame] | 86 | ExecutionOptions executionOptions = Options.getDefaults(ExecutionOptions.class); |
Chi Wang | 0cb411b | 2020-09-03 01:49:50 -0700 | [diff] [blame] | 87 | |
| 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 Wang | 39512a4 | 2020-09-14 07:13:20 -0700 | [diff] [blame] | 97 | when(options.getOptions(ExecutionOptions.class)).thenReturn(executionOptions); |
Chi Wang | 0cb411b | 2020-09-03 01:49:50 -0700 | [diff] [blame] | 98 | |
| 99 | String productName = "bazel"; |
janakr | 97c0bd1 | 2020-09-08 13:19:03 -0700 | [diff] [blame] | 100 | Scratch scratch = new Scratch(new InMemoryFileSystem(DigestHashFunction.SHA256)); |
Chi Wang | 0cb411b | 2020-09-03 01:49:50 -0700 | [diff] [blame] | 101 | 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 Wang | 0cb411b | 2020-09-03 01:49:50 -0700 | [diff] [blame] | 111 | .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); |
janakr | 8e881f7 | 2021-07-07 22:57:33 -0700 | [diff] [blame] | 121 | return workspace.initCommand( |
| 122 | command, options, new ArrayList<>(), 0, 0, ImmutableList.of(), s -> {}); |
Chi Wang | 0cb411b | 2020-09-03 01:49:50 -0700 | [diff] [blame] | 123 | } |
| 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 | |
jhorvitz | 4182af4 | 2020-12-28 09:57:34 -0800 | [diff] [blame] | 142 | int getRequestCount() { |
Chi Wang | 0cb411b | 2020-09-03 01:49:50 -0700 | [diff] [blame] | 143 | 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( |
Googler | 7f40eb9 | 2021-08-12 01:42:29 -0700 | [diff] [blame] | 170 | CacheCapabilities.newBuilder().addDigestFunctions(Value.SHA256).build()) |
Chi Wang | 0cb411b | 2020-09-03 01:49:50 -0700 | [diff] [blame] | 171 | .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() |
Googler | 7f40eb9 | 2021-08-12 01:42:29 -0700 | [diff] [blame] | 206 | .addDigestFunctions(Value.SHA256) |
Chi Wang | 0cb411b | 2020-09-03 01:49:50 -0700 | [diff] [blame] | 207 | .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( |
Googler | 7f40eb9 | 2021-08-12 01:42:29 -0700 | [diff] [blame] | 249 | CacheCapabilities.newBuilder().addDigestFunctions(Value.SHA256).build()) |
Chi Wang | 0cb411b | 2020-09-03 01:49:50 -0700 | [diff] [blame] | 250 | .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 Wang | 39512a4 | 2020-09-14 07:13:20 -0700 | [diff] [blame] | 306 | .setLowApiVersion(ApiVersion.current.toSemVer()) |
| 307 | .setHighApiVersion(ApiVersion.current.toSemVer()) |
Chi Wang | 0cb411b | 2020-09-03 01:49:50 -0700 | [diff] [blame] | 308 | .setCacheCapabilities( |
Googler | 7f40eb9 | 2021-08-12 01:42:29 -0700 | [diff] [blame] | 309 | CacheCapabilities.newBuilder().addDigestFunctions(Value.SHA256).build()) |
Chi Wang | 0cb411b | 2020-09-03 01:49:50 -0700 | [diff] [blame] | 310 | .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 Wang | 39512a4 | 2020-09-14 07:13:20 -0700 | [diff] [blame] | 341 | |
| 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 | |
jhorvitz | 4182af4 | 2020-12-28 09:57:34 -0800 | [diff] [blame] | 366 | assertThrows(AbruptExitException.class, () -> remoteModule.beforeCommand(env)); |
Chi Wang | 39512a4 | 2020-09-14 07:13:20 -0700 | [diff] [blame] | 367 | } 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 | |
jhorvitz | 4182af4 | 2020-12-28 09:57:34 -0800 | [diff] [blame] | 399 | assertThrows(AbruptExitException.class, () -> remoteModule.beforeCommand(env)); |
Chi Wang | 39512a4 | 2020-09-14 07:13:20 -0700 | [diff] [blame] | 400 | } 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 Wang | 7643d3a | 2021-06-09 00:29:55 -0700 | [diff] [blame] | 434 | RemoteActionContextProvider actionContextProvider = remoteModule.getActionContextProvider(); |
| 435 | assertThat(actionContextProvider).isNotNull(); |
| 436 | assertThat(actionContextProvider.getRemoteCache()).isNull(); |
| 437 | assertThat(actionContextProvider.getRemoteExecutionClient()).isNull(); |
Chi Wang | 39512a4 | 2020-09-14 07:13:20 -0700 | [diff] [blame] | 438 | } finally { |
| 439 | cacheServer.shutdownNow(); |
| 440 | cacheServer.awaitTermination(); |
| 441 | } |
| 442 | } |
Chi Wang | ae28ab7 | 2020-10-11 21:05:27 -0700 | [diff] [blame] | 443 | |
| 444 | @Test |
Chi Wang | 7643d3a | 2021-06-09 00:29:55 -0700 | [diff] [blame] | 445 | 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 Wang | ae28ab7 | 2020-10-11 21:05:27 -0700 | [diff] [blame] | 483 | 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 | |
Googler | c5ce703 | 2020-11-12 01:26:47 -0800 | [diff] [blame] | 553 | @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 Wang | ae28ab7 | 2020-10-11 21:05:27 -0700 | [diff] [blame] | 571 | 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 Wang | 0cb411b | 2020-09-03 01:49:50 -0700 | [diff] [blame] | 577 | } |