|
22 | 22 |
|
23 | 23 | import warehouse.packaging.services
|
24 | 24 |
|
25 |
| -from warehouse.packaging.interfaces import IDocsStorage, IFileStorage |
| 25 | +from warehouse.packaging.interfaces import IDocsStorage, IFileStorage, ISimpleStorage |
26 | 26 | from warehouse.packaging.services import (
|
27 | 27 | GCSFileStorage,
|
| 28 | + GCSSimpleStorage, |
| 29 | + GenericLocalBlobStorage, |
28 | 30 | LocalDocsStorage,
|
29 | 31 | LocalFileStorage,
|
| 32 | + LocalSimpleStorage, |
30 | 33 | S3DocsStorage,
|
31 | 34 | S3FileStorage,
|
32 | 35 | )
|
@@ -137,6 +140,67 @@ def test_delete_already_gone(self, tmpdir):
|
137 | 140 | assert response is None
|
138 | 141 |
|
139 | 142 |
|
| 143 | +class TestLocalSimpleStorage: |
| 144 | + def test_verify_service(self): |
| 145 | + assert verifyClass(ISimpleStorage, LocalSimpleStorage) |
| 146 | + |
| 147 | + def test_basic_init(self): |
| 148 | + storage = LocalSimpleStorage("/foo/bar/") |
| 149 | + assert storage.base == "/foo/bar/" |
| 150 | + |
| 151 | + def test_create_service(self): |
| 152 | + request = pretend.stub( |
| 153 | + registry=pretend.stub(settings={"simple.path": "/simple/one/two/"}) |
| 154 | + ) |
| 155 | + storage = LocalSimpleStorage.create_service(None, request) |
| 156 | + assert storage.base == "/simple/one/two/" |
| 157 | + |
| 158 | + def test_gets_file(self, tmpdir): |
| 159 | + with open(str(tmpdir.join("file.txt")), "wb") as fp: |
| 160 | + fp.write(b"my test file contents") |
| 161 | + |
| 162 | + storage = LocalSimpleStorage(str(tmpdir)) |
| 163 | + file_object = storage.get("file.txt") |
| 164 | + assert file_object.read() == b"my test file contents" |
| 165 | + |
| 166 | + def test_raises_when_file_non_existent(self, tmpdir): |
| 167 | + storage = LocalSimpleStorage(str(tmpdir)) |
| 168 | + with pytest.raises(FileNotFoundError): |
| 169 | + storage.get("file.txt") |
| 170 | + |
| 171 | + def test_stores_file(self, tmpdir): |
| 172 | + filename = str(tmpdir.join("testfile.txt")) |
| 173 | + with open(filename, "wb") as fp: |
| 174 | + fp.write(b"Test File!") |
| 175 | + |
| 176 | + storage_dir = str(tmpdir.join("storage")) |
| 177 | + storage = LocalSimpleStorage(storage_dir) |
| 178 | + storage.store("foo/bar.txt", filename) |
| 179 | + |
| 180 | + with open(os.path.join(storage_dir, "foo/bar.txt"), "rb") as fp: |
| 181 | + assert fp.read() == b"Test File!" |
| 182 | + |
| 183 | + def test_stores_two_files(self, tmpdir): |
| 184 | + filename1 = str(tmpdir.join("testfile1.txt")) |
| 185 | + with open(filename1, "wb") as fp: |
| 186 | + fp.write(b"First Test File!") |
| 187 | + |
| 188 | + filename2 = str(tmpdir.join("testfile2.txt")) |
| 189 | + with open(filename2, "wb") as fp: |
| 190 | + fp.write(b"Second Test File!") |
| 191 | + |
| 192 | + storage_dir = str(tmpdir.join("storage")) |
| 193 | + storage = LocalSimpleStorage(storage_dir) |
| 194 | + storage.store("foo/first.txt", filename1) |
| 195 | + storage.store("foo/second.txt", filename2) |
| 196 | + |
| 197 | + with open(os.path.join(storage_dir, "foo/first.txt"), "rb") as fp: |
| 198 | + assert fp.read() == b"First Test File!" |
| 199 | + |
| 200 | + with open(os.path.join(storage_dir, "foo/second.txt"), "rb") as fp: |
| 201 | + assert fp.read() == b"Second Test File!" |
| 202 | + |
| 203 | + |
140 | 204 | class TestS3FileStorage:
|
141 | 205 | def test_verify_service(self):
|
142 | 206 | assert verifyClass(IFileStorage, S3FileStorage)
|
@@ -479,3 +543,98 @@ def test_delete_by_prefix_with_storage_prefix(self):
|
479 | 543 | },
|
480 | 544 | ),
|
481 | 545 | ]
|
| 546 | + |
| 547 | + |
| 548 | +class TestGCSSimpleStorage: |
| 549 | + def test_verify_service(self): |
| 550 | + assert verifyClass(ISimpleStorage, GCSSimpleStorage) |
| 551 | + |
| 552 | + def test_basic_init(self): |
| 553 | + bucket = pretend.stub() |
| 554 | + storage = GCSSimpleStorage(bucket) |
| 555 | + assert storage.bucket is bucket |
| 556 | + |
| 557 | + def test_create_service(self): |
| 558 | + service = pretend.stub( |
| 559 | + get_bucket=pretend.call_recorder(lambda bucket_name: pretend.stub()) |
| 560 | + ) |
| 561 | + request = pretend.stub( |
| 562 | + find_service=pretend.call_recorder(lambda name: service), |
| 563 | + registry=pretend.stub(settings={"simple.bucket": "froblob"}), |
| 564 | + ) |
| 565 | + GCSSimpleStorage.create_service(None, request) |
| 566 | + |
| 567 | + assert request.find_service.calls == [pretend.call(name="gcloud.gcs")] |
| 568 | + assert service.get_bucket.calls == [pretend.call("froblob")] |
| 569 | + |
| 570 | + def test_gets_file_raises(self): |
| 571 | + storage = GCSSimpleStorage(pretend.stub()) |
| 572 | + |
| 573 | + with pytest.raises(NotImplementedError): |
| 574 | + storage.get("file.txt") |
| 575 | + |
| 576 | + def test_stores_file(self, tmpdir): |
| 577 | + filename = str(tmpdir.join("testfile.txt")) |
| 578 | + with open(filename, "wb") as fp: |
| 579 | + fp.write(b"Test File!") |
| 580 | + |
| 581 | + blob = pretend.stub( |
| 582 | + upload_from_filename=pretend.call_recorder(lambda file_path: None), |
| 583 | + exists=lambda: False, |
| 584 | + ) |
| 585 | + bucket = pretend.stub(blob=pretend.call_recorder(lambda path: blob)) |
| 586 | + storage = GCSSimpleStorage(bucket) |
| 587 | + storage.store("foo/bar.txt", filename) |
| 588 | + |
| 589 | + assert bucket.blob.calls == [pretend.call("foo/bar.txt")] |
| 590 | + assert blob.upload_from_filename.calls == [pretend.call(filename)] |
| 591 | + |
| 592 | + def test_stores_two_files(self, tmpdir): |
| 593 | + filename1 = str(tmpdir.join("testfile1.txt")) |
| 594 | + with open(filename1, "wb") as fp: |
| 595 | + fp.write(b"First Test File!") |
| 596 | + |
| 597 | + filename2 = str(tmpdir.join("testfile2.txt")) |
| 598 | + with open(filename2, "wb") as fp: |
| 599 | + fp.write(b"Second Test File!") |
| 600 | + |
| 601 | + blob = pretend.stub( |
| 602 | + upload_from_filename=pretend.call_recorder(lambda file_path: None), |
| 603 | + exists=lambda: False, |
| 604 | + ) |
| 605 | + bucket = pretend.stub(blob=pretend.call_recorder(lambda path: blob)) |
| 606 | + storage = GCSSimpleStorage(bucket) |
| 607 | + storage.store("foo/first.txt", filename1) |
| 608 | + storage.store("foo/second.txt", filename2) |
| 609 | + |
| 610 | + assert bucket.blob.calls == [ |
| 611 | + pretend.call("foo/first.txt"), |
| 612 | + pretend.call("foo/second.txt"), |
| 613 | + ] |
| 614 | + assert blob.upload_from_filename.calls == [ |
| 615 | + pretend.call(filename1), |
| 616 | + pretend.call(filename2), |
| 617 | + ] |
| 618 | + |
| 619 | + def test_stores_metadata(self, tmpdir): |
| 620 | + filename = str(tmpdir.join("testfile.txt")) |
| 621 | + with open(filename, "wb") as fp: |
| 622 | + fp.write(b"Test File!") |
| 623 | + |
| 624 | + blob = pretend.stub( |
| 625 | + upload_from_filename=pretend.call_recorder(lambda file_path: None), |
| 626 | + patch=pretend.call_recorder(lambda: None), |
| 627 | + exists=lambda: False, |
| 628 | + ) |
| 629 | + bucket = pretend.stub(blob=pretend.call_recorder(lambda path: blob)) |
| 630 | + storage = GCSSimpleStorage(bucket) |
| 631 | + meta = {"foo": "bar"} |
| 632 | + storage.store("foo/bar.txt", filename, meta=meta) |
| 633 | + |
| 634 | + assert blob.metadata == meta |
| 635 | + |
| 636 | + |
| 637 | +class TestGenericLocalBlobStorage: |
| 638 | + def test_notimplementederror(self): |
| 639 | + with pytest.raises(NotImplementedError): |
| 640 | + GenericLocalBlobStorage.create_service(pretend.stub(), pretend.stub()) |
0 commit comments