Exemple #1
0
    def test_class_properties(self):
        ctx = self._get_ctx()
        with patch(PATCH_PREFIX + 'LambdaBase'):
            fun = function.LambdaFunction(ctx)
            fun.resource_id = 'test_function'
            fake_client = self.make_client_function('get_function',
                                                    return_value={
                                                        'Configuration': {
                                                            'FunctionName':
                                                            'test_function'
                                                        }
                                                    })
            fun.client = fake_client

            result = fun.properties
            self.assertEqual(result, {'FunctionName': 'test_function'})

            fake_client = self.make_client_function(
                'get_function', return_value={'Configuration': None})
            fun.client = fake_client
            result = fun.properties
            self.assertIsNone(result)

            fake_client = self.make_client_function(
                'get_function',
                side_effect=self.get_client_error_exception('get_function'))

            fun.client = fake_client
            result = fun.properties
            self.assertIsNone(result)
Exemple #2
0
    def test_create(self, mock_subnet_attach, mock_security_groups_attach,
                    mock_iam_role_attach):
        ctx = MockCloudifyContext("test_create")
        zip_file = self._mock_function_file()
        with patch(PATCH_PREFIX + 'LambdaBase'):
            fun = function.LambdaFunction(ctx)
            fun.logger = MagicMock()
            fun.resource_id = 'test_function'
            fake_client = self.make_client_function('create_function',
                                                    return_value={
                                                        'FunctionArn':
                                                        'test_function_arn',
                                                        'FunctionName':
                                                        'test_function'
                                                    })
            fun.client = fake_client
            resource_config = {
                'VpcConfig': {
                    'SubnetIds': []
                },
                'Code': {
                    'ZipFile': zip_file
                }
            }
            mock_iam_role_attach.return_value = ['role-1']
            mock_security_groups_attach.return_value = ['sg-1']
            mock_subnet_attach.return_value = ['subnet-1']

            function.create(ctx, fun, resource_config)
            self.assertEqual(zip_file, resource_config['Code']['ZipFile'])
            self.assertEqual(
                {
                    'SubnetIds': ['subnet-1'],
                    'SecurityGroupIds': ['sg-1']
                }, resource_config['VpcConfig'])
Exemple #3
0
    def test_class_invoke_payload(self):
        ctx = self._get_ctx()
        with patch(PATCH_PREFIX + 'LambdaBase'):
            fun = function.LambdaFunction(ctx)
            fun.logger = MagicMock()
            fun.resource_id = 'test_function'
            fake_client = self.make_client_function(
                'invoke',
                side_effect=lambda FunctionName, Payload: self.assertEqual(
                    Payload, 0))
            fun.client = fake_client
            result = fun.invoke({'Payload': 0})
            self.assertEqual(result, None)

            fake_client = self.make_client_function(
                'invoke',
                side_effect=lambda FunctionName, Payload: self.assertEqual(
                    Payload, u'{"key": "value"}'))

            fun.client = fake_client
            result = fun.invoke({'Payload': {"key": "value"}})
            self.assertEqual(result, None)

            self._mock_function_file()
            fake_client = self.make_client_function(
                'invoke',
                side_effect=lambda FunctionName, Payload: self.assertEqual(
                    Payload.read(), 'test'))
            fun.client = fake_client
            result = fun.invoke({'Payload': '/tmp/mock_function.txt'})
            self.assertEqual(result, None)
Exemple #4
0
 def test_class_delete(self):
     ctx = self._get_ctx()
     with patch(PATCH_PREFIX + 'LambdaBase'):
         fun = function.LambdaFunction(ctx)
         fun.logger = MagicMock()
         fun.resource_id = 'test_function'
         fake_client = self.make_client_function('delete_function',
                                                 return_value=None)
         fun.client = fake_client
         fun.delete({'param': 'params'})
Exemple #5
0
    def test_class_invoke(self):
        ctx = self._get_ctx()
        with patch(PATCH_PREFIX + 'LambdaBase'):
            fun = function.LambdaFunction(ctx)
            fun.logger = MagicMock()
            fun.resource_id = 'test_function'
            fake_client = self.make_client_function(
                'invoke', return_value={'Payload': StringIO(u"text")})
            fun.client = fake_client
            result = fun.invoke({'param': 'params'})
            self.assertEqual(result, {'Payload': u'text'})

            fake_client = self.make_client_function('invoke', return_value='')
            fun.client = fake_client
            result = fun.invoke({'param': 'params'})
            self.assertEqual(result, '')
Exemple #6
0
 def test_class_create(self):
     ctx = self._get_ctx()
     with patch(PATCH_PREFIX + 'LambdaBase'):
         fun = function.LambdaFunction(ctx)
         fun.logger = MagicMock()
         fun.resource_id = 'test_function'
         fake_client = self.make_client_function('create_function',
                                                 return_value={
                                                     'FunctionArn':
                                                     'test_function_arn',
                                                     'FunctionName':
                                                     'test_function'
                                                 })
         fun.client = fake_client
         create_response = fun.create({'param': 'params'})
         self.assertEqual(create_response['FunctionName'], fun.resource_id)
         self.assertEqual(create_response['FunctionArn'],
                          'test_function_arn')
Exemple #7
0
 def test_create_with_download(self):
     subnettarget = MockRelationshipContext(
         target=MockCloudifyContext('subnet'))
     subnettarget.target.node.type_hierarchy =\
         ['cloudify.nodes.aws.ec2.Subnet']
     ctx = MockCloudifyContext("test_create")
     ctx.download_resource = MagicMock(return_value='abc')
     with patch(PATCH_PREFIX + 'LambdaBase'),\
         patch(PATCH_PREFIX + 'utils') as utils,\
         patch(PATCH_PREFIX + 'path_exists',
               MagicMock(return_value=False)),\
         patch(PATCH_PREFIX + 'os_remove', MagicMock(return_value=True)),\
         patch(PATCH_PREFIX + 'open',
               MagicMock(return_value=StringIO(u"test"))):
         fun = function.LambdaFunction(ctx)
         fun.logger = MagicMock()
         fun.resource_id = 'test_function'
         fake_client = self.make_client_function('create_function',
                                                 return_value={
                                                     'FunctionArn':
                                                     'test_function_arn',
                                                     'FunctionName':
                                                     'test_function'
                                                 })
         fun.client = fake_client
         resource_config = {
             'VpcConfig': {
                 'SubnetIds': []
             },
             'Code': {
                 'ZipFile': True
             }
         }
         utils.find_rels_by_node_type = MagicMock(
             return_value=[subnettarget])
         utils.get_resource_id = MagicMock(return_value='test_id')
         utils.find_rel_by_node_type = MagicMock(return_value=subnettarget)
         utils.get_resource_id = MagicMock(return_value='Role')
         function.create(ctx, fun, resource_config)
         self.assertEqual(True, resource_config['Code']['ZipFile'])
         self.assertEqual({'SubnetIds': []}, resource_config['VpcConfig'])
Exemple #8
0
    def test_class_status(self):
        ctx = self._get_ctx()
        with patch(PATCH_PREFIX + 'LambdaBase'):
            fun = function.LambdaFunction(ctx)
            fun.resource_id = 'test_function'
            fake_client = self.make_client_function('get_function',
                                                    return_value={
                                                        'Configuration': {
                                                            'FunctionName':
                                                            'test_function'
                                                        }
                                                    })
            fun.client = fake_client
            status = fun.status
            self.assertEqual(status, 'available')

            fake_client = self.make_client_function('get_function',
                                                    return_value={})
            fun.client = fake_client
            status = fun.status
            self.assertIsNone(status)