明天的太阳

导航

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

进行自动化测试需要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>

phase和goal

在Maven中,插件的goal就是绑定到特定phase上的“其他人”提供的外部功能。这个“其他人”指的是其他的插件或者Maven的生命周期阶段。

当使用exec-maven-plugin的exec goal时,你实际上是在告诉Maven在特定的phase(例如,pre-integration-test)执行你指定的命令或脚本。这个命令或脚本需要符合exec-maven-plugin的要求,因为exec-maven-plugin提供了一种方式来执行外部命令。

换句话说,绑定的goal需要符合插件的要求。但是,这个“其他人”不一定是其他插件,它也可以是Maven的生命周期阶段。例如,你可以在pre-integration-test阶段使用exec-maven-plugin来执行一些操作,这些操作可能是为了准备集成测试的环境。

所以,绑定的goal需要符合插件的要求,但不一定需要符合其他插件的要求。它主要取决于如何使用这个插件以及想在哪个阶段执行什么样的操作。

posted on 2023-12-20 23:16  东方来客  阅读(17)  评论(0编辑  收藏  举报