Execute JUnit tests inside Docker container - java

I would like to build a test environment with Docker, where I can remotely send JUnit test classes (including the code that is tested), execute the tests and retrieve the results.
I found some articles which explained how to use docker for testing databaseconntection/writing inside a redis, but not how i can simple let my tests perform on docker and retrieve the results.
Do you have any recommendations how You would actually achieve this?
I don't know much about Jenkins, but would this might solve my problem?
Is there any good framework outside for this?

In a dockerfile, checkout your code and do a "maven test" command, redirect the result in a file that is on a mounted directory.
Each time you build the dockerfile, you do a unit test on your project.
With docker you also have a "docker test" command. I dont know if there is a plugin to use it on jenkins.

One way I found that works (using Gradle) is as follows. I know you are specifically referencing JUnit as your testing framework, but I actually think something similar to this could work.
Dockerfile (I called mine Dockerfile.UnitTests):
FROM gradle:jdk8 AS test-stage
WORKDIR /app
COPY . ./
RUN gradle clean
RUN gradle test
FROM scratch AS export-stage
COPY --from=test-stage /app/build/reports/tests/test/* /
I then run this with (in Gitbash on Windows 10):
> DOCKER_BUILDKIT=1 docker build -f Dockerfile.UnitTests --output type=tar,dest=UnitTests.tar .
This results in a tar file containing the test results displayed in an html file.
I executed the above in a Gitlab CI/CD pipeline and then sent the results to a web API for analysis.
A couple of assumptions:
My project is set up for Gradle builds so I have the structure from the root of my project src/test/java/groupname/projectname/testfile.java
I am working in Windows 10 targeting Linux containers and using Gitbash.

Related

maven+deploy => docker+tag+push

Small question regarding maven and the deploy command mvn deploy and push it to Dockerhub please.
I have a small springboot project, very straightforward.
I build and create a docker image from this project using mvn spring-boot:build-image after this command is run, I get a jar, but I also get a docker image which I can see using docker images, very happy.
My next step would consist into running a small two lines shell script which does:
docker tag myapp dockerhub/myapp
docker push dockerhub/myapp
Therefore, my pipeline can be summarized as:
step 1 maven to clean install build the jar + generate the docker image
step 2 docker to tag + docker push to docker hub.
I am wondering, is it possible to simply use the maven deploy command, and have the deploy tagging and pushing to dockerhub please?
What I tried:
Runing the deploy command, but it is not pushing to dockerhub at all.
What would be a way to leverage maven deploy, or maybe other maven commands, to have a pipeline build a jar + generate the docker image + tag it + push it please?
I cannot afford any CI/CD pipeline, and this is light enough, so hoping to get a maven command which can also push to dockerhub.
Thank you

How to make maven install in docker container

I have a multi-module project on maven. It is quite ancient and is going with a special dance with a tambourine.
Project structure
root
|__api
|__build
|__flash
|__gwt
|__server
|__service
|__shared
|__target
|__toolset
To build such a project, I have a special script that needs to be executed while at the root of the project.
./build/build_and_deploy.sh
When building on Windows, there are a lot of problems (problems with long paths, symbols and line separators get lost, etc.), so I want to build this project in docker.
At first I wanted to connect docker-maven-plugin from io.fabric8 as a plugin in maven, but as I understand it, it cannot run the build of itself in docker.
So I tried to write Dockerfile and ran into the following problems
I don't want to copy the .m2 folder to docker, there are a lot of dependencies there, it will be quite a long time.
I don't want to copy the project sources inside the container
I couldn't run the script./build/build_and_deploy.sh
How I see the solution to this problem.
Create a dockerfile, connect maven and java8 to it, and bash
Using Volume to connect the sources and maven repository
Because I work through VPN and the script is deployed, you need to find a solution to the problem through it (proxy/port forwarding???)
If you have experience or examples of a similar script or competent advice, then I will be glad to hear it
You can perform the build with Maven inside Docker.
For that you basically trigger something like docker build ., and the rest is inside the Dockerfile.
Start off from a container that has Maven, such as maven.
Add your whole project structure
Run your build script
Save your build result
To save your build result, you might want to upload it to some repository, or store it in a mounted volume that is available after the container run as well. Alternatively copy it to the next stage if you use a multistage docker build.
If you want to prevent repeated downloads of the .m2 directory or have many other dependencies in there, also mount it as volume when running the container.

Installing maven depencies from pom.xml in docker

I am trying to run a spring-boot maven project inside a docker environment. So the setup is as follows:
Docker is set up and installs Java, etc. (done only once)
App is run (can be any number of times)
What I am experiencing
Every time I run the spring-boot project by mvn spring-boot:run, it installs all the required libraries (every time I run the project) from the pom.xml (Java, Maven, etc. are preinstalled from the docker) and then runs the project.
What I am trying to do
This process of reinstalling every time is redundant and time-consuming, so I want to delegate this installation thing to the docker as well. Ideally, using the pom.xml to do the installations, though alternative ways are also welcome.
What I have tried so far
Install npm using a good tutorial, but it fails in Docker as we can't restart the terminal during docker build, while source ~/.bash_profile doesn't seem to work either.
Tried to build that project directly in docker (by RUN mvn clean install --fail-never) and copying both npm and node folders to the directory where I run the app. But it doesn't seem to work either as it's installing them every time without any change.
Can anyone please help me there? This problem has stuck the project. Thanks a lot!
From your question I understand that, in the Dockerfile you just install java, maven, etc. but does not build your project using mvn clean package install before executing mvn spring-boot:run (and that is redundant as well because mvn spring-boot:run does the build for you before staring the application).
You cannot skip installing maven dependency while running on containers as they are spun as they run. So it will be installed either while you call mvn clean install or mvn spring-boot:run.
What the max you can do is, using your devops pipeline, build the jar previously and in the Dockerfile just copy the build jar and execute.
Example Dockerfile in this case:
FROM openjdk:8-jdk-alpine
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
Here the previously build artifact is already available at target/

How to deploy Spring Boot + gradle service on Openshift

I've read tons of documentation and tutorials, but still cannot get through this.
I want to start developing a service using Spring boot and Gradle and deploy it on openshift.
With fabric8, there is a handy command 'mvn' clean install -Dfabric8.mode=openshift
to run the deployment.
This uses maven tho, and I'm with Gradle.
How can I do it? I know that I need an s2i-builder, but I cannot understand how to use them.
I know that fabric8, uses jboss-fuse-6/fis-java-openshift as s2i build, I may want to use the same for my builds.
Also, I would like to know if there is a way to redeploy from local files (this should be called a binary deploy) for dev purposes. As last thing, the very next step for me is setting up Jenkins but to get started I just really want to know how to proceed.
I've this simple Dockerfile:
FROM openjdk:8-jdk-alpine
VOLUME /tmp
ARG JAR_FILE
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom", "-XX:+UnlockExperimentalVMOptions", "-XX:+UseCGroupMemoryLimitForHeap", "-jar", "/app.jar"]
I'm using this plugin: "gradle.plugin.com.palantir.gradle.docker:gradle-docker:0.13.0" that gives me the Gradle task: ./gradlew build docker.
This container gets built successfully, and if I run it locally with docker run -p 8080:8080 it.example/microservice it runs perfectly fine. Added this bit of content just because I feel I'm not too far.

Apache Samza does not run

I am trying to set up a Apache Samza and Kafka environment. I am experiencing some problems when trying to run the modules.
I have Kafka working correctly but I can not make Samza work. I have installed two Debian Jeesy AMD64 boxes and followed the instructions of the Samza documentation:
apt-get install openjdk-7-jdk openjdk-7-jre git maven
git clone http://git-wip-us.apache.org/repos/asf/samza.git
cd samza
./gradlew clean build
When I try to launch the script that should start the Yarn AppMaster with the script provided with Samza:
/opt/samza/samza-shell/src/main/bash/run-am.sh
I get this error:
Error: Main class org.apache.samza.job.yarn.SamzaAppMaster has not been found or loaded
If I try to run a test job with the run-job.sh script
./run-job.sh --config-factory=org.apache.samza.config.factories.PropertiesConfigFactory --config-path=file://$PWD/deploy/samza/config/wikipedia-feed.properties
and I get a similar error referencing the org.apache.samza.job.JobRunner class.
I am thinking that I have a java configuration issue, but I am not able to find much help or reference.
Does anyone know what I am doing wrong?
Still not working but I have gone one step ahead. When executing the Samza provided scripts from a path, they expect to be located in a /bin/ folder and they need to have a /lib/ one where all the samza .jar files should be located.
I am still having some dependencies issues, but different ones.

Categories

Resources