Beispiel #1
0
 def test_present_with_failure(self):
     self.conn.list_functions.side_effect = [{
         'Functions': []
     }, {
         'Functions': [function_ret]
     }]
     self.conn.create_function.side_effect = ClientError(
         error_content, 'create_function')
     with patch.dict(
             funcs,
         {'boto_iam.get_account_id': MagicMock(return_value='1234')}):
         with TempZipFile() as zipfile:
             with patch('hashlib.sha256') as sha256:
                 with patch('os.path.getsize', return_value=199):
                     sha = sha256()
                     digest = sha.digest()
                     encoded = sha.encode()
                     encoded.strip.return_value = function_ret['CodeSha256']
                     result = salt_states['boto_lambda.function_present'](
                         'function present',
                         FunctionName=function_ret['FunctionName'],
                         Runtime=function_ret['Runtime'],
                         Role=function_ret['Role'],
                         Handler=function_ret['Handler'],
                         ZipFile=zipfile,
                         Description=function_ret['Description'],
                         Timeout=function_ret['Timeout'])
     self.assertFalse(result['result'])
     self.assertTrue('An error occurred' in result['comment'])
Beispiel #2
0
    def test_present_when_function_does_not_exist(self):
        '''
        Tests present on a function that does not exist.
        '''
        self.conn.list_functions.side_effect = [{
            'Functions': []
        }, {
            'Functions': [function_ret]
        }]
        self.conn.create_function.return_value = function_ret
        with patch.dict(
                funcs,
            {'boto_iam.get_account_id': MagicMock(return_value='1234')}):
            with TempZipFile() as zipfile:
                result = salt_states['boto_lambda.function_present'](
                    'function present',
                    FunctionName=function_ret['FunctionName'],
                    Runtime=function_ret['Runtime'],
                    Role=function_ret['Role'],
                    Handler=function_ret['Handler'],
                    ZipFile=zipfile)

        self.assertTrue(result['result'])
        self.assertEqual(result['changes']['new']['function']['FunctionName'],
                         function_ret['FunctionName'])
Beispiel #3
0
    def test_present_when_function_exists(self):
        self.conn.list_functions.return_value = {'Functions': [function_ret]}
        self.conn.update_function_code.return_value = function_ret

        with patch.dict(
                funcs,
            {'boto_iam.get_account_id': MagicMock(return_value='1234')}):
            with TempZipFile() as zipfile:
                with patch('hashlib.sha256') as sha256:
                    with patch('os.path.getsize', return_value=199):
                        sha = sha256()
                        digest = sha.digest()
                        encoded = sha.encode()
                        encoded.strip.return_value = function_ret['CodeSha256']
                        result = salt_states['boto_lambda.function_present'](
                            'function present',
                            FunctionName=function_ret['FunctionName'],
                            Runtime=function_ret['Runtime'],
                            Role=function_ret['Role'],
                            Handler=function_ret['Handler'],
                            ZipFile=zipfile,
                            Description=function_ret['Description'],
                            Timeout=function_ret['Timeout'])
        self.assertTrue(result['result'])
        self.assertEqual(result['changes'], {})
Beispiel #4
0
    def test_present_when_function_exists_and_permissions(self):
        self.conn.list_functions.return_value = {'Functions': [function_ret]}
        self.conn.update_function_code.return_value = function_ret
        self.conn.get_policy.return_value = {
          "Policy": json.dumps(
            {"Version": "2012-10-17",
             "Statement": [
               {"Condition":
                {"ArnLike": {"AWS:SourceArn": "arn:aws:events:us-east-1:9999999999:rule/fooo"}},
                "Action": "lambda:InvokeFunction",
                "Resource": "arn:aws:lambda:us-east-1:999999999999:function:testfunction",
                "Effect": "Allow",
                "Principal": {"Service": "events.amazonaws.com"},
                "Sid": "AWSEvents_foo-bar999999999999"}],
             "Id": "default"})
        }

        with patch.dict(funcs, {'boto_iam.get_account_id': MagicMock(return_value='1234')}):
            with TempZipFile() as zipfile:
                with patch('hashlib.sha256') as sha256:
                    with patch('os.path.getsize', return_value=199):
                        sha = sha256()
                        digest = sha.digest()
                        encoded = sha.encode()
                        encoded.strip.return_value = function_ret['CodeSha256']
                        result = salt_states['boto_lambda.function_present'](
                                     'function present',
                                     FunctionName=function_ret['FunctionName'],
                                     Runtime=function_ret['Runtime'],
                                     Role=function_ret['Role'],
                                     Handler=function_ret['Handler'],
                                     ZipFile=zipfile,
                                     Description=function_ret['Description'],
                                     Timeout=function_ret['Timeout'])
        self.assertTrue(result['result'])
        self.assertEqual(result['changes'], {
          'old': {
            'Permissions': {
              'AWSEvents_foo-bar999999999999':
              {'Action': 'lambda:InvokeFunction',
               'Principal': 'events.amazonaws.com',
               'SourceArn': 'arn:aws:events:us-east-1:9999999999:rule/fooo'}}},
          'new': {
            'Permissions': {
              'AWSEvents_foo-bar999999999999': {}}}})