11package org .testcontainers .utility ;
22
3+ import org .apache .commons .lang3 .StringUtils ;
34import org .assertj .core .api .Condition ;
45import org .junit .jupiter .api .Test ;
56import org .testcontainers .containers .GenericContainer ;
67import org .testcontainers .containers .startupcheck .OneShotStartupCheckStrategy ;
78import org .testcontainers .images .builder .ImageFromDockerfile ;
89
910import java .io .File ;
11+ import java .util .ArrayList ;
12+ import java .util .Arrays ;
13+ import java .util .List ;
14+ import java .util .function .Consumer ;
1015import java .util .function .Predicate ;
1116
1217import static org .assertj .core .api .Assertions .assertThat ;
@@ -18,26 +23,28 @@ void simpleRecursiveFileTest() {
1823 // 'src' is expected to be the project base directory, so all source code/resources should be copied in
1924 File directory = new File ("src" );
2025
21- GenericContainer container = new GenericContainer (
22- new ImageFromDockerfile ()
23- .withDockerfileFromBuilder (builder -> {
24- builder
25- .from ("alpine:3.17" )
26- .copy ("/tmp/foo" , "/foo" )
27- .cmd ("cat /foo/test/resources/test-recursive-file.txt" )
28- .build ();
29- })
30- .withFileFromFile ("/tmp/foo" , directory )
31- )
32- .withStartupCheckStrategy (new OneShotStartupCheckStrategy ());
33-
34- container .start ();
35-
36- final String results = container .getLogs ();
37-
38- assertThat (results )
39- .as ("The container has a file that was copied in via a recursive copy" )
40- .contains ("Used for DirectoryTarResourceTest" );
26+ try (
27+ GenericContainer container = new GenericContainer (
28+ new ImageFromDockerfile ()
29+ .withDockerfileFromBuilder (builder -> {
30+ builder
31+ .from ("alpine:3.17" )
32+ .copy ("/tmp/foo" , "/foo" )
33+ .cmd ("cat /foo/test/resources/test-recursive-file.txt" )
34+ .build ();
35+ })
36+ .withFileFromFile ("/tmp/foo" , directory )
37+ )
38+ .withStartupCheckStrategy (new OneShotStartupCheckStrategy ())
39+ ) {
40+ container .start ();
41+
42+ final String results = container .getLogs ();
43+
44+ assertThat (results )
45+ .as ("The container has a file that was copied in via a recursive copy" )
46+ .contains ("Used for DirectoryTarResourceTest" );
47+ }
4148 }
4249
4350 @ Test
@@ -94,4 +101,87 @@ void simpleRecursiveClasspathResourceTest() {
94101 .contains ("content.txt" );
95102 }
96103 }
104+
105+ @ Test
106+ public void transferFileDockerDaemon () {
107+ final File theFile = new File ("src/test/resources/mappable-resource/test-resource.txt" );
108+ try (
109+ GenericContainer container = new GenericContainer (
110+ new ImageFromDockerfile ()
111+ .withDockerfileFromBuilder (builder -> {
112+ builder .from ("alpine:3.3" ).copy ("." , "/foo/" ).cmd ("ls" , "-lapR" , "/foo" ).build ();
113+ })
114+ .withFileFromFile ("bar1" , theFile )
115+ .withFileFromFile ("./bar2" , theFile )
116+ .withFileFromFile ("../bar3" , theFile )
117+ .withFileFromFile (".bar4" , theFile )
118+ .withFileFromFile ("..bar5" , theFile )
119+ .withFileFromFile ("xxx/../bar6" , theFile )
120+ .withFileFromFile ("x7/./bar7" , theFile )
121+ .withFileFromFile ("x8/././bar8" , theFile )
122+ .withFileFromFile ("x9/../../bar9" , theFile )
123+ )
124+ .withStartupCheckStrategy (new OneShotStartupCheckStrategy ())
125+ ) {
126+ container .start ();
127+
128+ final List <String > logLines = Arrays .asList (container .getLogs ().split ("\\ n" ));
129+ assertThat (logLines .stream ().filter (StringUtils ::isEmpty ).count ())
130+ .describedAs ("Three groups of dirs" )
131+ .isEqualTo (2 );
132+
133+ final LsOutput lsOutput = LsOutput .parse (logLines );
134+
135+ assertThat (lsOutput .parentDir ).satisfiesOnlyOnce (endsWith (" bar1" ));
136+ assertThat (lsOutput .parentDir ).satisfiesOnlyOnce (endsWith (" bar2" ));
137+ assertThat (lsOutput .parentDir ).satisfiesOnlyOnce (endsWith (" bar3" ));
138+ assertThat (lsOutput .parentDir ).satisfiesOnlyOnce (endsWith (" .bar4" ));
139+ assertThat (lsOutput .parentDir ).satisfiesOnlyOnce (endsWith (" ..bar5" ));
140+ assertThat (lsOutput .parentDir ).satisfiesOnlyOnce (endsWith (" bar6" ));
141+ assertThat (lsOutput .parentDir ).satisfiesOnlyOnce (endsWith (" x7/" ));
142+ assertThat (lsOutput .parentDir ).satisfiesOnlyOnce (endsWith (" x8/" ));
143+ assertThat (lsOutput .parentDir ).satisfiesOnlyOnce (endsWith (" bar9" ));
144+ assertThat (lsOutput .subDir1 ).satisfiesOnlyOnce (endsWith (" bar7" ));
145+ assertThat (lsOutput .subDir2 ).satisfiesOnlyOnce (endsWith (" bar8" ));
146+ }
147+ }
148+
149+ private static class LsOutput {
150+
151+ private final List <String > parentDir ;
152+
153+ private final List <String > subDir1 ;
154+
155+ private final List <String > subDir2 ;
156+
157+ private static LsOutput parse (List <String > logLines ) {
158+ List <String > parentDir = null ;
159+ List <String > subDir1 = null ;
160+ int start = 0 ;
161+ for (int i = 0 ; i < logLines .size (); i ++) {
162+ if (logLines .get (i ).isEmpty ()) {
163+ if (parentDir == null ) {
164+ parentDir = new ArrayList <>(logLines .subList (start , i ));
165+ start = i ;
166+ } else if (subDir1 == null ) {
167+ subDir1 = new ArrayList <>(logLines .subList (start , i ));
168+ start = i ;
169+ }
170+ }
171+ }
172+ List <String > subDir2 = new ArrayList <>(logLines .subList (start , logLines .size ()));
173+
174+ return new LsOutput (parentDir , subDir1 , subDir2 );
175+ }
176+
177+ private LsOutput (List <String > parentDir , List <String > subDir1 , List <String > subDir2 ) {
178+ this .parentDir = parentDir ;
179+ this .subDir1 = subDir1 ;
180+ this .subDir2 = subDir2 ;
181+ }
182+ }
183+
184+ public static Consumer <String > endsWith (String suffix ) {
185+ return value -> assertThat (value ).endsWith (suffix );
186+ }
97187}
0 commit comments