You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_azure_container_code_executor.py 3.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # File based from: https://github.com/microsoft/autogen/blob/main/test/coding/test_commandline_code_executor.py
  2. # Credit to original authors
  3. import asyncio
  4. import os
  5. import sys
  6. import pytest
  7. from azure.identity import DefaultAzureCredential
  8. from agnext.components.code_executor import CodeBlock, AzureContainerCodeExecutor
  9. from agnext.core import CancellationToken
  10. UNIX_SHELLS = ["bash", "sh", "shell"]
  11. WINDOWS_SHELLS = ["ps1", "pwsh", "powershell"]
  12. PYTHON_VARIANTS = ["python", "Python", "py"]
  13. ENVIRON_KEY_AZURE_POOL_ENDPOINT = "AZURE_POOL_ENDPOINT"
  14. POOL_ENDPOINT = os.getenv(ENVIRON_KEY_AZURE_POOL_ENDPOINT)
  15. @pytest.mark.skipif(
  16. not POOL_ENDPOINT,
  17. reason="do not run if pool endpoint is not defined",
  18. )
  19. @pytest.mark.asyncio
  20. async def test_execute_code() -> None:
  21. assert POOL_ENDPOINT is not None
  22. cancellation_token = CancellationToken()
  23. executor = AzureContainerCodeExecutor(pool_management_endpoint=POOL_ENDPOINT, credential=DefaultAzureCredential())
  24. # Test single code block.
  25. code_blocks = [CodeBlock(code="import sys; print('hello world!')", language="python")]
  26. code_result = await executor.execute_code_blocks(code_blocks, cancellation_token)
  27. assert code_result.exit_code == 0 and "hello world!" in code_result.output
  28. # Test multiple code blocks.
  29. code_blocks = [
  30. CodeBlock(code="import sys; print('hello world!')", language="python"),
  31. CodeBlock(code="a = 100 + 100; print(a)", language="python"),
  32. ]
  33. code_result = await executor.execute_code_blocks(code_blocks, cancellation_token)
  34. assert (
  35. code_result.exit_code == 0
  36. and "hello world!" in code_result.output
  37. and "200" in code_result.output
  38. )
  39. # Test bash script.
  40. if sys.platform not in ["win32"]:
  41. code_blocks = [CodeBlock(code="echo 'hello world!'", language="bash")]
  42. code_result = await executor.execute_code_blocks(code_blocks, cancellation_token)
  43. assert "unknown language" in code_result.output
  44. assert code_result.exit_code == 1
  45. # Test running code.
  46. file_lines = ["import sys", "print('hello world!')", "a = 100 + 100", "print(a)"]
  47. code_blocks = [CodeBlock(code="\n".join(file_lines), language="python")]
  48. code_result = await executor.execute_code_blocks(code_blocks, cancellation_token)
  49. assert (
  50. code_result.exit_code == 0
  51. and "hello world!" in code_result.output
  52. and "200" in code_result.output
  53. )
  54. @pytest.mark.skipif(
  55. not POOL_ENDPOINT,
  56. reason="do not run if pool endpoint is not defined",
  57. )
  58. @pytest.mark.asyncio
  59. async def test_azure_container_code_executor_timeout() -> None:
  60. assert POOL_ENDPOINT is not None
  61. cancellation_token = CancellationToken()
  62. executor = AzureContainerCodeExecutor(pool_management_endpoint=POOL_ENDPOINT, credential=DefaultAzureCredential(), timeout=1)
  63. code_blocks = [CodeBlock(code="import time; time.sleep(10); print('hello world!')", language="python")]
  64. with pytest.raises(asyncio.TimeoutError):
  65. await executor.execute_code_blocks(code_blocks, cancellation_token)
  66. @pytest.mark.skipif(
  67. not POOL_ENDPOINT,
  68. reason="do not run if pool endpoint is not defined",
  69. )
  70. @pytest.mark.asyncio
  71. async def test_azure_container_code_executor_cancellation() -> None:
  72. assert POOL_ENDPOINT is not None
  73. cancellation_token = CancellationToken()
  74. executor = AzureContainerCodeExecutor(pool_management_endpoint=POOL_ENDPOINT, credential=DefaultAzureCredential())
  75. code_blocks = [CodeBlock(code="import time; time.sleep(10); print('hello world!')", language="python")]
  76. coro = executor.execute_code_blocks(code_blocks, cancellation_token)
  77. await asyncio.sleep(1)
  78. cancellation_token.cancel()
  79. with pytest.raises(asyncio.CancelledError):
  80. await coro