blob: db9671af4425f3ea2035984dd336cfdd5c6f0b02 [file] [log] [blame]
Ulf Adamsfd370042017-06-16 15:52:06 +02001// 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.
14package com.google.devtools.build.lib.runtime;
15
16import static com.google.common.truth.Truth.assertThat;
17
18import com.google.common.collect.ImmutableList;
janakrb2f33ea2019-05-08 10:56:43 -070019import com.google.common.eventbus.EventBus;
20import com.google.devtools.build.lib.analysis.BlazeDirectories;
21import com.google.devtools.build.lib.analysis.ServerDirectories;
22import com.google.devtools.build.lib.analysis.config.BuildOptions;
23import com.google.devtools.build.lib.exec.BinTools;
24import com.google.devtools.build.lib.runtime.commands.VersionCommand;
25import com.google.devtools.build.lib.util.ExitCode;
26import com.google.devtools.build.lib.vfs.FileSystem;
27import com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem;
28import com.google.devtools.common.options.OptionsBase;
29import com.google.devtools.common.options.OptionsParser;
30import com.google.devtools.common.options.OptionsParsingResult;
Ulf Adamsfd370042017-06-16 15:52:06 +020031import java.util.Arrays;
32import java.util.List;
33import org.junit.Test;
34import org.junit.runner.RunWith;
35import org.junit.runners.JUnit4;
janakrb2f33ea2019-05-08 10:56:43 -070036import org.mockito.Mockito;
Ulf Adamsfd370042017-06-16 15:52:06 +020037
38/** Tests for {@link BlazeRuntime} static methods. */
39@RunWith(JUnit4.class)
40public class BlazeRuntimeTest {
41 @Test
42 public void requestLogStringParsing() {
43 assertThat(BlazeRuntime.getRequestLogString(ImmutableList.of("--client_env=A=B")))
44 .isEqualTo("[--client_env=A=B]");
45 assertThat(BlazeRuntime.getRequestLogString(ImmutableList.of("--client_env=BROKEN")))
46 .isEqualTo("[--client_env=BROKEN]");
47 assertThat(BlazeRuntime.getRequestLogString(ImmutableList.of("--client_env=auth=notprinted")))
48 .isEqualTo("[--client_env=auth=__private_value_removed__]");
49 assertThat(
50 BlazeRuntime.getRequestLogString(ImmutableList.of("--client_env=MY_COOKIE=notprinted")))
51 .isEqualTo("[--client_env=MY_COOKIE=__private_value_removed__]");
52 assertThat(
53 BlazeRuntime.getRequestLogString(
54 ImmutableList.of("--client_env=dont_paSS_ME=notprinted")))
55 .isEqualTo("[--client_env=dont_paSS_ME=__private_value_removed__]");
56 assertThat(BlazeRuntime.getRequestLogString(ImmutableList.of("--client_env=ok=COOKIE")))
57 .isEqualTo("[--client_env=ok=COOKIE]");
58 assertThat(BlazeRuntime.getRequestLogString(
59 ImmutableList.of("--client_env=foo=bar", "--client_env=pass=notprinted")))
60 .isEqualTo("[--client_env=foo=bar, --client_env=pass=__private_value_removed__]");
61
62 List<String> complexCommandLine = ImmutableList.of(
63 "blaze",
64 "build",
65 "--client_env=FOO=BAR",
66 "--client_env=FOOPASS=mypassword",
67 "--package_path=./MY_PASSWORD/foo",
68 "--client_env=SOMEAuThCode=something");
69 assertThat(BlazeRuntime.getRequestLogString(complexCommandLine)).isEqualTo(
70 "[blaze, build, --client_env=FOO=BAR, --client_env=FOOPASS=__private_value_removed__, "
71 + "--package_path=./MY_PASSWORD/foo, "
72 + "--client_env=SOMEAuThCode=__private_value_removed__]");
73 }
74
75 @Test
76 public void optionSplitting() throws Exception {
77 BlazeRuntime.CommandLineOptions options =
78 BlazeRuntime.splitStartupOptions(
79 ImmutableList.<BlazeModule>of(),
80 "--install_base=/foo --host_jvm_args=-Xmx1B", "build", "//foo:bar", "--nobuild");
81 assertThat(options.getStartupArgs())
82 .isEqualTo(Arrays.asList("--install_base=/foo --host_jvm_args=-Xmx1B"));
83 assertThat(options.getOtherArgs()).isEqualTo(Arrays.asList("build", "//foo:bar", "--nobuild"));
84 }
85
86 // A regression test to make sure that the 'no' prefix is handled correctly.
87 @Test
88 public void optionSplittingNoPrefix() throws Exception {
89 BlazeRuntime.CommandLineOptions options = BlazeRuntime.splitStartupOptions(
90 ImmutableList.<BlazeModule>of(), "--nobatch", "build");
91 assertThat(options.getStartupArgs()).isEqualTo(Arrays.asList("--nobatch"));
92 assertThat(options.getOtherArgs()).isEqualTo(Arrays.asList("build"));
93 }
janakrb2f33ea2019-05-08 10:56:43 -070094
95 private static final ImmutableList<Class<? extends OptionsBase>> COMMAND_ENV_REQUIRED_OPTIONS =
96 ImmutableList.of(CommonCommandOptions.class, ClientOptions.class);
97
98 @Test
99 public void crashTest() throws Exception {
100 FileSystem fs = new InMemoryFileSystem();
101 ServerDirectories serverDirectories =
102 new ServerDirectories(
103 fs.getPath("/install"), fs.getPath("/output"), fs.getPath("/output_user"));
104 BlazeRuntime runtime =
105 new BlazeRuntime.Builder()
106 .addBlazeModule(
107 new BlazeModule() {
108 @Override
109 public BuildOptions getDefaultBuildOptions(BlazeRuntime runtime) {
110 return BuildOptions.builder().build();
111 }
112 })
113 .setFileSystem(fs)
114 .setProductName("bazel")
115 .setServerDirectories(serverDirectories)
116 .setStartupOptionsProvider(Mockito.mock(OptionsParsingResult.class))
117 .build();
118 BlazeDirectories directories =
119 new BlazeDirectories(
120 serverDirectories, fs.getPath("/workspace"), fs.getPath("/system_javabase"), "blaze");
121 BlazeWorkspace workspace = runtime.initWorkspace(directories, BinTools.empty(directories));
122 EventBus eventBus = Mockito.mock(EventBus.class);
123 OptionsParser options = OptionsParser.newOptionsParser(COMMAND_ENV_REQUIRED_OPTIONS);
124 CommandEnvironment env =
125 new CommandEnvironment(
126 runtime,
127 workspace,
128 eventBus,
129 Thread.currentThread(),
130 VersionCommand.class.getAnnotation(Command.class),
131 options,
132 ImmutableList.of());
133 runtime.beforeCommand(env, options.getOptions(CommonCommandOptions.class));
134 runtime.cleanUpForCrash(ExitCode.OOM_ERROR);
135 BlazeCommandResult mainThreadCrash = BlazeCommandResult.exitCode(ExitCode.BLAZE_INTERNAL_ERROR);
136 assertThat(runtime.afterCommand(env, mainThreadCrash).getExitCode())
137 .isEqualTo(ExitCode.OOM_ERROR);
138 }
Ulf Adamsfd370042017-06-16 15:52:06 +0200139}