首页 > 其他分享 >使用Docker和Jenkin进行自动化测试、部署、回滚(2)

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

时间:2023-12-20 23:22:06浏览次数:29  
标签:pre 回滚 plugin Jenkin exec integration maven test Docker

进行自动化测试需要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需要符合插件的要求,但不一定需要符合其他插件的要求。它主要取决于如何使用这个插件以及想在哪个阶段执行什么样的操作。

标签:pre,回滚,plugin,Jenkin,exec,integration,maven,test,Docker
From: https://www.cnblogs.com/shames/p/17917862.html

相关文章

  • Docker Alpine Linux 安装 Python3
      参考文档:DockerAlpineLinux安装Pytho1、DockerfileDockerfile文件一般包含基础镜像信息、维护者信息、镜像操作指令和容器启动时执行指令,’#’为Dockerfile中的注释。参考文件::Docker构建自定义镜像和Dockerfile文件2、直接安装 通过Dockerfile文件构建镜像时,直......
  • ubuntu 安装 docker.
    参考链接:https://zhuanlan.zhihu.com/p/651148141检查卸载老版本docker$sudoapt-getremovedockerdocker-enginedocker.iocontainerdrunc更新软件包sudoaptupdatesudoaptupgrade安装docker依赖sudoapt-getinstallca-certificatescurlgnupglsb-releas......
  • docker 工作目录下overlay目录清理办法
    1、查看docker服务占用的系统空间[root@jenkins]#dockersystemdf2、使用docker修剪命令进行操作--谨慎使用,明确知道自己在做什么dockersystemprune是将docker整个服务的各个资源都进行修剪,一般不直接用,按需修剪docker imageprune删除所有悬挂的镜像dockernetworkprune......
  • 最新docker多系统安装技术
    在Ubuntu操作系统中安装Docker在Ubuntu操作系统中安装Docker的步骤如下。1.卸载旧版本Docker卸载旧版本Docker的命令如下:$sudoapt-get remove docker\              docker-engine\              docker.io2.使用脚本自动安装在测试或开......
  • docker常用命令日志记录(二)
    1.拉取镜像dockerpullnginx2.查看镜像 dockerimages[root@VM-8-2-centos~]#dockerimagesREPOSITORYTAGIMAGEIDCREATEDSIZEnginxlatest605c77e624dd24monthsago141MB3.保存镜像为一个压缩包dockersave[root@V......
  • docker容器跨主机通信
    一、Docker网络基本原理直观上看,要实现网络通信,机器需要至少一个网络接口(物理接口或虚拟接口)与外界相通,并可以收发数据包;此外,如果不同子网之间要进行通信,需要额外的路由机制。Docker中的网络接口默认都是虚拟的接口。虚拟接口的最大优势就是转发效率极高。这是因为Linux通过......
  • docker综合应用
    1.容器资源限制官网文档https://docs.docker.com/config/containers/resource_constraints/ 2.docker内存限制 -m或者--memory=容器可以使用的最大内存量。如果设置此选项,则允许的最小值为6m(6兆字节)。也就是说,您必须将该值设置为至少6兆字节。--oom-kill-di......
  • docker网络模式
    Docker网络我们使用容器,不单是运行单机程序,当然是需要运行网络服务在容器中,那么如何配置docker的容器网络,基础网络配置,网桥配置,端口映射,还是很重要。docker网络功能docker的网络功能就是利用Linux的networknamespace,networkbridge,虚拟网络设备实现的。默认情况下,docker安装......
  • docker容器单机编排
    随着网站架构的升级,容器也使用的越发频繁,应用服务和容器间的关系也越发复杂。这就要求研发人员能够更好的方法去管理数量较多的容器服务,而不能手动的去挨个管理。例如一个LNMP的架构,就得部署web服务器,后台程序,数据库,负载均衡等等都需要统一部署在容器里,那么这时候就需要使用统一......
  • docker安装部署
    1.国内源安装docker-ce配置linux内核流量转发功能因为docker和宿主机的端口映射,本质是内核的流量转发功能##若未配置,需要执行如下$cat<<EOF>/etc/sysctl.d/docker.confnet.bridge.bridge-nf-call-ip6tables=1net.bridge.bridge-nf-call-iptables=1net.ipv4.ip_f......