|
16 | 16 | # limitations under the License. |
17 | 17 | ################################################################################ |
18 | 18 | import os |
| 19 | +import pickle |
19 | 20 | import tempfile |
20 | 21 | import unittest |
21 | 22 | from unittest.mock import patch |
22 | 23 |
|
23 | 24 | from pypaimon.catalog.rest.rest_token_file_io import RESTTokenFileIO |
| 25 | +from pypaimon.common.config import CatalogOptions, OssOptions |
24 | 26 | from pypaimon.common.file_io import FileIO |
25 | 27 | from pypaimon.common.identifier import Identifier |
26 | 28 |
|
@@ -110,6 +112,84 @@ def test_new_output_stream_behavior_matches_parent(self): |
110 | 112 | read_content = stream.read() |
111 | 113 | self.assertEqual(read_content, test_content) |
112 | 114 |
|
| 115 | + def test_pickle_serialization(self): |
| 116 | + with patch.object(RESTTokenFileIO, 'try_to_refresh_token'): |
| 117 | + original_file_io = RESTTokenFileIO( |
| 118 | + self.identifier, |
| 119 | + self.warehouse_path, |
| 120 | + self.catalog_options |
| 121 | + ) |
| 122 | + |
| 123 | + self.assertTrue(hasattr(original_file_io, 'lock')) |
| 124 | + self.assertIsNotNone(original_file_io.lock) |
| 125 | + |
| 126 | + pickled = pickle.dumps(original_file_io) |
| 127 | + |
| 128 | + deserialized_file_io = pickle.loads(pickled) |
| 129 | + |
| 130 | + self.assertEqual(deserialized_file_io.identifier, original_file_io.identifier) |
| 131 | + self.assertEqual(deserialized_file_io.path, original_file_io.path) |
| 132 | + self.assertEqual(deserialized_file_io.properties, original_file_io.properties) |
| 133 | + |
| 134 | + self.assertTrue(hasattr(deserialized_file_io, 'lock')) |
| 135 | + self.assertIsNotNone(deserialized_file_io.lock) |
| 136 | + self.assertIsNot(deserialized_file_io.lock, original_file_io.lock) |
| 137 | + |
| 138 | + self.assertIsNone(deserialized_file_io.api_instance) |
| 139 | + |
| 140 | + test_file_path = f"file://{self.temp_dir}/pickle_test.txt" |
| 141 | + test_content = b"pickle test content" |
| 142 | + |
| 143 | + with deserialized_file_io.new_output_stream(test_file_path) as stream: |
| 144 | + stream.write(test_content) |
| 145 | + |
| 146 | + expected_path = f"{self.temp_dir}/pickle_test.txt" |
| 147 | + self.assertTrue(os.path.exists(expected_path)) |
| 148 | + with open(expected_path, 'rb') as f: |
| 149 | + self.assertEqual(f.read(), test_content) |
| 150 | + |
| 151 | + def test_dlf_oss_endpoint_overrides_token_endpoint(self): |
| 152 | + """Test that DLF OSS endpoint overrides the standard OSS endpoint in token.""" |
| 153 | + dlf_oss_endpoint = "https://dlf-custom-endpoint.oss-cn-hangzhou.aliyuncs.com" |
| 154 | + catalog_options = { |
| 155 | + CatalogOptions.DLF_OSS_ENDPOINT: dlf_oss_endpoint |
| 156 | + } |
| 157 | + |
| 158 | + with patch.object(RESTTokenFileIO, 'try_to_refresh_token'): |
| 159 | + file_io = RESTTokenFileIO( |
| 160 | + self.identifier, |
| 161 | + self.warehouse_path, |
| 162 | + catalog_options |
| 163 | + ) |
| 164 | + |
| 165 | + # Create a token with a standard OSS endpoint |
| 166 | + token = { |
| 167 | + OssOptions.OSS_ENDPOINT: "https://standard-endpoint.oss-cn-beijing.aliyuncs.com", |
| 168 | + "fs.oss.accessKeyId": "test-access-key", |
| 169 | + "fs.oss.accessKeySecret": "test-secret-key" |
| 170 | + } |
| 171 | + |
| 172 | + # Merge token with catalog options |
| 173 | + merged_token = file_io._merge_token_with_catalog_options(token) |
| 174 | + |
| 175 | + # Verify DLF OSS endpoint overrides the standard endpoint |
| 176 | + self.assertEqual( |
| 177 | + merged_token[OssOptions.OSS_ENDPOINT], |
| 178 | + dlf_oss_endpoint, |
| 179 | + "DLF OSS endpoint should override the standard OSS endpoint in token" |
| 180 | + ) |
| 181 | + # Verify other token properties are preserved |
| 182 | + self.assertEqual( |
| 183 | + merged_token["fs.oss.accessKeyId"], |
| 184 | + "test-access-key", |
| 185 | + "Other token properties should be preserved" |
| 186 | + ) |
| 187 | + self.assertEqual( |
| 188 | + merged_token["fs.oss.accessKeySecret"], |
| 189 | + "test-secret-key", |
| 190 | + "Other token properties should be preserved" |
| 191 | + ) |
| 192 | + |
113 | 193 |
|
114 | 194 | if __name__ == '__main__': |
115 | 195 | unittest.main() |
0 commit comments