kyetter / ansible-devspace-demo

17 stars 42 forks source link

Update README for TDD demo with molecule on backup_file role #16

Open jeffcpullen opened 1 year ago

jeffcpullen commented 1 year ago

The backup_file role is setup nicely to demonstrate a simple test driven development example by adding verification to the role.

This would involve adding code to the molecule/default/verify.yml file like this.

---
- name: Verify
  hosts: all
  connection: community.okd.oc
  gather_facts: false
  tasks:

  - name: Check if backup file was created
    ansible.builtin.stat:
      path: "{{ backup_file_destination_path }}"
    register: __backup_file_verify_file

  - name: Debug backup file variable
    ansible.builtin.debug:
      var: __backup_file_verify_file

  - name: Example assertion
    ansible.builtin.assert:
      that:
        - __backup_file_verify_file.stat.exists

This assumes that the backup_file_destination_path is defined. The playbook will fail if this isn't defined (which is another verification that could be added). Or it can be defined in the molecule.yml file under the group_vars.

Running molecule test or molecule verify if the molecule create && molecule converge has already been run.

To have this test pass the role could be updated to include tasks like this.

---
- name: Backup file
  ansible.builtin.copy:
    src: "{{ backup_file_source_path }}"
    dest: "{{ backup_file_destination_path }}"
    mode: '0600'
    remote_src: yes

backup_file/tasks/verify.yml

We will also need to make sure that backup_file_source_path is provided same as the backup_file_destination_path mentioned above.