| // Copyright 2026 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.sandbox; | |
| import static com.google.common.truth.Truth.assertThat; | |
| import com.google.devtools.build.lib.testutil.Scratch; | |
| import com.google.devtools.build.lib.vfs.DigestHashFunction; | |
| import com.google.devtools.build.lib.vfs.FileSystem; | |
| import com.google.devtools.build.lib.vfs.Path; | |
| import com.google.devtools.build.lib.vfs.PathFragment; | |
| import com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem; | |
| import java.io.IOException; | |
| import org.junit.Before; | |
| import org.junit.Test; | |
| import org.junit.runner.RunWith; | |
| import org.junit.runners.JUnit4; | |
| /** Tests for {@link SandboxModule}. */ | |
| @RunWith(JUnit4.class) | |
| public final class SandboxModuleTest { | |
| private Scratch scratch; | |
| private Path sandboxBase; | |
| @Before | |
| public void setUp() throws Exception { | |
| scratch = new Scratch(); | |
| sandboxBase = scratch.dir("/sandbox_base"); | |
| } | |
| @Test | |
| public void checkSandboxBaseTopOnlyContainsPersistentDirs_persistentDirsSuccess() | |
| throws Exception { | |
| scratch.dir("/sandbox_base/_moved_trash_dir"); | |
| scratch.dir("/sandbox_base/sandbox_stash"); | |
| scratch.file("/sandbox_base/.DS_Store"); | |
| SandboxModule.checkSandboxBaseTopOnlyContainsPersistentDirs(sandboxBase); | |
| } | |
| @Test | |
| public void | |
| checkSandboxBaseTopOnlyContainsPersistentDirs_withUnexpectedDirs_logsWarningWithoutCrashing() | |
| throws Exception { | |
| scratch.dir("/sandbox_base/_moved_trash_dir"); | |
| scratch.dir("/sandbox_base/sandbox_stash"); | |
| scratch.dir("/sandbox_base/linux-sandbox"); | |
| // Must not throw IllegalStateException (issue #27892) | |
| SandboxModule.checkSandboxBaseTopOnlyContainsPersistentDirs(sandboxBase); | |
| } | |
| @Test | |
| public void checkSandboxBaseTopOnlyContainsPersistentDirs_withInaccessibleHelpers_cleansUp() | |
| throws Exception { | |
| Path helperDir = sandboxBase.getChild(SandboxHelpers.INACCESSIBLE_HELPER_DIR); | |
| helperDir.createDirectoryAndParents(); | |
| helperDir.getChild("nested_file").createDirectoryAndParents(); | |
| Path helperFile = sandboxBase.getChild(SandboxHelpers.INACCESSIBLE_HELPER_FILE); | |
| scratch.file(helperFile.getPathString(), "dummy content"); | |
| SandboxModule.checkSandboxBaseTopOnlyContainsPersistentDirs(sandboxBase); | |
| assertThat(helperDir.exists()).isFalse(); | |
| assertThat(helperFile.exists()).isFalse(); | |
| } | |
| @Test | |
| public void cleanSandboxBaseTopOnlyContainsPersistentDirs_cleansUnexpectedDirsAndFiles() | |
| throws Exception { | |
| scratch.dir("/sandbox_base/_moved_trash_dir"); | |
| scratch.dir("/sandbox_base/sandbox_stash"); | |
| scratch.dir("/sandbox_base/linux-sandbox"); | |
| scratch.file("/sandbox_base/stale_file.txt", "stale content"); | |
| SandboxModule.cleanSandboxBaseTopOnlyContainsPersistentDirs( | |
| sandboxBase, new SynchronousTreeDeleter()); | |
| assertThat(sandboxBase.getChild("_moved_trash_dir").exists()).isTrue(); | |
| assertThat(sandboxBase.getChild("sandbox_stash").exists()).isTrue(); | |
| assertThat(sandboxBase.getChild("linux-sandbox").exists()).isFalse(); | |
| assertThat(sandboxBase.getChild("stale_file.txt").exists()).isFalse(); | |
| SandboxModule.checkSandboxBaseTopOnlyContainsPersistentDirs(sandboxBase); | |
| } | |
| @Test | |
| public void cleanSandboxBaseOnFirstBuild_renameFails_fallsBackToSynchronousDeletion() | |
| throws Exception { | |
| FileSystem failingRenameFs = | |
| new InMemoryFileSystem(DigestHashFunction.SHA256) { | |
| @Override | |
| public void renameTo(PathFragment sourcePath, PathFragment targetPath) | |
| throws IOException { | |
| throw new IOException("Simulated overlayfs EXDEV on rename"); | |
| } | |
| }; | |
| Scratch failingScratch = new Scratch(failingRenameFs); | |
| Path failingSandboxBase = failingScratch.dir("/sandbox_base"); | |
| Path trashBase = failingScratch.dir("/sandbox_base/_moved_trash_dir"); | |
| failingScratch.file("/sandbox_base/_moved_trash_dir/stale_trash.txt", "stale"); | |
| failingScratch.file("/sandbox_base/stale_sandbox.txt", "stale"); | |
| SandboxModule sandboxModule = new SandboxModule(); | |
| sandboxModule.treeDeleter = new AsynchronousTreeDeleter(trashBase); | |
| sandboxModule.cleanSandboxBaseOnFirstBuild(failingSandboxBase, trashBase, /* idleThreads= */ 4); | |
| assertThat(failingSandboxBase.exists()).isFalse(); | |
| assertThat(sandboxModule.treeDeleter).isInstanceOf(AsynchronousTreeDeleter.class); | |
| sandboxModule.blazeShutdown(); | |
| } | |
| } |