Skip to content

Commit

Permalink
Fix SandboxHelpersTest
Browse files Browse the repository at this point in the history
  • Loading branch information
fmeum committed May 8, 2024
1 parent 21fcad5 commit 3a641ff
Showing 1 changed file with 32 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,14 @@
public class SandboxHelpersTest {

private final Scratch scratch = new Scratch();
private Path execRoot;
private Path execRootPath;
private Root execRoot;
@Nullable private ExecutorService executorToCleanup;

@Before
public void createExecRoot() throws IOException {
execRoot = scratch.dir("/execRoot");
execRootPath = scratch.dir("/execRoot");
execRoot = Root.fromPath(execRootPath);
}

@After
Expand All @@ -88,6 +90,10 @@ public void shutdownExecutor() throws InterruptedException {
executorToCleanup.awaitTermination(TestUtils.WAIT_TIMEOUT_SECONDS, SECONDS);
}

private RootedPath execRootedPath(String execPath) {
return RootedPath.toRootedPath(execRoot, PathFragment.create(execPath));
}

@Test
public void processInputFiles_materializesParamFile() throws Exception {
SandboxHelpers sandboxHelpers = new SandboxHelpers();
Expand All @@ -98,15 +104,15 @@ public void processInputFiles_materializesParamFile() throws Exception {
ParameterFileType.UNQUOTED,
UTF_8);

SandboxInputs inputs = sandboxHelpers.processInputFiles(inputMap(paramFile), execRoot);
SandboxInputs inputs = sandboxHelpers.processInputFiles(inputMap(paramFile), execRootPath);

assertThat(inputs.getFiles())
.containsExactly(PathFragment.create("paramFile"), execRoot.getChild("paramFile"));
.containsExactly(PathFragment.create("paramFile"), execRootedPath("paramFile"));
assertThat(inputs.getSymlinks()).isEmpty();
assertThat(FileSystemUtils.readLines(execRoot.getChild("paramFile"), UTF_8))
assertThat(FileSystemUtils.readLines(execRootPath.getChild("paramFile"), UTF_8))
.containsExactly("-a", "-b")
.inOrder();
assertThat(execRoot.getChild("paramFile").isExecutable()).isTrue();
assertThat(execRootPath.getChild("paramFile").isExecutable()).isTrue();
}

@Test
Expand All @@ -117,16 +123,15 @@ public void processInputFiles_materializesBinToolsFile() throws Exception {
scratch.file("tool", "#!/bin/bash", "echo hello"),
PathFragment.create("_bin/say_hello"));

SandboxInputs inputs = sandboxHelpers.processInputFiles(inputMap(tool), execRoot);
SandboxInputs inputs = sandboxHelpers.processInputFiles(inputMap(tool), execRootPath);

assertThat(inputs.getFiles())
.containsExactly(
PathFragment.create("_bin/say_hello"), execRoot.getRelative("_bin/say_hello"));
.containsExactly(PathFragment.create("_bin/say_hello"), execRootedPath("_bin/say_hello"));
assertThat(inputs.getSymlinks()).isEmpty();
assertThat(FileSystemUtils.readLines(execRoot.getRelative("_bin/say_hello"), UTF_8))
assertThat(FileSystemUtils.readLines(execRootPath.getRelative("_bin/say_hello"), UTF_8))
.containsExactly("#!/bin/bash", "echo hello")
.inOrder();
assertThat(execRoot.getRelative("_bin/say_hello").isExecutable()).isTrue();
assertThat(execRootPath.getRelative("_bin/say_hello").isExecutable()).isTrue();
}

/**
Expand Down Expand Up @@ -235,7 +240,7 @@ public void cleanExisting_updatesDirs() throws IOException, InterruptedException
RootedPath inputTxt =
RootedPath.toRootedPath(
Root.fromPath(scratch.getFileSystem().getPath("/")), PathFragment.create("hello.txt"));
Path rootDir = execRoot.getParentDirectory();
Path rootDir = execRootPath.getParentDirectory();
PathFragment input1 = PathFragment.create("existing/directory/with/input1.txt");
PathFragment input2 = PathFragment.create("partial/directory/input2.txt");
PathFragment input3 = PathFragment.create("new/directory/input3.txt");
Expand Down Expand Up @@ -263,17 +268,17 @@ public void cleanExisting_updatesDirs() throws IOException, InterruptedException
assertThat(inputsToCreate).containsExactly(input1, input2, input3);

// inputdir1 exists fully
execRoot.getRelative(inputDir1).createDirectoryAndParents();
execRootPath.getRelative(inputDir1).createDirectoryAndParents();
// inputdir2 exists partially, should be kept nonetheless.
execRoot
execRootPath
.getRelative(inputDir2)
.getParentDirectory()
.getRelative("doomedSubdir")
.createDirectoryAndParents();
// inputDir3 just doesn't exist
// outputDir only exists partially
execRoot.getRelative(outputDir).getParentDirectory().createDirectoryAndParents();
execRoot.getRelative("justSomeDir/thatIsDoomed").createDirectoryAndParents();
execRootPath.getRelative(outputDir).getParentDirectory().createDirectoryAndParents();
execRootPath.getRelative("justSomeDir/thatIsDoomed").createDirectoryAndParents();
// `thiswillbeafile/output` simulates a directory that was in the stashed dir but whose same
// path is used later for a regular file.
scratch.dir("/execRoot/thiswillbeafile/output");
Expand All @@ -285,21 +290,21 @@ public void cleanExisting_updatesDirs() throws IOException, InterruptedException
ImmutableMap.of(input1, inputTxt, input2, inputTxt, input3, inputTxt, input4, inputTxt),
ImmutableMap.of(),
ImmutableMap.of(), ImmutableSet.of());
SandboxHelpers.cleanExisting(rootDir, inputs2, inputsToCreate, dirsToCreate, execRoot);
SandboxHelpers.cleanExisting(rootDir, inputs2, inputsToCreate, dirsToCreate, execRootPath);
assertThat(dirsToCreate).containsExactly(inputDir2, inputDir3, outputDir);
assertThat(execRoot.getRelative("existing/directory/with").exists()).isTrue();
assertThat(execRoot.getRelative("partial").exists()).isTrue();
assertThat(execRoot.getRelative("partial/doomedSubdir").exists()).isFalse();
assertThat(execRoot.getRelative("partial/directory").exists()).isFalse();
assertThat(execRoot.getRelative("justSomeDir/thatIsDoomed").exists()).isFalse();
assertThat(execRoot.getRelative("out").exists()).isTrue();
assertThat(execRoot.getRelative("out/dir").exists()).isFalse();
assertThat(execRootPath.getRelative("existing/directory/with").exists()).isTrue();
assertThat(execRootPath.getRelative("partial").exists()).isTrue();
assertThat(execRootPath.getRelative("partial/doomedSubdir").exists()).isFalse();
assertThat(execRootPath.getRelative("partial/directory").exists()).isFalse();
assertThat(execRootPath.getRelative("justSomeDir/thatIsDoomed").exists()).isFalse();
assertThat(execRootPath.getRelative("out").exists()).isTrue();
assertThat(execRootPath.getRelative("out/dir").exists()).isFalse();
}

@Test
public void populateInputsAndDirsToCreate_createsMappedDirectories() {
ArtifactRoot outputRoot =
ArtifactRoot.asDerivedRoot(execRoot, ArtifactRoot.RootType.Output, "outputs");
ArtifactRoot.asDerivedRoot(execRootPath, ArtifactRoot.RootType.Output, "outputs");
ActionInput outputFile = ActionsTestUtil.createArtifact(outputRoot, "bin/config/dir/file");
ActionInput outputDir =
ActionsTestUtil.createTreeArtifactWithGeneratingAction(
Expand Down Expand Up @@ -339,13 +344,13 @@ public void moveOutputs_mappedPathMovedToUnmappedPath() throws Exception {
.setPathMapper(pathMapper)
.build();
var sandboxHelpers = new SandboxHelpers();
Path sandboxBase = execRoot.getRelative("sandbox");
Path sandboxBase = execRootPath.getRelative("sandbox");
PathFragment mappedOutputPath = PathFragment.create("bin/output");
sandboxBase.getRelative(mappedOutputPath).getParentDirectory().createDirectoryAndParents();
FileSystemUtils.writeLinesAs(
sandboxBase.getRelative(mappedOutputPath), UTF_8, "hello", "pathmapper");

Path realBase = execRoot.getRelative("real");
Path realBase = execRootPath.getRelative("real");
SandboxHelpers.moveOutputs(sandboxHelpers.getOutputs(spawn), sandboxBase, realBase);

assertThat(
Expand Down

0 comments on commit 3a641ff

Please sign in to comment.