跳转到主要内容

使用YAML填充和验证系统

项目描述

Satellite-Populate

https://img.shields.io/pypi/v/satellite-populate.svg https://img.shields.io/travis/SatelliteQE/satellite-populate.svg Documentation Status Updates

使用YAML填充和验证系统

安装

安装最新发布版本

pip install satellite-populate

从github master分支安装

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操作,如 创建删除更新,如果 action: 未定义,默认为 创建

此外还有 特殊操作自定义操作,将在后面进行说明。

使用实体填充Satellite

考虑到上述 office.yaml 文件,您可以使用命令行填充卫星系统

$ satellite-populate office.yaml -h yourserver.com --output=office.yaml -v

在上面的命令行中,-h 表示 --hostname--output 是输出文件,该文件将被写入以用于验证系统,而 -v 是详细程度。

要查看可用参数的列表,请运行

# satellite-populate --help

验证系统是否具有实体

一旦运行 satellite-populate,您可以使用输出的文件来验证系统。由于所有输出文件都命名为 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 模块,并且已经在 PYTHONPATH 中正确设置

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

测试用例的装饰器

拥有如下数据文件

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.

卫星版本

默认情况下,此代码是为运行卫星 最新 版本而准备的,这意味着使用 最新 的来自 nailgun 仓库的 master。

如果您需要在此工具中运行旧版本,例如:运行升级测试,您必须设置 nailgun 版本。

您有两个选项

手动

在安装 satellite-populate 之前,安装以下列表中的特定 nailgun 版本。

  • 卫星 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

Docker

如果您需要在使用旧版本的卫星中运行 satellite-populate,您可以使用 docker images,这样它将管理与特定系统版本一起使用的正确 nailgun 版本。

https://hub.docker.com/r/satelliteqe/satellite-populate/

首先从 Docker Hub 拉取镜像

docker pull satelliteqe/satellite-populate:latest

:latest 更改为特定标签。例如::6.1:6.2

测试它

docker run satelliteqe/satellite-populate --test

然后运行

docker run -v $PWD:/datafiles satelliteqe/satellite-populate /datafiles/theoffice.yaml -v -h server.com

您必须映射包含数据文件的本地文件夹

致谢

此包是用 Cookiecutteraudreyr/cookiecutter-pypackage 项目模板创建的。

历史

0.1.3 (2017-01-13)

  • Docker 支持

0.1.2 (2017-01-12)

  • 修复装饰器。

0.1.0 (2017-01-10)

  • 首次发布于 PyPI。

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。

源分布

satellite_populate-0.1.3.tar.gz (36.2 kB 查看哈希值)

上传时间: 源代码

构建发行版

satellite_populate-0.1.3-py2.py3-none-any.whl (26.9 kB 查看哈希值)

上传时间: Python 2 Python 3