tests_tool.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # Copyright (c) 2023 Project CHIP Authors
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import relative_importer # isort: split # noqa: F401
  15. import asyncio
  16. import json
  17. from typing import List
  18. import click
  19. from matter_chip_tool_adapter.decoder import MatterLog
  20. from matter_yamltests.websocket_runner import WebSocketRunner, WebSocketRunnerConfig
  21. from runner import runner_base
  22. from tests_logger import TestColoredLogPrinter, WebSocketRunnerLogger
  23. @click.pass_context
  24. def send_yaml_command(ctx, test_tool, test_name: str, server_path: str, server_arguments: str, show_adapter_logs: bool, specifications_paths: str, pics: str, additional_pseudo_clusters_directory: str, commands: List[str]):
  25. kwargs = {'test_name': test_name, 'show_adapter_logs': show_adapter_logs, 'specifications_paths': specifications_paths, 'pics': pics,
  26. 'additional_pseudo_clusters_directory': additional_pseudo_clusters_directory}
  27. index = 0
  28. while len(commands) - index > 1:
  29. kwargs[commands[index].replace('--', '')] = commands[index+1]
  30. index += 2
  31. ctx.invoke(runner_base, **kwargs)
  32. del ctx.params['commands']
  33. del ctx.params['specifications_paths']
  34. del ctx.params['pics']
  35. del ctx.params['additional_pseudo_clusters_directory']
  36. return ctx.forward(test_tool)
  37. def send_raw_command(command: str, server_path: str, server_arguments: str):
  38. websocket_runner_hooks = WebSocketRunnerLogger()
  39. websocket_runner_config = WebSocketRunnerConfig(
  40. server_path=server_path, server_arguments=server_arguments, hooks=websocket_runner_hooks)
  41. runner = WebSocketRunner(websocket_runner_config)
  42. async def send_over_websocket():
  43. payload = None
  44. try:
  45. await runner.start()
  46. payload = await runner.execute(command)
  47. finally:
  48. await runner.stop()
  49. return payload
  50. payload = asyncio.run(send_over_websocket())
  51. json_payload = json.loads(payload)
  52. log_printer = TestColoredLogPrinter()
  53. log_printer.print(MatterLog.decode_logs(json_payload.get('logs')))
  54. success = not bool(len([lambda x: x.get('error') for x in json_payload.get('results')]))
  55. return success