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

时间:2015-09-09 07:36:45

标签: ansible ansible-playbook 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系统上测试我的角色。我使用带有ansible_playbook配置程序的Test-Kitchen。

例如:

我的.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 playbook(test / integration / default / default.yml)非常简单

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

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

通过一个致电厨房返回一些更改的项目:

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

但是通过两次调用,它返回项目总和而不是两个单独的结果

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

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

2 个答案:

答案 0 :(得分:4)

用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 Serverspec变体:

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

答案 1 :(得分:1)

现在,ansible_playbookansible_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'