# Test launchpad bug https://bugs.launchpad.net/nova/+bug/1779818

fixtures:
    - APIFixture

defaults:
    request_headers:
        x-auth-token: admin
        accept: application/json
        openstack-api-version: placement latest

tests:

- name: post a resource provider as alt_parent
  POST: /resource_providers
  request_headers:
      content-type: application/json
  data:
      name: alt_parent
      uuid: $ENVIRON['ALT_PARENT_PROVIDER_UUID']
  status: 200
  response_json_paths:
      $.uuid: $ENVIRON['ALT_PARENT_PROVIDER_UUID']
      $.name: alt_parent
      $.parent_provider_uuid: null
      $.generation: 0

- name: post another resource provider as parent
  POST: /resource_providers
  request_headers:
      content-type: application/json
  data:
      name: parent
      uuid: $ENVIRON['PARENT_PROVIDER_UUID']
  status: 200
  response_json_paths:
      $.uuid: $ENVIRON['PARENT_PROVIDER_UUID']
      $.name: parent
      $.parent_provider_uuid: null
      $.generation: 0

- name: post a child resource provider of the parent
  POST: /resource_providers
  request_headers:
      content-type: application/json
  data:
      name: child
      uuid: $ENVIRON['RP_UUID']
      parent_provider_uuid: $ENVIRON['PARENT_PROVIDER_UUID']
  status: 200
  response_json_paths:
      $.uuid: $ENVIRON['RP_UUID']
      $.name: child
      $.parent_provider_uuid: $ENVIRON['PARENT_PROVIDER_UUID']
      $.generation: 0

# Let's validate that now we have two tree structures
#  * alt_parent
#  * parent
#      |
#      +-- child
- name: list all resource providers
  GET: /resource_providers
  response_json_paths:
      $.resource_providers.`len`: 3
      $.resource_providers[?uuid="$ENVIRON['ALT_PARENT_PROVIDER_UUID']"].root_provider_uuid: $ENVIRON['ALT_PARENT_PROVIDER_UUID']
      $.resource_providers[?uuid="$ENVIRON['ALT_PARENT_PROVIDER_UUID']"].parent_provider_uuid: null
      $.resource_providers[?uuid="$ENVIRON['PARENT_PROVIDER_UUID']"].root_provider_uuid: $ENVIRON['PARENT_PROVIDER_UUID']
      $.resource_providers[?uuid="$ENVIRON['PARENT_PROVIDER_UUID']"].parent_provider_uuid: null
      $.resource_providers[?uuid="$ENVIRON['RP_UUID']"].root_provider_uuid: $ENVIRON['PARENT_PROVIDER_UUID']
      $.resource_providers[?uuid="$ENVIRON['RP_UUID']"].parent_provider_uuid: $ENVIRON['PARENT_PROVIDER_UUID']

# Let's re-parent the parent to the alternative parent
# so that we have only one tree.
#  * alt_parent
#        |
#        +-- parent
#              |
#              +-- child
- name: update a parent of the parent
  PUT: /resource_providers/$ENVIRON['PARENT_PROVIDER_UUID']
  request_headers:
      content-type: application/json
  data:
      name: parent
      parent_provider_uuid: $ENVIRON['ALT_PARENT_PROVIDER_UUID']
  status: 200

# Let's validate that we have only one root provider now
- name: list all resource providers updated
  GET: /resource_providers
  response_json_paths:
      $.resource_providers.`len`: 3
      $.resource_providers[?uuid="$ENVIRON['ALT_PARENT_PROVIDER_UUID']"].root_provider_uuid: $ENVIRON['ALT_PARENT_PROVIDER_UUID']
      $.resource_providers[?uuid="$ENVIRON['ALT_PARENT_PROVIDER_UUID']"].parent_provider_uuid: null
      $.resource_providers[?uuid="$ENVIRON['PARENT_PROVIDER_UUID']"].root_provider_uuid: $ENVIRON['ALT_PARENT_PROVIDER_UUID']
      $.resource_providers[?uuid="$ENVIRON['PARENT_PROVIDER_UUID']"].parent_provider_uuid: $ENVIRON['ALT_PARENT_PROVIDER_UUID']
      $.resource_providers[?uuid="$ENVIRON['RP_UUID']"].root_provider_uuid: $ENVIRON['ALT_PARENT_PROVIDER_UUID']
      $.resource_providers[?uuid="$ENVIRON['RP_UUID']"].parent_provider_uuid: $ENVIRON['PARENT_PROVIDER_UUID']

- name: list all resource providers in a tree with the child
  GET: /resource_providers?in_tree=$ENVIRON['RP_UUID']
  response_json_paths:
      $.resource_providers.`len`: 3
      $.resource_providers[?uuid="$ENVIRON['RP_UUID']"].root_provider_uuid: $ENVIRON['ALT_PARENT_PROVIDER_UUID']
      $.resource_providers[?uuid="$ENVIRON['ALT_PARENT_PROVIDER_UUID']"].root_provider_uuid: $ENVIRON['ALT_PARENT_PROVIDER_UUID']
      $.resource_providers[?uuid="$ENVIRON['PARENT_PROVIDER_UUID']"].root_provider_uuid: $ENVIRON['ALT_PARENT_PROVIDER_UUID']

- name: list all resource providers in a tree with the parent
  GET: /resource_providers?in_tree=$ENVIRON['PARENT_PROVIDER_UUID']
  response_json_paths:
      $.resource_providers.`len`: 3
      $.resource_providers[?uuid="$ENVIRON['RP_UUID']"].root_provider_uuid: $ENVIRON['ALT_PARENT_PROVIDER_UUID']
      $.resource_providers[?uuid="$ENVIRON['ALT_PARENT_PROVIDER_UUID']"].root_provider_uuid: $ENVIRON['ALT_PARENT_PROVIDER_UUID']
      $.resource_providers[?uuid="$ENVIRON['PARENT_PROVIDER_UUID']"].root_provider_uuid: $ENVIRON['ALT_PARENT_PROVIDER_UUID']

- name: list all resource providers in a tree with the alternative parent
  GET: /resource_providers?in_tree=$ENVIRON['ALT_PARENT_PROVIDER_UUID']
  response_json_paths:
      $.resource_providers.`len`: 3
      $.resource_providers[?uuid="$ENVIRON['RP_UUID']"].root_provider_uuid: $ENVIRON['ALT_PARENT_PROVIDER_UUID']
      $.resource_providers[?uuid="$ENVIRON['ALT_PARENT_PROVIDER_UUID']"].root_provider_uuid: $ENVIRON['ALT_PARENT_PROVIDER_UUID']
      $.resource_providers[?uuid="$ENVIRON['PARENT_PROVIDER_UUID']"].root_provider_uuid: $ENVIRON['ALT_PARENT_PROVIDER_UUID']
