用Ansible Molecule测试剧本的失败

时间:2019-12-05 09:20:12

标签: ansible molecule

我正在创建 sudo 角色,并希望与Molecule一起测试,如果规则不正确,该角色将失败。

我们如何定义我们期望剧本因分子而失败?

例如,如果我具有以下配置:

sudo__entries:
  - name: super_alice
    content: "alice ALL NOPASSWD"

该角色将失败,因为visudo无法验证文件。

这就是我要测试的行为。

1 个答案:

答案 0 :(得分:0)

您可以修改rvest以使用抢救模块测试故障场景,并使用与此单元测试范例类似的方法:

converge.yml

角色try { foo(); Assert.fail(); } catch(FooException e) { // Caught expected exception from foo() } 的示例故障场景将有一个sudo,看起来像:

converge.yml

您还可以使用--- - name: Does not converge hosts: all tasks: - block: - name: "Include sudo" include_role: name: "sudo" register: expected_failure - name: "Check execution halted" fail: msg: "Execution should stop before this task" register: should_not_run rescue: - assert: that: - expected_failure is defined - should_not_run is not defined 来补充这一点,以断言故障情况并未使主机处于故障状态。

相关问题