#!/usr/bin/ansible-playbook --module-path=../../../library/
---
- hosts: "{{ cli_master_test }}"
  gather_facts: no
  user: root

  vars:
    namespace: default
    service_account_name: someserviceaccountname
    secret_name: somesecretname

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

  post_tasks:
  - name: create service account to test with - Arrange
    oc_serviceaccount:
      namespace: "{{ namespace }}"
      name: "{{ service_account_name }}"

  - name: create secret to test with - Arrange
    oc_secret:
      namespace: "{{ namespace }}"
      name: "{{ secret_name }}"
      contents:
      - path: blah
        data: blahdeblah

  - name: Ensure the service account and secret are not linked - Arrange
    oc_serviceaccount_secret:
      state: absent
      service_account: "{{ service_account_name }}"
      secret: "{{ secret_name }}"
      namespace: "{{ namespace }}"

  - name: get secrets of a service account - Act
    oc_serviceaccount_secret:
      state: list
      service_account: builder
      namespace: "{{ namespace }}"
    register: sasecretout

  - name: get secrets of a service account - Assert
    assert:
      that:
      - "sasecretout.changed == False"
      - "sasecretout.state == 'list'"
      - "sasecretout.results | length > 0"

  - name: Test linking a service account and secret - Act
    oc_serviceaccount_secret:
      service_account: "{{ service_account_name }}"
      secret: "{{ secret_name }}"
      namespace: "{{ namespace }}"
    register: sasecretout

  - name: Test linking a service account and secret - Assert
    assert:
      that:
      - "sasecretout.changed == True"
      - "sasecretout.state == 'present'"
      - "sasecretout.results.returncode == 0"
      - "sasecretout.results.results | length > 0"

  - name: Test linking a service account and secret - idempotency - Act
    oc_serviceaccount_secret:
      service_account: "{{ service_account_name }}"
      secret: "{{ secret_name }}"
      namespace: "{{ namespace }}"
    register: sasecretout

  - name: Test linking a service account and secret - idempotency - Assert
    assert:
      that:
      - "sasecretout.changed == False"
      - "sasecretout.state == 'present'"
