|
| 1 | +""" |
| 2 | +Tests for setup_stitch AWS profile handling. |
| 3 | +
|
| 4 | +Tests that setup_stitch command properly uses AWS profile from config |
| 5 | +for all S3 operations (temp directory validation, manifest upload, init script upload). |
| 6 | +""" |
| 7 | + |
| 8 | +from unittest.mock import patch, MagicMock |
| 9 | + |
| 10 | + |
| 11 | +class TestCreateS3ClientHelper: |
| 12 | + """Test _create_s3_client_with_profile helper function.""" |
| 13 | + |
| 14 | + @patch("builtins.__import__", side_effect=__import__) |
| 15 | + @patch("chuck_data.commands.setup_stitch.get_aws_profile") |
| 16 | + @patch("chuck_data.commands.setup_stitch.get_aws_region") |
| 17 | + def test_creates_client_with_profile( |
| 18 | + self, mock_get_region, mock_get_profile, mock_import |
| 19 | + ): |
| 20 | + """Helper creates S3 client with AWS profile from config.""" |
| 21 | + from chuck_data.commands.setup_stitch import _create_s3_client_with_profile |
| 22 | + |
| 23 | + # Setup mocks |
| 24 | + mock_get_profile.return_value = "sales" |
| 25 | + mock_get_region.return_value = "eu-north-1" |
| 26 | + |
| 27 | + # Mock boto3 module |
| 28 | + mock_boto3 = MagicMock() |
| 29 | + mock_session = MagicMock() |
| 30 | + mock_s3_client = MagicMock() |
| 31 | + mock_boto3.Session.return_value = mock_session |
| 32 | + mock_session.client.return_value = mock_s3_client |
| 33 | + |
| 34 | + def import_side_effect(name, *args, **kwargs): |
| 35 | + if name == "boto3": |
| 36 | + return mock_boto3 |
| 37 | + return __import__(name, *args, **kwargs) |
| 38 | + |
| 39 | + mock_import.side_effect = import_side_effect |
| 40 | + |
| 41 | + # Call helper |
| 42 | + result = _create_s3_client_with_profile() |
| 43 | + |
| 44 | + # Verify boto3.Session was created with profile and region |
| 45 | + mock_boto3.Session.assert_called_once_with( |
| 46 | + profile_name="sales", region_name="eu-north-1" |
| 47 | + ) |
| 48 | + |
| 49 | + # Verify S3 client was created from session |
| 50 | + mock_session.client.assert_called_once_with("s3") |
| 51 | + |
| 52 | + # Verify returned client |
| 53 | + assert result == mock_s3_client |
| 54 | + |
| 55 | + @patch("builtins.__import__", side_effect=__import__) |
| 56 | + @patch("chuck_data.commands.setup_stitch.get_aws_profile") |
| 57 | + @patch("chuck_data.commands.setup_stitch.get_aws_region") |
| 58 | + def test_creates_client_without_profile( |
| 59 | + self, mock_get_region, mock_get_profile, mock_import |
| 60 | + ): |
| 61 | + """Helper creates S3 client without profile when none configured.""" |
| 62 | + from chuck_data.commands.setup_stitch import _create_s3_client_with_profile |
| 63 | + |
| 64 | + # Setup mocks - no profile |
| 65 | + mock_get_profile.return_value = None |
| 66 | + mock_get_region.return_value = "us-east-1" |
| 67 | + |
| 68 | + # Mock boto3 module |
| 69 | + mock_boto3 = MagicMock() |
| 70 | + mock_s3_client = MagicMock() |
| 71 | + mock_boto3.client.return_value = mock_s3_client |
| 72 | + |
| 73 | + def import_side_effect(name, *args, **kwargs): |
| 74 | + if name == "boto3": |
| 75 | + return mock_boto3 |
| 76 | + return __import__(name, *args, **kwargs) |
| 77 | + |
| 78 | + mock_import.side_effect = import_side_effect |
| 79 | + |
| 80 | + # Call helper |
| 81 | + result = _create_s3_client_with_profile() |
| 82 | + |
| 83 | + # Verify boto3.client was called directly with region (no session) |
| 84 | + mock_boto3.client.assert_called_once_with("s3", region_name="us-east-1") |
| 85 | + |
| 86 | + # Verify returned client |
| 87 | + assert result == mock_s3_client |
| 88 | + |
| 89 | + |
| 90 | +class TestS3TempDirectoryValidation: |
| 91 | + """Test AWS profile usage in S3 temp directory validation.""" |
| 92 | + |
| 93 | + @patch("chuck_data.commands.setup_stitch._create_s3_client_with_profile") |
| 94 | + def test_temp_dir_validation_uses_helper(self, mock_create_client): |
| 95 | + """S3 temp directory validation uses _create_s3_client_with_profile helper.""" |
| 96 | + from chuck_data.commands.setup_stitch import _ensure_s3_temp_dir_exists |
| 97 | + |
| 98 | + # Setup mock S3 client |
| 99 | + mock_s3_client = MagicMock() |
| 100 | + mock_create_client.return_value = mock_s3_client |
| 101 | + |
| 102 | + # Mock S3 responses to indicate directory exists |
| 103 | + mock_s3_client.head_bucket.return_value = {} |
| 104 | + mock_s3_client.head_object.return_value = {"ContentLength": 0} |
| 105 | + |
| 106 | + # Call function |
| 107 | + result = _ensure_s3_temp_dir_exists("s3://test-bucket/temp/") |
| 108 | + |
| 109 | + # Verify helper was called |
| 110 | + mock_create_client.assert_called_once() |
| 111 | + |
| 112 | + # Should succeed |
| 113 | + assert result is True |
| 114 | + |
| 115 | + @patch("chuck_data.commands.setup_stitch._create_s3_client_with_profile") |
| 116 | + def test_temp_dir_validation_creates_marker(self, mock_create_client): |
| 117 | + """S3 temp directory validation creates marker file when directory doesn't exist.""" |
| 118 | + from botocore.exceptions import ClientError |
| 119 | + from chuck_data.commands.setup_stitch import _ensure_s3_temp_dir_exists |
| 120 | + |
| 121 | + # Setup mock S3 client |
| 122 | + mock_s3_client = MagicMock() |
| 123 | + mock_create_client.return_value = mock_s3_client |
| 124 | + |
| 125 | + # Mock S3 responses |
| 126 | + mock_s3_client.head_bucket.return_value = {} |
| 127 | + # Mock head_object to raise 404 (directory doesn't exist) |
| 128 | + mock_s3_client.head_object.side_effect = ClientError( |
| 129 | + {"Error": {"Code": "404"}}, "head_object" |
| 130 | + ) |
| 131 | + # Mock put_object to succeed |
| 132 | + mock_s3_client.put_object.return_value = {"ETag": "test-etag"} |
| 133 | + |
| 134 | + # Call function |
| 135 | + result = _ensure_s3_temp_dir_exists("s3://chuck-bucket/redshift-temp/") |
| 136 | + |
| 137 | + # Verify helper was called |
| 138 | + mock_create_client.assert_called_once() |
| 139 | + |
| 140 | + # Verify put_object was called to create marker |
| 141 | + mock_s3_client.put_object.assert_called_once() |
| 142 | + call_args = mock_s3_client.put_object.call_args |
| 143 | + assert call_args[1]["Bucket"] == "chuck-bucket" |
| 144 | + assert call_args[1]["Key"] == "redshift-temp/.spark-redshift-temp-marker" |
| 145 | + |
| 146 | + # Should succeed |
| 147 | + assert result is True |
| 148 | + |
| 149 | + |
| 150 | +class TestManifestUploadAwsProfileUsage: |
| 151 | + """Test AWS profile usage in manifest upload step.""" |
| 152 | + |
| 153 | + def test_manifest_upload_uses_get_aws_profile(self): |
| 154 | + """Manifest upload step uses get_aws_profile() from config, not kwargs.""" |
| 155 | + # This test verifies that the code imports and can use get_aws_profile |
| 156 | + from chuck_data.commands.setup_stitch import get_aws_profile |
| 157 | + |
| 158 | + # Verify function is accessible (actual behavior tested in integration) |
| 159 | + assert callable(get_aws_profile) |
0 commit comments