Saturday, December 19, 2015

Testing with Ansible


IEEE Spectrum has recently posted an article about Yahoo resigning from QA team. After that manoeuvre quality of final product not only didn't degrade, but actually improved. Of course if we delve deeper into the article we will realise that the products got better after continuous delivery introduction (image credit to www.cloudofit.com) and QA team wasn't entirely to blame. However that's clear signal to old school testers that programming/scripting skills are now necessary.

One of my favourite tools which supports Continuous Delivery (or Deployment if we assume what deploy to production is automatic) is Ansible. This simple automation agent allows us to execute commands on external hosts via plays, which can later be organised to playbooks (list of plays). This is how example playbook looks like:
---
- hosts: droplets
  tasks:
    - name: Installs nginx web server
      apt: name=nginx force=yes update_cache=true

    - name: start nginx
      service: name=nginx state=started
What is this playbook doing?
a) It checks hosts file for [droplets] string and connects to IP addresses below. Hosts file example:
[droplets]
192.168.0.2
192.168.0.3
[webservers]
192.168.3.222
b) apt-get update is called (update_cache=true)
c) apt-get install nginx is called with automatic confirmation (name=nginx force=yes)
d) nginx service is started

With very few lines we were able to install necessary application on two hosts. Imagine how much of time you'll save if the number of machines would be bigger. Probably you have already realised how powerful Ansible can it be to testing, but let me give you few more examples:

1. Environment setup (see example above)
2. Updating app configuration on all testing servers
- hosts: test_machines
  tasks:
    - name: Update config
      template: src=config.xml.j2 dest={{ app_folder }}/config.xml
3. Jenkins / Selenium Grid / Selenium nodes auto configuration
4. Smoke tests after prod deployment
- hosts: PROD_machines
  tasks:
    - name: Check service
      service: name=vsftpd state=started
5. Running commands and checking output
- hosts: test_machines
  tasks:
    - name: Run X
      shell: usr/bin/somefancycommand
      register: cmd_result

    - name: assert results
      that: "not ready not in cmd_result.stderr"
And much, much more. Trust me, it's really simple and easy to learn :)

Free knowledge: