def s3_mock(request, os_environ): # we don't want any real aws credentials this environment might have used in the tests os_environ.update({ "AWS_ACCESS_KEY_ID": "AKIAIABCDABCDABCDABC", "AWS_SECRET_ACCESS_KEY": "foobarfoobarfoobarfoobarfoobarfoobarfoob", }) m = mock_s3() m.start() yield m m.stop()
def test_my_model_save(): mock = mock_s3() mock.start() conn = boto3.resource('s3', region_name='us-east-1') conn.create_bucket(Bucket='mybucket') model_instance = S3Obj('steve', 'is awesome') model_instance.save() body = conn.Object('mybucket', 'steve') print(body) mock.stop()
def s3_utils(): LOGGER.info(f">>>>> Setup mock S3") with mock_s3(): s3 = boto3.client("s3") s3.create_bucket(Bucket=S3_BUCKET_NAME) s3.put_object(Bucket=S3_BUCKET_NAME, Key=S3_KEY, Body=b"col0\n1") s3.put_object(Bucket=S3_BUCKET_NAME, Key=S3_FOLDER_FILE_0, Body=b"col0\n1") s3.put_object(Bucket=S3_BUCKET_NAME, Key=S3_FOLDER_FILE_1, Body=b"col0\n1") yield S3Utils(MOCK_AWS_CONFIG) LOGGER.info(f">>>>> Teardown mock S3")
def setUpMock(self): """ Let Moto take over all socket communications """ self.ec2mock = mock_ec2() self.ec2mock.start() self.s3mock = mock_s3() self.s3mock.start() HTTPretty.register_uri( method="GET", uri="https://swift.rc.nectar.org.au:8888/v1/" "AUTH_377/cloud-bridge/aws/instance_data.json", body=""" [ { "family": "General Purpose", "enhanced_networking": false, "vCPU": 1, "generation": "previous", "ebs_iops": 0, "network_performance": "Low", "ebs_throughput": 0, "vpc": { "ips_per_eni": 4, "max_enis": 2 }, "arch": [ "i386", "x86_64" ], "linux_virtualization_types": [], "ebs_optimized": false, "storage": { "ssd": false, "devices": 1, "size": 160 }, "max_bandwidth": 0, "instance_type": "t1.micro", "ECU": 1.0, "memory": 1.7 } ] """ )
def setUpMock(self): """ Let Moto take over all socket communications """ self.ec2mock = mock_ec2() self.ec2mock.start() self.s3mock = mock_s3() self.s3mock.start() HTTPretty.register_uri( method="GET", uri="https://d168wakzal7fp0.cloudfront.net/aws_instance_data.json", body=""" [ { "family": "General Purpose", "enhanced_networking": false, "vCPU": 1, "generation": "current", "ebs_iops": 0, "network_performance": "Low", "ebs_throughput": 0, "vpc": { "ips_per_eni": 2, "max_enis": 2 }, "arch": [ "x86_64" ], "linux_virtualization_types": [ "HVM" ], "ebs_optimized": false, "storage": null, "max_bandwidth": 0, "instance_type": "t2.nano", "ECU": "variable, "memory": 0.5, "ebs_max_bandwidth": 0 } ] """ )
def remote(cls): with mock_s3(): yield RemoteS3(None, {"url": cls.get_url()})
def s3_client(): with mock_s3(): s3 = boto3.client('s3') yield s3
def s3(): with mock_s3(): yield boto3.client('s3', region_name='us-east-1')
def remote(cls, repo): with mock_s3(): yield S3Remote(repo, {"url": cls.get_url()})
def apply_moto_mocks(): with mock_cloudformation(), mock_iam(), mock_s3(), mock_lambda(), mock_kms( ): boto3.setup_default_session() yield None
def s3(): """Mocked S3 Fixture.""" with mock_s3(): yield boto3.client('s3', region_name='us-east-1')