使用Docker和Jenkin进行自动化测试、部署、回滚(2)

发布时间 2023-12-20 23:19:38作者: 东方来客

进行自动化测试需要maven-failsafe-plugin进行集成测试和maven-surefire-plugin进行运行单元测试,
引入exec-maven-plugin用来执行一些脚本。

failsafe & surefire

<plugin>
    <!-- for unit test -->
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>3.2.2</version>
    <configuration>
        <excludes>
            <!-- 排除掉集成测试 -->
            <exclude>**/*IntegrationTest</exclude>
        </excludes>
    </configuration>
</plugin>

 <plugin>
    <!-- run integration tests -->
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>3.2.2</version>
    <configuration>
        <includes>
            <!-- 要测试的代码 -->
            <include>**/*IntegrationTest</include>
        </includes>
    </configuration>
</plugin>

上面的incluede和excluede符合的是我项目中的命名方式,请注意。

exec-maven-plugin

自己的测试的时候需要自己运行命令启动Docker容器,像Redis、MySQL、RabbitMQ。想要自动化测试就不能手动输入了,所以需要exec-maven-plugin

pre-integration-test phase

pre-integration-test即集成测试开始前。

  1. pre-integration-test阶段先删除可能存在的测试用的容器
  2. 启动项目需要的MySQL数据库和Redis。
  3. 因为启动容器可能需要一定的时间,所以使用sleep延迟一下,可以使用exec-maven-plugin或者maven-antrun-plugin(因为可能有系统差异)

post-integration-test phase

post-integration-test即集成测试完成后。

pom配置

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.6.0</version>
    <executions>
        <execution>
            <id>delete-exist-test-database</id>
            <phase>pre-integration-test</phase>
            <goals>
                <goal>exec</goal>
            </goals>
            <configuration>
                <longModulepath>false</longModulepath>
                    <executable>docker</executable>
                    <arguments>
                        <argument>rm</argument>
                        <argument>-f</argument>
                        <argument>test-mysql</argument>
                        <argument>test-redis</argument>
                    </arguments>
            </configuration>
        </execution>
        <execution>
            <id>start-test-database</id>
            <phase>pre-integration-test</phase>
            <goals>
                <goal>exec</goal>
            </goals>
            <configuration>
                <longModulepath>false</longModulepath>
                <executable>docker</executable>
                <arguments>
                    <argument>run</argument>
                    <argument>--name</argument>
                    <argument>test-mysql</argument>
                    <argument>-e</argument>
                    <argument>MYSQL_ROOT_PASSWORD=root</argument>
                    <argument>-e</argument>
                    <argument>MYSQL_DATABASE=test-cake</argument>
                    <argument>-p</argument>
                    <argument>3310:3306</argument>
                    <argument>-d</argument>
                    <argument>mysql</argument>
                </arguments>
            </configuration>
        </execution>
        <execution>
            <id>start-test-redis</id>
            <phase>pre-integration-test</phase>
            <goals>
                <goal>exec</goal>
            </goals>
            <configuration>
                <longModulepath>false</longModulepath>
                <executable>docker</executable>
                <arguments>
                    <argument>run</argument>
                    <argument>--name</argument>
                    <argument>test-redis</argument>
                    <argument>-p</argument>
                    <argument>6380:6379</argument>
                    <argument>-d</argument>
                    <argument>redis</argument>
                </arguments>
            </configuration>
        </execution>
        <execution>
            <id>wait-test-database</id>
            <phase>pre-integration-test</phase>
            <goals>
                <goal>exec</goal>
            </goals>
            <configuration>
                <longModulepath>false</longModulepath>
                <executable>sleep</executable>
                <arguments>
                    <argument>20</argument>
                </arguments>
            </configuration>
        </execution>
        <execution>
            <id>teardown-test-database</id>
            <phase>post-integration-test</phase>
            <goals>
                <goal>exec</goal>
            </goals>
            <configuration>
                <longModulepath>false</longModulepath>
                <executable>docker</executable>
                <arguments>
                    <argument>rm</argument>
                    <argument>-f</argument>
                    <argument>test-mysql</argument>
                </arguments>
            </configuration>
        </execution>
        <execution>
            <id>teardown-test-redis</id>
            <phase>post-integration-test</phase>
            <goals>
                <goal>exec</goal>
            </goals>
            <configuration>
                <longModulepath>false</longModulepath>
                <executable>docker</executable>
                <arguments>
                    <argument>rm</argument>
                    <argument>-f</argument>
                    <argument>test-redis</argument>
                </arguments>
            </configuration>
        </execution>
    </executions>
</plugin>

如果使用maven-antrun-plugin进行延时则配置

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>3.0.0</version>
    <executions>
        <execution>
            <id>wait-test-database</id>
            <phase>pre-integration-test</phase>
            <goals>
                <goal>run</goal>
            </goals>
            <configuration>
                <target>
                    <sleep seconds="20"/>
                </target>
            </configuration>
        </execution>
    </executions>
</plugin>