# GCE Tag Integration Tests.

## Parameter checking tests ##
# ============================================================
- name: "test missing param: instance_name"
  gce_tag:
    service_account_email: "{{ service_account_email }}"
    pem_file: "{{ pem_file }}"
    project_id: "{{ project_id }}"
  register: result
  ignore_errors: true
  tags:
    - param-check

- name: "assert failure when param: instance_name missing"
  assert:
    that:
       - 'result.failed'
       - 'result.msg == "missing required arguments: instance_name"'


# ============================================================
- name: "test missing param: tags"
  gce_tag:
    service_account_email: "{{ service_account_email }}"
    pem_file: "{{ pem_file }}"
    project_id: "{{ project_id }}"
    zone: "{{ zone }}"
    instance_name: "{{ instance_name }}"
  register: result
  ignore_errors: true
  tags:
    - param-check

- name: "assert failure when param: tags missing"
  assert:
    that:
       - 'result.failed'
       - 'result.msg == "Must specify \"tags\""'


## Non-existant instance tests ##
# # ============================================================
- name: "test tag non-existant instance (state==present)"
  gce_tag:
    service_account_email: "{{ service_account_email }}"
    pem_file: "{{ pem_file }}"
    project_id: "{{ project_id }}"
    instance_name: "blahblahblah123"
    zone: "{{ zone }}"
    state: present
    tags: foo,bar
  register: result
  ignore_errors: true

- name: "assert tag failed"
  assert:
    that:
       - 'result.failed'

# # ============================================================
- name: "test tag non-existant instance (state==absent)"
  gce_tag:
    service_account_email: "{{ service_account_email }}"
    pem_file: "{{ pem_file }}"
    project_id: "{{ project_id }}"
    instance_name: "blahblahblah123"
    zone: "{{ zone }}"
    state: absent
    tags: foo,bar
  register: result
  ignore_errors: true

- name: "assert tag failed"
  assert:
    that:
       - 'result.failed'
       
## tagging tests ##
# # ============================================================
- name: "test tags added (state==present)"
  gce_tag:
    service_account_email: "{{ service_account_email }}"
    pem_file: "{{ pem_file }}"
    project_id: "{{ project_id }}"
    instance_name: "{{ instance_name }}"
    zone: "{{ zone }}"
    tags: foo,bar
    state: present
  register: result

- name: "assert tag successful"
  assert:
    that:
       - 'result.changed'
       - 'result.tags|length == 2'
       - 'result.tags == ["foo", "bar"]'
       
# # ============================================================
- name: "add existing tags, no change (state==present)"
  gce_tag:
    service_account_email: "{{ service_account_email }}"
    pem_file: "{{ pem_file }}"
    project_id: "{{ project_id }}"
    instance_name: "{{ instance_name }}"
    zone: "{{ zone }}"
    tags: foo,bar
    state: present
  register: result

- name: "assert tag successful"
  assert:
    that:
       - 'result.changed == False'
       - 'result.tags == None'

# # ============================================================
- name: "test tags removed from instance (state==absent)"
  gce_tag:
    service_account_email: "{{ service_account_email }}"
    pem_file: "{{ pem_file }}"
    project_id: "{{ project_id }}"
    instance_name: "{{ instance_name }}"
    zone: "{{ zone }}"
    tags: foo,bar
    state: absent
  register: result

- name: "assert tags removed"
  assert:
    that:
       - 'result.changed'
       - 'result.tags == ["foo", "bar"]'
