Ulf Adams | fd37004 | 2017-06-16 15:52:06 +0200 | [diff] [blame] | 1 | // Copyright 2017 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.runtime; |
| 15 | |
| 16 | import static com.google.common.truth.Truth.assertThat; |
janakr | d9e8f58 | 2021-06-09 13:37:53 -0700 | [diff] [blame] | 17 | import static org.mockito.Mockito.verify; |
Ulf Adams | fd37004 | 2017-06-16 15:52:06 +0200 | [diff] [blame] | 18 | |
| 19 | import com.google.common.collect.ImmutableList; |
janakr | b2f33ea | 2019-05-08 10:56:43 -0700 | [diff] [blame] | 20 | import com.google.common.eventbus.EventBus; |
| 21 | import com.google.devtools.build.lib.analysis.BlazeDirectories; |
| 22 | import com.google.devtools.build.lib.analysis.ServerDirectories; |
janakr | b2f33ea | 2019-05-08 10:56:43 -0700 | [diff] [blame] | 23 | import com.google.devtools.build.lib.exec.BinTools; |
| 24 | import com.google.devtools.build.lib.runtime.commands.VersionCommand; |
mschaller | fbc43bb | 2020-05-20 00:59:41 -0700 | [diff] [blame] | 25 | import com.google.devtools.build.lib.server.FailureDetails.Crash; |
| 26 | import com.google.devtools.build.lib.server.FailureDetails.Crash.Code; |
| 27 | import com.google.devtools.build.lib.server.FailureDetails.FailureDetail; |
janakr | 86e9538 | 2020-04-10 09:00:47 -0700 | [diff] [blame] | 28 | import com.google.devtools.build.lib.util.DetailedExitCode; |
janakr | 97c0bd1 | 2020-09-08 13:19:03 -0700 | [diff] [blame] | 29 | import com.google.devtools.build.lib.vfs.DigestHashFunction; |
janakr | b2f33ea | 2019-05-08 10:56:43 -0700 | [diff] [blame] | 30 | import com.google.devtools.build.lib.vfs.FileSystem; |
janakr | b2a9434 | 2022-02-05 22:02:14 -0800 | [diff] [blame] | 31 | import com.google.devtools.build.lib.vfs.SyscallCache; |
janakr | b2f33ea | 2019-05-08 10:56:43 -0700 | [diff] [blame] | 32 | import com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem; |
| 33 | import com.google.devtools.common.options.OptionsBase; |
| 34 | import com.google.devtools.common.options.OptionsParser; |
| 35 | import com.google.devtools.common.options.OptionsParsingResult; |
Googler | 6ebc9fb | 2021-02-10 10:10:59 -0800 | [diff] [blame] | 36 | import com.google.protobuf.Any; |
| 37 | import com.google.protobuf.ByteString; |
| 38 | import com.google.protobuf.BytesValue; |
| 39 | import com.google.protobuf.StringValue; |
Ulf Adams | fd37004 | 2017-06-16 15:52:06 +0200 | [diff] [blame] | 40 | import java.util.Arrays; |
janakr | 8e881f7 | 2021-07-07 22:57:33 -0700 | [diff] [blame] | 41 | import java.util.concurrent.atomic.AtomicReference; |
Ulf Adams | fd37004 | 2017-06-16 15:52:06 +0200 | [diff] [blame] | 42 | import org.junit.Test; |
| 43 | import org.junit.runner.RunWith; |
| 44 | import org.junit.runners.JUnit4; |
janakr | b2f33ea | 2019-05-08 10:56:43 -0700 | [diff] [blame] | 45 | import org.mockito.Mockito; |
Ulf Adams | fd37004 | 2017-06-16 15:52:06 +0200 | [diff] [blame] | 46 | |
| 47 | /** Tests for {@link BlazeRuntime} static methods. */ |
| 48 | @RunWith(JUnit4.class) |
| 49 | public class BlazeRuntimeTest { |
Ulf Adams | fd37004 | 2017-06-16 15:52:06 +0200 | [diff] [blame] | 50 | |
| 51 | @Test |
jhorvitz | 1e803ab | 2021-05-06 10:22:28 -0700 | [diff] [blame] | 52 | public void optionSplitting() { |
Ulf Adams | fd37004 | 2017-06-16 15:52:06 +0200 | [diff] [blame] | 53 | BlazeRuntime.CommandLineOptions options = |
| 54 | BlazeRuntime.splitStartupOptions( |
jhorvitz | 1e803ab | 2021-05-06 10:22:28 -0700 | [diff] [blame] | 55 | ImmutableList.of(), |
| 56 | "--install_base=/foo --host_jvm_args=-Xmx1B", |
| 57 | "build", |
| 58 | "//foo:bar", |
| 59 | "--nobuild"); |
Ulf Adams | fd37004 | 2017-06-16 15:52:06 +0200 | [diff] [blame] | 60 | assertThat(options.getStartupArgs()) |
| 61 | .isEqualTo(Arrays.asList("--install_base=/foo --host_jvm_args=-Xmx1B")); |
| 62 | assertThat(options.getOtherArgs()).isEqualTo(Arrays.asList("build", "//foo:bar", "--nobuild")); |
| 63 | } |
| 64 | |
| 65 | // A regression test to make sure that the 'no' prefix is handled correctly. |
| 66 | @Test |
jhorvitz | 1e803ab | 2021-05-06 10:22:28 -0700 | [diff] [blame] | 67 | public void optionSplittingNoPrefix() { |
| 68 | BlazeRuntime.CommandLineOptions options = |
| 69 | BlazeRuntime.splitStartupOptions(ImmutableList.of(), "--nobatch", "build"); |
Ulf Adams | fd37004 | 2017-06-16 15:52:06 +0200 | [diff] [blame] | 70 | assertThat(options.getStartupArgs()).isEqualTo(Arrays.asList("--nobatch")); |
| 71 | assertThat(options.getOtherArgs()).isEqualTo(Arrays.asList("build")); |
| 72 | } |
janakr | b2f33ea | 2019-05-08 10:56:43 -0700 | [diff] [blame] | 73 | |
| 74 | private static final ImmutableList<Class<? extends OptionsBase>> COMMAND_ENV_REQUIRED_OPTIONS = |
| 75 | ImmutableList.of(CommonCommandOptions.class, ClientOptions.class); |
| 76 | |
| 77 | @Test |
| 78 | public void crashTest() throws Exception { |
janakr | 97c0bd1 | 2020-09-08 13:19:03 -0700 | [diff] [blame] | 79 | FileSystem fs = new InMemoryFileSystem(DigestHashFunction.SHA256); |
janakr | b2f33ea | 2019-05-08 10:56:43 -0700 | [diff] [blame] | 80 | ServerDirectories serverDirectories = |
| 81 | new ServerDirectories( |
| 82 | fs.getPath("/install"), fs.getPath("/output"), fs.getPath("/output_user")); |
| 83 | BlazeRuntime runtime = |
| 84 | new BlazeRuntime.Builder() |
janakr | b2f33ea | 2019-05-08 10:56:43 -0700 | [diff] [blame] | 85 | .setFileSystem(fs) |
janakr | 8e881f7 | 2021-07-07 22:57:33 -0700 | [diff] [blame] | 86 | .setProductName("foo product") |
janakr | b2f33ea | 2019-05-08 10:56:43 -0700 | [diff] [blame] | 87 | .setServerDirectories(serverDirectories) |
| 88 | .setStartupOptionsProvider(Mockito.mock(OptionsParsingResult.class)) |
| 89 | .build(); |
janakr | 8e881f7 | 2021-07-07 22:57:33 -0700 | [diff] [blame] | 90 | AtomicReference<String> shutdownMessage = new AtomicReference<>(); |
janakr | b2f33ea | 2019-05-08 10:56:43 -0700 | [diff] [blame] | 91 | BlazeDirectories directories = |
| 92 | new BlazeDirectories( |
| 93 | serverDirectories, fs.getPath("/workspace"), fs.getPath("/system_javabase"), "blaze"); |
| 94 | BlazeWorkspace workspace = runtime.initWorkspace(directories, BinTools.empty(directories)); |
| 95 | EventBus eventBus = Mockito.mock(EventBus.class); |
jcater | 706a22e | 2019-06-17 09:56:03 -0700 | [diff] [blame] | 96 | OptionsParser options = |
| 97 | OptionsParser.builder().optionsClasses(COMMAND_ENV_REQUIRED_OPTIONS).build(); |
janakr | d9e8f58 | 2021-06-09 13:37:53 -0700 | [diff] [blame] | 98 | Thread commandThread = Mockito.mock(Thread.class); |
janakr | b2f33ea | 2019-05-08 10:56:43 -0700 | [diff] [blame] | 99 | CommandEnvironment env = |
| 100 | new CommandEnvironment( |
| 101 | runtime, |
| 102 | workspace, |
| 103 | eventBus, |
janakr | d9e8f58 | 2021-06-09 13:37:53 -0700 | [diff] [blame] | 104 | commandThread, |
janakr | b2f33ea | 2019-05-08 10:56:43 -0700 | [diff] [blame] | 105 | VersionCommand.class.getAnnotation(Command.class), |
| 106 | options, |
janakr | b2a9434 | 2022-02-05 22:02:14 -0800 | [diff] [blame] | 107 | SyscallCache.NO_CACHE, |
janakr | 45b568b | 2020-05-13 12:39:27 -0700 | [diff] [blame] | 108 | ImmutableList.of(), |
| 109 | 0L, |
jhorvitz | 4182af4 | 2020-12-28 09:57:34 -0800 | [diff] [blame] | 110 | 0L, |
janakr | 8e881f7 | 2021-07-07 22:57:33 -0700 | [diff] [blame] | 111 | ImmutableList.of(), |
| 112 | shutdownMessage::set); |
janakr | b2f33ea | 2019-05-08 10:56:43 -0700 | [diff] [blame] | 113 | runtime.beforeCommand(env, options.getOptions(CommonCommandOptions.class)); |
mschaller | fbc43bb | 2020-05-20 00:59:41 -0700 | [diff] [blame] | 114 | DetailedExitCode oom = |
| 115 | DetailedExitCode.of( |
| 116 | FailureDetail.newBuilder() |
| 117 | .setCrash(Crash.newBuilder().setCode(Code.CRASH_OOM)) |
| 118 | .build()); |
| 119 | runtime.cleanUpForCrash(oom); |
| 120 | BlazeCommandResult mainThreadCrash = |
| 121 | BlazeCommandResult.failureDetail( |
| 122 | FailureDetail.newBuilder() |
| 123 | .setCrash(Crash.newBuilder().setCode(Code.CRASH_UNKNOWN)) |
| 124 | .build()); |
| 125 | assertThat(runtime.afterCommand(env, mainThreadCrash).getDetailedExitCode()).isEqualTo(oom); |
janakr | d9e8f58 | 2021-06-09 13:37:53 -0700 | [diff] [blame] | 126 | // Confirm that runtime interrupted the command thread. |
| 127 | verify(commandThread).interrupt(); |
janakr | 8e881f7 | 2021-07-07 22:57:33 -0700 | [diff] [blame] | 128 | assertThat(shutdownMessage.get()).isEqualTo("foo product is crashing: "); |
janakr | b2f33ea | 2019-05-08 10:56:43 -0700 | [diff] [blame] | 129 | } |
michajlo | 8454a18 | 2020-08-25 16:04:27 -0700 | [diff] [blame] | 130 | |
| 131 | @Test |
Googler | 6ebc9fb | 2021-02-10 10:10:59 -0800 | [diff] [blame] | 132 | public void resultExtensions() throws Exception { |
| 133 | FileSystem fs = new InMemoryFileSystem(DigestHashFunction.SHA256); |
| 134 | ServerDirectories serverDirectories = |
| 135 | new ServerDirectories( |
| 136 | fs.getPath("/install"), fs.getPath("/output"), fs.getPath("/output_user")); |
| 137 | BlazeRuntime runtime = |
| 138 | new BlazeRuntime.Builder() |
Googler | 6ebc9fb | 2021-02-10 10:10:59 -0800 | [diff] [blame] | 139 | .setFileSystem(fs) |
| 140 | .setProductName("bazel") |
| 141 | .setServerDirectories(serverDirectories) |
| 142 | .setStartupOptionsProvider(Mockito.mock(OptionsParsingResult.class)) |
| 143 | .build(); |
| 144 | BlazeDirectories directories = |
| 145 | new BlazeDirectories( |
| 146 | serverDirectories, fs.getPath("/workspace"), fs.getPath("/system_javabase"), "blaze"); |
| 147 | BlazeWorkspace workspace = runtime.initWorkspace(directories, BinTools.empty(directories)); |
| 148 | CommandEnvironment env = |
| 149 | new CommandEnvironment( |
| 150 | runtime, |
| 151 | workspace, |
| 152 | Mockito.mock(EventBus.class), |
| 153 | Thread.currentThread(), |
| 154 | VersionCommand.class.getAnnotation(Command.class), |
| 155 | OptionsParser.builder().optionsClasses(COMMAND_ENV_REQUIRED_OPTIONS).build(), |
janakr | b2a9434 | 2022-02-05 22:02:14 -0800 | [diff] [blame] | 156 | SyscallCache.NO_CACHE, |
Googler | 6ebc9fb | 2021-02-10 10:10:59 -0800 | [diff] [blame] | 157 | ImmutableList.of(), |
| 158 | 0L, |
| 159 | 0L, |
janakr | 8e881f7 | 2021-07-07 22:57:33 -0700 | [diff] [blame] | 160 | ImmutableList.of(), |
| 161 | s -> {}); |
Googler | 6ebc9fb | 2021-02-10 10:10:59 -0800 | [diff] [blame] | 162 | Any anyFoo = Any.pack(StringValue.of("foo")); |
| 163 | Any anyBar = Any.pack(BytesValue.of(ByteString.copyFromUtf8("bar"))); |
| 164 | env.addResponseExtensions(ImmutableList.of(anyFoo, anyBar)); |
| 165 | assertThat(runtime.afterCommand(env, BlazeCommandResult.success()).getResponseExtensions()) |
| 166 | .containsExactly(anyFoo, anyBar); |
| 167 | } |
| 168 | |
| 169 | @Test |
michajlo | 8454a18 | 2020-08-25 16:04:27 -0700 | [diff] [blame] | 170 | public void addsCommandsFromModules() throws Exception { |
janakr | 97c0bd1 | 2020-09-08 13:19:03 -0700 | [diff] [blame] | 171 | FileSystem fs = new InMemoryFileSystem(DigestHashFunction.SHA256); |
michajlo | 8454a18 | 2020-08-25 16:04:27 -0700 | [diff] [blame] | 172 | ServerDirectories serverDirectories = |
| 173 | new ServerDirectories( |
| 174 | fs.getPath("/install"), fs.getPath("/output"), fs.getPath("/output_user")); |
| 175 | BlazeRuntime runtime = |
| 176 | new BlazeRuntime.Builder() |
| 177 | .addBlazeModule(new FooCommandModule()) |
| 178 | .addBlazeModule(new BarCommandModule()) |
| 179 | .setFileSystem(fs) |
| 180 | .setProductName("bazel") |
| 181 | .setServerDirectories(serverDirectories) |
| 182 | .setStartupOptionsProvider(Mockito.mock(OptionsParsingResult.class)) |
| 183 | .build(); |
| 184 | |
| 185 | assertThat(runtime.getCommandMap().keySet()).containsExactly("foo", "bar").inOrder(); |
| 186 | assertThat(runtime.getCommandMap().get("foo")).isInstanceOf(FooCommandModule.FooCommand.class); |
| 187 | assertThat(runtime.getCommandMap().get("bar")).isInstanceOf(BarCommandModule.BarCommand.class); |
| 188 | } |
| 189 | |
| 190 | private static class FooCommandModule extends BlazeModule { |
| 191 | @Command(name = "foo", shortDescription = "", help = "") |
| 192 | private static class FooCommand implements BlazeCommand { |
| 193 | |
| 194 | @Override |
| 195 | public BlazeCommandResult exec(CommandEnvironment env, OptionsParsingResult options) { |
| 196 | return null; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | @Override |
| 201 | public void serverInit(OptionsParsingResult startupOptions, ServerBuilder builder) { |
| 202 | builder.addCommands(new FooCommand()); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | private static class BarCommandModule extends BlazeModule { |
| 207 | @Command(name = "bar", shortDescription = "", help = "") |
| 208 | private static class BarCommand implements BlazeCommand { |
| 209 | |
| 210 | @Override |
| 211 | public BlazeCommandResult exec(CommandEnvironment env, OptionsParsingResult options) { |
| 212 | return null; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | @Override |
| 217 | public void serverInit(OptionsParsingResult startupOptions, ServerBuilder builder) { |
| 218 | builder.addCommands(new BarCommand()); |
| 219 | } |
| 220 | } |
Ulf Adams | fd37004 | 2017-06-16 15:52:06 +0200 | [diff] [blame] | 221 | } |