|
2 | 2 | import shlex |
3 | 3 | import subprocess |
4 | 4 | import time |
| 5 | +from datetime import datetime |
5 | 6 |
|
6 | 7 | import pytest |
7 | 8 |
|
@@ -208,3 +209,87 @@ def test_protocol_prefixed_path(hdfs_cluster): |
208 | 209 |
|
209 | 210 | file_info = fs.ls(protocol_prefixed_path, detail=True) |
210 | 211 | assert len(file_info) == 0 |
| 212 | + |
| 213 | + |
| 214 | +def test_modified_nonexistent_path(hdfs_cluster): |
| 215 | + fs = WebHDFS( |
| 216 | + hdfs_cluster, |
| 217 | + user="testuser", |
| 218 | + data_proxy={"worker.example.com": "localhost"}, |
| 219 | + ) |
| 220 | + nonexistent_path = "/user/testuser/nonexistent_file.txt" |
| 221 | + |
| 222 | + with pytest.raises(FileNotFoundError): |
| 223 | + fs.modified(nonexistent_path) |
| 224 | + |
| 225 | + |
| 226 | +def test_modified_time(hdfs_cluster): |
| 227 | + fs = WebHDFS( |
| 228 | + hdfs_cluster, |
| 229 | + user="testuser", |
| 230 | + data_proxy={"worker.example.com": "localhost"}, |
| 231 | + ) |
| 232 | + dir_path = "/user/testuser/" |
| 233 | + file_path = f"{dir_path}/testfile.txt" |
| 234 | + |
| 235 | + fs.mkdir(dir_path) |
| 236 | + |
| 237 | + # Check first modified time for directories |
| 238 | + modified_dir_date: datetime = fs.modified(dir_path) |
| 239 | + |
| 240 | + # I think it is the only thing we can assume, but I'm not sure if the server has a different time |
| 241 | + assert modified_dir_date <= datetime.now() |
| 242 | + |
| 243 | + # Create a file and check modified time again |
| 244 | + with fs.open(file_path, "wb") as f: |
| 245 | + f.write(b"test content") |
| 246 | + |
| 247 | + modified_file_date: datetime = fs.modified(file_path) |
| 248 | + assert modified_file_date >= modified_dir_date |
| 249 | + assert modified_file_date <= datetime.now() |
| 250 | + |
| 251 | + |
| 252 | +# NOTE: These following two tests are a copy of the modified ones, as |
| 253 | +# WebHDFS does not have a created time API, we are using modified as a proxy. |
| 254 | + |
| 255 | + |
| 256 | +def test_created_nonexistent_path(hdfs_cluster): |
| 257 | + fs = WebHDFS( |
| 258 | + hdfs_cluster, |
| 259 | + user="testuser", |
| 260 | + data_proxy={"worker.example.com": "localhost"}, |
| 261 | + ) |
| 262 | + nonexistent_path = "/user/testuser/nonexistent_file.txt" |
| 263 | + |
| 264 | + with pytest.raises(FileNotFoundError): |
| 265 | + fs.created(nonexistent_path) |
| 266 | + |
| 267 | + |
| 268 | +def test_created_time(hdfs_cluster): |
| 269 | + fs = WebHDFS( |
| 270 | + hdfs_cluster, |
| 271 | + user="testuser", |
| 272 | + data_proxy={"worker.example.com": "localhost"}, |
| 273 | + ) |
| 274 | + dir_path = "/user/testuser/" |
| 275 | + file_path = f"{dir_path}/testfile.txt" |
| 276 | + |
| 277 | + fs.mkdir(dir_path) |
| 278 | + |
| 279 | + time.sleep(1) |
| 280 | + |
| 281 | + # Check first created time for directories |
| 282 | + created_dir_date: datetime = fs.created(dir_path) |
| 283 | + |
| 284 | + # I think it is the only thing we can assume, but I'm not sure if the server has a different time |
| 285 | + assert created_dir_date < datetime.now() |
| 286 | + |
| 287 | + # Create a file and check created time again |
| 288 | + with fs.open(file_path, "wb") as f: |
| 289 | + f.write(b"test content") |
| 290 | + |
| 291 | + time.sleep(1) |
| 292 | + |
| 293 | + created_file_date: datetime = fs.created(file_path) |
| 294 | + assert created_file_date > created_dir_date |
| 295 | + assert created_file_date < datetime.now() |
0 commit comments