additional_paths: []
category: tutorial-internal
commands:
- command: script
  id: 5d7d9876168311eeb43b9606c285d82a
  kwargs:
    code: |
      # 事前確認など
      preperation_completed = True

      response = {
          "code": 202 if preperation_completed else 500,
          "body": {
              "xid": xid,
              "xname": xname,
          }
      }

      context.session.set_status(response["code"])
      context.session.finish(response["body"])
  label: Response
- command: script
  id: c55394c4165f11eeac26ce9e3c5e954e
  kwargs:
    code: |
      # Get Testsuites
      r = await callout(
          url="http://scenario:9000/testsuites",
          method="GET")
      assert r.code == 200, "Failed to get testsuites %r" % r.code
      assert json.loads(r.body) != [], "Testsuite does not exists"
      testsuite_names = [testsuite["name"] for testsuite in json.loads(r.body)]

      # Execute Testsuites
      execute_requests = []
      for testsuite_name in testsuite_names:
          execute_requests.append(
              callout(
                  url="http://scenario:9000/unittests/testsuites",
                  method="POST",
                  body={
                      "name": testsuite_name,
                      "autoCleanup": False,
                  }))
      responses = await asyncio.gather(*execute_requests)

      # Assert each response
      testsuite_ids = []
      for r in responses:
          assert r.code == 200, "Invalid response code %r" % r.code
          body = json.loads(r.body)
          testsuite_ids.append(body["instanceID"])

      # Check each testsuite has completed
      check_interval_sec = 30
      max_retry_count = 10
      all_testsuites_completed = False
      for i in range(max_retry_count):
          get_requests = []
          for testsuite_id in testsuite_ids:
              get_requests.append(
                  callout(
                      url=f"http://scenario:9000/unittests/testsuites/{testsuite_id}",
                      method="GET"
                  ))
          responses = await asyncio.gather(*get_requests)

          testsuite_status = []
          for response in responses:
              testsuite_status.append(json.loads(response.body))
          if any([status["fault"] for status in testsuite_status]):
              raise Error(500, 'Some testusuites have failed.')
          elif all([status["completedAt"] for status in testsuite_status]):
              all_testsuites_completed = True
              break
          await asyncio.sleep(check_interval_sec)

      if not all_testsuites_completed:
          raise Error(408, 'Timeout waiting for testsuites to be completed.')
  label: ExecuteTest
connect_timeout: 60
global_variables: {}
method: POST
name: TestsuiteTestInternal
request_timeout: 60
routing_auto_generation_mode: true
routing_options:
  scope: local
spec:
  response:
    normal:
      codes:
      - 200
transaction:
  async: true
  auto_response: false
  enable: true
  xname: ''
update: '2023-07-13T18:09:33.716807+09:00'
uri: /test/internal/testsuite/local/employees
variable_groups: []
version: 12
