feat: per-client Makefile infrastructure for 4-mode testing
Add per-client Makefiles for C, Python, and Go with: - CLI mode: Tests --help, arg parsing, syntax validation - Library mode: Unit tests, import verification - Integration mode: API contract validation (with credentials) - Functional mode: Real-world scenario tests C client: - 22 library tests (SHA-256, HMAC-SHA256, detect_language) - Full unsandbox.c implementation with examples Python client (sync + async): - Delegates to sync/ and async/ subdirectories - pytest-based test suites with coverage - Examples for concurrent execution, streaming Go client: - Delegates to sync/ and async/ subdirectories - go test integration with vet and fmt Also update detect-changes.sh to detect changes in both root-level un.* files AND clients/ directory.
This commit is contained in:
parent
2701b29945
commit
1e01d09883
46 changed files with 7988 additions and 125 deletions
77
clients/python/sync/examples/fibonacci_client.py
Normal file
77
clients/python/sync/examples/fibonacci_client.py
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
#!/usr/bin/env python3
|
||||
"""
|
||||
Fibonacci Client example for unsandbox Python SDK - Synchronous Version
|
||||
|
||||
Demonstrates executing CPU-bound calculations through the sync SDK.
|
||||
Shows proper error handling and result processing.
|
||||
|
||||
To run:
|
||||
export UNSANDBOX_PUBLIC_KEY="your-public-key"
|
||||
export UNSANDBOX_SECRET_KEY="your-secret-key"
|
||||
python3 fibonacci_client.py
|
||||
|
||||
Expected output:
|
||||
Calculating fibonacci(10)...
|
||||
Result status: completed
|
||||
Output: fib(10) = 55
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
|
||||
# Add the SDK path
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "src"))
|
||||
|
||||
from un import execute_code, CredentialsError
|
||||
|
||||
|
||||
def main():
|
||||
"""Execute fibonacci calculation using the SDK."""
|
||||
|
||||
# The code to execute
|
||||
code = """
|
||||
def fib(n):
|
||||
if n <= 1:
|
||||
return n
|
||||
return fib(n-1) + fib(n-2)
|
||||
|
||||
print(f"fib(10) = {fib(10)}")
|
||||
"""
|
||||
|
||||
try:
|
||||
# Resolve credentials from environment
|
||||
public_key = os.environ.get("UNSANDBOX_PUBLIC_KEY")
|
||||
secret_key = os.environ.get("UNSANDBOX_SECRET_KEY")
|
||||
|
||||
if not public_key or not secret_key:
|
||||
print("Error: UNSANDBOX_PUBLIC_KEY and UNSANDBOX_SECRET_KEY environment variables required")
|
||||
print("Run with: export UNSANDBOX_PUBLIC_KEY=your-key UNSANDBOX_SECRET_KEY=your-key")
|
||||
sys.exit(1)
|
||||
|
||||
# Execute the code synchronously
|
||||
print("Calculating fibonacci(10)...")
|
||||
result = execute_code("python", code, public_key, secret_key)
|
||||
|
||||
# Check for errors
|
||||
if result.get("status") == "completed":
|
||||
print(f"Result status: {result.get('status')}")
|
||||
print(f"Output: {result.get('stdout', '').strip()}")
|
||||
if result.get("stderr"):
|
||||
print(f"Errors: {result.get('stderr', '')}")
|
||||
else:
|
||||
print(f"Execution failed with status: {result.get('status')}")
|
||||
print(f"Error: {result.get('error', 'Unknown error')}")
|
||||
sys.exit(1)
|
||||
|
||||
except CredentialsError as e:
|
||||
print(f"Credentials error: {e}")
|
||||
sys.exit(1)
|
||||
except Exception as e:
|
||||
print(f"Error: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
111
clients/python/sync/examples/file_operations.py
Normal file
111
clients/python/sync/examples/file_operations.py
Normal file
|
|
@ -0,0 +1,111 @@
|
|||
#!/usr/bin/env python3
|
||||
"""
|
||||
File Operations example for unsandbox Python SDK - Synchronous Version
|
||||
|
||||
This example demonstrates reading and writing files in sandboxed environments.
|
||||
Shows temporary file creation and manipulation within the sandbox.
|
||||
|
||||
To run:
|
||||
export UNSANDBOX_PUBLIC_KEY="your-public-key"
|
||||
export UNSANDBOX_SECRET_KEY="your-secret-key"
|
||||
python3 file_operations.py
|
||||
|
||||
Expected output:
|
||||
File created at: /tmp/example.txt
|
||||
File contents:
|
||||
Line 1: Hello from the sandbox
|
||||
Line 2: This is temporary storage
|
||||
Line 3: File operations work!
|
||||
Total lines written: 3
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
|
||||
# Add the SDK path
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "src"))
|
||||
|
||||
from un import execute_code, CredentialsError
|
||||
|
||||
|
||||
def main():
|
||||
"""Execute file operations code in sandbox."""
|
||||
|
||||
# The code to execute - demonstrates file I/O
|
||||
code = """
|
||||
import os
|
||||
import tempfile
|
||||
|
||||
# Create temporary file
|
||||
temp_file = "/tmp/example.txt"
|
||||
|
||||
try:
|
||||
# Write to file
|
||||
with open(temp_file, "w") as f:
|
||||
f.write("Line 1: Hello from the sandbox\\n")
|
||||
f.write("Line 2: This is temporary storage\\n")
|
||||
f.write("Line 3: File operations work!\\n")
|
||||
|
||||
print(f"File created at: {temp_file}")
|
||||
|
||||
# Check if file exists
|
||||
if os.path.exists(temp_file):
|
||||
print(f"File exists: {os.path.isfile(temp_file)}")
|
||||
|
||||
# Get file size
|
||||
file_size = os.path.getsize(temp_file)
|
||||
print(f"File size: {file_size} bytes")
|
||||
|
||||
# Read from file
|
||||
print("File contents:")
|
||||
with open(temp_file, "r") as f:
|
||||
lines = f.readlines()
|
||||
for line in lines:
|
||||
print(line.rstrip())
|
||||
|
||||
print(f"Total lines written: {len(lines)}")
|
||||
|
||||
except IOError as e:
|
||||
print(f"File operation error: {e}")
|
||||
except Exception as e:
|
||||
print(f"Error: {e}")
|
||||
"""
|
||||
|
||||
try:
|
||||
# Resolve credentials from environment
|
||||
public_key = os.environ.get("UNSANDBOX_PUBLIC_KEY")
|
||||
secret_key = os.environ.get("UNSANDBOX_SECRET_KEY")
|
||||
|
||||
if not public_key or not secret_key:
|
||||
print("Error: UNSANDBOX_PUBLIC_KEY and UNSANDBOX_SECRET_KEY environment variables required")
|
||||
print("Run with: export UNSANDBOX_PUBLIC_KEY=your-key UNSANDBOX_SECRET_KEY=your-key")
|
||||
sys.exit(1)
|
||||
|
||||
# Execute the code
|
||||
print("Executing file operations in sandbox...")
|
||||
result = execute_code("python", code, public_key, secret_key)
|
||||
|
||||
# Check for errors
|
||||
if result.get("status") == "completed":
|
||||
print("\n=== STDOUT ===")
|
||||
print(result.get("stdout", ""))
|
||||
if result.get("stderr"):
|
||||
print("\n=== STDERR ===")
|
||||
print(result.get("stderr", ""))
|
||||
else:
|
||||
print(f"Execution failed with status: {result.get('status')}")
|
||||
print(f"Error: {result.get('error', 'Unknown error')}")
|
||||
sys.exit(1)
|
||||
|
||||
except CredentialsError as e:
|
||||
print(f"Credentials error: {e}")
|
||||
sys.exit(1)
|
||||
except Exception as e:
|
||||
print(f"Error: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
70
clients/python/sync/examples/hello_world_client.py
Normal file
70
clients/python/sync/examples/hello_world_client.py
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
#!/usr/bin/env python3
|
||||
"""
|
||||
Hello World Client example for unsandbox Python SDK - Synchronous Version
|
||||
|
||||
This example demonstrates basic synchronous execution using the SDK client.
|
||||
Shows how to execute code from a Python program using the sync SDK.
|
||||
|
||||
To run:
|
||||
export UNSANDBOX_PUBLIC_KEY="your-public-key"
|
||||
export UNSANDBOX_SECRET_KEY="your-secret-key"
|
||||
python3 hello_world_client.py
|
||||
|
||||
Expected output:
|
||||
Executing code synchronously...
|
||||
Result status: completed
|
||||
Output: Hello from unsandbox!
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
|
||||
# Add the SDK path
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "src"))
|
||||
|
||||
from un import execute_code, CredentialsError
|
||||
|
||||
|
||||
def main():
|
||||
"""Execute hello world code using the SDK."""
|
||||
|
||||
# The code to execute
|
||||
code = 'print("Hello from unsandbox!")'
|
||||
|
||||
try:
|
||||
# Resolve credentials from environment
|
||||
public_key = os.environ.get("UNSANDBOX_PUBLIC_KEY")
|
||||
secret_key = os.environ.get("UNSANDBOX_SECRET_KEY")
|
||||
|
||||
if not public_key or not secret_key:
|
||||
print("Error: UNSANDBOX_PUBLIC_KEY and UNSANDBOX_SECRET_KEY environment variables required")
|
||||
print("Run with: export UNSANDBOX_PUBLIC_KEY=your-key UNSANDBOX_SECRET_KEY=your-key")
|
||||
sys.exit(1)
|
||||
|
||||
# Execute the code synchronously
|
||||
print("Executing code synchronously...")
|
||||
result = execute_code("python", code, public_key, secret_key)
|
||||
|
||||
# Check for errors
|
||||
if result.get("status") == "completed":
|
||||
print(f"Result status: {result.get('status')}")
|
||||
print(f"Output: {result.get('stdout', '').strip()}")
|
||||
if result.get("stderr"):
|
||||
print(f"Errors: {result.get('stderr', '')}")
|
||||
else:
|
||||
print(f"Execution failed with status: {result.get('status')}")
|
||||
print(f"Error: {result.get('error', 'Unknown error')}")
|
||||
sys.exit(1)
|
||||
|
||||
except CredentialsError as e:
|
||||
print(f"Credentials error: {e}")
|
||||
sys.exit(1)
|
||||
except Exception as e:
|
||||
print(f"Error: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
87
clients/python/sync/examples/http_request.py
Normal file
87
clients/python/sync/examples/http_request.py
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
#!/usr/bin/env python3
|
||||
"""
|
||||
HTTP Request example for unsandbox Python SDK - Synchronous Version
|
||||
|
||||
This example demonstrates making HTTP requests from within a sandboxed environment.
|
||||
Uses semitrusted mode which provides internet access through an egress proxy.
|
||||
|
||||
To run:
|
||||
export UNSANDBOX_PUBLIC_KEY="your-public-key"
|
||||
export UNSANDBOX_SECRET_KEY="your-secret-key"
|
||||
python3 http_request.py
|
||||
|
||||
Expected output:
|
||||
Status Code: 200
|
||||
Response: {"origin": "..."}
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
|
||||
# Add the SDK path
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "src"))
|
||||
|
||||
from un import execute_code, CredentialsError
|
||||
|
||||
|
||||
def main():
|
||||
"""Execute HTTP request code in sandbox."""
|
||||
|
||||
# The code to execute - uses requests library (pre-installed)
|
||||
code = """
|
||||
import requests
|
||||
import json
|
||||
|
||||
try:
|
||||
# Make HTTP request to httpbin.org
|
||||
response = requests.get('https://httpbin.org/ip', timeout=10)
|
||||
print(f"Status Code: {response.status_code}")
|
||||
|
||||
# Parse and display response
|
||||
data = response.json()
|
||||
print(f"Response: {json.dumps(data)}")
|
||||
|
||||
except requests.RequestException as e:
|
||||
print(f"Request failed: {e}")
|
||||
except Exception as e:
|
||||
print(f"Error: {e}")
|
||||
"""
|
||||
|
||||
try:
|
||||
# Resolve credentials from environment
|
||||
public_key = os.environ.get("UNSANDBOX_PUBLIC_KEY")
|
||||
secret_key = os.environ.get("UNSANDBOX_SECRET_KEY")
|
||||
|
||||
if not public_key or not secret_key:
|
||||
print("Error: UNSANDBOX_PUBLIC_KEY and UNSANDBOX_SECRET_KEY environment variables required")
|
||||
print("Run with: export UNSANDBOX_PUBLIC_KEY=your-key UNSANDBOX_SECRET_KEY=your-key")
|
||||
sys.exit(1)
|
||||
|
||||
# Execute the code
|
||||
print("Executing HTTP request in sandbox...")
|
||||
result = execute_code("python", code, public_key, secret_key)
|
||||
|
||||
# Check for errors
|
||||
if result.get("status") == "completed":
|
||||
print("\n=== STDOUT ===")
|
||||
print(result.get("stdout", ""))
|
||||
if result.get("stderr"):
|
||||
print("\n=== STDERR ===")
|
||||
print(result.get("stderr", ""))
|
||||
else:
|
||||
print(f"Execution failed with status: {result.get('status')}")
|
||||
print(f"Error: {result.get('error', 'Unknown error')}")
|
||||
sys.exit(1)
|
||||
|
||||
except CredentialsError as e:
|
||||
print(f"Credentials error: {e}")
|
||||
sys.exit(1)
|
||||
except Exception as e:
|
||||
print(f"Error: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
100
clients/python/sync/examples/json_processing.py
Normal file
100
clients/python/sync/examples/json_processing.py
Normal file
|
|
@ -0,0 +1,100 @@
|
|||
#!/usr/bin/env python3
|
||||
"""
|
||||
JSON Processing example for unsandbox Python SDK - Synchronous Version
|
||||
|
||||
This example demonstrates JSON parsing and manipulation operations.
|
||||
Shows how to work with structured data in sandboxed environments.
|
||||
|
||||
To run:
|
||||
export UNSANDBOX_PUBLIC_KEY="your-public-key"
|
||||
export UNSANDBOX_SECRET_KEY="your-secret-key"
|
||||
python3 json_processing.py
|
||||
|
||||
Expected output:
|
||||
Original JSON: {"name": "Alice", "age": 30, "skills": ["Python", "JavaScript"]}
|
||||
Parsed successfully!
|
||||
Name: Alice
|
||||
Age: 30
|
||||
Skills: Python, JavaScript
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
|
||||
# Add the SDK path
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "src"))
|
||||
|
||||
from un import execute_code, CredentialsError
|
||||
|
||||
|
||||
def main():
|
||||
"""Execute JSON processing code in sandbox."""
|
||||
|
||||
# The code to execute - demonstrates JSON parsing and manipulation
|
||||
code = """
|
||||
import json
|
||||
|
||||
# Original JSON data
|
||||
json_string = '{"name": "Alice", "age": 30, "skills": ["Python", "JavaScript"]}'
|
||||
print(f"Original JSON: {json_string}")
|
||||
|
||||
try:
|
||||
# Parse JSON
|
||||
data = json.loads(json_string)
|
||||
print("Parsed successfully!")
|
||||
|
||||
# Access fields
|
||||
print(f"Name: {data['name']}")
|
||||
print(f"Age: {data['age']}")
|
||||
print(f"Skills: {', '.join(data['skills'])}")
|
||||
|
||||
# Modify and re-serialize
|
||||
data['age'] = 31
|
||||
data['skills'].append("Go")
|
||||
modified_json = json.dumps(data, indent=2)
|
||||
print(f"\\nModified JSON:\\n{modified_json}")
|
||||
|
||||
except json.JSONDecodeError as e:
|
||||
print(f"JSON parsing error: {e}")
|
||||
except Exception as e:
|
||||
print(f"Error: {e}")
|
||||
"""
|
||||
|
||||
try:
|
||||
# Resolve credentials from environment
|
||||
public_key = os.environ.get("UNSANDBOX_PUBLIC_KEY")
|
||||
secret_key = os.environ.get("UNSANDBOX_SECRET_KEY")
|
||||
|
||||
if not public_key or not secret_key:
|
||||
print("Error: UNSANDBOX_PUBLIC_KEY and UNSANDBOX_SECRET_KEY environment variables required")
|
||||
print("Run with: export UNSANDBOX_PUBLIC_KEY=your-key UNSANDBOX_SECRET_KEY=your-key")
|
||||
sys.exit(1)
|
||||
|
||||
# Execute the code
|
||||
print("Executing JSON processing in sandbox...")
|
||||
result = execute_code("python", code, public_key, secret_key)
|
||||
|
||||
# Check for errors
|
||||
if result.get("status") == "completed":
|
||||
print("\n=== STDOUT ===")
|
||||
print(result.get("stdout", ""))
|
||||
if result.get("stderr"):
|
||||
print("\n=== STDERR ===")
|
||||
print(result.get("stderr", ""))
|
||||
else:
|
||||
print(f"Execution failed with status: {result.get('status')}")
|
||||
print(f"Error: {result.get('error', 'Unknown error')}")
|
||||
sys.exit(1)
|
||||
|
||||
except CredentialsError as e:
|
||||
print(f"Credentials error: {e}")
|
||||
sys.exit(1)
|
||||
except Exception as e:
|
||||
print(f"Error: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Loading…
Add table
Add a link
Reference in a new issue