我有这个功能,它将存档文件上传到S3存储桶:
def upload_file_to_s3_bucket(self, bucket, file, key, log):
if not os.path.exists(file):
log.error("File '%s' does not exist." % file)
tools.exit_gracefully(log)
log.info("Uploading file '%s' to bucket '%s' ..." % (file, bucket))
try:
self._s3.upload_file(file, bucket, key)
except botocore.exceptions.ClientError as e:
log.error("Unexpected uploading error : %s" % e)
tools.exit_gracefully(log)
log.info("Uploading finished.")
我想对它进行单元测试,这是我到目前为止所能写的:
class TestUploadFilesToS3(unittest.TestCase):
""" Tests unitaires upload_file_to_s3_bucket"""
def setUp(self):
conf.LOG_FILE = "/tmp/test.log"
conf.BUCKET_OUTPUT="name.of.the.bucket"
conf.Conf.get_level_log()
self.log = logger(conf.LOG_FILE, conf.LEVEL_LOG).logger
tools.create_workdir(self.log)
conf.WORKDIR = os.path.join(conf.LOCAL_DIR, "files/output")
archive = "file_archive.tar.gz"
archivePath = "/tmp/clients/file_archive.tar.gz"
_aws = None
def tearDown(self):
tools.delete_workdir(self.log)
os.remove(conf.LOG_FILE)
def test_upload_file_to_s3_bucket_success(self):
self._aws.upload_file_to_s3_bucket(conf.BUCKET_OUTPUT, archivePath, archive, self._log)
要进行单元测试,我不知道我应该在测试函数test_upload_file_to_s3_bucket_success
中使用哪个函数Assert,我应该准确比较什么。
我可以测试一下文件的URL是否存在......?有任何想法吗?
谢谢
答案 0 :(得分:2)
您可以使用此库模拟与S3的交互:
答案 1 :(得分:1)
我建议使用botocore stubber。该文档页面上有几个示例可帮助您入门。
答案 2 :(得分:0)
这是我为s3上传功能编写的测试的剪辑
self.ti.uploadTemplate(contentsOfFile) # this is what is being tested
# also supplied from elsewhere "contentsOfFile" and "nameOfFile"
# bucket is assumed to be called "cloud-test-cf"
s3 = boto3.resource('s3')
mytname = nameOfFile
obj = s3.Object(bucket_name='cloud-test-cf', key=mytname)
response = obj.get()
self.assertEqual(response['ContentLength'], len(contentsOfFile))
remoteData = response['Body'].read()
self.assertEqual(remoteData, contentsOfFile)
正如您所看到的那样,在使用“uploadTemplate”之后立即获取文件。如果您使用相同的区域/ AZ设置,那么它应该可以正常工作,请参阅http://docs.aws.amazon.com/AmazonS3/latest/dev/Introduction.html#ConsistencyModel
此测试的unittest方法使用像这样的装饰器
@unittest.skipIf(
not(boto3.session.Config().region_name),
"aws creds not loaded")
def testuploadTemplate(self):
#....test code here
此装饰器意味着如果运行unittest套件但AWS键不可用,则跳过此特定测试