使用yaml填充和验证系统
satellite_populate的Python项目详细描述
卫星填充
使用yaml填充并验证系统
- 自由软件:GNU通用公共许可v3
- 文档:https://satellite-populate.readthedocs.io。
安装
要安装最新发布的版本:
pip install satellite-populate
从github主分支安装:
pip install https://github.com/SatelliteQE/satellite-populate/tarball/master
用于开发:
# fork https://github.com/SatelliteQE/satellite-populate/ to YOUR_GITHUB # clone your repo locally git clone git@github.com:YOUR_GITHUB/satellite-populate.git cd satellite-populate # add upstream remote git remote add upstream git@github.com:SatelliteQE/satellite-populate.git # create a virtualenv mkvirtualenv satellite-populate workon satellite-populate # install for development (editable) pip install -r requirements.txt
测试安装是否良好:
$ satellite-populate --test satellite_populate.base - INFO - ECHO: Hello, if you can see this it means that I am working!!!
功能
基于yaml的操作
数据填充定义转到yaml文件,例如office.yaml,如下所示 示例我们将使用列表创建2个组织和2个管理员用户:
vars: org_names: - Dunder Mifflin - Wernham Hogg user_list: - firstname: Michael lastname: Scott - firstname: David lastname: Brent actions: - model: Organization with_items: org_names register: default_orgs data: name: "{{ item }}" label: org{{ item.replace(' ', '') }} description: This is a satellite organization named {{ item }} - model: User with_items: user_list data: admin: true firstname: "{{ item.firstname }}" lastname: "{{ item.lastname }}" login: "{{ '{0}{1}'.format(item.firstname[0], item.lastname) | lower }}" password: from_factory: alpha organization: from_registry: default_orgs default_organization: from_registry: default_orgs[loop_index]
在填充文件中,可以定义crud操作,例如create,delete,update 如果未定义action:,则默认值为create。
还有特殊操作和自定义操作将在后面解释。
用实体填充卫星
考虑到上面的office.yaml文件,您可以用 命令行:
$ satellite-populate office.yaml -h yourserver.com --output=office.yaml -v
在上面的命令行中,-h代表--hostname,--output是 要用来验证系统的输出文件,-v是 详细级别。
要查看可用参数列表,请运行:
# satellite-populate --help
验证系统是否有实体
一旦运行satellite-populate,就可以使用输出的文件来验证系统。 由于所有输出文件在office中都命名为validation_<name>.yaml,因此您可以运行:
$ satellite-populate validation_office.yaml -v
使用该验证文件,系统将检查实体是否存在,只读。 验证文件的存在是因为在生成人口动态数据时 密码和字符串from_factory以及一些实体可以被删除或更新 所以验证文件会处理它。
特别行动
一些内置的特殊操作是:
- 断言
- 回声
- 注册
- 注销
在下面的示例中,我们将使用 在yaml文件中定义的操作,如果验证失败,系统返回状态0 可用于自动化测试:
# A TEST CASE USING SPECIAL ACTIONS # Create a plain vanilla activation key # Check that activation key is created and its "unlimited_hosts" # attribute defaults to true - action: create log: Create a plain vanilla activation key model: ActivationKey register: vanilla_key data: name: vanilla organization: from_registry: default_orgs[0] - action: assertion log: > Check that activation key is created and its "unlimited_hosts" attribute defaults to true operation: eq register: vanilla_key_unlimited_hosts data: - from_registry: vanilla_key.unlimited_hosts - true - action: echo log: Vanilla Key Unlimited Host is False!!!! level: error print: true when: vanilla_key_unlimited_hosts == False - action: echo log: Vanilla Key Unlimited Host is True!!!! level: info print: true when: vanilla_key_unlimited_hosts - action: register data: you_must_update_vanilla_key: true when: vanilla_key_unlimited_hosts == False
自定义操作
您还可以在自定义填充器中定义特殊操作。
假设您的项目中有这个python模块,在 Python路径:
from satellite_populate.api import APIPopulator class MyPopulator(APIPopulator): def action_writeinfile(self, rendered_data, action_data): with open(rendered_data['path'], 'w') as output: output.write(rendered_data['content'])
现在去你的test.yaml写:
config: populator: mine populators: mine: module: mypath.mymodule.MyPopulator actions: - action: writeinfile path: /tmp/test.txt text: Hello World!!!
然后运行:
$ satellite-populate test.yaml -v
测试用例的decorator
拥有如下数据文件:
actions: - model: Organization register: organization_1 data: name: My Org
然后您可以在装饰器中使用:
@populate_with('file.yaml') def test_case_(self): 'My Org exists in system test anything here'
在测试用例中获取填充的实体:
@populate_with('file.yaml', context_name='my_context') def test_case_(self, my_context=None): assert my_context.organization_1.name == 'My Org' You can also set a customized context wrapper to the context_wrapper argument:: def my_custom_context_wrapper(result): # create an object using result my_context = MyResultContext(result) return my_context @populate_with('file.yaml', context_name='my_context', content_wrapper=my_custom_context_wrapper) def test_case_(self, my_context=None): # assert with some expression using my_context object returned # my_custom_context_wrapper assert some_expression
注意:
That is important that ``context`` argument always be declared using either a default value ``my_context=None`` or handle in ``**kwargs`` Otherwise ``py.test`` may try to use this as a fixture placeholder. if context_wrapper is set to None, my_context will be the pure unmodified result of populate function.
卫星版本
默认情况下,此代码准备对卫星latest版本运行 这意味着使用latest来自nailgun存储库的master。
如果需要在旧版本中运行此工具,例如:要tun升级测试,请 必须设置nailgun版本。
您有两个选项:
手动
在安装Satellite之前,填充安装特定的钉枪版本为 下表。
卫星6.1.x:
pip install -e git+https://github.com/SatelliteQE/nailgun.git@0.28.0#egg=nailgun pip install satellite-populate
卫星6.2.x:
pip install -e git+https://github.com/SatelliteQE/nailgun.git@6.2.z#egg=nailgun pip install satellite-populate
卫星6.3.x(最新):
pip install -e git+https://github.com/SatelliteQE/nailgun.git#egg=nailgun pip install satellite-populate
码头工人
如果需要在较旧的附属版本中运行satellite-populate,则可以 使用docker images,以便管理正确的钉枪版本 与特定的系统版本一起使用。
https://hub.docker.com/r/satelliteqe/satellite-populate/
第一次从Docker Hub中提取图像:
docker pull satelliteqe/satellite-populate:latest
将:latest更改为特定标记。例如::6.1或:6.2
测试:
^{公关20}$然后运行:
docker run -v $PWD:/datafiles satelliteqe/satellite-populate /datafiles/theoffice.yaml -v -h server.com
必须映射包含数据文件的本地文件夹
学分
这个包是用Cookiecutter和audreyr/cookiecutter-pypackage项目模板创建的。
历史记录
0.1.3(2017-01-13)
- Docker支持
0.1.2(2017-01-12)
- 修理装饰工。
0.1.0(2017-01-10)
- pypi上的第一个版本。