1515
1616import static com .google .common .truth .Truth .assertThat ;
1717import static com .google .common .truth .Truth8 .assertThat ;
18- import static org .junit .Assert .assertThrows ;
1918import static org .mockito .ArgumentMatchers .eq ;
2019import static org .mockito .Mockito .doAnswer ;
2120import static org .mockito .Mockito .mock ;
4645import com .google .devtools .build .lib .vfs .Symlinks ;
4746import com .google .devtools .build .lib .vfs .SyscallCache ;
4847import com .google .devtools .build .lib .vfs .inmemoryfs .InMemoryFileSystem ;
49- import java .io .FileNotFoundException ;
5048import java .io .IOException ;
5149import java .nio .charset .StandardCharsets ;
5250import java .util .Map ;
5856
5957/** Tests for {@link RemoteActionFileSystem} */
6058@ RunWith (JUnit4 .class )
61- public final class RemoteActionFileSystemTest {
59+ public final class RemoteActionFileSystemTest extends RemoteActionFileSystemTestBase {
6260
6361 private static final DigestHashFunction HASH_FUNCTION = DigestHashFunction .SHA256 ;
6462
@@ -70,17 +68,48 @@ public final class RemoteActionFileSystemTest {
7068 ArtifactRoot .asDerivedRoot (execRoot , RootType .Output , "out" );
7169
7270 @ Before
73- public void createOutputRoot () throws IOException {
71+ public void setUp () throws IOException {
7472 outputRoot .getRoot ().asPath ().createDirectoryAndParents ();
7573 }
7674
75+ @ Override
76+ protected RemoteActionFileSystem createActionFileSystem (
77+ ActionInputMap inputs , Iterable <Artifact > outputs ) throws IOException {
78+ RemoteActionFileSystem remoteActionFileSystem =
79+ new RemoteActionFileSystem (
80+ fs ,
81+ execRoot .asFragment (),
82+ outputRoot .getRoot ().asPath ().relativeTo (execRoot ).getPathString (),
83+ inputs ,
84+ outputs ,
85+ inputFetcher );
86+ remoteActionFileSystem .updateContext (metadataInjector );
87+ remoteActionFileSystem .createDirectoryAndParents (outputRoot .getRoot ().asPath ().asFragment ());
88+ return remoteActionFileSystem ;
89+ }
90+
91+ @ Override
92+ protected FileSystem getLocalFileSystem (FileSystem actionFs ) {
93+ return ((RemoteActionFileSystem ) actionFs ).getLocalFileSystem ();
94+ }
95+
96+ @ Override
97+ protected FileSystem getRemoteFileSystem (FileSystem actionFs ) {
98+ return ((RemoteActionFileSystem ) actionFs ).getRemoteOutputTree ();
99+ }
100+
101+ @ Override
102+ protected PathFragment getOutputPath (String outputRootRelativePath ) {
103+ return outputRoot .getRoot ().asPath ().getRelative (outputRootRelativePath ).asFragment ();
104+ }
105+
77106 @ Test
78107 public void testGetInputStream () throws Exception {
79108 // arrange
80109 ActionInputMap inputs = new ActionInputMap (2 );
81110 Artifact remoteArtifact = createRemoteArtifact ("remote-file" , "remote contents" , inputs );
82111 Artifact localArtifact = createLocalArtifact ("local-file" , "local contents" , inputs );
83- FileSystem actionFs = newRemoteActionFileSystem (inputs );
112+ FileSystem actionFs = createActionFileSystem (inputs );
84113 doAnswer (
85114 invocationOnMock -> {
86115 FileSystemUtils .writeContent (
@@ -114,7 +143,7 @@ public void createSymbolicLink_localFileArtifact() throws IOException {
114143 Artifact localArtifact = createLocalArtifact ("local-file" , "local contents" , inputs );
115144 Artifact outputArtifact = ActionsTestUtil .createArtifact (outputRoot , "out" );
116145 ImmutableList <Artifact > outputs = ImmutableList .of (outputArtifact );
117- RemoteActionFileSystem actionFs = newRemoteActionFileSystem (inputs , outputs );
146+ RemoteActionFileSystem actionFs = createActionFileSystem (inputs , outputs );
118147
119148 // act
120149 PathFragment linkPath = outputArtifact .getPath ().asFragment ();
@@ -144,7 +173,7 @@ public void createSymbolicLink_remoteFileArtifact() throws IOException {
144173 Artifact remoteArtifact = createRemoteArtifact ("remote-file" , "remote contents" , inputs );
145174 Artifact outputArtifact = ActionsTestUtil .createArtifact (outputRoot , "out" );
146175 ImmutableList <Artifact > outputs = ImmutableList .of (outputArtifact );
147- RemoteActionFileSystem actionFs = newRemoteActionFileSystem (inputs , outputs );
176+ RemoteActionFileSystem actionFs = createActionFileSystem (inputs , outputs );
148177
149178 // act
150179 PathFragment linkPath = outputArtifact .getPath ().asFragment ();
@@ -184,7 +213,7 @@ public void createSymbolicLink_localTreeArtifact() throws IOException {
184213 SpecialArtifact outputArtifact =
185214 ActionsTestUtil .createTreeArtifactWithGeneratingAction (outputRoot , "out" );
186215 ImmutableList <Artifact > outputs = ImmutableList .of (outputArtifact );
187- RemoteActionFileSystem actionFs = newRemoteActionFileSystem (inputs , outputs );
216+ RemoteActionFileSystem actionFs = createActionFileSystem (inputs , outputs );
188217
189218 // act
190219 PathFragment linkPath = outputArtifact .getPath ().asFragment ();
@@ -217,7 +246,7 @@ public void createSymbolicLink_remoteTreeArtifact() throws IOException {
217246 SpecialArtifact outputArtifact =
218247 ActionsTestUtil .createTreeArtifactWithGeneratingAction (outputRoot , "out" );
219248 ImmutableList <Artifact > outputs = ImmutableList .of (outputArtifact );
220- RemoteActionFileSystem actionFs = newRemoteActionFileSystem (inputs , outputs );
249+ RemoteActionFileSystem actionFs = createActionFileSystem (inputs , outputs );
221250
222251 // act
223252 PathFragment linkPath = outputArtifact .getPath ().asFragment ();
@@ -252,7 +281,7 @@ public void createSymbolicLink_unresolvedSymlink() throws IOException {
252281 SpecialArtifact outputArtifact =
253282 ActionsTestUtil .createUnresolvedSymlinkArtifact (outputRoot , "out" );
254283 ImmutableList <Artifact > outputs = ImmutableList .of (outputArtifact );
255- RemoteActionFileSystem actionFs = newRemoteActionFileSystem (inputs , outputs );
284+ RemoteActionFileSystem actionFs = createActionFileSystem (inputs , outputs );
256285 PathFragment targetPath = PathFragment .create ("some/path" );
257286
258287 // act
@@ -275,222 +304,36 @@ public void createSymbolicLink_unresolvedSymlink() throws IOException {
275304 verifyNoInteractions (metadataInjector );
276305 }
277306
278- @ Test
279- public void exists_fileDoesNotExist_returnsFalse () throws Exception {
280- ActionInputMap inputs = new ActionInputMap (0 );
281- RemoteActionFileSystem actionFs = newRemoteActionFileSystem (inputs );
282- PathFragment path = outputRoot .getRoot ().asPath ().getRelative ("file" ).asFragment ();
283-
284- assertThat (actionFs .exists (path )).isFalse ();
285- }
286-
287- @ Test
288- public void exists_localFile_returnsTrue () throws Exception {
289- ActionInputMap inputs = new ActionInputMap (0 );
290- RemoteActionFileSystem actionFs = newRemoteActionFileSystem (inputs );
291- PathFragment path = outputRoot .getRoot ().asPath ().getRelative ("file" ).asFragment ();
292-
293- writeLocalFile (actionFs , path , "local contents" );
294-
295- assertThat (actionFs .exists (path )).isTrue ();
296- }
297-
298- @ Test
299- public void exists_remoteFile_returnsTrue () throws Exception {
300- ActionInputMap inputs = new ActionInputMap (0 );
301- RemoteActionFileSystem actionFs = newRemoteActionFileSystem (inputs );
302- PathFragment path = outputRoot .getRoot ().asPath ().getRelative ("file" ).asFragment ();
303-
304- injectRemoteFile (actionFs , path , "remote contents" );
305-
306- assertThat (actionFs .exists (path )).isTrue ();
307- }
308-
309- @ Test
310- public void exists_localAndRemoteFile_returnsTrue () throws Exception {
311- ActionInputMap inputs = new ActionInputMap (0 );
312- RemoteActionFileSystem actionFs = newRemoteActionFileSystem (inputs );
313- PathFragment path = outputRoot .getRoot ().asPath ().getRelative ("file" ).asFragment ();
314-
315- writeLocalFile (actionFs , path , "local contents" );
316- injectRemoteFile (actionFs , path , "remote contents" );
317-
318- assertThat (actionFs .exists (path )).isTrue ();
319- }
320-
321- @ Test
322- public void delete_fileDoesNotExist_returnsFalse () throws Exception {
323- ActionInputMap inputs = new ActionInputMap (0 );
324- RemoteActionFileSystem actionFs = newRemoteActionFileSystem (inputs );
325- PathFragment path = outputRoot .getRoot ().asPath ().getRelative ("file" ).asFragment ();
326-
327- assertThat (actionFs .delete (path )).isFalse ();
328- }
329-
330- @ Test
331- public void delete_localFile_succeeds () throws Exception {
332- ActionInputMap inputs = new ActionInputMap (0 );
333- RemoteActionFileSystem actionFs = newRemoteActionFileSystem (inputs );
334- PathFragment path = outputRoot .getRoot ().asPath ().getRelative ("file" ).asFragment ();
335- writeLocalFile (actionFs , path , "local contents" );
336- assertThat (actionFs .getLocalFileSystem ().exists (path )).isTrue ();
337-
338- boolean success = actionFs .delete (path );
339-
340- assertThat (success ).isTrue ();
341- assertThat (actionFs .getLocalFileSystem ().exists (path )).isFalse ();
342- }
343-
344- @ Test
345- public void delete_remoteFile_succeeds () throws Exception {
346- ActionInputMap inputs = new ActionInputMap (0 );
347- RemoteActionFileSystem actionFs = newRemoteActionFileSystem (inputs );
348- PathFragment path = outputRoot .getRoot ().asPath ().getRelative ("file" ).asFragment ();
349- injectRemoteFile (actionFs , path , "remote contents" );
350- assertThat (actionFs .getRemoteOutputTree ().exists (path )).isTrue ();
351-
352- boolean success = actionFs .delete (path );
353-
354- assertThat (success ).isTrue ();
355- assertThat (actionFs .exists (path )).isFalse ();
356- assertThat (actionFs .getRemoteOutputTree ().exists (path )).isFalse ();
357- }
358-
359- @ Test
360- public void delete_localAndRemoteFile_succeeds () throws Exception {
361- ActionInputMap inputs = new ActionInputMap (0 );
362- RemoteActionFileSystem actionFs = newRemoteActionFileSystem (inputs );
363- PathFragment path = outputRoot .getRoot ().asPath ().getRelative ("file" ).asFragment ();
364- writeLocalFile (actionFs , path , "local contents" );
365- injectRemoteFile (actionFs , path , "remote contents" );
366- assertThat (actionFs .getLocalFileSystem ().exists (path )).isTrue ();
367- assertThat (actionFs .getRemoteOutputTree ().exists (path )).isTrue ();
368-
369- boolean success = actionFs .delete (path );
370-
371- assertThat (success ).isTrue ();
372- assertThat (actionFs .exists (path )).isFalse ();
373- assertThat (actionFs .getLocalFileSystem ().exists (path )).isFalse ();
374- assertThat (actionFs .getRemoteOutputTree ().exists (path )).isFalse ();
375- }
376-
377- @ Test
378- public void renameTo_fileDoesNotExist_throwError () throws Exception {
379- RemoteActionFileSystem actionFs = newRemoteActionFileSystem ();
380- PathFragment path = outputRoot .getRoot ().asPath ().getRelative ("file" ).asFragment ();
381- PathFragment newPath = outputRoot .getRoot ().asPath ().getRelative ("file-new" ).asFragment ();
382-
383- assertThrows (FileNotFoundException .class , () -> actionFs .renameTo (path , newPath ));
384- }
385-
386- @ Test
387- public void renameTo_onlyRemoteFile_renameRemoteFile () throws Exception {
388- RemoteActionFileSystem actionFs = newRemoteActionFileSystem ();
389- PathFragment path = outputRoot .getRoot ().asPath ().getRelative ("file" ).asFragment ();
390- injectRemoteFile (actionFs , path , "remote-content" );
391- PathFragment newPath = outputRoot .getRoot ().asPath ().getRelative ("file-new" ).asFragment ();
392-
393- actionFs .renameTo (path , newPath );
394-
395- assertThat (actionFs .exists (path )).isFalse ();
396- assertThat (actionFs .exists (newPath )).isTrue ();
397- assertThat (actionFs .getRemoteOutputTree ().exists (path )).isFalse ();
398- assertThat (actionFs .getRemoteOutputTree ().exists (newPath )).isTrue ();
399- }
400-
401307 @ Test
402308 public void renameTo_onlyLocalFile_renameLocalFile () throws Exception {
403- RemoteActionFileSystem actionFs = newRemoteActionFileSystem ();
404- PathFragment path = outputRoot . getRoot (). asPath (). getRelative ( "file" ). asFragment ( );
309+ FileSystem actionFs = createActionFileSystem ();
310+ PathFragment path = getOutputPath ( "file" );
405311 writeLocalFile (actionFs , path , "local-content" );
406- PathFragment newPath = outputRoot . getRoot (). asPath (). getRelative ( "file-new" ). asFragment ( );
312+ PathFragment newPath = getOutputPath ( "file-new" );
407313
408314 actionFs .renameTo (path , newPath );
409315
410316 assertThat (actionFs .exists (path )).isFalse ();
411317 assertThat (actionFs .exists (newPath )).isTrue ();
412- assertThat (actionFs .getLocalFileSystem ().exists (path )).isFalse ();
413- assertThat (actionFs .getLocalFileSystem ().exists (newPath )).isTrue ();
414- }
415-
416- @ Test
417- public void renameTo_localAndRemoteFile_renameBoth () throws Exception {
418- RemoteActionFileSystem actionFs = newRemoteActionFileSystem ();
419- PathFragment path = outputRoot .getRoot ().asPath ().getRelative ("file" ).asFragment ();
420- injectRemoteFile (actionFs , path , "remote-content" );
421- writeLocalFile (actionFs , path , "local-content" );
422- PathFragment newPath = outputRoot .getRoot ().asPath ().getRelative ("file-new" ).asFragment ();
423-
424- actionFs .renameTo (path , newPath );
425-
426- assertThat (actionFs .exists (path )).isFalse ();
427- assertThat (actionFs .exists (newPath )).isTrue ();
428- assertThat (actionFs .getRemoteOutputTree ().exists (path )).isFalse ();
429- assertThat (actionFs .getRemoteOutputTree ().exists (newPath )).isTrue ();
430- assertThat (actionFs .getLocalFileSystem ().exists (path )).isFalse ();
431- assertThat (actionFs .getLocalFileSystem ().exists (newPath )).isTrue ();
432- }
433-
434- @ Test
435- public void createDirectoryAndParents_createLocallyAndRemotely () throws Exception {
436- RemoteActionFileSystem actionFs = newRemoteActionFileSystem ();
437- PathFragment path = outputRoot .getRoot ().asPath ().getRelative ("dir" ).asFragment ();
438-
439- actionFs .createDirectoryAndParents (path );
440-
441- assertThat (actionFs .getRemoteOutputTree ().getPath (path ).isDirectory ()).isTrue ();
442- assertThat (actionFs .getLocalFileSystem ().getPath (path ).isDirectory ()).isTrue ();
443- }
444-
445- @ Test
446- public void createDirectory_createLocallyAndRemotely () throws Exception {
447- RemoteActionFileSystem actionFs = newRemoteActionFileSystem ();
448- actionFs .createDirectoryAndParents (outputRoot .getRoot ().asPath ().asFragment ());
449- PathFragment path = outputRoot .getRoot ().asPath ().getRelative ("dir" ).asFragment ();
450-
451- actionFs .createDirectory (path );
452-
453- assertThat (actionFs .getRemoteOutputTree ().getPath (path ).isDirectory ()).isTrue ();
454- assertThat (actionFs .getLocalFileSystem ().getPath (path ).isDirectory ()).isTrue ();
318+ assertThat (getLocalFileSystem (actionFs ).exists (path )).isFalse ();
319+ assertThat (getLocalFileSystem (actionFs ).exists (newPath )).isTrue ();
455320 }
456321
457- private void injectRemoteFile (RemoteActionFileSystem actionFs , PathFragment path , String content )
322+ @ Override
323+ protected void injectRemoteFile (FileSystem actionFs , PathFragment path , String content )
458324 throws IOException {
459325 byte [] contentBytes = content .getBytes (StandardCharsets .UTF_8 );
460326 HashCode hashCode = HASH_FUNCTION .getHashFunction ().hashBytes (contentBytes );
461- actionFs .injectRemoteFile (path , hashCode .asBytes (), contentBytes .length , "action-id" );
327+ ((RemoteActionFileSystem ) actionFs )
328+ .injectRemoteFile (path , hashCode .asBytes (), contentBytes .length , "action-id" );
462329 }
463330
464- private void writeLocalFile (RemoteActionFileSystem actionFs , PathFragment path , String content )
331+ @ Override
332+ protected void writeLocalFile (FileSystem actionFs , PathFragment path , String content )
465333 throws IOException {
466334 FileSystemUtils .writeContent (actionFs .getPath (path ), StandardCharsets .UTF_8 , content );
467335 }
468336
469- private RemoteActionFileSystem newRemoteActionFileSystem () throws IOException {
470- ActionInputMap inputs = new ActionInputMap (0 );
471- return newRemoteActionFileSystem (inputs , ImmutableList .of ());
472- }
473-
474- private RemoteActionFileSystem newRemoteActionFileSystem (ActionInputMap inputs )
475- throws IOException {
476- return newRemoteActionFileSystem (inputs , ImmutableList .of ());
477- }
478-
479- private RemoteActionFileSystem newRemoteActionFileSystem (
480- ActionInputMap inputs , Iterable <Artifact > outputs ) throws IOException {
481- RemoteActionFileSystem remoteActionFileSystem =
482- new RemoteActionFileSystem (
483- fs ,
484- execRoot .asFragment (),
485- outputRoot .getRoot ().asPath ().relativeTo (execRoot ).getPathString (),
486- inputs ,
487- outputs ,
488- inputFetcher );
489- remoteActionFileSystem .updateContext (metadataInjector );
490- remoteActionFileSystem .createDirectoryAndParents (outputRoot .getRoot ().asPath ().asFragment ());
491- return remoteActionFileSystem ;
492- }
493-
494337 /** Returns a remote artifact and puts its metadata into the action input map. */
495338 private Artifact createRemoteArtifact (
496339 String pathFragment , String contents , ActionInputMap inputs ) {
0 commit comments