Continuous integration testing (1) -- A preliminary study of docker-maven-plugin (transfer)

Background

Recently, I want to do continuous integration testing. The initial idea is to use Git, jekins, maven, and Docker as the basic components of continuous integration, of course, the most commonly used basic components.
Initially, I want to put the Java test cases into the docker container for testing. The test environment is deployed only once. The image is pushed to the warehouse, and the test is pulled with it. The docker command is used to run it directly. The environment to which the code is attached; the disadvantage is docker, docker, docker, which is the basic environment docker that is necessary to run test cases.

The command line execution of junit

Our most common execution test cases are mostly through eclipse run as, or mvn test to execute test cases. If the test case is plugged into the container, either mvn is installed in the container, or built on a physical machine, and the built environment, that is, the jar package, is plugged into the container and executed with java. After weighing it, I
decided to build on a physical machine and stuff the build results into the container.
1. Repeatedly building the mvn environment wastes time; 2. Stuffing
mvn into the container wastes resources; The art is not good, here is an article on the use of junitcore: command line execution using java -cp *.jar org.junit.runner.JUnitCore classname can refer to this article to understand and execute. The execution example uses junit simple output statement, no function, just as an example demonstration: public class startTest {     @Before     public void setup(){














        System.out.println("this is setup...");
    }

    @After
    public void teardown(){
        System.out.println("this is teardown...");
    }

    @Test
    public void test1(){
        System.out.println("this is test1...");
    }
}



Command line trip junit:

java -cp $CLASS_PATH:target/test-classes org.junit.runner.JUnitCore $CLASS_NAME

Problems encountered and solutions The method is

because I am not very familiar with the execution of junit test cases in java. After mvn finishes the package, executing

java -cp $CLASS_PATH org.junit.runner.JUnitCore $CLASS_NAME

can never find the test class (I am not familiar with the technology, so don’t be a troll Spray ^o^), the reason is that the default query in the jar package is the files under src/main, and the test cases are mostly

mv src/test src/main
mvn clean package -DskipTests=true under src/test
java -cp $CLASS_PATH org.junit.runner.JUnitCore $CLASS_NAME
bingo

or specify classpath, specify to search for test classes in target/test-classes directory

java -cp $CLASS_PATH:target/test-classes org.junit.runner.JUnitCore $ CLASS_NAME
enters the topic

There are two ways to use this plug-in:
1. Without dockerfile
2. With dockerfile

without dockerfile

As the name implies, the image is built without relying on dockerfile, that is, the image building process is defined by tags.

<plugin>
  <groupId>com.spotify</groupId>
  <artifactId>docker-maven-plugin</artifactId>
  <version>0.4.13</version>
  <configuration>
    <imageName>registry.test.com/example</ imageName>
    <baseImage>registry.test.com/base_image</baseImage>
    <entryPoint>["sh", "start.sh"

      <resource>
        <targetPath>/</targetPath>
        <directory>${project.build.directory}</directory>
        <include>**/*</include>
      </resource>
    </resources>
  </configuration>
< /plugin>


where:

imageName: the name of the image to be built
baseImage: the base image needed to build the image, similar to the From entryPoint of the
Dockerfile: the command executed when the container starts
resources: copy some extra files to the specified path, such as ${ project.build.finalName}.jar file
some maven built-in variables

${basedir} project root directory
${project.build.directory} build directory, default is target
${project.build.outputDirectory} build process output directory, default is target/classes, target/test-classes
${project.build.finalName} The result name generated after packaging, the default is ${project.artifactId}-${project.version}
\${project.packaging} packaging type, the default is jar
Some maven tags

targetPath: specify which destination directory to build resources to, the default is base directory
directory: specify the directory of the property file, the build process needs to find it, and put it under targetPath.
includes: specifies the patterns of the included files, conforming to the style and the files in the directory directory will be the resource files included in the project
excludes: specifies the patterns that are not included, if the includes conflict with the excludes, then the excludes win, those that meet the conflict The style file is still not included.
Using dockerfile

depends on Dockerfile to build images. You need to specify dockerDirectory to find the file directory where Dockerfile is located. After specifying dockerDirectory, tags such as baseImage, maintainer, cmd, and entryPoint are invalid. Everything in the dockerDirectory directory will be copied to the ${project.build.directory}/docker directory by default.

<plugin>
  <groupId>com.spotify</groupId>
  <artifactId>docker-maven-plugin</artifactId>
  <version>0.4.13</version>
  <configuration>
    <imageName>registry.test.com/example</ imageName>
    <dockerDirectory>${project.basedir}/</dockerDirectory>
    <resources>
      <resource>
        <targetPath>/</targetPath>
        <directory>${project.build.directory}</directory>
        <include>${project.build.finalName}.jar</include>
      </resource>
    </resources>
  </configuration>
</plugin>

我的根目录结构为:

-rw-r--r-- 1 ** **  437 Sep 21 10:38 Dockerfile
drwxr-xr-x 2 ** ** 4096 Sep 20 13:59 lib
drwxr-xr-x 3 ** ** 4096 Sep 20 11:18 logs
-rwxrwxrwx 1 ** ** 3015 Sep 20 18:40 pom.xml
-rwxr-xr-x 1 ** ** 3102 Sep 20 18:54 pom.xml.undocker
drwxrwxrwx 3 ** ** 4096 Sep 20 11:18 src
-rw-r - r-- 1 ** ** 1028 Sep 20 10:27 start.sh

My Dockerfile is specified in the root directory. When mvn clean package docker:build is executed, a target file will be generated. After viewing the target directory, the docker folder will be generated by default. When continuing to view the docker folder, it will be visible, and all the contents of the root directory will be copied. In this folder, in the process of docker build, it is also executed according to the Dockerfile in the target/docker directory.

The content of the Dockerfile file is as follows, jdk is installed, and some libraries required for the test code are added:

From registry.test.com/base_image:centos6.6

ADD target/test-classes /data1/target/test-classes
ADD target/dockermaven/lib /data1/target/dockermaven/lib
ADD start.sh /data1/
ADD lib/jdk-1.8.0_77-2.el6.x86_64.rpm /jdk-1.8.0_77-2.el6.x86_64.rpm
RUN rpm -ivh / jdk-1.8.0_77-2.el6.x86_64.rpm
ENV JAVA_HOME /usr/local/jdk1.8.0_77
ENV PATH $PATH:$JAVA_HOME/bin
RUN chmod +x /data1/start.sh
WORKDIR /data1
CMD sh start. sh

build command

# maven package and build the image
mvn clean package -DskipTests=true docker:build
# maven packs and builds the image and pushes it to the warehouse
mvn clean package docker:build -DpushImage
# maven packs and builds the mirror, pushes the mirror with the specified tag to the warehouse, this command needs to use
# <imageTags> <imageTag>...</imageTag></imageTags> tags
mvn clean package docker:build -DpushImageTag

<plugin>
  <configuration>
    ...
    <imageTags>
       <imageTag>${project.version}</imageTag>
       <imageTag >latest</imageTag>
    </imageTags>
  </configuration>
</plugin>

# About tags configuration, you can also set
mvn directly on the command line ... docker:build -DpushImageTags -DdockerImageTag=latest -DdockerImageTag=another-

tag :
http://blog.csdn.net/neven7/article/details/46675845
https://github.com/spotify/docker-maven-plugin
https://www.zybuluo.com/babydragon/note/352069
http://dockone.io/article/582
http://www.cnblogs.com/puroc/p/5799228.html

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326626643&siteId=291194637