ulfjack | 9274cba | 2017-08-11 23:19:48 +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.exec; |
| 15 | |
| 16 | import static com.google.common.truth.Truth.assertThat; |
ruperts | 70d018b | 2017-09-22 02:20:27 +0200 | [diff] [blame] | 17 | import static org.junit.Assert.fail; |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 18 | import static org.mockito.Matchers.any; |
| 19 | import static org.mockito.Matchers.eq; |
| 20 | import static org.mockito.Mockito.mock; |
| 21 | import static org.mockito.Mockito.never; |
| 22 | import static org.mockito.Mockito.verify; |
| 23 | import static org.mockito.Mockito.when; |
| 24 | |
| 25 | import com.google.devtools.build.lib.actions.ActionExecutionContext; |
| 26 | import com.google.devtools.build.lib.actions.Spawn; |
ruperts | da40fbf | 2017-09-22 05:59:42 +0200 | [diff] [blame] | 27 | import com.google.devtools.build.lib.actions.SpawnResult; |
| 28 | import com.google.devtools.build.lib.actions.SpawnResult.Status; |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 29 | import com.google.devtools.build.lib.exec.SpawnCache.CacheHandle; |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 30 | import com.google.devtools.build.lib.exec.SpawnRunner.SpawnExecutionPolicy; |
| 31 | import com.google.devtools.build.lib.exec.util.SpawnBuilder; |
| 32 | import com.google.devtools.build.lib.testutil.Suite; |
| 33 | import com.google.devtools.build.lib.testutil.TestSpec; |
| 34 | import com.google.devtools.build.lib.vfs.FileSystem; |
| 35 | import com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem; |
| 36 | import java.util.Collection; |
ruperts | 940ce20 | 2017-09-26 11:47:24 -0400 | [diff] [blame^] | 37 | import java.util.Set; |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 38 | import org.junit.Before; |
| 39 | import org.junit.Test; |
| 40 | import org.junit.runner.RunWith; |
| 41 | import org.junit.runners.JUnit4; |
| 42 | import org.mockito.Mock; |
| 43 | import org.mockito.MockitoAnnotations; |
| 44 | |
| 45 | /** Tests for {@link BlazeExecutor}. */ |
| 46 | @RunWith(JUnit4.class) |
| 47 | @TestSpec(size = Suite.SMALL_TESTS) |
| 48 | public class AbstractSpawnStrategyTest { |
| 49 | private static class TestedSpawnStrategy extends AbstractSpawnStrategy { |
| 50 | public TestedSpawnStrategy(SpawnRunner spawnRunner) { |
| 51 | super(spawnRunner); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | private static final Spawn SIMPLE_SPAWN = |
| 56 | new SpawnBuilder("/bin/echo", "Hi!").withEnvironment("VARIABLE", "value").build(); |
| 57 | |
| 58 | private final FileSystem fs = new InMemoryFileSystem(); |
| 59 | @Mock private SpawnRunner spawnRunner; |
| 60 | @Mock private ActionExecutionContext actionExecutionContext; |
| 61 | |
| 62 | @Before |
| 63 | public final void setUp() throws Exception { |
| 64 | MockitoAnnotations.initMocks(this); |
| 65 | } |
| 66 | |
| 67 | @Test |
| 68 | public void testZeroExit() throws Exception { |
| 69 | when(actionExecutionContext.getContext(eq(SpawnCache.class))).thenReturn(SpawnCache.NO_CACHE); |
| 70 | when(actionExecutionContext.getExecRoot()).thenReturn(fs.getPath("/execroot")); |
ruperts | 940ce20 | 2017-09-26 11:47:24 -0400 | [diff] [blame^] | 71 | SpawnResult spawnResult = new SpawnResult.Builder().setStatus(Status.SUCCESS).build(); |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 72 | when(spawnRunner.exec(any(Spawn.class), any(SpawnExecutionPolicy.class))) |
ruperts | 940ce20 | 2017-09-26 11:47:24 -0400 | [diff] [blame^] | 73 | .thenReturn(spawnResult); |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 74 | |
ruperts | 940ce20 | 2017-09-26 11:47:24 -0400 | [diff] [blame^] | 75 | Set<SpawnResult> spawnResults = new TestedSpawnStrategy(spawnRunner) |
| 76 | .exec(SIMPLE_SPAWN, actionExecutionContext); |
| 77 | |
| 78 | assertThat(spawnResults).containsExactly(spawnResult); |
| 79 | |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 80 | // Must only be called exactly once. |
| 81 | verify(spawnRunner).exec(any(Spawn.class), any(SpawnExecutionPolicy.class)); |
| 82 | } |
| 83 | |
| 84 | @Test |
| 85 | public void testNonZeroExit() throws Exception { |
| 86 | when(actionExecutionContext.getContext(eq(SpawnCache.class))).thenReturn(SpawnCache.NO_CACHE); |
| 87 | when(actionExecutionContext.getExecRoot()).thenReturn(fs.getPath("/execroot")); |
| 88 | SpawnResult result = new SpawnResult.Builder().setStatus(Status.SUCCESS).setExitCode(1).build(); |
| 89 | when(spawnRunner.exec(any(Spawn.class), any(SpawnExecutionPolicy.class))) |
| 90 | .thenReturn(result); |
| 91 | |
| 92 | try { |
ruperts | 940ce20 | 2017-09-26 11:47:24 -0400 | [diff] [blame^] | 93 | // Ignoring the Set<SpawnResult> return value. |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 94 | new TestedSpawnStrategy(spawnRunner).exec(SIMPLE_SPAWN, actionExecutionContext); |
ruperts | 70d018b | 2017-09-22 02:20:27 +0200 | [diff] [blame] | 95 | fail("Expected SpawnExecException"); |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 96 | } catch (SpawnExecException e) { |
| 97 | assertThat(e.getSpawnResult()).isSameAs(result); |
| 98 | } |
| 99 | // Must only be called exactly once. |
| 100 | verify(spawnRunner).exec(any(Spawn.class), any(SpawnExecutionPolicy.class)); |
| 101 | } |
| 102 | |
| 103 | @Test |
| 104 | public void testCacheHit() throws Exception { |
| 105 | SpawnCache cache = mock(SpawnCache.class); |
ruperts | 940ce20 | 2017-09-26 11:47:24 -0400 | [diff] [blame^] | 106 | SpawnResult spawnResult = new SpawnResult.Builder().setStatus(Status.SUCCESS).build(); |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 107 | when(cache.lookup(any(Spawn.class), any(SpawnExecutionPolicy.class))) |
ruperts | 940ce20 | 2017-09-26 11:47:24 -0400 | [diff] [blame^] | 108 | .thenReturn(SpawnCache.success(spawnResult)); |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 109 | when(actionExecutionContext.getContext(eq(SpawnCache.class))).thenReturn(cache); |
| 110 | when(actionExecutionContext.getExecRoot()).thenReturn(fs.getPath("/execroot")); |
| 111 | |
ruperts | 940ce20 | 2017-09-26 11:47:24 -0400 | [diff] [blame^] | 112 | Set<SpawnResult> spawnResults = new TestedSpawnStrategy(spawnRunner) |
| 113 | .exec(SIMPLE_SPAWN, actionExecutionContext); |
| 114 | assertThat(spawnResults).containsExactly(spawnResult); |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 115 | verify(spawnRunner, never()).exec(any(Spawn.class), any(SpawnExecutionPolicy.class)); |
| 116 | } |
| 117 | |
| 118 | @SuppressWarnings("unchecked") |
| 119 | @Test |
| 120 | public void testCacheMiss() throws Exception { |
| 121 | SpawnCache cache = mock(SpawnCache.class); |
| 122 | CacheHandle entry = mock(CacheHandle.class); |
| 123 | when(cache.lookup(any(Spawn.class), any(SpawnExecutionPolicy.class))).thenReturn(entry); |
| 124 | when(entry.hasResult()).thenReturn(false); |
| 125 | when(entry.willStore()).thenReturn(true); |
| 126 | |
| 127 | when(actionExecutionContext.getContext(eq(SpawnCache.class))).thenReturn(cache); |
| 128 | when(actionExecutionContext.getExecRoot()).thenReturn(fs.getPath("/execroot")); |
ruperts | 940ce20 | 2017-09-26 11:47:24 -0400 | [diff] [blame^] | 129 | SpawnResult spawnResult = new SpawnResult.Builder().setStatus(Status.SUCCESS).build(); |
| 130 | when(spawnRunner.exec(any(Spawn.class), any(SpawnExecutionPolicy.class))) |
| 131 | .thenReturn(spawnResult); |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 132 | |
ruperts | 940ce20 | 2017-09-26 11:47:24 -0400 | [diff] [blame^] | 133 | Set<SpawnResult> spawnResults = new TestedSpawnStrategy(spawnRunner) |
| 134 | .exec(SIMPLE_SPAWN, actionExecutionContext); |
| 135 | |
| 136 | assertThat(spawnResults).containsExactly(spawnResult); |
| 137 | |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 138 | // Must only be called exactly once. |
| 139 | verify(spawnRunner).exec(any(Spawn.class), any(SpawnExecutionPolicy.class)); |
ruperts | 940ce20 | 2017-09-26 11:47:24 -0400 | [diff] [blame^] | 140 | verify(entry).store(eq(spawnResult), any(Collection.class)); |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | @SuppressWarnings("unchecked") |
| 144 | @Test |
| 145 | public void testCacheMissWithNonZeroExit() throws Exception { |
| 146 | SpawnCache cache = mock(SpawnCache.class); |
| 147 | CacheHandle entry = mock(CacheHandle.class); |
| 148 | when(cache.lookup(any(Spawn.class), any(SpawnExecutionPolicy.class))).thenReturn(entry); |
| 149 | when(entry.hasResult()).thenReturn(false); |
| 150 | when(entry.willStore()).thenReturn(true); |
| 151 | |
| 152 | when(actionExecutionContext.getContext(eq(SpawnCache.class))).thenReturn(cache); |
| 153 | when(actionExecutionContext.getExecRoot()).thenReturn(fs.getPath("/execroot")); |
| 154 | SpawnResult result = new SpawnResult.Builder().setStatus(Status.SUCCESS).setExitCode(1).build(); |
| 155 | when(spawnRunner.exec(any(Spawn.class), any(SpawnExecutionPolicy.class))).thenReturn(result); |
| 156 | |
| 157 | try { |
ruperts | 940ce20 | 2017-09-26 11:47:24 -0400 | [diff] [blame^] | 158 | // Ignoring the Set<SpawnResult> return value. |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 159 | new TestedSpawnStrategy(spawnRunner).exec(SIMPLE_SPAWN, actionExecutionContext); |
ruperts | 70d018b | 2017-09-22 02:20:27 +0200 | [diff] [blame] | 160 | fail("Expected SpawnExecException"); |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 161 | } catch (SpawnExecException e) { |
| 162 | assertThat(e.getSpawnResult()).isSameAs(result); |
| 163 | } |
| 164 | // Must only be called exactly once. |
| 165 | verify(spawnRunner).exec(any(Spawn.class), any(SpawnExecutionPolicy.class)); |
| 166 | verify(entry).store(eq(result), any(Collection.class)); |
| 167 | } |
| 168 | |
| 169 | @Test |
| 170 | public void testTagNoCache() throws Exception { |
| 171 | SpawnCache cache = mock(SpawnCache.class); |
| 172 | when(actionExecutionContext.getContext(eq(SpawnCache.class))).thenReturn(cache); |
| 173 | when(actionExecutionContext.getExecRoot()).thenReturn(fs.getPath("/execroot")); |
| 174 | when(spawnRunner.exec(any(Spawn.class), any(SpawnExecutionPolicy.class))) |
| 175 | .thenReturn(new SpawnResult.Builder().setStatus(Status.SUCCESS).build()); |
| 176 | |
| 177 | Spawn uncacheableSpawn = |
| 178 | new SpawnBuilder("/bin/echo", "Hi").withExecutionInfo("no-cache", "").build(); |
ruperts | 940ce20 | 2017-09-26 11:47:24 -0400 | [diff] [blame^] | 179 | |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 180 | new TestedSpawnStrategy(spawnRunner).exec(uncacheableSpawn, actionExecutionContext); |
ruperts | 940ce20 | 2017-09-26 11:47:24 -0400 | [diff] [blame^] | 181 | |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 182 | // Must only be called exactly once. |
| 183 | verify(spawnRunner).exec(any(Spawn.class), any(SpawnExecutionPolicy.class)); |
| 184 | // Must not be called. |
| 185 | verify(cache, never()).lookup(any(Spawn.class), any(SpawnExecutionPolicy.class)); |
| 186 | } |
| 187 | } |