2

我正在创建一个sudo角色,并想用 Molecule 测试如果规则不正确,该角色会失败。

我们如何定义我们期望剧本在 Molecule 上失败?

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

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

该角色将失败,因为visudo不会验证文件。

这就是我要测试的行为。

4

1 回答 1

1

您可以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

您还可以用 a 来补充它,verify.yml以断言故障场景没有使主机处于损坏状态。

于 2020-03-13T23:09:36.310 回答