---
- name: Test using existing binding
  hosts: all
  tasks:
    - name: Test 1 - initial binding performed by nbde_client role
      block:
        - name: Set up test environment
          include_tasks: tasks/setup_test.yml

        - name: Add binding to slot 1
          include_role:
            name: linux-system-roles.nbde_client
          vars:
            nbde_client_bindings:
              - device: "{{ nbde_client_test_device }}"
                encryption_password: "{{ nbde_client_test_pass }}"
                slot: 1
                servers:
                  - http://localhost

        - name: Add binding to slot 2 without providing encryption_password
          include_role:
            name: linux-system-roles.nbde_client
          vars:
            nbde_client_bindings:
              - device: "{{ nbde_client_test_device }}"
                slot: 2
                servers:
                  - http://localhost

        - name: Remove binding from slot 1
          include_role:
            name: linux-system-roles.nbde_client
          vars:
            nbde_client_bindings:
              - device: "{{ nbde_client_test_device }}"
                slot: 1
                state: absent

        - name: Attempt to unlock device
          include_tasks: tasks/verify_unlock_device.yml

        - name: Make sure the attempt to unlock succeeded
          assert:
            that:
              - not nbde_client_unlock.failed
              - not nbde_client_close.failed

      always:
        - name: Clean up test environment
          include_tasks: tasks/cleanup_test.yml

    - name: Test 2 - initial binding performed by clevis
      block:
        - name: Set up test environment
          include_tasks: tasks/setup_test.yml

        - name: Create temp file for the advertisement
          tempfile:
            suffix: adv
          register: nbde_client_temp_adv

        - name: Make sure temp file was created
          fail:
            msg: Temp file for advertisement was not created
          when: nbde_client_temp_adv is not defined

        - name: Download advertisement
          get_url:
            url: http://localhost/adv
            force: true
            dest: "{{ nbde_client_temp_adv.path }}"
            mode: "0600"

        - name: Add binding to slot 1
          shell: >
            clevis luks bind -f -d {{ nbde_client_test_device }} tang
            '{"url": "http://localhost",
            "adv": "{{ nbde_client_temp_adv.path }}"}'
            <<< {{ nbde_client_test_pass }}
          changed_when: false

        - name: Remove temp advertisement file
          file:
            path: "{{ nbde_client_temp_adv.path }}"
            state: absent

        - name: Add binding to slot 2 without providing encryption_password
          include_role:
            name: linux-system-roles.nbde_client
          vars:
            nbde_client_bindings:
              - device: "{{ nbde_client_test_device }}"
                slot: 2
                servers:
                  - http://localhost

        - name: Remove binding from slot 1
          include_role:
            name: linux-system-roles.nbde_client
          vars:
            nbde_client_bindings:
              - device: "{{ nbde_client_test_device }}"
                slot: 1
                state: absent

        - name: Attempt to unlock device
          include_tasks: tasks/verify_unlock_device.yml

        - name: Make sure the attempt to unlock succeeded
          assert:
            that:
              - not nbde_client_unlock.failed
              - not nbde_client_close.failed

      always:
        - name: Clean up test environment
          include_tasks: tasks/cleanup_test.yml

# vim:set ts=2 sw=2 et:
