|
| 1 | +"""Integration tests for sagemaker dependency injection in remote functions. |
| 2 | +
|
| 3 | +These tests verify that the sagemaker>=3.2.0 dependency is properly injected |
| 4 | +into remote function jobs, preventing version mismatch issues. |
| 5 | +""" |
| 6 | + |
| 7 | +import os |
| 8 | +import sys |
| 9 | +import tempfile |
| 10 | +import pytest |
| 11 | + |
| 12 | +# Skip decorator for AWS configuration |
| 13 | +# skip_if_no_aws_region = pytest.mark.skipif( |
| 14 | +# not os.environ.get('AWS_DEFAULT_REGION'), |
| 15 | +# reason="AWS credentials not configured" |
| 16 | +# ) |
| 17 | + |
| 18 | +# Add src to path |
| 19 | +sys.path.insert(0, os.path.join(os.path.dirname(__file__), '../../../../src')) |
| 20 | + |
| 21 | +from sagemaker.core.remote_function import remote |
| 22 | + |
| 23 | + |
| 24 | +class TestRemoteFunctionDependencyInjection: |
| 25 | + """Integration tests for dependency injection in remote functions.""" |
| 26 | + |
| 27 | + @pytest.mark.integ |
| 28 | + # @skip_if_no_aws_region |
| 29 | + def test_remote_function_without_dependencies(self): |
| 30 | + """Test remote function execution without explicit dependencies. |
| 31 | + |
| 32 | + This test verifies that when no dependencies are provided, the remote |
| 33 | + function still executes successfully because sagemaker>=3.2.0 is |
| 34 | + automatically injected. |
| 35 | + """ |
| 36 | + @remote( |
| 37 | + instance_type="ml.m5.large", |
| 38 | + # No dependencies specified - sagemaker should be injected automatically |
| 39 | + ) |
| 40 | + def simple_add(x, y): |
| 41 | + """Simple function that adds two numbers.""" |
| 42 | + return x + y |
| 43 | + |
| 44 | + # Execute the function |
| 45 | + result = simple_add(5, 3) |
| 46 | + |
| 47 | + # Verify result |
| 48 | + assert result == 8, f"Expected 8, got {result}" |
| 49 | + print("✓ Remote function without dependencies executed successfully") |
| 50 | + |
| 51 | + @pytest.mark.integ |
| 52 | + # @skip_if_no_aws_region |
| 53 | + def test_remote_function_with_user_dependencies_no_sagemaker(self): |
| 54 | + """Test remote function with user dependencies but no sagemaker. |
| 55 | + |
| 56 | + This test verifies that when user provides dependencies without sagemaker, |
| 57 | + sagemaker>=3.2.0 is automatically appended. |
| 58 | + """ |
| 59 | + # Create a temporary requirements.txt without sagemaker |
| 60 | + with tempfile.NamedTemporaryFile(mode='w', suffix='.txt', delete=False) as f: |
| 61 | + f.write("numpy>=1.20.0\npandas>=1.3.0\n") |
| 62 | + req_file = f.name |
| 63 | + |
| 64 | + try: |
| 65 | + @remote( |
| 66 | + instance_type="ml.m5.large", |
| 67 | + dependencies=req_file, |
| 68 | + ) |
| 69 | + def compute_with_numpy(x): |
| 70 | + """Function that uses numpy.""" |
| 71 | + import numpy as np |
| 72 | + return np.array([x, x*2, x*3]).sum() |
| 73 | + |
| 74 | + # Execute the function |
| 75 | + result = compute_with_numpy(5) |
| 76 | + |
| 77 | + # Verify result (5 + 10 + 15 = 30) |
| 78 | + assert result == 30, f"Expected 30, got {result}" |
| 79 | + print("✓ Remote function with user dependencies executed successfully") |
| 80 | + finally: |
| 81 | + os.remove(req_file) |
| 82 | + |
| 83 | + |
| 84 | +class TestRemoteFunctionVersionCompatibility: |
| 85 | + """Tests for version compatibility between local and remote environments.""" |
| 86 | + |
| 87 | + @pytest.mark.integ |
| 88 | + # @skip_if_no_aws_region |
| 89 | + def test_deserialization_with_injected_sagemaker(self): |
| 90 | + """Test that deserialization works with injected sagemaker dependency. |
| 91 | + |
| 92 | + This test verifies that the remote environment can properly deserialize |
| 93 | + functions when sagemaker>=3.2.0 is available. |
| 94 | + """ |
| 95 | + @remote( |
| 96 | + instance_type="ml.m5.large", |
| 97 | + ) |
| 98 | + def complex_computation(data): |
| 99 | + """Function that performs complex computation.""" |
| 100 | + result = sum(data) * len(data) |
| 101 | + return result |
| 102 | + |
| 103 | + # Execute with various data types |
| 104 | + test_data = [1, 2, 3, 4, 5] |
| 105 | + result = complex_computation(test_data) |
| 106 | + |
| 107 | + # Verify result (sum=15, len=5, 15*5=75) |
| 108 | + assert result == 75, f"Expected 75, got {result}" |
| 109 | + print("✓ Deserialization with injected sagemaker works correctly") |
| 110 | + |
| 111 | + @pytest.mark.integ |
| 112 | + # @skip_if_no_aws_region |
| 113 | + def test_multiple_remote_functions_with_dependencies(self): |
| 114 | + """Test multiple remote functions with different dependency configurations. |
| 115 | + |
| 116 | + This test verifies that the dependency injection works correctly |
| 117 | + when multiple remote functions are defined and executed. |
| 118 | + """ |
| 119 | + @remote(instance_type="ml.m5.large") |
| 120 | + def func1(x): |
| 121 | + return x + 1 |
| 122 | + |
| 123 | + @remote(instance_type="ml.m5.large") |
| 124 | + def func2(x): |
| 125 | + return x * 2 |
| 126 | + |
| 127 | + # Execute both functions |
| 128 | + result1 = func1(5) |
| 129 | + result2 = func2(5) |
| 130 | + |
| 131 | + assert result1 == 6, f"func1: Expected 6, got {result1}" |
| 132 | + assert result2 == 10, f"func2: Expected 10, got {result2}" |
| 133 | + print("✓ Multiple remote functions with dependencies executed successfully") |
| 134 | + |
| 135 | + |
| 136 | +if __name__ == "__main__": |
| 137 | + pytest.main([__file__, "-v", "-m", "integ"]) |
0 commit comments