33import com .dajudge .kindcontainer .BaseSidecarContainer .ExecInContainer ;
44import com .dajudge .kindcontainer .BaseSidecarContainer .FileTarget ;
55import com .dajudge .kindcontainer .exception .ExecutionException ;
6- import org .testcontainers .utility .MountableFile ;
6+ import org .slf4j .Logger ;
7+ import org .slf4j .LoggerFactory ;
8+ import org .testcontainers .images .builder .Transferable ;
9+ import org .testcontainers .shaded .org .apache .commons .io .IOUtils ;
710
811import java .io .IOException ;
912import java .util .ArrayList ;
1013import java .util .List ;
14+ import java .util .function .Function ;
15+
16+ import static java .lang .Thread .currentThread ;
17+ import static java .util .Arrays .asList ;
18+ import static java .util .function .Function .identity ;
1119
1220public class ApplyFluent <P > {
21+ private static final Logger LOG = LoggerFactory .getLogger (ApplyFluent .class );
1322 private final ExecInContainer exec ;
1423 private final FileTarget fileTarget ;
1524 private final P parent ;
@@ -29,17 +38,36 @@ public ApplyFluent<P> namespace(final String namespace) {
2938 }
3039
3140 public ApplyFluent <P > fileFromClasspath (final String resourceName ) {
41+ return fileFromClasspath (resourceName , identity ());
42+ }
43+
44+ public ApplyFluent <P > fileFromClasspath (final String resourceName , final Function <byte [], byte []> transform ) {
3245 final String path = "/tmp/classpath:" + resourceName
3346 .replaceAll ("_" , "__" )
3447 .replaceAll ("[^a-zA-Z0-9.]" , "_" );
3548 files .add (path );
36- preExecutionRunnables .add (() -> fileTarget .copyFileToContainer (
37- MountableFile .forClasspathResource (resourceName ),
38- path
39- ));
49+ final byte [] bytes = transform .apply (resourceToByteArray (resourceName ));
50+ final Transferable transferable = Transferable .of (bytes );
51+ preExecutionRunnables .add (() -> fileTarget .copyFileToContainer (transferable , path ));
4052 return this ;
4153 }
4254
55+ private byte [] resourceToByteArray (final String resourceName ) {
56+ final List <ClassLoader > classloaders = asList (
57+ ApplyFluent .class .getClassLoader (),
58+ currentThread ().getContextClassLoader (),
59+ ClassLoader .getSystemClassLoader ()
60+ );
61+ for (final ClassLoader classloader : classloaders ) {
62+ try {
63+ return IOUtils .resourceToByteArray (resourceName , ApplyFluent .class .getClassLoader ());
64+ } catch (final IOException e ) {
65+ LOG .debug ("Failed to read classpath resource '{}' via classloader '{}'" , resourceName , classloader , e );
66+ }
67+ }
68+ throw new IllegalArgumentException ("Unable to locate resource: " + resourceName );
69+ }
70+
4371 public P run () throws IOException , ExecutionException , InterruptedException {
4472 final List <String > cmdline = new ArrayList <>();
4573 cmdline .add ("kubectl" );
0 commit comments