Coverage for tests/test_s3.py: 25%
79 statements
« prev ^ index » next coverage.py v6.5.0, created at 2023-03-09 03:06 -0800
« prev ^ index » next coverage.py v6.5.0, created at 2023-03-09 03:06 -0800
1# This file is part of lsst-resources.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (https://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# Use of this source code is governed by a 3-clause BSD-style
10# license that can be found in the LICENSE file.
12import unittest
14from lsst.resources import ResourcePath
15from lsst.resources.tests import GenericReadWriteTestCase, GenericTestCase
17try:
18 import boto3
19 import botocore
20 from moto import mock_s3
21except ImportError:
22 boto3 = None
24 def mock_s3(cls):
25 """A no-op decorator in case moto mock_s3 can not be imported."""
26 return cls
29class GenericS3TestCase(GenericTestCase, unittest.TestCase):
30 scheme = "s3"
31 netloc = "my_bucket"
34@unittest.skipIf(not boto3, "Warning: boto3 AWS SDK not found!")
35class S3ReadWriteTestCase(GenericReadWriteTestCase, unittest.TestCase):
36 scheme = "s3"
37 netloc = "my_2nd_bucket"
39 mock_s3 = mock_s3()
40 """The mocked s3 interface from moto."""
42 def setUp(self):
43 # Enable S3 mocking of tests.
44 self.mock_s3.start()
46 # set up some fake credentials if they do not exist
47 # self.usingDummyCredentials = setAwsEnvCredentials()
49 # MOTO needs to know that we expect Bucket bucketname to exist
50 s3 = boto3.resource("s3")
51 s3.create_bucket(Bucket=self.netloc)
53 super().setUp()
55 def tearDown(self):
56 s3 = boto3.resource("s3")
57 bucket = s3.Bucket(self.netloc)
58 try:
59 bucket.objects.all().delete()
60 except botocore.exceptions.ClientError as e:
61 if e.response["Error"]["Code"] == "404":
62 # the key was not reachable - pass
63 pass
64 else:
65 raise
67 bucket = s3.Bucket(self.netloc)
68 bucket.delete()
70 # Stop the S3 mock.
71 self.mock_s3.stop()
73 super().tearDown()
75 def test_bucket_fail(self):
76 # Deliberately create URI with unknown bucket.
77 uri = ResourcePath("s3://badbucket/something/")
79 with self.assertRaises(ValueError):
80 uri.mkdir()
82 with self.assertRaises(FileNotFoundError):
83 uri.remove()
85 def test_transfer_progress(self):
86 """Test progress bar reporting for upload and download."""
87 remote = self.root_uri.join("test.dat")
88 remote.write(b"42")
89 with ResourcePath.temporary_uri(suffix=".dat") as tmp:
90 # Download from S3.
91 with self.assertLogs("lsst.resources", level="DEBUG") as cm:
92 tmp.transfer_from(remote, transfer="auto")
93 self.assertRegex("".join(cm.output), r"test\.dat.*100\%")
95 # Upload to S3.
96 with self.assertLogs("lsst.resources", level="DEBUG") as cm:
97 remote.transfer_from(tmp, transfer="auto", overwrite=True)
98 self.assertRegex("".join(cm.output), rf"{tmp.basename()}.*100\%")
100 def test_handle(self):
101 remote = self.root_uri.join("test_handle.dat")
102 with remote.open("wb") as handle:
103 self.assertTrue(handle.writable())
104 # write 6 megabytes to make sure partial write work
105 handle.write(6 * 1024 * 1024 * b"a")
106 self.assertEqual(handle.tell(), 6 * 1024 * 1024)
107 handle.flush()
108 self.assertGreaterEqual(len(handle._multiPartUpload), 1)
110 # verify file can't be seeked back
111 with self.assertRaises(OSError):
112 handle.seek(0)
114 # write more bytes
115 handle.write(1024 * b"c")
117 # seek back and overwrite
118 handle.seek(6 * 1024 * 1024)
119 handle.write(1024 * b"b")
121 with remote.open("rb") as handle:
122 self.assertTrue(handle.readable())
123 # read the first 6 megabytes
124 result = handle.read(6 * 1024 * 1024)
125 self.assertEqual(result, 6 * 1024 * 1024 * b"a")
126 self.assertEqual(handle.tell(), 6 * 1024 * 1024)
127 # verify additional read gets the next part
128 result = handle.read(1024)
129 self.assertEqual(result, 1024 * b"b")
130 # see back to the beginning to verify seeking
131 handle.seek(0)
132 result = handle.read(1024)
133 self.assertEqual(result, 1024 * b"a")
136if __name__ == "__main__": 136 ↛ 137line 136 didn't jump to line 137, because the condition on line 136 was never true
137 unittest.main()