|
| 1 | +// Copyright 2014 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.actions.cache; |
| 15 | + |
| 16 | +import static com.google.common.truth.Truth.assertThat; |
| 17 | +import static com.google.devtools.build.lib.vfs.DigestHashFunction.SHA256; |
| 18 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 19 | + |
| 20 | +import com.google.devtools.build.lib.actions.util.ActionsTestUtil; |
| 21 | +import com.google.devtools.build.lib.unix.UnixFileSystem; |
| 22 | +import com.google.devtools.build.lib.util.OS; |
| 23 | +import com.google.devtools.build.lib.vfs.Dirent; |
| 24 | +import com.google.devtools.build.lib.vfs.FileSystem; |
| 25 | +import com.google.devtools.build.lib.vfs.FileSystemUtils; |
| 26 | +import com.google.devtools.build.lib.vfs.JavaIoFileSystem; |
| 27 | +import com.google.devtools.build.lib.vfs.Path; |
| 28 | +import com.google.devtools.build.lib.vfs.Symlinks; |
| 29 | +import com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem; |
| 30 | +import com.google.devtools.build.lib.windows.WindowsFileSystem; |
| 31 | +import com.google.testing.junit.testparameterinjector.TestParameter; |
| 32 | +import com.google.testing.junit.testparameterinjector.TestParameterInjector; |
| 33 | +import org.junit.Rule; |
| 34 | +import org.junit.Test; |
| 35 | +import org.junit.rules.TemporaryFolder; |
| 36 | +import org.junit.runner.RunWith; |
| 37 | + |
| 38 | +@RunWith(TestParameterInjector.class) |
| 39 | +public class VirtualActionInputTest { |
| 40 | + @Rule public TemporaryFolder tempFolder = new TemporaryFolder(); |
| 41 | + |
| 42 | + public enum FileSystemType { |
| 43 | + IN_MEMORY, |
| 44 | + JAVA, |
| 45 | + NATIVE; |
| 46 | + |
| 47 | + FileSystem getFileSystem() { |
| 48 | + return switch (this) { |
| 49 | + case IN_MEMORY -> new InMemoryFileSystem(SHA256); |
| 50 | + case JAVA -> new JavaIoFileSystem(SHA256); |
| 51 | + case NATIVE -> |
| 52 | + OS.getCurrent() == OS.WINDOWS |
| 53 | + ? new WindowsFileSystem(SHA256, /* createSymbolicLinks= */ false) |
| 54 | + : new UnixFileSystem(SHA256, "hash"); |
| 55 | + }; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void testAtomicallyWriteRelativeTo(@TestParameter FileSystemType fileSystemType) |
| 61 | + throws Exception { |
| 62 | + FileSystem fs = fileSystemType.getFileSystem(); |
| 63 | + Path execRoot = fs.getPath(tempFolder.getRoot().getPath()); |
| 64 | + |
| 65 | + Path outputFile = execRoot.getRelative("some/file"); |
| 66 | + VirtualActionInput input = |
| 67 | + ActionsTestUtil.createVirtualActionInput( |
| 68 | + outputFile.relativeTo(execRoot).getPathString(), "hello"); |
| 69 | + |
| 70 | + var digest = input.atomicallyWriteRelativeTo(execRoot); |
| 71 | + |
| 72 | + assertThat(outputFile.getParentDirectory().readdir(Symlinks.NOFOLLOW)) |
| 73 | + .containsExactly(new Dirent("file", Dirent.Type.FILE)); |
| 74 | + assertThat(FileSystemUtils.readLines(outputFile, UTF_8)).containsExactly("hello"); |
| 75 | + assertThat(outputFile.isExecutable()).isTrue(); |
| 76 | + assertThat(digest).isEqualTo(SHA256.getHashFunction().hashString("hello", UTF_8).asBytes()); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void testAtomicallyWriteRelativeTo_concurrentRead( |
| 81 | + @TestParameter FileSystemType fileSystemType) throws Exception { |
| 82 | + FileSystem fs = fileSystemType.getFileSystem(); |
| 83 | + Path execRoot = fs.getPath(tempFolder.getRoot().getPath()); |
| 84 | + |
| 85 | + Path outputFile = execRoot.getRelative("some/file"); |
| 86 | + VirtualActionInput input = |
| 87 | + ActionsTestUtil.createVirtualActionInput( |
| 88 | + outputFile.relativeTo(execRoot).getPathString(), "hello"); |
| 89 | + |
| 90 | + input.atomicallyWriteRelativeTo(execRoot); |
| 91 | + byte[] digest; |
| 92 | + byte[] bytes; |
| 93 | + try (var in = outputFile.getInputStream()) { |
| 94 | + digest = input.atomicallyWriteRelativeTo(execRoot); |
| 95 | + bytes = in.readAllBytes(); |
| 96 | + } |
| 97 | + |
| 98 | + assertThat(outputFile.getParentDirectory().readdir(Symlinks.NOFOLLOW)) |
| 99 | + .containsExactly(new Dirent("file", Dirent.Type.FILE)); |
| 100 | + assertThat(FileSystemUtils.readLines(outputFile, UTF_8)).containsExactly("hello"); |
| 101 | + assertThat(outputFile.isExecutable()).isTrue(); |
| 102 | + assertThat(digest).isEqualTo(SHA256.getHashFunction().hashString("hello", UTF_8).asBytes()); |
| 103 | + assertThat(bytes).isEqualTo("hello".getBytes(UTF_8)); |
| 104 | + } |
| 105 | +} |
0 commit comments