如何在多个AnsibleAssert上运行JUnit测试

s6fujrry  于 2022-11-11  发布在  其他
关注(0)|答案(2)|浏览(138)

我有一个GitLab管道,在一个容器中运行一些Ansible剧本。最后,我想运行一些测试,以确保一切都正确配置,例如:

- name: Get the Frobzy value
  raw: "frobzy -x foobar"
  register: frobzy_val
  changed_when: false

- name: Check that Frobzy is valid
  assert:
    that:
      - frobzy_val.stdout is match "oogah"
    fail_msg: "bad frobzy"
  ignore_errors: true # see comments

- name: Get the Blobzy value
  raw: "blobzy -y barfoo"
  register: blobzy_val
  changed_when: false

- name: Check that Blobzy is valid
  assert:
    that:
      - blobzy_val.stdout is match "warra"
    fail_msg: "bad blobzy"
  ignore_errors: true # see comments

这一切都经过了很好的配置,因此我得到了一个JUnit报告,它显示在我的GitLab管道结果中。
问题是,假设这两个测试都失败了。如果我查看我的管道的测试结果,它正确地显示测试失败了。然而管道本身却标记为已通过!如果我没有仔细查看测试结果,我会认为一切都很顺利,并合并了一些可能损坏的代码。
管道被标记为成功的原因是因为那些ignore_errors: true设置。它注册了一个错误,但是遵循了这样做不应该使管道失败的指令。但是如果我设置ignore_errors: false(这是默认行为),则一旦Frobzy测试失败,流水线将中止并显示Frobzy测试失败,但由于它从未运行Blobzy测试,我不知道Blobzy也会失败。
基本上,我想要一种方法来运行我的所有AnsibleAssert,即使其中一些失败;我希望看到我的管道中的 * 所有 * 测试结果,并且如果 * 任何 * 测试失败,我希望管道 * 失败 *。
我该怎么做?

xv8emn3q

xv8emn3q1#

  • 这里有一个快速的解决方案,可能会有所帮助。
  • 设置一个任务来检查是否有任务失败。在这种情况下,将failed变量设置为true
  • 如果此变量设置为true,则行动手册将在结尾处失败。
- hosts: localhost
  vars:
    failed: false
  gather_facts: no
  tasks:

  - name: Get the Frobzy value
    raw: "echo frobzy"
    register: frobzy_val
    changed_when: false

  - name: Check that Frobzy is valid
    assert:
      that:
        - frobzy_val.stdout is match "frobzy"
      fail_msg: "bad frobzy"
    ignore_errors: true # see comments

  - name: Get the Blobzy value
    raw: "echo blobzy"
    register: blobzy_val
    changed_when: false

  - name: Check that Blobzy is valid
    assert:
      that:
        - blobzy_val.stdout is match "blobzy"
      fail_msg: "bad blobzy"
    ignore_errors: true # see comments

  - name: Set a variable to true if the task fails
    set_fact:
      failed: true
    ignore_errors: true
    when: not ("'blobzy' in blobzy_val.stdout" ) or 
          not ("'frobzy' in frobzy_val.msg")

  - name: Fail if failed is true
    fail:
      msg: "Failed"
    when: failed
  • 执行
─ ansible-playbook test.yaml 

PLAY [localhost]********************************************************************************************************************************************************************************

TASK [Get the Frobzy value]*********************************************************************************************************************************************************************
ok: [localhost]

TASK [Check that Frobzy is valid]***************************************************************************************************************************************************************
ok: [localhost] => {
    "changed": false,
    "msg": "All assertions passed"
}

TASK [Get the Blobzy value]*********************************************************************************************************************************************************************
ok: [localhost]

TASK [Check that Blobzy is valid]***************************************************************************************************************************************************************
ok: [localhost] => {
    "changed": false,
    "msg": "All assertions passed"
}

TASK [Set a variable to true if the task fails]*************************************************************************************************************************************************
skipping: [localhost]

TASK [Fail if failed is true]*******************************************************************************************************************************************************************
skipping: [localhost]

PLAY RECAP**************************************************************************************************************************************************************************************
localhost                  : ok=4    changed=0    unreachable=0    failed=0    skipped=2    rescued=0    ignored=0
ylamdve6

ylamdve62#

  • 对这两个条件都进行一次Assert,并将其延迟到最后,以便在完成两个测试后失败。
  • 添加调试任务以显示管道的结果。
- name: Get the Frobzy value
  raw: "frobzy -x foobar"
  register: frobzy_val
  changed_when: false

- name: Get the Blobzy value
  raw: "blobzy -y barfoo"
  register: blobzy_val
  changed_when: false

- name: Show results for Frobzy and Blobzy
  debug:
    msg:
      - "Frobzy is {{ 'ok' if frobzy_val.stdout is match 'oogah' else 'bad' }}"
      - "Blobzy is {{ 'ok' if lobzy_val.stdout is match 'warra' else 'bad' }}"

- name: Fail if any test is bad
  assert:
    that:
      - frobzy_val.stdout is match "oogah"
      - blobzy_val.stdout is match "warra"
    fail_msg: "At least one of Frobzy or Blobzy has bad results. See debug above"
``

相关问题