Coverage for tests/test_s3.py: 25%

79 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-02-16 02:51 -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. 

11 

12import unittest 

13 

14from lsst.resources import ResourcePath 

15from lsst.resources.tests import GenericReadWriteTestCase, GenericTestCase 

16 

17try: 

18 import boto3 

19 import botocore 

20 from moto import mock_s3 

21except ImportError: 

22 boto3 = None 

23 

24 def mock_s3(cls): 

25 """A no-op decorator in case moto mock_s3 can not be imported.""" 

26 return cls 

27 

28 

29class GenericS3TestCase(GenericTestCase, unittest.TestCase): 

30 scheme = "s3" 

31 netloc = "my_bucket" 

32 

33 

34@unittest.skipIf(not boto3, "Warning: boto3 AWS SDK not found!") 

35class S3ReadWriteTestCase(GenericReadWriteTestCase, unittest.TestCase): 

36 scheme = "s3" 

37 netloc = "my_2nd_bucket" 

38 

39 mock_s3 = mock_s3() 

40 """The mocked s3 interface from moto.""" 

41 

42 def setUp(self): 

43 # Enable S3 mocking of tests. 

44 self.mock_s3.start() 

45 

46 # set up some fake credentials if they do not exist 

47 # self.usingDummyCredentials = setAwsEnvCredentials() 

48 

49 # MOTO needs to know that we expect Bucket bucketname to exist 

50 s3 = boto3.resource("s3") 

51 s3.create_bucket(Bucket=self.netloc) 

52 

53 super().setUp() 

54 

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 

66 

67 bucket = s3.Bucket(self.netloc) 

68 bucket.delete() 

69 

70 # Stop the S3 mock. 

71 self.mock_s3.stop() 

72 

73 super().tearDown() 

74 

75 def test_bucket_fail(self): 

76 # Deliberately create URI with unknown bucket. 

77 uri = ResourcePath("s3://badbucket/something/") 

78 

79 with self.assertRaises(ValueError): 

80 uri.mkdir() 

81 

82 with self.assertRaises(FileNotFoundError): 

83 uri.remove() 

84 

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\%") 

94 

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\%") 

99 

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) 

109 

110 # verify file can't be seeked back 

111 with self.assertRaises(OSError): 

112 handle.seek(0) 

113 

114 # write more bytes 

115 handle.write(1024 * b"c") 

116 

117 # seek back and overwrite 

118 handle.seek(6 * 1024 * 1024) 

119 handle.write(1024 * b"b") 

120 

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") 

134 

135 

136if __name__ == "__main__": 136 ↛ 137line 136 didn't jump to line 137, because the condition on line 136 was never true

137 unittest.main()