Ulf Adams | c0a8444 | 2017-03-21 10:08:03 +0000 | [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; |
felly | f6408d6 | 2019-07-16 12:51:14 -0700 | [diff] [blame] | 17 | import static com.google.devtools.build.lib.actions.FilesetManifest.RelativeSymlinkBehavior.ERROR; |
| 18 | import static com.google.devtools.build.lib.actions.FilesetManifest.RelativeSymlinkBehavior.IGNORE; |
| 19 | import static com.google.devtools.build.lib.actions.FilesetManifest.RelativeSymlinkBehavior.RESOLVE; |
michajlo | 660d17f | 2020-03-27 09:01:57 -0700 | [diff] [blame] | 20 | import static org.junit.Assert.assertThrows; |
Ulf Adams | c0a8444 | 2017-03-21 10:08:03 +0000 | [diff] [blame] | 21 | import static org.junit.Assert.fail; |
| 22 | |
ulfjack | 1973be4 | 2018-05-30 03:25:35 -0700 | [diff] [blame] | 23 | import com.google.common.collect.ImmutableList; |
| 24 | import com.google.common.collect.ImmutableMap; |
Ulf Adams | c0a8444 | 2017-03-21 10:08:03 +0000 | [diff] [blame] | 25 | import com.google.devtools.build.lib.actions.ActionInput; |
Ulf Adams | c0a8444 | 2017-03-21 10:08:03 +0000 | [diff] [blame] | 26 | import com.google.devtools.build.lib.actions.ActionInputHelper; |
| 27 | import com.google.devtools.build.lib.actions.Artifact; |
buchgr | d4d3d50 | 2018-08-02 06:47:19 -0700 | [diff] [blame] | 28 | import com.google.devtools.build.lib.actions.Artifact.ArtifactExpander; |
| 29 | import com.google.devtools.build.lib.actions.Artifact.SpecialArtifact; |
| 30 | import com.google.devtools.build.lib.actions.Artifact.SpecialArtifactType; |
| 31 | import com.google.devtools.build.lib.actions.Artifact.TreeFileArtifact; |
tomlu | 1cdcdf9 | 2018-01-16 11:07:51 -0800 | [diff] [blame] | 32 | import com.google.devtools.build.lib.actions.ArtifactRoot; |
Ulf Adams | c0a8444 | 2017-03-21 10:08:03 +0000 | [diff] [blame] | 33 | import com.google.devtools.build.lib.actions.EmptyRunfilesSupplier; |
shahan | 602cc85 | 2018-06-06 20:09:57 -0700 | [diff] [blame] | 34 | import com.google.devtools.build.lib.actions.FileArtifactValue; |
ulfjack | 1973be4 | 2018-05-30 03:25:35 -0700 | [diff] [blame] | 35 | import com.google.devtools.build.lib.actions.FilesetOutputSymlink; |
Ulf Adams | c0a8444 | 2017-03-21 10:08:03 +0000 | [diff] [blame] | 36 | import com.google.devtools.build.lib.actions.RunfilesSupplier; |
buchgr | d4d3d50 | 2018-08-02 06:47:19 -0700 | [diff] [blame] | 37 | import com.google.devtools.build.lib.actions.Spawn; |
janakr | aea0560 | 2019-05-22 15:41:29 -0700 | [diff] [blame] | 38 | import com.google.devtools.build.lib.actions.util.ActionsTestUtil; |
Ulf Adams | c0a8444 | 2017-03-21 10:08:03 +0000 | [diff] [blame] | 39 | import com.google.devtools.build.lib.analysis.Runfiles; |
| 40 | import com.google.devtools.build.lib.analysis.RunfilesSupplierImpl; |
ulfjack | 3903c92 | 2017-07-06 04:30:34 -0400 | [diff] [blame] | 41 | import com.google.devtools.build.lib.exec.util.FakeActionInputFileCache; |
buchgr | d4d3d50 | 2018-08-02 06:47:19 -0700 | [diff] [blame] | 42 | import com.google.devtools.build.lib.exec.util.SpawnBuilder; |
janakr | 97c0bd1 | 2020-09-08 13:19:03 -0700 | [diff] [blame] | 43 | import com.google.devtools.build.lib.vfs.DigestHashFunction; |
Ulf Adams | c0a8444 | 2017-03-21 10:08:03 +0000 | [diff] [blame] | 44 | import com.google.devtools.build.lib.vfs.FileSystem; |
| 45 | import com.google.devtools.build.lib.vfs.FileSystemUtils; |
| 46 | import com.google.devtools.build.lib.vfs.Path; |
| 47 | import com.google.devtools.build.lib.vfs.PathFragment; |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 48 | import com.google.devtools.build.lib.vfs.Root; |
Ulf Adams | c0a8444 | 2017-03-21 10:08:03 +0000 | [diff] [blame] | 49 | import com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem; |
| 50 | import java.io.IOException; |
buchgr | d4d3d50 | 2018-08-02 06:47:19 -0700 | [diff] [blame] | 51 | import java.util.Arrays; |
| 52 | import java.util.Collection; |
ulfjack | 1973be4 | 2018-05-30 03:25:35 -0700 | [diff] [blame] | 53 | import java.util.HashMap; |
Ulf Adams | c0a8444 | 2017-03-21 10:08:03 +0000 | [diff] [blame] | 54 | import java.util.Map; |
Ulf Adams | c0a8444 | 2017-03-21 10:08:03 +0000 | [diff] [blame] | 55 | import org.junit.Test; |
| 56 | import org.junit.runner.RunWith; |
| 57 | import org.junit.runners.JUnit4; |
Ulf Adams | c0a8444 | 2017-03-21 10:08:03 +0000 | [diff] [blame] | 58 | |
buchgr | d4d3d50 | 2018-08-02 06:47:19 -0700 | [diff] [blame] | 59 | /** Tests for {@link SpawnInputExpander}. */ |
Ulf Adams | c0a8444 | 2017-03-21 10:08:03 +0000 | [diff] [blame] | 60 | @RunWith(JUnit4.class) |
| 61 | public class SpawnInputExpanderTest { |
ulfjack | 3903c92 | 2017-07-06 04:30:34 -0400 | [diff] [blame] | 62 | private static final byte[] FAKE_DIGEST = new byte[] {1, 2, 3, 4}; |
| 63 | |
buchgr | d4d3d50 | 2018-08-02 06:47:19 -0700 | [diff] [blame] | 64 | private static final ArtifactExpander NO_ARTIFACT_EXPANDER = |
| 65 | (a, b) -> fail("expected no interactions"); |
| 66 | |
janakr | 97c0bd1 | 2020-09-08 13:19:03 -0700 | [diff] [blame] | 67 | private final FileSystem fs = new InMemoryFileSystem(DigestHashFunction.SHA256); |
Googler | 12498fd | 2018-10-10 10:47:00 -0700 | [diff] [blame] | 68 | private final Path execRoot = fs.getPath("/root"); |
janakr | 448f1cf | 2020-03-30 09:12:44 -0700 | [diff] [blame] | 69 | private final ArtifactRoot rootDir = ArtifactRoot.asDerivedRoot(execRoot, "out"); |
Ulf Adams | c0a8444 | 2017-03-21 10:08:03 +0000 | [diff] [blame] | 70 | |
Googler | 12498fd | 2018-10-10 10:47:00 -0700 | [diff] [blame] | 71 | private SpawnInputExpander expander = new SpawnInputExpander(execRoot, /*strict=*/ true); |
| 72 | private Map<PathFragment, ActionInput> inputMappings = new HashMap<>(); |
Ulf Adams | c0a8444 | 2017-03-21 10:08:03 +0000 | [diff] [blame] | 73 | |
| 74 | @Test |
| 75 | public void testEmptyRunfiles() throws Exception { |
| 76 | RunfilesSupplier supplier = EmptyRunfilesSupplier.INSTANCE; |
buchgr | d4d3d50 | 2018-08-02 06:47:19 -0700 | [diff] [blame] | 77 | FakeActionInputFileCache mockCache = new FakeActionInputFileCache(); |
twerth | c4dfb91 | 2020-07-20 10:32:42 -0700 | [diff] [blame] | 78 | expander.addRunfilesToInputs(inputMappings, supplier, mockCache, NO_ARTIFACT_EXPANDER); |
Ulf Adams | c0a8444 | 2017-03-21 10:08:03 +0000 | [diff] [blame] | 79 | assertThat(inputMappings).isEmpty(); |
| 80 | } |
| 81 | |
| 82 | @Test |
| 83 | public void testRunfilesSingleFile() throws Exception { |
| 84 | Artifact artifact = |
janakr | aea0560 | 2019-05-22 15:41:29 -0700 | [diff] [blame] | 85 | ActionsTestUtil.createArtifact( |
| 86 | ArtifactRoot.asSourceRoot(Root.fromPath(fs.getPath("/root"))), |
| 87 | fs.getPath("/root/dir/file")); |
Ulf Adams | c0a8444 | 2017-03-21 10:08:03 +0000 | [diff] [blame] | 88 | Runfiles runfiles = new Runfiles.Builder("workspace").addArtifact(artifact).build(); |
nharmata | b4060b6 | 2017-04-04 17:11:39 +0000 | [diff] [blame] | 89 | RunfilesSupplier supplier = new RunfilesSupplierImpl(PathFragment.create("runfiles"), runfiles); |
ulfjack | 3903c92 | 2017-07-06 04:30:34 -0400 | [diff] [blame] | 90 | FakeActionInputFileCache mockCache = new FakeActionInputFileCache(); |
janakr | 39969b1 | 2019-03-03 16:25:48 -0800 | [diff] [blame] | 91 | mockCache.put( |
| 92 | artifact, |
lberki | 812e6fe | 2019-07-25 07:50:55 -0700 | [diff] [blame] | 93 | FileArtifactValue.createForNormalFile( |
janakr | 39969b1 | 2019-03-03 16:25:48 -0800 | [diff] [blame] | 94 | FAKE_DIGEST, /*proxy=*/ null, 0L, /*isShareable=*/ true)); |
Ulf Adams | c0a8444 | 2017-03-21 10:08:03 +0000 | [diff] [blame] | 95 | |
twerth | c4dfb91 | 2020-07-20 10:32:42 -0700 | [diff] [blame] | 96 | expander.addRunfilesToInputs(inputMappings, supplier, mockCache, NO_ARTIFACT_EXPANDER); |
Ulf Adams | c0a8444 | 2017-03-21 10:08:03 +0000 | [diff] [blame] | 97 | assertThat(inputMappings).hasSize(1); |
| 98 | assertThat(inputMappings) |
nharmata | b4060b6 | 2017-04-04 17:11:39 +0000 | [diff] [blame] | 99 | .containsEntry(PathFragment.create("runfiles/workspace/dir/file"), artifact); |
Ulf Adams | c0a8444 | 2017-03-21 10:08:03 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | @Test |
felly | 99ad9b9 | 2019-06-12 20:33:57 -0700 | [diff] [blame] | 103 | public void testRunfilesWithFileset() throws Exception { |
| 104 | Artifact artifact = createFilesetArtifact("foo/biz/fs_out"); |
| 105 | Runfiles runfiles = new Runfiles.Builder("workspace").addArtifact(artifact).build(); |
| 106 | RunfilesSupplier supplier = new RunfilesSupplierImpl(PathFragment.create("runfiles"), runfiles); |
| 107 | FakeActionInputFileCache mockCache = new FakeActionInputFileCache(); |
| 108 | mockCache.put( |
| 109 | artifact, |
lberki | 812e6fe | 2019-07-25 07:50:55 -0700 | [diff] [blame] | 110 | FileArtifactValue.createForNormalFile( |
felly | 99ad9b9 | 2019-06-12 20:33:57 -0700 | [diff] [blame] | 111 | FAKE_DIGEST, /*proxy=*/ null, 0L, /*isShareable=*/ true)); |
| 112 | |
| 113 | ArtifactExpander filesetExpander = |
| 114 | new ArtifactExpander() { |
| 115 | @Override |
| 116 | public void expand(Artifact artifact, Collection<? super Artifact> output) { |
| 117 | throw new IllegalStateException("Unexpected tree expansion"); |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public ImmutableList<FilesetOutputSymlink> getFileset(Artifact artifact) { |
| 122 | return ImmutableList.of( |
| 123 | FilesetOutputSymlink.createForTesting( |
| 124 | PathFragment.create("zizz"), |
| 125 | PathFragment.create("/foo/fake_exec/xyz/zizz"), |
| 126 | PathFragment.create("/foo/fake_exec/"))); |
| 127 | } |
| 128 | }; |
| 129 | |
twerth | c4dfb91 | 2020-07-20 10:32:42 -0700 | [diff] [blame] | 130 | expander.addRunfilesToInputs(inputMappings, supplier, mockCache, filesetExpander); |
felly | 99ad9b9 | 2019-06-12 20:33:57 -0700 | [diff] [blame] | 131 | assertThat(inputMappings).hasSize(1); |
| 132 | assertThat(inputMappings) |
| 133 | .containsEntry( |
| 134 | PathFragment.create("runfiles/workspace/foo/biz/fs_out/zizz"), |
| 135 | ActionInputHelper.fromPath("/root/xyz/zizz")); |
| 136 | } |
| 137 | |
| 138 | @Test |
buchgr | d4d3d50 | 2018-08-02 06:47:19 -0700 | [diff] [blame] | 139 | public void testRunfilesDirectoryStrict() { |
Ulf Adams | c0a8444 | 2017-03-21 10:08:03 +0000 | [diff] [blame] | 140 | Artifact artifact = |
janakr | aea0560 | 2019-05-22 15:41:29 -0700 | [diff] [blame] | 141 | ActionsTestUtil.createArtifact( |
| 142 | ArtifactRoot.asSourceRoot(Root.fromPath(fs.getPath("/root"))), |
| 143 | fs.getPath("/root/dir/file")); |
Ulf Adams | c0a8444 | 2017-03-21 10:08:03 +0000 | [diff] [blame] | 144 | Runfiles runfiles = new Runfiles.Builder("workspace").addArtifact(artifact).build(); |
nharmata | b4060b6 | 2017-04-04 17:11:39 +0000 | [diff] [blame] | 145 | RunfilesSupplier supplier = new RunfilesSupplierImpl(PathFragment.create("runfiles"), runfiles); |
ulfjack | 3903c92 | 2017-07-06 04:30:34 -0400 | [diff] [blame] | 146 | FakeActionInputFileCache mockCache = new FakeActionInputFileCache(); |
lberki | 812e6fe | 2019-07-25 07:50:55 -0700 | [diff] [blame] | 147 | mockCache.put(artifact, FileArtifactValue.createForDirectoryWithMtime(-1)); |
Ulf Adams | c0a8444 | 2017-03-21 10:08:03 +0000 | [diff] [blame] | 148 | |
jcater | fb5ed30 | 2019-04-29 13:30:34 -0700 | [diff] [blame] | 149 | IOException expected = |
| 150 | assertThrows( |
| 151 | IOException.class, |
| 152 | () -> |
| 153 | expander.addRunfilesToInputs( |
twerth | c4dfb91 | 2020-07-20 10:32:42 -0700 | [diff] [blame] | 154 | inputMappings, supplier, mockCache, NO_ARTIFACT_EXPANDER)); |
jcater | fb5ed30 | 2019-04-29 13:30:34 -0700 | [diff] [blame] | 155 | assertThat(expected).hasMessageThat().isEqualTo("Not a file: dir/file"); |
Ulf Adams | c0a8444 | 2017-03-21 10:08:03 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | @Test |
| 159 | public void testRunfilesDirectoryNonStrict() throws Exception { |
| 160 | Artifact artifact = |
janakr | aea0560 | 2019-05-22 15:41:29 -0700 | [diff] [blame] | 161 | ActionsTestUtil.createArtifact( |
| 162 | ArtifactRoot.asSourceRoot(Root.fromPath(fs.getPath("/root"))), |
| 163 | fs.getPath("/root/dir/file")); |
Ulf Adams | c0a8444 | 2017-03-21 10:08:03 +0000 | [diff] [blame] | 164 | Runfiles runfiles = new Runfiles.Builder("workspace").addArtifact(artifact).build(); |
nharmata | b4060b6 | 2017-04-04 17:11:39 +0000 | [diff] [blame] | 165 | RunfilesSupplier supplier = new RunfilesSupplierImpl(PathFragment.create("runfiles"), runfiles); |
ulfjack | 3903c92 | 2017-07-06 04:30:34 -0400 | [diff] [blame] | 166 | FakeActionInputFileCache mockCache = new FakeActionInputFileCache(); |
lberki | 812e6fe | 2019-07-25 07:50:55 -0700 | [diff] [blame] | 167 | mockCache.put(artifact, FileArtifactValue.createForDirectoryWithMtime(-1)); |
Ulf Adams | c0a8444 | 2017-03-21 10:08:03 +0000 | [diff] [blame] | 168 | |
tomlu | 1a19b62 | 2018-01-11 15:17:28 -0800 | [diff] [blame] | 169 | expander = new SpawnInputExpander(execRoot, /*strict=*/ false); |
twerth | c4dfb91 | 2020-07-20 10:32:42 -0700 | [diff] [blame] | 170 | expander.addRunfilesToInputs(inputMappings, supplier, mockCache, NO_ARTIFACT_EXPANDER); |
Ulf Adams | c0a8444 | 2017-03-21 10:08:03 +0000 | [diff] [blame] | 171 | assertThat(inputMappings).hasSize(1); |
| 172 | assertThat(inputMappings) |
nharmata | b4060b6 | 2017-04-04 17:11:39 +0000 | [diff] [blame] | 173 | .containsEntry(PathFragment.create("runfiles/workspace/dir/file"), artifact); |
Ulf Adams | c0a8444 | 2017-03-21 10:08:03 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | @Test |
| 177 | public void testRunfilesTwoFiles() throws Exception { |
| 178 | Artifact artifact1 = |
janakr | aea0560 | 2019-05-22 15:41:29 -0700 | [diff] [blame] | 179 | ActionsTestUtil.createArtifact( |
| 180 | ArtifactRoot.asSourceRoot(Root.fromPath(fs.getPath("/root"))), |
| 181 | fs.getPath("/root/dir/file")); |
Ulf Adams | c0a8444 | 2017-03-21 10:08:03 +0000 | [diff] [blame] | 182 | Artifact artifact2 = |
janakr | aea0560 | 2019-05-22 15:41:29 -0700 | [diff] [blame] | 183 | ActionsTestUtil.createArtifact( |
| 184 | ArtifactRoot.asSourceRoot(Root.fromPath(fs.getPath("/root"))), |
| 185 | fs.getPath("/root/dir/baz")); |
buchgr | d4d3d50 | 2018-08-02 06:47:19 -0700 | [diff] [blame] | 186 | Runfiles runfiles = |
| 187 | new Runfiles.Builder("workspace").addArtifact(artifact1).addArtifact(artifact2).build(); |
nharmata | b4060b6 | 2017-04-04 17:11:39 +0000 | [diff] [blame] | 188 | RunfilesSupplier supplier = new RunfilesSupplierImpl(PathFragment.create("runfiles"), runfiles); |
ulfjack | 3903c92 | 2017-07-06 04:30:34 -0400 | [diff] [blame] | 189 | FakeActionInputFileCache mockCache = new FakeActionInputFileCache(); |
janakr | 39969b1 | 2019-03-03 16:25:48 -0800 | [diff] [blame] | 190 | mockCache.put( |
| 191 | artifact1, |
lberki | 812e6fe | 2019-07-25 07:50:55 -0700 | [diff] [blame] | 192 | FileArtifactValue.createForNormalFile( |
janakr | 39969b1 | 2019-03-03 16:25:48 -0800 | [diff] [blame] | 193 | FAKE_DIGEST, /*proxy=*/ null, 1L, /*isShareable=*/ true)); |
| 194 | mockCache.put( |
| 195 | artifact2, |
lberki | 812e6fe | 2019-07-25 07:50:55 -0700 | [diff] [blame] | 196 | FileArtifactValue.createForNormalFile( |
janakr | 39969b1 | 2019-03-03 16:25:48 -0800 | [diff] [blame] | 197 | FAKE_DIGEST, /*proxy=*/ null, 12L, /*isShareable=*/ true)); |
Ulf Adams | c0a8444 | 2017-03-21 10:08:03 +0000 | [diff] [blame] | 198 | |
twerth | c4dfb91 | 2020-07-20 10:32:42 -0700 | [diff] [blame] | 199 | expander.addRunfilesToInputs(inputMappings, supplier, mockCache, NO_ARTIFACT_EXPANDER); |
Ulf Adams | c0a8444 | 2017-03-21 10:08:03 +0000 | [diff] [blame] | 200 | assertThat(inputMappings).hasSize(2); |
| 201 | assertThat(inputMappings) |
nharmata | b4060b6 | 2017-04-04 17:11:39 +0000 | [diff] [blame] | 202 | .containsEntry(PathFragment.create("runfiles/workspace/dir/file"), artifact1); |
Ulf Adams | c0a8444 | 2017-03-21 10:08:03 +0000 | [diff] [blame] | 203 | assertThat(inputMappings) |
nharmata | b4060b6 | 2017-04-04 17:11:39 +0000 | [diff] [blame] | 204 | .containsEntry(PathFragment.create("runfiles/workspace/dir/baz"), artifact2); |
Ulf Adams | c0a8444 | 2017-03-21 10:08:03 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | @Test |
| 208 | public void testRunfilesSymlink() throws Exception { |
| 209 | Artifact artifact = |
janakr | aea0560 | 2019-05-22 15:41:29 -0700 | [diff] [blame] | 210 | ActionsTestUtil.createArtifact( |
| 211 | ArtifactRoot.asSourceRoot(Root.fromPath(fs.getPath("/root"))), |
| 212 | fs.getPath("/root/dir/file")); |
buchgr | d4d3d50 | 2018-08-02 06:47:19 -0700 | [diff] [blame] | 213 | Runfiles runfiles = |
| 214 | new Runfiles.Builder("workspace") |
| 215 | .addSymlink(PathFragment.create("symlink"), artifact) |
| 216 | .build(); |
nharmata | b4060b6 | 2017-04-04 17:11:39 +0000 | [diff] [blame] | 217 | RunfilesSupplier supplier = new RunfilesSupplierImpl(PathFragment.create("runfiles"), runfiles); |
ulfjack | 3903c92 | 2017-07-06 04:30:34 -0400 | [diff] [blame] | 218 | FakeActionInputFileCache mockCache = new FakeActionInputFileCache(); |
janakr | 39969b1 | 2019-03-03 16:25:48 -0800 | [diff] [blame] | 219 | mockCache.put( |
| 220 | artifact, |
lberki | 812e6fe | 2019-07-25 07:50:55 -0700 | [diff] [blame] | 221 | FileArtifactValue.createForNormalFile( |
janakr | 39969b1 | 2019-03-03 16:25:48 -0800 | [diff] [blame] | 222 | FAKE_DIGEST, /*proxy=*/ null, 1L, /*isShareable=*/ true)); |
Ulf Adams | c0a8444 | 2017-03-21 10:08:03 +0000 | [diff] [blame] | 223 | |
twerth | c4dfb91 | 2020-07-20 10:32:42 -0700 | [diff] [blame] | 224 | expander.addRunfilesToInputs(inputMappings, supplier, mockCache, NO_ARTIFACT_EXPANDER); |
Ulf Adams | c0a8444 | 2017-03-21 10:08:03 +0000 | [diff] [blame] | 225 | assertThat(inputMappings).hasSize(1); |
| 226 | assertThat(inputMappings) |
nharmata | b4060b6 | 2017-04-04 17:11:39 +0000 | [diff] [blame] | 227 | .containsEntry(PathFragment.create("runfiles/workspace/symlink"), artifact); |
Ulf Adams | c0a8444 | 2017-03-21 10:08:03 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | @Test |
| 231 | public void testRunfilesRootSymlink() throws Exception { |
| 232 | Artifact artifact = |
janakr | aea0560 | 2019-05-22 15:41:29 -0700 | [diff] [blame] | 233 | ActionsTestUtil.createArtifact( |
| 234 | ArtifactRoot.asSourceRoot(Root.fromPath(fs.getPath("/root"))), |
| 235 | fs.getPath("/root/dir/file")); |
buchgr | d4d3d50 | 2018-08-02 06:47:19 -0700 | [diff] [blame] | 236 | Runfiles runfiles = |
| 237 | new Runfiles.Builder("workspace") |
| 238 | .addRootSymlink(PathFragment.create("symlink"), artifact) |
| 239 | .build(); |
nharmata | b4060b6 | 2017-04-04 17:11:39 +0000 | [diff] [blame] | 240 | RunfilesSupplier supplier = new RunfilesSupplierImpl(PathFragment.create("runfiles"), runfiles); |
ulfjack | 3903c92 | 2017-07-06 04:30:34 -0400 | [diff] [blame] | 241 | FakeActionInputFileCache mockCache = new FakeActionInputFileCache(); |
janakr | 39969b1 | 2019-03-03 16:25:48 -0800 | [diff] [blame] | 242 | mockCache.put( |
| 243 | artifact, |
lberki | 812e6fe | 2019-07-25 07:50:55 -0700 | [diff] [blame] | 244 | FileArtifactValue.createForNormalFile( |
janakr | 39969b1 | 2019-03-03 16:25:48 -0800 | [diff] [blame] | 245 | FAKE_DIGEST, /*proxy=*/ null, 1L, /*isShareable=*/ true)); |
Ulf Adams | c0a8444 | 2017-03-21 10:08:03 +0000 | [diff] [blame] | 246 | |
twerth | c4dfb91 | 2020-07-20 10:32:42 -0700 | [diff] [blame] | 247 | expander.addRunfilesToInputs(inputMappings, supplier, mockCache, NO_ARTIFACT_EXPANDER); |
Ulf Adams | c0a8444 | 2017-03-21 10:08:03 +0000 | [diff] [blame] | 248 | assertThat(inputMappings).hasSize(2); |
nharmata | b4060b6 | 2017-04-04 17:11:39 +0000 | [diff] [blame] | 249 | assertThat(inputMappings).containsEntry(PathFragment.create("runfiles/symlink"), artifact); |
Ulf Adams | c0a8444 | 2017-03-21 10:08:03 +0000 | [diff] [blame] | 250 | // If there's no other entry, Runfiles adds an empty file in the workspace to make sure the |
| 251 | // directory gets created. |
| 252 | assertThat(inputMappings) |
| 253 | .containsEntry( |
aiuto | 0f626a4 | 2021-01-08 10:51:30 -0800 | [diff] [blame^] | 254 | PathFragment.create("runfiles/workspace/.runfile"), SpawnInputExpander.EMPTY_FILE); |
Ulf Adams | c0a8444 | 2017-03-21 10:08:03 +0000 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | @Test |
buchgr | d4d3d50 | 2018-08-02 06:47:19 -0700 | [diff] [blame] | 258 | public void testRunfilesWithTreeArtifacts() throws Exception { |
| 259 | SpecialArtifact treeArtifact = createTreeArtifact("treeArtifact"); |
| 260 | assertThat(treeArtifact.isTreeArtifact()).isTrue(); |
Googler | 1d8d138 | 2020-05-18 12:10:49 -0700 | [diff] [blame] | 261 | TreeFileArtifact file1 = TreeFileArtifact.createTreeOutput(treeArtifact, "file1"); |
| 262 | TreeFileArtifact file2 = TreeFileArtifact.createTreeOutput(treeArtifact, "file2"); |
buchgr | d4d3d50 | 2018-08-02 06:47:19 -0700 | [diff] [blame] | 263 | FileSystemUtils.writeContentAsLatin1(file1.getPath(), "foo"); |
| 264 | FileSystemUtils.writeContentAsLatin1(file2.getPath(), "bar"); |
| 265 | |
| 266 | Runfiles runfiles = new Runfiles.Builder("workspace").addArtifact(treeArtifact).build(); |
| 267 | ArtifactExpander artifactExpander = |
| 268 | (Artifact artifact, Collection<? super Artifact> output) -> { |
| 269 | if (artifact.equals(treeArtifact)) { |
| 270 | output.addAll(Arrays.asList(file1, file2)); |
| 271 | } |
| 272 | }; |
| 273 | RunfilesSupplier supplier = new RunfilesSupplierImpl(PathFragment.create("runfiles"), runfiles); |
| 274 | FakeActionInputFileCache fakeCache = new FakeActionInputFileCache(); |
lberki | 812e6fe | 2019-07-25 07:50:55 -0700 | [diff] [blame] | 275 | fakeCache.put(file1, FileArtifactValue.createForTesting(file1)); |
| 276 | fakeCache.put(file2, FileArtifactValue.createForTesting(file2)); |
buchgr | d4d3d50 | 2018-08-02 06:47:19 -0700 | [diff] [blame] | 277 | |
twerth | c4dfb91 | 2020-07-20 10:32:42 -0700 | [diff] [blame] | 278 | expander.addRunfilesToInputs(inputMappings, supplier, fakeCache, artifactExpander); |
buchgr | d4d3d50 | 2018-08-02 06:47:19 -0700 | [diff] [blame] | 279 | assertThat(inputMappings).hasSize(2); |
| 280 | assertThat(inputMappings) |
| 281 | .containsEntry(PathFragment.create("runfiles/workspace/treeArtifact/file1"), file1); |
| 282 | assertThat(inputMappings) |
| 283 | .containsEntry(PathFragment.create("runfiles/workspace/treeArtifact/file2"), file2); |
| 284 | } |
| 285 | |
| 286 | @Test |
| 287 | public void testRunfilesWithTreeArtifactsInSymlinks() throws Exception { |
| 288 | SpecialArtifact treeArtifact = createTreeArtifact("treeArtifact"); |
| 289 | assertThat(treeArtifact.isTreeArtifact()).isTrue(); |
Googler | 1d8d138 | 2020-05-18 12:10:49 -0700 | [diff] [blame] | 290 | TreeFileArtifact file1 = TreeFileArtifact.createTreeOutput(treeArtifact, "file1"); |
| 291 | TreeFileArtifact file2 = TreeFileArtifact.createTreeOutput(treeArtifact, "file2"); |
buchgr | d4d3d50 | 2018-08-02 06:47:19 -0700 | [diff] [blame] | 292 | FileSystemUtils.writeContentAsLatin1(file1.getPath(), "foo"); |
| 293 | FileSystemUtils.writeContentAsLatin1(file2.getPath(), "bar"); |
| 294 | Runfiles runfiles = |
| 295 | new Runfiles.Builder("workspace") |
| 296 | .addSymlink(PathFragment.create("symlink"), treeArtifact) |
| 297 | .build(); |
| 298 | |
| 299 | ArtifactExpander artifactExpander = |
| 300 | (Artifact artifact, Collection<? super Artifact> output) -> { |
| 301 | if (artifact.equals(treeArtifact)) { |
| 302 | output.addAll(Arrays.asList(file1, file2)); |
| 303 | } |
| 304 | }; |
| 305 | RunfilesSupplier supplier = new RunfilesSupplierImpl(PathFragment.create("runfiles"), runfiles); |
| 306 | FakeActionInputFileCache fakeCache = new FakeActionInputFileCache(); |
lberki | 812e6fe | 2019-07-25 07:50:55 -0700 | [diff] [blame] | 307 | fakeCache.put(file1, FileArtifactValue.createForTesting(file1)); |
| 308 | fakeCache.put(file2, FileArtifactValue.createForTesting(file2)); |
buchgr | d4d3d50 | 2018-08-02 06:47:19 -0700 | [diff] [blame] | 309 | |
twerth | c4dfb91 | 2020-07-20 10:32:42 -0700 | [diff] [blame] | 310 | expander.addRunfilesToInputs(inputMappings, supplier, fakeCache, artifactExpander); |
buchgr | d4d3d50 | 2018-08-02 06:47:19 -0700 | [diff] [blame] | 311 | assertThat(inputMappings).hasSize(2); |
| 312 | assertThat(inputMappings) |
| 313 | .containsEntry(PathFragment.create("runfiles/workspace/symlink/file1"), file1); |
| 314 | assertThat(inputMappings) |
| 315 | .containsEntry(PathFragment.create("runfiles/workspace/symlink/file2"), file2); |
| 316 | } |
| 317 | |
| 318 | @Test |
| 319 | public void testTreeArtifactsInInputs() throws Exception { |
| 320 | SpecialArtifact treeArtifact = createTreeArtifact("treeArtifact"); |
| 321 | assertThat(treeArtifact.isTreeArtifact()).isTrue(); |
Googler | 1d8d138 | 2020-05-18 12:10:49 -0700 | [diff] [blame] | 322 | TreeFileArtifact file1 = TreeFileArtifact.createTreeOutput(treeArtifact, "file1"); |
| 323 | TreeFileArtifact file2 = TreeFileArtifact.createTreeOutput(treeArtifact, "file2"); |
buchgr | d4d3d50 | 2018-08-02 06:47:19 -0700 | [diff] [blame] | 324 | FileSystemUtils.writeContentAsLatin1(file1.getPath(), "foo"); |
| 325 | FileSystemUtils.writeContentAsLatin1(file2.getPath(), "bar"); |
| 326 | |
| 327 | ArtifactExpander artifactExpander = |
| 328 | (Artifact artifact, Collection<? super Artifact> output) -> { |
| 329 | if (artifact.equals(treeArtifact)) { |
| 330 | output.addAll(Arrays.asList(file1, file2)); |
| 331 | } |
| 332 | }; |
| 333 | FakeActionInputFileCache fakeCache = new FakeActionInputFileCache(); |
lberki | 812e6fe | 2019-07-25 07:50:55 -0700 | [diff] [blame] | 334 | fakeCache.put(file1, FileArtifactValue.createForTesting(file1)); |
| 335 | fakeCache.put(file2, FileArtifactValue.createForTesting(file2)); |
buchgr | d4d3d50 | 2018-08-02 06:47:19 -0700 | [diff] [blame] | 336 | |
| 337 | Spawn spawn = new SpawnBuilder("/bin/echo", "Hello World").withInput(treeArtifact).build(); |
twerth | c4dfb91 | 2020-07-20 10:32:42 -0700 | [diff] [blame] | 338 | inputMappings = expander.getInputMapping(spawn, artifactExpander, fakeCache); |
buchgr | d4d3d50 | 2018-08-02 06:47:19 -0700 | [diff] [blame] | 339 | assertThat(inputMappings).hasSize(2); |
janakr | aea0560 | 2019-05-22 15:41:29 -0700 | [diff] [blame] | 340 | assertThat(inputMappings).containsEntry(PathFragment.create("out/treeArtifact/file1"), file1); |
| 341 | assertThat(inputMappings).containsEntry(PathFragment.create("out/treeArtifact/file2"), file2); |
buchgr | d4d3d50 | 2018-08-02 06:47:19 -0700 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | private SpecialArtifact createTreeArtifact(String relPath) throws IOException { |
Googler | 1d8d138 | 2020-05-18 12:10:49 -0700 | [diff] [blame] | 345 | SpecialArtifact treeArtifact = createSpecialArtifact(relPath, SpecialArtifactType.TREE); |
| 346 | treeArtifact.setGeneratingActionKey(ActionsTestUtil.NULL_ACTION_LOOKUP_DATA); |
| 347 | return treeArtifact; |
felly | 99ad9b9 | 2019-06-12 20:33:57 -0700 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | private SpecialArtifact createFilesetArtifact(String relPath) throws IOException { |
| 351 | return createSpecialArtifact(relPath, SpecialArtifactType.FILESET); |
| 352 | } |
| 353 | |
| 354 | private SpecialArtifact createSpecialArtifact(String relPath, SpecialArtifactType type) |
| 355 | throws IOException { |
janakr | 448f1cf | 2020-03-30 09:12:44 -0700 | [diff] [blame] | 356 | String outputSegment = "out"; |
| 357 | Path outputDir = execRoot.getRelative(outputSegment); |
janakr | aea0560 | 2019-05-22 15:41:29 -0700 | [diff] [blame] | 358 | Path outputPath = outputDir.getRelative(relPath); |
buchgr | d4d3d50 | 2018-08-02 06:47:19 -0700 | [diff] [blame] | 359 | outputPath.createDirectoryAndParents(); |
janakr | 448f1cf | 2020-03-30 09:12:44 -0700 | [diff] [blame] | 360 | ArtifactRoot derivedRoot = ArtifactRoot.asDerivedRoot(execRoot, outputSegment); |
buchgr | d4d3d50 | 2018-08-02 06:47:19 -0700 | [diff] [blame] | 361 | return new SpecialArtifact( |
| 362 | derivedRoot, |
| 363 | derivedRoot.getExecPath().getRelative(derivedRoot.getRoot().relativize(outputPath)), |
janakr | efb3f15 | 2019-06-05 17:42:34 -0700 | [diff] [blame] | 364 | ActionsTestUtil.NULL_ARTIFACT_OWNER, |
felly | 99ad9b9 | 2019-06-12 20:33:57 -0700 | [diff] [blame] | 365 | type); |
buchgr | d4d3d50 | 2018-08-02 06:47:19 -0700 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | @Test |
Ulf Adams | c0a8444 | 2017-03-21 10:08:03 +0000 | [diff] [blame] | 369 | public void testEmptyManifest() throws Exception { |
Googler | 12498fd | 2018-10-10 10:47:00 -0700 | [diff] [blame] | 370 | Map<Artifact, ImmutableList<FilesetOutputSymlink>> filesetMappings = |
| 371 | ImmutableMap.of(createFileset("out"), ImmutableList.of()); |
Ulf Adams | c0a8444 | 2017-03-21 10:08:03 +0000 | [diff] [blame] | 372 | |
Googler | 12498fd | 2018-10-10 10:47:00 -0700 | [diff] [blame] | 373 | expander.addFilesetManifests(filesetMappings, inputMappings); |
| 374 | |
Ulf Adams | c0a8444 | 2017-03-21 10:08:03 +0000 | [diff] [blame] | 375 | assertThat(inputMappings).isEmpty(); |
| 376 | } |
| 377 | |
| 378 | @Test |
| 379 | public void testManifestWithSingleFile() throws Exception { |
Googler | 12498fd | 2018-10-10 10:47:00 -0700 | [diff] [blame] | 380 | Map<Artifact, ImmutableList<FilesetOutputSymlink>> filesetMappings = |
| 381 | ImmutableMap.of( |
| 382 | createFileset("out"), ImmutableList.of(filesetSymlink("foo/bar", "/dir/file"))); |
Ulf Adams | c0a8444 | 2017-03-21 10:08:03 +0000 | [diff] [blame] | 383 | |
Googler | 12498fd | 2018-10-10 10:47:00 -0700 | [diff] [blame] | 384 | expander.addFilesetManifests(filesetMappings, inputMappings); |
| 385 | |
Ulf Adams | c0a8444 | 2017-03-21 10:08:03 +0000 | [diff] [blame] | 386 | assertThat(inputMappings) |
Googler | 12498fd | 2018-10-10 10:47:00 -0700 | [diff] [blame] | 387 | .containsExactly( |
| 388 | PathFragment.create("out/foo/bar"), ActionInputHelper.fromPath("/dir/file")); |
Ulf Adams | c0a8444 | 2017-03-21 10:08:03 +0000 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | @Test |
| 392 | public void testManifestWithTwoFiles() throws Exception { |
Googler | 12498fd | 2018-10-10 10:47:00 -0700 | [diff] [blame] | 393 | Map<Artifact, ImmutableList<FilesetOutputSymlink>> filesetMappings = |
| 394 | ImmutableMap.of( |
| 395 | createFileset("out"), |
| 396 | ImmutableList.of( |
| 397 | filesetSymlink("foo/bar", "/dir/file"), filesetSymlink("foo/baz", "/dir/file"))); |
Ulf Adams | c0a8444 | 2017-03-21 10:08:03 +0000 | [diff] [blame] | 398 | |
Googler | 12498fd | 2018-10-10 10:47:00 -0700 | [diff] [blame] | 399 | expander.addFilesetManifests(filesetMappings, inputMappings); |
| 400 | |
Ulf Adams | c0a8444 | 2017-03-21 10:08:03 +0000 | [diff] [blame] | 401 | assertThat(inputMappings) |
Googler | 12498fd | 2018-10-10 10:47:00 -0700 | [diff] [blame] | 402 | .containsExactly( |
| 403 | PathFragment.create("out/foo/bar"), ActionInputHelper.fromPath("/dir/file"), |
| 404 | PathFragment.create("out/foo/baz"), ActionInputHelper.fromPath("/dir/file")); |
Ulf Adams | c0a8444 | 2017-03-21 10:08:03 +0000 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | @Test |
| 408 | public void testManifestWithDirectory() throws Exception { |
Googler | 12498fd | 2018-10-10 10:47:00 -0700 | [diff] [blame] | 409 | Map<Artifact, ImmutableList<FilesetOutputSymlink>> filesetMappings = |
| 410 | ImmutableMap.of(createFileset("out"), ImmutableList.of(filesetSymlink("foo/bar", "/some"))); |
Ulf Adams | c0a8444 | 2017-03-21 10:08:03 +0000 | [diff] [blame] | 411 | |
Googler | 12498fd | 2018-10-10 10:47:00 -0700 | [diff] [blame] | 412 | expander.addFilesetManifests(filesetMappings, inputMappings); |
| 413 | |
Ulf Adams | c0a8444 | 2017-03-21 10:08:03 +0000 | [diff] [blame] | 414 | assertThat(inputMappings) |
Googler | 12498fd | 2018-10-10 10:47:00 -0700 | [diff] [blame] | 415 | .containsExactly(PathFragment.create("out/foo/bar"), ActionInputHelper.fromPath("/some")); |
Ulf Adams | c0a8444 | 2017-03-21 10:08:03 +0000 | [diff] [blame] | 416 | } |
ulfjack | 1973be4 | 2018-05-30 03:25:35 -0700 | [diff] [blame] | 417 | |
Googler | 18b0844 | 2018-10-04 09:55:06 -0700 | [diff] [blame] | 418 | private static FilesetOutputSymlink filesetSymlink(String from, String to) { |
kush | 4b120e7 | 2018-07-11 16:21:27 -0700 | [diff] [blame] | 419 | return FilesetOutputSymlink.createForTesting( |
Googler | 18b0844 | 2018-10-04 09:55:06 -0700 | [diff] [blame] | 420 | PathFragment.create(from), PathFragment.create(to), PathFragment.create("/root")); |
ulfjack | 1973be4 | 2018-05-30 03:25:35 -0700 | [diff] [blame] | 421 | } |
| 422 | |
tomlu | 73eccc2 | 2018-09-06 08:02:37 -0700 | [diff] [blame] | 423 | private ImmutableMap<Artifact, ImmutableList<FilesetOutputSymlink>> simpleFilesetManifest() { |
ulfjack | 1973be4 | 2018-05-30 03:25:35 -0700 | [diff] [blame] | 424 | return ImmutableMap.of( |
tomlu | 73eccc2 | 2018-09-06 08:02:37 -0700 | [diff] [blame] | 425 | createFileset("out"), |
ulfjack | 1973be4 | 2018-05-30 03:25:35 -0700 | [diff] [blame] | 426 | ImmutableList.of( |
Googler | 18b0844 | 2018-10-04 09:55:06 -0700 | [diff] [blame] | 427 | filesetSymlink("workspace/bar", "foo"), filesetSymlink("workspace/foo", "/root/bar"))); |
ulfjack | 1973be4 | 2018-05-30 03:25:35 -0700 | [diff] [blame] | 428 | } |
| 429 | |
tomlu | 73eccc2 | 2018-09-06 08:02:37 -0700 | [diff] [blame] | 430 | private SpecialArtifact createFileset(String execPath) { |
| 431 | return new SpecialArtifact( |
| 432 | rootDir, |
| 433 | PathFragment.create(execPath), |
janakr | efb3f15 | 2019-06-05 17:42:34 -0700 | [diff] [blame] | 434 | ActionsTestUtil.NULL_ARTIFACT_OWNER, |
tomlu | 73eccc2 | 2018-09-06 08:02:37 -0700 | [diff] [blame] | 435 | SpecialArtifactType.FILESET); |
| 436 | } |
| 437 | |
ulfjack | 1973be4 | 2018-05-30 03:25:35 -0700 | [diff] [blame] | 438 | @Test |
| 439 | public void testManifestWithErrorOnRelativeSymlink() throws Exception { |
| 440 | expander = new SpawnInputExpander(execRoot, /*strict=*/ true, ERROR); |
jcater | fb5ed30 | 2019-04-29 13:30:34 -0700 | [diff] [blame] | 441 | IOException e = |
| 442 | assertThrows( |
| 443 | IOException.class, |
| 444 | () -> expander.addFilesetManifests(simpleFilesetManifest(), inputMappings)); |
| 445 | assertThat(e).hasMessageThat().contains("runfiles target is not absolute: foo"); |
ulfjack | 1973be4 | 2018-05-30 03:25:35 -0700 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | @Test |
| 449 | public void testManifestWithIgnoredRelativeSymlink() throws Exception { |
| 450 | expander = new SpawnInputExpander(execRoot, /*strict=*/ true, IGNORE); |
Googler | 12498fd | 2018-10-10 10:47:00 -0700 | [diff] [blame] | 451 | expander.addFilesetManifests(simpleFilesetManifest(), inputMappings); |
| 452 | assertThat(inputMappings) |
ulfjack | 1973be4 | 2018-05-30 03:25:35 -0700 | [diff] [blame] | 453 | .containsExactly( |
Googler | 18b0844 | 2018-10-04 09:55:06 -0700 | [diff] [blame] | 454 | PathFragment.create("out/workspace/foo"), ActionInputHelper.fromPath("/root/bar")); |
ulfjack | 1973be4 | 2018-05-30 03:25:35 -0700 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | @Test |
| 458 | public void testManifestWithResolvedRelativeSymlink() throws Exception { |
| 459 | expander = new SpawnInputExpander(execRoot, /*strict=*/ true, RESOLVE); |
Googler | 12498fd | 2018-10-10 10:47:00 -0700 | [diff] [blame] | 460 | expander.addFilesetManifests(simpleFilesetManifest(), inputMappings); |
| 461 | assertThat(inputMappings) |
ulfjack | 1973be4 | 2018-05-30 03:25:35 -0700 | [diff] [blame] | 462 | .containsExactly( |
Googler | 18b0844 | 2018-10-04 09:55:06 -0700 | [diff] [blame] | 463 | PathFragment.create("out/workspace/bar"), ActionInputHelper.fromPath("/root/bar"), |
| 464 | PathFragment.create("out/workspace/foo"), ActionInputHelper.fromPath("/root/bar")); |
ulfjack | 1973be4 | 2018-05-30 03:25:35 -0700 | [diff] [blame] | 465 | } |
Ulf Adams | c0a8444 | 2017-03-21 10:08:03 +0000 | [diff] [blame] | 466 | } |