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