如何通过 Test-Kitchen 运行 对 ansible 角色进行幂等测试?

How to run idempotence test for ansible role via Test-Kitchen?

使用 Travis 非常简单-CI:

  # Run the role/playbook again, checking to make sure it's idempotent.
  - >
    ansible-playbook -i tests/inventory tests/test.yml --connection=local --sudo --extra-vars "take_ownership_of_tmp=$OWN_TMP"
    | grep -q 'changed=0.*failed=0'
    && (echo 'Idempotence test: pass' && exit 0)
    || (echo 'Idempotence test: fail' && exit 1)

但我不能使用 Travis,因为我需要在 Debian 系统上测试我的角色。我将 Test-Kitchen 与 ansible_playbook provisioner 一起使用。

例如:

我的.kitchen.yml

---
driver:
  name: vagrant

provisioner:
  name: ansible_playbook
  hosts: test-kitchen
  ansible_verbosity: 2
  ansible_verbose: true
  require_ansible_repo: false
  require_chef_omnibus: false
  require_ansible_omnibus: true
  require_chef_for_busser: false
  ansible_omnibus_url: https://raw.githubusercontent.com/neillturner/omnibus-ansible/master/ansible_install.sh

platforms:
  - name: debian-6.0.10-64-nocm
    driver_config: 
      customize:
           memory: 1024
           cpus: 2
      box: puppetlabs/debian-6.0.10-64-nocm
      box_url: https://atlas.hashicorp.com/puppetlabs/boxes/debian-6.0.10-64-nocm/versions/1.0.2/providers/virtualbox.box


suites:
  - name: default

verifier:
  ruby_bindir: '/usr/bin'

测试剧本(test/integration/default/default.yml)非常简单

---
- hosts: all
  roles:
    - preconf
  vars:
    #some vars here

我可以向 default.yml 添加第二次调用 preconf 角色,但这没有帮助。

一个电话厨房returns我改变了一些项目:

   PLAY RECAP ******************************************************************** 
   localhost                  : ok=12   changed=8    unreachable=0    failed=0  

但是有两个调用它 returns 项目的总和而不是两个单独的结果

   PLAY RECAP ******************************************************************** 
   localhost                  : ok=24   changed=10   unreachable=0    failed=0 

那么,我如何才能 运行 剧本第二次检查幂等性测试的结果?

自己通过 BATS 测试解决了问题:

#!/usr/bin/env bats
#

#
# Idempotence test
#

@test "Second run should change nothing" {
    run bash -c "ansible-playbook -i /tmp/kitchen/hosts /tmp/kitchen/default.yml -c local | grep -q 'changed=0.*failed=0' && exit 0 || exit 1"
    [ "$status" -eq 0 ]
}

UPD 服务器规格变体:

describe command('ansible-playbook -i /tmp/kitchen/hosts /tmp/kitchen/default.yml -c local') do
  its(:stderr) { should match /changed=0.*failed=0/ }

  its(:exit_status) { should eq 0 }
end

两个 ansible_playbook and ansible_push 厨房供应商现在都支持 idempotency_test 参数,它实际上是第二次运行剧本并检查 failedchanged 任务。

您可以像这样简单地将 idempotency_test: true 添加到您的 .kitchen.yml 中:

---
driver:
  name: vagrant

provisioner:
  name: ansible_playbook
  hosts: test-kitchen
  ansible_verbosity: 2
  ansible_verbose: true
  require_ansible_repo: false
  require_chef_omnibus: false
  require_ansible_omnibus: true
  require_chef_for_busser: false
  ansible_omnibus_url: https://raw.githubusercontent.com/neillturner/omnibus-ansible/master/ansible_install.sh
  idempotency_test: true

platforms:
  - name: debian-6.0.10-64-nocm
    driver_config: 
      customize:
           memory: 1024
           cpus: 2
      box: puppetlabs/debian-6.0.10-64-nocm
      box_url: https://atlas.hashicorp.com/puppetlabs/boxes/debian-6.0.10-64-nocm/versions/1.0.2/providers/virtualbox.box


suites:
  - name: default

verifier:
  ruby_bindir: '/usr/bin'