#!/usr/bin/ansible-playbook --module-path=../../../library/
## ./oc_configmap.yml -M ../../../library -e "cli_master_test=$OPENSHIFT_MASTER
---
- hosts: "{{ cli_master_test }}"
  gather_facts: no
  user: root
  vars:
    filename: /tmp/test_configmap_from_file

  post_tasks:
  - name: Setup a file with known contents
    copy:
      content: This is a file
      dest: "{{ filename }}"

  - name: create a test project
    oc_project:
      name: test
      description: for tests only

  ###### create test ###########
  - name: create a configmap
    oc_configmap:
      state: present
      name: configmaptest
      namespace: test
      from_file:
        config: "{{ filename }}"
      from_literal:
        foo: bar

  - name: fetch the created configmap
    oc_configmap:
      name: configmaptest
      state: list
      namespace: test
    register: cmout

  - debug: var=cmout

  - name: assert configmaptest exists
    assert:
      that:
      - cmout.results.results[0].metadata.name == 'configmaptest'
      - cmout.results.results[0].data.foo == 'bar'
  ###### end create test ###########

  ###### update test ###########
  - name: create a configmap
    oc_configmap:
      state: present
      name: configmaptest
      namespace: test
      from_file:
        config: "{{ filename }}"
      from_literal:
        foo: notbar
        deployment_type: openshift-enterprise

  - name: fetch the updated configmap
    oc_configmap:
      name: configmaptest
      state: list
      namespace: test
    register: cmout

  - debug: var=cmout

  - name: assert configmaptest exists
    assert:
      that:
      - cmout.results.results[0].metadata.name == 'configmaptest'
      - cmout.results.results[0].data.deployment_type == 'openshift-enterprise'
      - cmout.results.results[0].data.foo == 'notbar'
  ###### end update test ###########

  ###### delete test ###########
  - name: delete a configmap
    oc_configmap:
      state: absent
      name: configmaptest
      namespace: test

  - name: fetch the updated configmap
    oc_configmap:
      name: configmaptest
      state: list
      namespace: test
    register: cmout

  - debug: var=cmout

  - name: assert configmaptest exists
    assert:
      that: "'\"configmaptest\" not found' in cmout.results.stderr"
