|
| 1 | +""" |
| 2 | +Tests for Project client functionality. |
| 3 | +""" |
| 4 | +import pytest |
| 5 | + |
| 6 | +from cocalc_api import Project |
| 7 | + |
| 8 | + |
| 9 | +class TestProjectCreation: |
| 10 | + """Tests for project creation and management.""" |
| 11 | + |
| 12 | + def test_create_temporary_project(self, temporary_project): |
| 13 | + """Test that a temporary project is created successfully.""" |
| 14 | + assert temporary_project is not None |
| 15 | + assert 'project_id' in temporary_project |
| 16 | + assert 'title' in temporary_project |
| 17 | + assert 'description' in temporary_project |
| 18 | + assert temporary_project['title'].startswith('CoCalc API Test ') |
| 19 | + assert temporary_project['description'] == "Temporary project created by cocalc-api tests" |
| 20 | + # Project ID should be a UUID-like string |
| 21 | + assert len(temporary_project['project_id']) > 0 |
| 22 | + assert '-' in temporary_project['project_id'] |
| 23 | + |
| 24 | + def test_project_exists_in_list(self, hub, temporary_project): |
| 25 | + """Test that the created project appears in the projects list.""" |
| 26 | + projects = hub.projects.get(all=True) |
| 27 | + project_ids = [p['project_id'] for p in projects] |
| 28 | + assert temporary_project['project_id'] in project_ids |
| 29 | + |
| 30 | + |
| 31 | +class TestProjectSystem: |
| 32 | + """Tests for Project system operations.""" |
| 33 | + |
| 34 | + def test_ping(self, project_client): |
| 35 | + """Test basic ping connectivity to project.""" |
| 36 | + result = project_client.system.ping() |
| 37 | + assert result is not None |
| 38 | + assert isinstance(result, dict) |
| 39 | + |
| 40 | + def test_project_initialization(self, api_key, cocalc_host): |
| 41 | + """Test Project client initialization.""" |
| 42 | + project_id = "test-project-id" |
| 43 | + project = Project(project_id=project_id, api_key=api_key, host=cocalc_host) |
| 44 | + assert project.project_id == project_id |
| 45 | + assert project.api_key == api_key |
| 46 | + assert project.host == cocalc_host |
| 47 | + assert project.client is not None |
| 48 | + |
| 49 | + def test_project_with_temporary_project(self, project_client, temporary_project): |
| 50 | + """Test Project client using the temporary project.""" |
| 51 | + assert project_client.project_id == temporary_project['project_id'] |
| 52 | + # Test that we can ping the specific project |
| 53 | + result = project_client.system.ping() |
| 54 | + assert result is not None |
| 55 | + assert isinstance(result, dict) |
| 56 | + |
| 57 | + def test_exec_command(self, project_client): |
| 58 | + """Test executing shell commands in the project.""" |
| 59 | + # Test running 'date -Is' to get ISO date with seconds |
| 60 | + result = project_client.system.exec(command="date", args=["-Is"]) |
| 61 | + |
| 62 | + # Check the result structure |
| 63 | + assert 'stdout' in result |
| 64 | + assert 'stderr' in result |
| 65 | + assert 'exit_code' in result |
| 66 | + |
| 67 | + # Should succeed |
| 68 | + assert result['exit_code'] == 0 |
| 69 | + |
| 70 | + # Should have minimal stderr |
| 71 | + assert result['stderr'] == '' or len(result['stderr']) == 0 |
| 72 | + |
| 73 | + # Parse the returned date and compare with current time |
| 74 | + from datetime import datetime |
| 75 | + import re |
| 76 | + |
| 77 | + date_output = result['stdout'].strip() |
| 78 | + # Expected format: 2025-09-29T12:34:56+00:00 or similar |
| 79 | + |
| 80 | + # Check if the output matches ISO format |
| 81 | + iso_pattern = r'^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[+-]\d{2}:\d{2}$' |
| 82 | + assert re.match(iso_pattern, date_output), f"Date output '{date_output}' doesn't match ISO format" |
| 83 | + |
| 84 | + # Parse the date from the command output |
| 85 | + # Remove the timezone for comparison (date -Is includes timezone) |
| 86 | + date_part = date_output[:19] # Take YYYY-MM-DDTHH:MM:SS part |
| 87 | + remote_time = datetime.fromisoformat(date_part) |
| 88 | + |
| 89 | + # Get current time |
| 90 | + current_time = datetime.now() |
| 91 | + |
| 92 | + # Check if the times are close (within 60 seconds) |
| 93 | + time_diff = abs((current_time - remote_time).total_seconds()) |
| 94 | + assert time_diff < 60, f"Time difference too large: {time_diff} seconds. Remote: {date_output}, Local: {current_time.isoformat()}" |
| 95 | + |
| 96 | + def test_exec_stderr_and_exit_code(self, project_client): |
| 97 | + """Test executing a command that writes to stderr and returns a specific exit code.""" |
| 98 | + # Use bash to echo to stderr and exit with code 42 |
| 99 | + bash_script = "echo 'test error message' >&2; exit 42" |
| 100 | + |
| 101 | + # The API raises an exception for non-zero exit codes |
| 102 | + # but includes the stderr and exit code information in the error message |
| 103 | + with pytest.raises(RuntimeError) as exc_info: |
| 104 | + project_client.system.exec(command=bash_script, bash=True) |
| 105 | + |
| 106 | + error_message = str(exc_info.value) |
| 107 | + |
| 108 | + # Verify the error message contains expected information |
| 109 | + assert "exited with nonzero code 42" in error_message |
| 110 | + assert "stderr='test error message" in error_message |
| 111 | + |
| 112 | + # Extract and verify the stderr content is properly captured |
| 113 | + import re |
| 114 | + stderr_match = re.search(r"stderr='([^']*)'", error_message) |
| 115 | + assert stderr_match is not None, "Could not find stderr in error message" |
| 116 | + stderr_content = stderr_match.group(1).strip() |
| 117 | + assert stderr_content == "test error message" |
0 commit comments