Coverage for tests/test_s3.py: 35%
56 statements
« prev ^ index » next coverage.py v6.4, created at 2022-06-01 11:01 +0000
« prev ^ index » next coverage.py v6.4, created at 2022-06-01 11:01 +0000
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 GenericFileTestCase(GenericTestCase, unittest.TestCase):
30 scheme = "s3"
31 netloc = "my_bucket"
34@unittest.skipIf(not boto3, "Warning: boto3 AWS SDK not found!")
35class FileReadWriteTestCase(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\%")
101if __name__ == "__main__": 101 ↛ 102line 101 didn't jump to line 102, because the condition on line 101 was never true
102 unittest.main()