MySQL是如何做容器测试的?(3)

Goss的配置文件如下所示:

file: /usr/sbin/mysqld: exists: true contains: [] package: mysql-community-server-minimal: installed: true mysql-shell: installed: true port: tcp6:3306: listening: true ip: [] tcp6:33060: listening: true ip: [] user: mysql: exists: true process: mysqld: running: true

除了mysqld文件,我们还要检查所需的软件包是否已安装、公开端口是否正确以及所需的进程是否在运行。Goss将为我们启动容器:

GOSS_SLEEP=10 dgoss run -p 3306:3306 mysql/mysql-server

因为设置了GOSS_SLEEP,所以我们的服务器有足够时间完成初始化,其余参数被传给docker run。输出如下:

INFO: Starting docker container INFO: Container ID: 75bc8869 INFO: Sleeping for 10 INFO: Running Tests File: /usr/sbin/mysqld: exists: matches expectation: [true] User: mysql: exists: matches expectation: [true] Process: mysqld: running: matches expectation: [true] Port: tcp6:33060: listening: matches expectation: [true] Port: tcp6:33060: ip: matches expectation: [[]] Port: tcp6:3306: listening: matches expectation: [true] Port: tcp6:3306: ip: matches expectation: [[]] Package: mysql-shell: installed: matches expectation: [true] Package: mysql-community-server-minimal: installed: matches expectation: [true] Total Duration: 0.038s Count: 9, Failed: 0, Skipped: 0 INFO: Deleting container

Container Structure Test的yaml配置片段如下:

schemaVersion: "2.0.0" metadataTest: exposedPorts: [ "3306", "33060" ] entrypoint: [ "/entrypoint.sh" ] cmd: [ "mysqld" ] volumes: [ "/var/lib/mysql" ] commandTests: - name: "mysqlsh" command: "mysqld" args: - "--version" expectedOutput: - "8.0.12" - name: "mysqlsh" command: "mysqlsh" args: - "--version" expectedOutput: - "8.0.12" fileExistenceTests: - name: "mysqld" path: "/usr/sbin/mysqld"

此外,我们还要检查公开的端口是否正确,然后直接运行二进制文件(而不是像其他工具那样使用内部包装器)以验证它们是否已就位。

container-structure-test --image mysql/mysql-server test --config mysql-server-container-structure-test.yml

与Goss类似,调用起来很简单,只需要提供镜像名称和配置文件。

================================================================== ====== Test file: mysql-server-container-structure-test.yml ====== ================================================================== INFO: stdout: /usr/sbin/mysqld Ver 8.0.12 for Linux on x86_64 (MySQL Community Server - GPL) === RUN: Command Test: mysqlsh --- PASS stdout: /usr/sbin/mysqld Ver 8.0.12 for Linux on x86_64 (MySQL Community Server - GPL) INFO: stdout: mysqlsh Ver 8.0.12 for Linux on x86_64 - for MySQL 8.0.12 (MySQL Community Server (GPL)) === RUN: Command Test: mysqlsh --- PASS stdout: mysqlsh Ver 8.0.12 for Linux on x86_64 - for MySQL 8.0.12 (MySQL Community Server (GPL)) INFO: File Existence Test: mysqld === RUN: File Existence Test: mysqld --- PASS === RUN: Metadata Test --- PASS =================================================================== ============================= RESULTS ============================= =================================================================== Passes: 4 Failures: 0 Total tests: 4 PASS

Container Structure Test运行速度快,易于调用,只不过只能用于容器。在大多数情况下,它可以确保容器的行为是正确的。

可以在存储库的根目录运行以下脚本来执行上述的测试:

./inspec.sh

./goss.sh

./container-structure-test.sh

MySQL的容器测试

我们已经开始使用InSpec测试所有的Docker镜像。从下一个MySQL版本(8.0.13)开始,基本的InSpec测试将成为MySQL Server、MySQL Cluster和MySQL Router Docker镜像自动发布流程的一部分。我们主要基于以下几点做出这样的决定:

它拥有最大的作用域(ssh/local/docker),可进一步在内部使用;

广泛的资源;

依赖机制;

由于它与Chef生态系统的关系以及与Serverspec的相似性而被广泛采用。

存在的不足:

因为是ruby调用,运行速度感觉比Go语言的替代品慢(但我们认为这不是关键问题);

InSpec更通用,但需要做出权衡,用更多的脚本来运行测试。

我们将InSpec作为自动发布管道的一部分,如果测试失败,将不会生成任何工件。我们的QA流程包含很多其他步骤,例如之前对MySQL Docker镜像中的rpm包进行的单独测试。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/bc3fd0528ddb27d2ffa347692e617c55.html