---

# ============================================================
- name: test with no parameters
  action: "{{module_name}}"
  register: result
  ignore_errors: true

- name: assert failure when called with no parameters
  assert:
    that:
       - 'result.failed'
       - 'result.msg == "missing required arguments: name"'

# ============================================================
- name: test with only name
  action: "{{module_name}} name={{ec2_key_name}}"
  register: result
  ignore_errors: true

- name: assert failure when called with only 'name'
  assert:
    that:
       - 'result.failed'
       - 'result.msg == "Either region or ec2_url must be specified"'

# ============================================================
- name: test invalid region parameter
  action: "{{module_name}} name='{{ec2_key_name}}' region='asdf querty 1234'"
  register: result
  ignore_errors: true

- name: assert invalid region parameter
  assert:
    that:
       - 'result.failed'
       - 'result.msg.startswith("value of region must be one of:")'

# ============================================================
- name: test valid region parameter
  action: "{{module_name}} name='{{ec2_key_name}}' region='{{ec2_region}}'"
  register: result
  ignore_errors: true

- name: assert valid region parameter
  assert:
    that:
       - 'result.failed'
       - 'result.msg.startswith("No handler was ready to authenticate.")'

# ============================================================
- name: test environment variable EC2_REGION
  action: "{{module_name}} name='{{ec2_key_name}}'"
  environment:
    EC2_REGION: '{{ec2_region}}'
  register: result
  ignore_errors: true

- name: assert environment variable EC2_REGION
  assert:
    that:
       - 'result.failed'
       - 'result.msg.startswith("No handler was ready to authenticate.")'

# ============================================================
- name: test invalid ec2_url parameter
  action: "{{module_name}} name='{{ec2_key_name}}'"
  environment:
    EC2_URL: bogus.example.com
  register: result
  ignore_errors: true

- name: assert invalid ec2_url parameter
  assert:
    that:
       - 'result.failed'
       - 'result.msg.startswith("No handler was ready to authenticate.")'

# ============================================================
- name: test valid ec2_url parameter
  action: "{{module_name}} name='{{ec2_key_name}}'"
  environment:
    EC2_URL: '{{ec2_url}}'
  register: result
  ignore_errors: true

- name: assert valid ec2_url parameter
  assert:
    that:
       - 'result.failed'
       - 'result.msg.startswith("No handler was ready to authenticate.")'

# ============================================================
- name: test credentials from environment
  action: "{{module_name}} name='{{ec2_key_name}}'"
  environment:
    EC2_REGION: '{{ec2_region}}'
    EC2_ACCESS_KEY: bogus_access_key
    EC2_SECRET_KEY: bogus_secret_key
  register: result
  ignore_errors: true

- name: assert ec2_key with valid ec2_url
  assert:
    that:
       - 'result.failed'
       - '"EC2ResponseError: 401 Unauthorized" in result.msg'

# ============================================================
- name: test credential parameters
  action: "{{module_name}} name='{{ec2_key_name}}' ec2_region='{{ec2_region}}' ec2_access_key=bogus_access_key ec2_secret_key=bogus_secret_key"
  register: result
  ignore_errors: true

- name: assert credential parameters
  assert:
    that:
       - 'result.failed'
       - '"EC2ResponseError: 401 Unauthorized" in result.msg'