#!/usr/bin/ansible-playbook --module-path=../../../library/

---
- hosts: "{{ cli_master_test }}"
  gather_facts: no

  vars_prompt:
  - name: cli_master_test
    prompt: "Master to run against"
    private: false
    default: localhost

  vars:
    service_account_name: serviceaccount-int-test
    ns_name: default

  tasks:
  - name: Make sure we start clean - Arrange
    oc_serviceaccount:
      state: absent
      name: "{{ service_account_name }}"
      namespace: "{{ ns_name }}"

  - name: List when account does not exist - Act
    oc_serviceaccount:
      state: list
      name: "{{ service_account_name }}"
      namespace: "{{ ns_name }}"
    register: saout

  - name: List when account does not exist - Assert
    assert:
      that:
      - "saout.changed == False"
      - "saout.state == 'list'"
      - "saout.module_results == [{}]"

  - name: create serviceaccount - Act
    oc_serviceaccount:
      name: "{{ service_account_name }}"
      namespace: "{{ ns_name }}"
      secrets:
      - one
      - two
      - three
    register: saout

  - name: create serviceaccount - Assert
    assert:
      that:
      - "saout.changed == True"
      - "saout.state == 'present'"
      - "saout.module_results.returncode == 0"
      - "saout.module_results.results.0.metadata.name == '{{ service_account_name }}'"
      - "saout.module_results.results.0.metadata.namespace == '{{ ns_name }}'"

  - name: create serviceaccount - check idempotency - Act
    oc_serviceaccount:
      name: "{{ service_account_name }}"
      namespace: "{{ ns_name }}"
      secrets:
      - one
      - two
      - three
    register: saout

  - name: create serviceaccount - check idempotency - Assert
    assert:
      that:
      - "saout.changed == False"
      - "saout.state == 'present'"
      - "saout.module_results.returncode == 0"
      - "saout.module_results.results.0.metadata.name == '{{ service_account_name }}'"
      - "saout.module_results.results.0.metadata.namespace == '{{ ns_name }}'"

  - name: Delete serviceaccount - Act
    oc_serviceaccount:
      state: absent
      name: "{{ service_account_name }}"
      namespace: "{{ ns_name }}"
    register: saout

  - name: Delete serviceaccount - Assert
    assert:
      that:
      - "saout.changed == True"
      - "saout.state == 'absent'"
      - "saout.module_results.returncode == 0"

  - name: Delete serviceaccount - check idempotency - Act
    oc_serviceaccount:
      state: absent
      name: "{{ service_account_name }}"
      namespace: "{{ ns_name }}"
    register: saout

  - name: Delete serviceaccount - check idempotency - Assert
    assert:
      that:
      - "saout.changed == False"
      - "saout.state == 'absent'"
