aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test/java/com/google/devtools/build/lib/shell/CommandUsingLinuxSandboxTest.java
blob: 2aee18cb31621db2b3772954be689c7073990111 (plain)
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
// Copyright 2017 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.shell;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assume.assumeTrue;

import com.google.common.collect.ImmutableList;
import com.google.devtools.build.lib.sandbox.LinuxSandboxUtil;
import com.google.devtools.build.lib.testutil.BlazeTestUtils;
import com.google.devtools.build.lib.testutil.TestConstants;
import com.google.devtools.build.lib.testutil.TestUtils;
import com.google.devtools.build.lib.unix.UnixFileSystem;
import com.google.devtools.build.lib.util.OS;
import com.google.devtools.build.lib.vfs.FileSystem;
import com.google.devtools.build.lib.vfs.Path;
import java.io.IOException;
import java.time.Duration;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** Unit tests for {@link Command}s that are run using the {@code linux-sandbox}. */
@RunWith(JUnit4.class)
public final class CommandUsingLinuxSandboxTest {
  private FileSystem testFS;
  private Path runfilesDir;

  @Before
  public final void createFileSystem() throws Exception {
    testFS = new UnixFileSystem();
    runfilesDir = testFS.getPath(BlazeTestUtils.runfilesDir());
  }

  private Path getLinuxSandboxPath() {
    return runfilesDir.getRelative(TestConstants.LINUX_SANDBOX_PATH);
  }

  private Path getCpuTimeSpenderPath() {
    return runfilesDir.getRelative(TestConstants.CPU_TIME_SPENDER_PATH);
  }

  @Test
  public void testCommand_Echo() throws Exception {
    ImmutableList<String> commandArguments = ImmutableList.of("echo", "colorless green ideas");

    Command command = new Command(commandArguments.toArray(new String[0]));
    CommandResult commandResult = command.execute();

    assertThat(commandResult.getTerminationStatus().success()).isTrue();
    assertThat(commandResult.getStdoutStream().toString()).contains("colorless green ideas");
  }

  @Test
  public void testLinuxSandboxedCommand_Echo() throws Exception {
    // TODO(b/62588075) Currently no linux-sandbox tool support in Windows.
    assumeTrue(OS.getCurrent() != OS.WINDOWS);
    // TODO(b/62588075) Currently no linux-sandbox tool support in MacOS.
    assumeTrue(OS.getCurrent() != OS.DARWIN);

    ImmutableList<String> commandArguments = ImmutableList.of("echo", "sleep furiously");

    List<String> fullCommandLine =
        LinuxSandboxUtil.commandLineBuilder(getLinuxSandboxPath(), commandArguments).build();

    Command command = new Command(fullCommandLine.toArray(new String[0]));
    CommandResult commandResult = command.execute();

    assertThat(commandResult.getTerminationStatus().success()).isTrue();
    assertThat(commandResult.getStdoutStream().toString()).contains("sleep furiously");
  }

  private void checkLinuxSandboxStatistics(Duration userTimeToSpend, Duration systemTimeToSpend)
      throws IOException, CommandException {
    ImmutableList<String> commandArguments =
        ImmutableList.of(
            getCpuTimeSpenderPath().getPathString(),
            Long.toString(userTimeToSpend.getSeconds()),
            Long.toString(systemTimeToSpend.getSeconds()));

    Path outputDir = testFS.getPath(TestUtils.makeTempDir().getCanonicalPath());
    Path statisticsFilePath = outputDir.getRelative("stats.out");

    List<String> fullCommandLine =
        LinuxSandboxUtil.commandLineBuilder(getLinuxSandboxPath(), commandArguments)
            .setStatisticsPath(statisticsFilePath)
            .build();

    ExecutionStatisticsTestUtil.executeCommandAndCheckStatisticsAboutCpuTimeSpent(
        userTimeToSpend, systemTimeToSpend, fullCommandLine, statisticsFilePath);
  }

  @Test
  public void testLinuxSandboxedCommand_WithStatistics_SpendUserTime()
      throws CommandException, IOException {
    // TODO(b/62588075) Currently no linux-sandbox tool support in Windows.
    assumeTrue(OS.getCurrent() != OS.WINDOWS);
    // TODO(b/62588075) Currently no linux-sandbox tool support in MacOS.
    assumeTrue(OS.getCurrent() != OS.DARWIN);

    Duration userTimeToSpend = Duration.ofSeconds(10);
    Duration systemTimeToSpend = Duration.ZERO;

    checkLinuxSandboxStatistics(userTimeToSpend, systemTimeToSpend);
  }

  @Test
  public void testLinuxSandboxedCommand_WithStatistics_SpendSystemTime()
      throws CommandException, IOException {
    // TODO(b/62588075) Currently no linux-sandbox tool support in Windows.
    assumeTrue(OS.getCurrent() != OS.WINDOWS);
    // TODO(b/62588075) Currently no linux-sandbox tool support in MacOS.
    assumeTrue(OS.getCurrent() != OS.DARWIN);

    Duration userTimeToSpend = Duration.ZERO;
    Duration systemTimeToSpend = Duration.ofSeconds(10);

    checkLinuxSandboxStatistics(userTimeToSpend, systemTimeToSpend);
  }

  @Test
  public void testLinuxSandboxedCommand_WithStatistics_SpendUserAndSystemTime()
      throws CommandException, IOException {
    // TODO(b/62588075) Currently no linux-sandbox tool support in Windows.
    assumeTrue(OS.getCurrent() != OS.WINDOWS);
    // TODO(b/62588075) Currently no linux-sandbox tool support in MacOS.
    assumeTrue(OS.getCurrent() != OS.DARWIN);

    Duration userTimeToSpend = Duration.ofSeconds(10);
    Duration systemTimeToSpend = Duration.ofSeconds(10);

    checkLinuxSandboxStatistics(userTimeToSpend, systemTimeToSpend);
  }
}