# GCE Managed Instance Group Integration Tests.
# Note: 'pause' is used to ensure the instances are available
# and the status checks are correct.  MIGs and their respective VMs
# will still be created/updated/deleted without the use of the pause module.

## Parameter checking tests ##
# ============================================================
- name: "test missing name"
  gce_mig:
    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 called without required params"
  assert:
    that:
       - 'result.failed'
       - 'result.msg == "missing required arguments: name,zone"'


# ============================================================
- name: "test missing param: size on creation"
  gce_mig:
    service_account_email: "{{ service_account_email }}"
    pem_file: "{{ pem_file }}"
    project_id: "{{ project_id }}"
    name: my-mig
    zone: "{{ zone }}"
    template: "{{ instance_template_name }}"
  register: result
  ignore_errors: true
  tags:
    - param-check

- name: "assert failure when param: size not specified"
  assert:
    that:
       - 'result.failed'
       - 'result.msg == "size must be of type: <type ''int''>"'

# ============================================================
- name: "test missing param: template on creation"
  gce_mig:
    service_account_email: "{{ service_account_email }}"
    pem_file: "{{ pem_file }}"
    project_id: "{{ project_id }}"
    name: my-mig
    zone: "{{ zone }}"
    size: 1
  register: result
  ignore_errors: true
  tags:
    - param-check

- name: "assert failure when param: template not specified"
  assert:
    that:
       - 'result.failed'
       - 'result.msg == "template must be of type: <type ''str''>"'

# ============================================================
- name: "test missing param: autoscaling.policy.max_instances"
  gce_mig:
    service_account_email: "{{ service_account_email }}"
    pem_file: "{{ pem_file }}"
    project_id: "{{ project_id }}"
    name: my-mig
    zone: "{{ zone }}"
    size: 1
    autoscaling:
      name: my-test-autoscaler
      enabled: yes
      policy:
        min_instances: 7
  register: result
  ignore_errors: true
  tags:
    - param-check

- name: "assert failure when param: max_instances not specified"
  assert:
    that:
       - 'result.failed'
       - 'result.msg == "max_instances is required and must be of type: <type ''int''>"'

# ============================================================
- name: "test missing param: autoscaling.policy"
  gce_mig:
    service_account_email: "{{ service_account_email }}"
    pem_file: "{{ pem_file }}"
    project_id: "{{ project_id }}"
    name: my-mig
    zone: "{{ zone }}"
    size: 1
    autoscaling:
      name: my-test-autoscaler
      enabled: yes
  register: result
  ignore_errors: true
  tags:
    - param-check

- name: "assert failure when param: policy not specified"
  assert:
    that:
       - 'result.failed'
       - 'result.msg == "policy is required and must be of type: <type ''dict''>"'

## MIG allocation tests ##
# ============================================================
- name: "test create MIG (state==present)"
  gce_mig:
    service_account_email: "{{ service_account_email }}"
    pem_file: "{{ pem_file }}"
    project_id: "{{ project_id }}"
    name: "{{ mig_name }}"
    zone: "{{ zone }}"
    state: present
    size: 1
    template: "{{ instance_template_name }}"
  register: result

- name: "assert MIG creation successful"
  assert:
    that:
       - 'result.changed == True'
       - 'result.created_instances|length == 1'
       - 'result.size == 1'

# ============================================================
- name: "test create Autoscaler"
  gce_mig:
    service_account_email: "{{ service_account_email }}"
    pem_file: "{{ pem_file }}"
    project_id: "{{ project_id }}"
    name: "{{ mig_name }}"
    zone: "{{ zone }}"
    state: present
    autoscaling:
      enabled: yes
      name: "{{ mig_name }}"
      policy:
        min_instances: 1
        max_instances: 3
        cool_down_period: 17
        cpu_utilization:
          target: .19
        load_balancing_utilization:
          target: 0.2
  register: result

- name: "assert Autoscaler created"
  assert:
    that:
       - 'result.changed == True'
       - 'result.created_autoscaler == True'
       - 'result.size == 1'
       - 'result.autoscaling.policy.max_instances == 3'
       - 'result.autoscaling.policy.cpu_utilization.target == 0.19'
       - 'result.autoscaling.policy.load_balancing_utilization.target == 0.2'

# ============================================================
- pause: seconds=30
# ============================================================
- name: "test resize MIG (state==present)"
  gce_mig:
    service_account_email: "{{ service_account_email }}"
    pem_file: "{{ pem_file }}"
    project_id: "{{ project_id }}"
    name: "{{ mig_name }}"
    zone: "{{ zone }}"
    state: present
    size: 3
    template: "{{ instance_template_name }}"
  register: result

- name: "assert MIG resize successful"
  assert:
    that:
       - 'result.changed == True'
       - 'result.resize_created_instances|length == 2'
       - 'result.size == 3'
# ============================================================
- pause: seconds=30
# ============================================================
- name: "test recreate instances in MIG (state==present)"
  gce_mig:
    service_account_email: "{{ service_account_email }}"
    pem_file: "{{ pem_file }}"
    project_id: "{{ project_id }}"
    name: "{{ mig_name }}"
    zone: "{{ zone }}"
    state: present
    size: 3
    recreate_instances: yes
    template: "{{ instance_template_name }}"
  register: result

- name: "assert MIG recreate successful"
  assert:
    that:
       - 'result.changed == True'
       - 'result.recreated_instances|length == 3'
       - 'result.size == 3'
# ============================================================
- pause: seconds=60
# ============================================================
- name: "test delete MIG and Autoscaler"
  gce_mig:
    service_account_email: "{{ service_account_email }}"
    pem_file: "{{ pem_file }}"
    project_id: "{{ project_id }}"
    name: "{{ mig_name }}"
    zone: "{{ zone }}"
    state: absent
    autoscaling:
      enabled: yes
      name: "{{ mig_name }}"
      policy:
        max_instances: 3
  register: result

- name: "assert MIG and Autoscaler deleted"
  assert:
    that:
       - 'result.changed == True'
       - 'result.deleted_autoscaler == True'
       - 'result.deleted_instances|length == 3'
