使用Docker生成测试期间所需服务的容器
docker-services的Python项目详细描述
使用Docker生成测试期间所需服务的容器
[![构建状态](https://travis-ci.org/dmonroy/docker-services.svg?(https://travis ci.org/dmonroy/docker-services)
>
您需要开始列出项目所依赖的所有服务,并利用Docker-Services在测试运行期间维护这些服务的生命周期。
这必须使用pytest.cfg/.ini文件或.services.yaml文件上的"Docker-Services"配置选项,val对于该选项,ue需要一个yaml结构,其中顶级成员是服务名,它们的值可以是空值、映像名或对象。
这是一个依赖于"postgres"服务的项目的基本示例:
`````
[pytest]
docker\u services=postgres:
`````
这是一样的:
``````
`````
````
````
``````````
>[pytest]
docker_services=
postgres:postgres
```
这也是(在.services.yaml上):
```yaml
postgres:
image:postgres
````
…这也是!
``yaml
数据库:
名称:postgres
图像:postgres
````
如果项目依赖于不同的服务,请列出所有这些服务:
````
[pytest]
docker\u services=
postgres:
redis:
````
服务名作为镜像名,但也可以指定镜像名和使用版本:
`````
[pytest]
docker\u服务=
postgres:postgres:10
````
postgres:10
>
``yaml
postgres:postgres:postgres:postgres:10
```````
现在服务名为"postgres",镜像名为"postgres:postgres:10```。现在服务名为"postgresres:postgres:10```postgres:
另一种选择是对服务使用不同的名称,比如:
`````
[pytest]
docker\u services=
db:postgres:10
`````
```yaml
db:
image:postgres:10
````
另一种选择是:服务名为`db`服务名为`db`服务名为`postgres:10`。如果您不想(或不需要)设置服务名为`db`服务名为`db`服务名,映像名为`postgresgres:10``postgresgres:10``。如果您不特定的图像对于版本,只要忽略这样的版本部分:
````
[pytest]
docker\u services=
db:postgres
````
docker\u services=
db:my.registry.com/custom/postgres
```````
>db:my.registry.com/custom/postgres
````
35分;1.1分。为您的服务配置环境变量
您可能希望通过设置环境变量自定义服务的行为,也可以通过添加到配置中(仅在使用`.services.yaml`文件时有效)。
``yaml
db:
image:postgres:10
环境:
postgres用户名:myuser
postgres_password:$3cr3t
postgres_db:mydb
`````
我们的应用程序或测试也是。
1.2。也要配置动态变量
是否计划基于服务的端口号配置"database\u url"环境变量?…然后不要等待并配置变量模板;)。
谈论postgres的"数据库url"通常需要类似"postgres://user:password@host:port/dbname"的内容,这可以通过替换
``yaml
postgres:
image:postgres:10
环境:
postgres用户名:myuser
postgres密码:$3cr3t
postgres数据库:mydb
u模板:
postgres-u-port:{env[postgres[postgres\u-5432\u-tcp\u-port]}"
>postgres://myuser:$s3cr3t@localhost:{env[postgres[postgres\u-5432\u-tcp\u-port]}/mydb"
````
`````
是的se_url:"postgres://{env[postgres_username]}:{env[postgres_password]}@localhost:{env[postgres_port_5432_tcp_port]}/{env[postgres_db]}"
```
模板上的参数使用python的`.format()`方法替换,目前只传递了'env'参数,它实际上引用了'os.env'的内容,因此所有env"环境变量可用。在启用了Docker服务的情况下运行测试
u docker-service s添加了`--use docker services`命令行选项用于`.test`,当设置此选项时,它将启用使用Docker的服务生成,运行方式如下:
````
````.test--使用Docker服务
```
3。与服务通信
我们生成服务,因为我们需要在测试会话期间与它们通信,以便从中使用数据或将数据发布到。因此,docker_service s依赖于服务的公开端口为每个端口创建唯一的环境变量,并为每个服务上公开的协议创建唯一的环境变量。
假设Docker在本地计算机上运行,并且端口暴露在本地主机上,则创建`*\u port`环境变量。
so,如果我们有一个'postgres'服务,我们希望使用端口5432进行通信,现在查看[Dockerfile](https://github.com/docker-library/postgres/blob/674466e0d47517f4e05ec2025ae996e71b26cae9/10/dockerfile)我们可以请确认它[公开端口5432](https://github.com/docker-library/postgres/blob/674466e0d4751f4e05ec2025ae996e71b26cae9/10/dockerfile l132)。
请注意,如果服务的映像不公开任何端口,则不会创建`*`port`变量。
tgres`服务用例,端口'5432'的变量名应该是:`postgres_port_5432_tcp_port`,此变量名是使用以下模板生成的:`{service_name}u port{port}{protocol}u port`.
还请记住,为服务配置的环境变量也可以在pytest会话的上下文中使用,这适用于两个静态和动态变量!
BR/>
[![构建状态](https://travis-ci.org/dmonroy/docker-services.svg?(https://travis ci.org/dmonroy/docker-services)
>
您需要开始列出项目所依赖的所有服务,并利用Docker-Services在测试运行期间维护这些服务的生命周期。
这必须使用pytest.cfg/.ini文件或.services.yaml文件上的"Docker-Services"配置选项,val对于该选项,ue需要一个yaml结构,其中顶级成员是服务名,它们的值可以是空值、映像名或对象。
这是一个依赖于"postgres"服务的项目的基本示例:
`````
[pytest]
docker\u services=postgres:
`````
这是一样的:
``````
`````
````
````
``````````
>[pytest]
docker_services=
postgres:postgres
```
这也是(在.services.yaml上):
```yaml
postgres:
image:postgres
````
…这也是!
``yaml
数据库:
名称:postgres
图像:postgres
````
如果项目依赖于不同的服务,请列出所有这些服务:
````
[pytest]
docker\u services=
postgres:
redis:
````
服务名作为镜像名,但也可以指定镜像名和使用版本:
`````
[pytest]
docker\u服务=
postgres:postgres:10
````
postgres:10
>
``yaml
postgres:postgres:postgres:postgres:10
```````
现在服务名为"postgres",镜像名为"postgres:postgres:10```。现在服务名为"postgresres:postgres:10```postgres:
另一种选择是对服务使用不同的名称,比如:
`````
[pytest]
docker\u services=
db:postgres:10
`````
```yaml
db:
image:postgres:10
````
另一种选择是:服务名为`db`服务名为`db`服务名为`postgres:10`。如果您不想(或不需要)设置服务名为`db`服务名为`db`服务名,映像名为`postgresgres:10``postgresgres:10``。如果您不特定的图像对于版本,只要忽略这样的版本部分:
````
[pytest]
docker\u services=
db:postgres
````
docker\u services=
db:my.registry.com/custom/postgres
```````
````
35分;1.1分。为您的服务配置环境变量
您可能希望通过设置环境变量自定义服务的行为,也可以通过添加到配置中(仅在使用`.services.yaml`文件时有效)。
``yaml
db:
image:postgres:10
环境:
postgres用户名:myuser
postgres_password:$3cr3t
postgres_db:mydb
`````
我们的应用程序或测试也是。
1.2。也要配置动态变量
是否计划基于服务的端口号配置"database\u url"环境变量?…然后不要等待并配置变量模板;)。
谈论postgres的"数据库url"通常需要类似"postgres://user:password@host:port/dbname"的内容,这可以通过替换
``yaml
postgres:
image:postgres:10
环境:
postgres用户名:myuser
postgres密码:$3cr3t
postgres数据库:mydb
u模板:
postgres-u-port:{env[postgres[postgres\u-5432\u-tcp\u-port]}"
>postgres://myuser:$s3cr3t@localhost:{env[postgres[postgres\u-5432\u-tcp\u-port]}/mydb"
````
`````
是的se_url:"postgres://{env[postgres_username]}:{env[postgres_password]}@localhost:{env[postgres_port_5432_tcp_port]}/{env[postgres_db]}"
```
模板上的参数使用python的`.format()`方法替换,目前只传递了'env'参数,它实际上引用了'os.env'的内容,因此所有env"环境变量可用。在启用了Docker服务的情况下运行测试
u docker-service s添加了`--use docker services`命令行选项用于`.test`,当设置此选项时,它将启用使用Docker的服务生成,运行方式如下:
````
````.test--使用Docker服务
```
3。与服务通信
我们生成服务,因为我们需要在测试会话期间与它们通信,以便从中使用数据或将数据发布到。因此,docker_service s依赖于服务的公开端口为每个端口创建唯一的环境变量,并为每个服务上公开的协议创建唯一的环境变量。
假设Docker在本地计算机上运行,并且端口暴露在本地主机上,则创建`*\u port`环境变量。
so,如果我们有一个'postgres'服务,我们希望使用端口5432进行通信,现在查看[Dockerfile](https://github.com/docker-library/postgres/blob/674466e0d47517f4e05ec2025ae996e71b26cae9/10/dockerfile)我们可以请确认它[公开端口5432](https://github.com/docker-library/postgres/blob/674466e0d4751f4e05ec2025ae996e71b26cae9/10/dockerfile l132)。
请注意,如果服务的映像不公开任何端口,则不会创建`*`port`变量。
tgres`服务用例,端口'5432'的变量名应该是:`postgres_port_5432_tcp_port`,此变量名是使用以下模板生成的:`{service_name}u port{port}{protocol}u port`.
还请记住,为服务配置的环境变量也可以在pytest会话的上下文中使用,这适用于两个静态和动态变量!
BR/>