fixtures:
    - AllocationFixture

defaults:
    request_headers:
        x-auth-token: admin
        accept: application/json
        content-type: application/json
        openstack-api-version: placement 1.28
#
# Scenarios to test
# Start with no consumers
#    old, no CG = success, consumer gets created
#    new, no CG = fail, due to schema
#    new, CG=None = success, consumer gets created
#    new, CG=<any> = fail
# Create an allocation, and with it, a consumer
# Now create another allocation
#    old, no CG = success
#    new, CG=None = fail
#    new, CG !match = fail
#    new, get CG from /allocations
#    new, CG matches = success

tests:

- name: old version get nonexistent
  GET: /allocations/11111111-1111-1111-1111-111111111111
  request_headers:
      openstack-api-version: placement 1.27
  response_json_paths:
      # This is the entire response. There is no generation or proj/user id.
      $:
        allocations: {}

- name: new version get nonexistent
  GET: /allocations/22222222-2222-2222-2222-222222222222
  response_json_paths:
      # This is the entire response. There is no generation or proj/user id.
      $:
        allocations: {}

- name: old version no gen no existing
  PUT: /allocations/11111111-1111-1111-1111-111111111111
  request_headers:
      openstack-api-version: placement 1.27
  data:
      allocations:
          $ENVIRON['RP_UUID']:
            resources:
              DISK_GB: 10
      project_id: $ENVIRON['PROJECT_ID']
      user_id: $ENVIRON['USER_ID']
  status: 204

- name: new version no gen no existing
  PUT: /allocations/22222222-2222-2222-2222-222222222222
  data:
      allocations:
          $ENVIRON['RP_UUID']:
            resources:
              DISK_GB: 10
      project_id: $ENVIRON['PROJECT_ID']
      user_id: $ENVIRON['USER_ID']
  status: 400
  response_strings:
    - JSON does not validate

- name: new version gen is not null no existing
  PUT: /allocations/22222222-2222-2222-2222-222222222222
  data:
      allocations:
          $ENVIRON['RP_UUID']:
            resources:
              DISK_GB: 10
      project_id: $ENVIRON['PROJECT_ID']
      user_id: $ENVIRON['USER_ID']
      consumer_generation: 5
  status: 409
  response_strings:
    - consumer generation conflict
    - expected null but got 5
  response_json_paths:
      $.errors[0].code: placement.concurrent_update

- name: new version gen is None no existing
  PUT: /allocations/22222222-2222-2222-2222-222222222222
  data:
      allocations:
          $ENVIRON['RP_UUID']:
            resources:
              DISK_GB: 10
      project_id: $ENVIRON['PROJECT_ID']
      user_id: $ENVIRON['USER_ID']
      consumer_generation: null
  status: 204

- name: new version any gen no existing
  PUT: /allocations/33333333-3333-3333-3333-333333333333
  data:
      allocations:
          $ENVIRON['RP_UUID']:
            resources:
              DISK_GB: 10
      project_id: $ENVIRON['PROJECT_ID']
      user_id: $ENVIRON['USER_ID']
      consumer_generation: 33
  status: 409
  response_strings:
    - consumer generation conflict

# Now create an allocation for a specific consumer
- name: put an allocation
  PUT: /allocations/44444444-4444-4444-4444-444444444444
  data:
      allocations:
          $ENVIRON['RP_UUID']:
              resources:
                DISK_GB: 10
      project_id: $ENVIRON['PROJECT_ID']
      user_id: $ENVIRON['USER_ID']
      consumer_generation: null
  status: 204

- name: new version no gen existing
  PUT: /allocations/44444444-4444-4444-4444-444444444444
  data:
      allocations:
          $ENVIRON['RP_UUID']:
              resources:
                DISK_GB: 10
      project_id: $ENVIRON['PROJECT_ID']
      user_id: $ENVIRON['USER_ID']
      consumer_generation: null
  status: 409
  response_strings:
    - consumer generation conflict

- name: get the current consumer generation
  GET: /allocations/44444444-4444-4444-4444-444444444444
  status: 200

- name: new version matching gen existing
  PUT: /allocations/44444444-4444-4444-4444-444444444444
  data:
      allocations:
          $ENVIRON['RP_UUID']:
              resources:
                DISK_GB: 10
      project_id: $ENVIRON['PROJECT_ID']
      user_id: $ENVIRON['USER_ID']
      consumer_generation: $HISTORY["get the current consumer generation"].$RESPONSE["consumer_generation"]
  status: 204

- name: new version mismatch gen existing
  PUT: /allocations/44444444-4444-4444-4444-444444444444
  data:
      allocations:
          $ENVIRON['RP_UUID']:
              resources:
                DISK_GB: 10
      project_id: $ENVIRON['PROJECT_ID']
      user_id: $ENVIRON['USER_ID']
      consumer_generation: 12
  status: 409
  response_strings:
    - consumer generation conflict
  response_json_paths:
      $.errors[0].code: placement.concurrent_update

- name: old version no gen existing
  PUT: /allocations/44444444-4444-4444-4444-444444444444
  request_headers:
      openstack-api-version: placement 1.27
  data:
      allocations:
          $ENVIRON['RP_UUID']:
            resources:
              DISK_GB: 10
      project_id: $ENVIRON['PROJECT_ID']
      user_id: $ENVIRON['USER_ID']
  status: 204

- name: new version serialization contains consumer generation
  GET: /allocations/44444444-4444-4444-4444-444444444444
  status: 200
  response_json_paths:
      $.consumer_generation: /^\d+$/

- name: empty allocations dict now possible in PUT /allocations/{consumer_uuid}
  PUT: /allocations/44444444-4444-4444-4444-444444444444
  data:
      allocations: {}
      project_id: $ENVIRON['PROJECT_ID']
      user_id: $ENVIRON['USER_ID']
      consumer_generation: $HISTORY["new version serialization contains consumer generation"].$RESPONSE["consumer_generation"]
  status: 204

- name: old version should now return no allocations for this consumer
  GET: /allocations/44444444-4444-4444-4444-444444444444
  request_headers:
      openstack-api-version: placement 1.27
  status: 200
  response_json_paths:
      # This is the entire response. There is no generation or proj/user id.
      $:
        allocations: {}

- name: new version should now return no allocations for this consumer
  GET: /allocations/44444444-4444-4444-4444-444444444444
  status: 200
  response_json_paths:
      # This is the entire response. There is no generation or proj/user id.
      $:
        allocations: {}

# The following tests cover cases where we are putting allocations to
# multiple resource providers from one consumer uuid, both a brand new
# consumer and an existing one.

- name: create shared disk
  POST: /resource_providers
  data:
      name: shared_disker
      uuid: 8aa83304-4b6d-4a23-b954-06d8b36b206a

- name: trait that disk
  PUT: /resource_providers/8aa83304-4b6d-4a23-b954-06d8b36b206a/traits
  data:
      resource_provider_generation: $RESPONSE['$.generation']
      traits:
          - MISC_SHARES_VIA_AGGREGATE
          - STORAGE_DISK_SSD


- name: set disk inventory
  PUT: /resource_providers/8aa83304-4b6d-4a23-b954-06d8b36b206a/inventories
  data:
      inventories:
          DISK_GB:
              total: 5000
      resource_provider_generation: $RESPONSE['$.resource_provider_generation']

- name: disk in aggregate
  PUT: /resource_providers/8aa83304-4b6d-4a23-b954-06d8b36b206a/aggregates
  data:
      resource_provider_generation: $RESPONSE['$.resource_provider_generation']
      aggregates:
          - 7fade9e1-ab01-4d1b-84db-ac74f740bb42

- name: compute in aggregate
  PUT: /resource_providers/$ENVIRON['RP_UUID']/aggregates
  request_headers:
      # avoid generation in aggregates
      openstack-api-version: placement 1.10
  data:
      - 7fade9e1-ab01-4d1b-84db-ac74f740bb42

- name: get candidates with shared
  GET: /allocation_candidates?resources=VCPU:1,DISK_GB:200&required=STORAGE_DISK_SSD
  response_json_paths:
      $.allocation_requests.`len`: 1
      $.allocation_requests[0].allocations['$ENVIRON["RP_UUID"]'].resources.VCPU: 1
      $.allocation_requests[0].allocations['8aa83304-4b6d-4a23-b954-06d8b36b206a'].resources.DISK_GB: 200

- name: put that allocation to new consumer
  PUT: /allocations/55555555-5555-5555-5555-555555555555
  data:
      allocations: $RESPONSE['$.allocation_requests[0].allocations']
      project_id: $ENVIRON['PROJECT_ID']
      user_id: $ENVIRON['USER_ID']
      consumer_generation: null
  status: 204

- name: put that allocation to existing consumer
  PUT: /allocations/22222222-2222-2222-2222-222222222222
  data:
      allocations: $HISTORY['get candidates with shared'].$RESPONSE['$.allocation_requests[0].allocations']
      project_id: $ENVIRON['PROJECT_ID']
      user_id: $ENVIRON['USER_ID']
      # we just happen to know this is supposed to be 1 here, so shortcutting
      consumer_generation: 1
  status: 204
