Esempio n. 1
0
    def test_inject_settings(self):

        with mock.patch.object(boto3, 'client') as mock_boto3_client:

            zip_content = StringIO.StringIO()
            zip_file = zipfile.ZipFile(zip_content, 'w')
            zip_file.close()

            mock_body = mock.MagicMock()
            mock_body.read = mock.MagicMock(return_value=zip_content.getvalue())

            mock_s3_client = mock_boto3_client.return_value
            mock_s3_client.get_object = mock.MagicMock(return_value={'Body': mock_body})
            mock_s3_client.put_object = mock.MagicMock()

            reload(LambdaConfigurationResourceHandler) # so it uses mocked methods

            settings = self.event['ResourceProperties']['Settings']
            runtime = self.event['ResourceProperties']['Runtime']
            bucket = self.event['ResourceProperties']['ConfigurationBucket']
            input_key = self.event['ResourceProperties']['ConfigurationKey']
            function_name = self.event['ResourceProperties']['FunctionName']

            mock_injector = mock.MagicMock()
            LambdaConfigurationResourceHandler._SETTINGS_INJECTORS[runtime] = mock_injector

            output_key = LambdaConfigurationResourceHandler._inject_settings(settings, runtime, bucket, input_key, function_name)

            mock_boto3_client.assert_called_with('s3')
            mock_s3_client.get_object.assert_called_once_with(Bucket=bucket, Key=input_key)
            mock_injector.assert_called_once_with(AnyZipFileObject(), settings)
            mock_s3_client.put_object.assert_called_once_with(Bucket='TestBucket', Key=output_key, Body=AnyValidZipFileContent())
Esempio n. 2
0
    def test_integration_inject_settings_python(self):
        
        # we need both the s3 client below and the one created by the 
        # custom resource handler to use this region
        boto3.setup_default_session(region_name=TEST_REGION)
        reload(LambdaConfigurationResourceHandler) # reset global s3 client object

        s3 = boto3.client('s3')

        bucket = 'lmbr_aws_settings_test_' + str(int(time() * 1000))
        input_key = 'TestKey/lambda-function-code.zip'
        output_key = None

        s3.create_bucket(Bucket=bucket)

        try:

            zip_content = StringIO.StringIO()

            with zipfile.ZipFile(zip_content, 'w') as zip_file:
                zip_file.writestr('InitialName', 'InitialContent')

            body = zip_content.getvalue()
            s3.put_object(Bucket=bucket, Key=input_key, Body=body)

            zip_content.close()

            sleep(10) # seconds

            expected_settings = self.event['ResourceProperties']['Settings']
            runtime = 'python2.7'
            function_name = 'TestFunction'
            output_key = LambdaConfigurationResourceHandler._inject_settings(expected_settings, runtime, bucket, input_key, function_name)

            expected_zip_name = 'CloudCanvas/settings.py'

            sleep(10) # seconds

            print 'output_key', output_key
            print 'bucket', bucket
            res = s3.get_object(Bucket=bucket, Key=output_key)
            body = res['Body'].read()

            zip_content = StringIO.StringIO(body)
            with zipfile.ZipFile(zip_content, 'r') as zip_file:
                with zip_file.open('InitialName', 'r') as zip_content_file:
                    actual_zip_content = zip_content_file.read()
                    self.assertEquals('InitialContent', actual_zip_content)
                with zip_file.open(expected_zip_name, 'r') as zip_content_file:
                    globals = {}
                    exec(zip_content_file.read(), globals)
                    actual_settings = globals['settings']
                    self.assertEquals(expected_settings, actual_settings)
                    zip_content.close()

        finally:

            try:
                s3.delete_object(Bucket=bucket, Key=input_key)
            except Exception as e:
                print 'Error when deleting object {} from bucket {}: {}'.format(input_key, bucket, e)

            if output_key is not None:
                try:
                    s3.delete_object(Bucket=bucket, Key=output_key)
                except Exception as e:
                    print 'Error when deleting object {} from bucket {}: {}'.format(output_key, bucket, e)

            try:
                s3.delete_bucket(Bucket=bucket)
            except Exception as e:
                print 'Error when deleting bucket {}: {}'.format(bucket, e)