Exemplo n.º 1
0
    def test_publish(self, _load_config, client, get_cfn_template,
                     _print_status):
        mock_cfn = mock.MagicMock()
        mock_lmb = mock.MagicMock()
        mock_cfn.describe_stacks.return_value = describe_stacks_response
        mock_lmb.publish_version.return_value = {'Version': '3'}
        client.side_effect = [mock_cfn, mock_lmb]

        cli.main(['publish', 'prod'])
        mock_cfn.describe_stacks.assert_called_once_with(StackName='foo')
        get_cfn_template.assert_called_once_with(config)
        mock_lmb.publish_version.assert_called_once_with(
            FunctionName='arn:lambda:foo')
        mock_cfn.update_stack.assert_called_once_with(
            StackName='foo', TemplateBody='cfn-template',
            Parameters=[
                {'ParameterKey': 'LambdaS3Bucket', 'ParameterValue': 'bucket'},
                {'ParameterKey': 'LambdaS3Key',
                 'ParameterValue': 'lambda-old.zip'},
                {'ParameterKey': 'DevVersion', 'ParameterValue': '$LATEST'},
                {'ParameterKey': 'ProdVersion', 'ParameterValue': '3'},
                {'ParameterKey': 'StagingVersion', 'ParameterValue': '1'}
            ],
            Capabilities=['CAPABILITY_IAM'])
        mock_cfn.get_waiter.assert_called_once_with('stack_update_complete')
        mock_cfn.get_waiter().wait.assert_called_once_with(StackName='foo')
        _print_status.assert_called_once_with(config)
Exemplo n.º 2
0
    def test_deploy_stage(self, _load_config, client, _get_aws_region, _build,
                          _ensure_bucket_exists, get_cfn_template,
                          _print_status, remove):
        mock_s3 = mock.MagicMock()
        mock_cfn = mock.MagicMock()
        mock_cfn.describe_stacks.return_value = describe_stacks_response
        client.side_effect = [mock_s3, mock_cfn]

        cli.main(['deploy', '--stage', 'staging'])
        mock_cfn.update_stack.assert_called_once_with(
            StackName='foo',
            TemplateBody='cfn-template',
            Parameters=[{
                'ParameterKey': 'LambdaS3Bucket',
                'ParameterValue': 'bucket'
            }, {
                'ParameterKey': 'LambdaS3Key',
                'ParameterValue': 'lambda.zip'
            }, {
                'ParameterKey': 'DevVersion',
                'ParameterValue': '$LATEST'
            }, {
                'ParameterKey': 'ProdVersion',
                'ParameterValue': '2'
            }, {
                'ParameterKey': 'StagingVersion',
                'ParameterValue': '$LATEST'
            }],
            Capabilities=['CAPABILITY_IAM'])
Exemplo n.º 3
0
 def test_init_with_tables(self):
     cli.main(['init', '--dynamodb-tables', 'a,b, c', 'app_module:app'])
     with open('slam.yaml') as f:
         cfg = yaml.load(f)
     t = {'attributes': {'id': 'S'}, 'key': 'id',
          'read_throughput': 1, 'write_throughput': 1}
     self.assertEqual(cfg['dynamodb_tables'], {'a': t, 'b': t, 'c': t})
Exemplo n.º 4
0
    def test_invoke_error(self, _load_config, client, mock_print):
        mock_cfn = mock.MagicMock()
        mock_lmb = mock.MagicMock()
        mock_cfn.describe_stacks.return_value = describe_stacks_response
        mock_lmb.invoke.return_value = {
            'StatusCode':
            200,
            'FunctionError':
            'Unhandled',
            'Payload':
            BytesIO(
                json.dumps({
                    'stackTrace': [['file.py', 123, 'module', 'code'],
                                   ['file2.py', 456, 'module2', 'code2']],
                    'errorMessage':
                    'foo-error',
                    'errorType':
                    'FooError'
                }).encode('utf-8'))
        }
        client.side_effect = [mock_cfn, mock_lmb]

        cli.main(['invoke'])
        output = ''.join([c[0][0] + '\n' for c in mock_print.call_args_list])
        self.assertEqual(
            output, 'Traceback (most recent call last):\n'
            '  File "file.py", line 123, in module\n'
            '    code\n'
            '  File "file2.py", line 456, in module2\n'
            '    code2\n'
            'FooError: foo-error\n')
Exemplo n.º 5
0
    def test_second_logs(self, _load_config, client, time):
        mock_cfn = mock.MagicMock()
        mock_logs = mock.MagicMock()
        mock_cfn.describe_stacks.return_value = describe_stacks_response
        mock_logs.filter_log_events.side_effect = [{
            'events': [
                {
                    'logStreamName': 'abc[$LATEST]',
                    'timestamp': 990000,
                    'message': 'foo'
                },
                {
                    'logStreamName': 'abc[1]',
                    'timestamp': 990000,
                    'message': 'foo'
                },
                {
                    'logStreamName': 'abc[$LATEST]',
                    'timestamp': 990050,
                    'message': 'baz'
                },
            ]
        }, {
            'events': []
        }]
        client.side_effect = [mock_cfn, mock_logs]

        cli.main(['logs', '--period', '6s'])
        mock_logs.filter_log_events.assert_any_call(
            logGroupName='/aws/lambda/foo',
            startTime=999994000,
            interleaved=True)
Exemplo n.º 6
0
 def test_init_with_stages(self):
     cli.main(['init', '--stages', 'd,s, p', 'app_module:app'])
     with open('slam.yaml') as f:
         cfg = yaml.load(f)
     self.assertEqual(cfg['devstage'], 'd')
     self.assertEqual(cfg['stage_environments'],
                      {'d': None, 's': None, 'p': None})
Exemplo n.º 7
0
 def test_init_with_bucket(self):
     cli.main(['init', '--bucket', 'foo-bar', 'app_module:app'])
     with open('slam.yaml') as f:
         cfg = yaml.load(f)
     self.assertEqual(cfg['function']['module'], 'app_module')
     self.assertEqual(cfg['function']['app'], 'app')
     self.assertEqual(cfg['name'], 'app-module')
     self.assertEqual(cfg['aws']['s3_bucket'], 'foo-bar')
Exemplo n.º 8
0
    def test_delete_fail_s3_bucket(self, _load_config, client):
        mock_s3 = mock.MagicMock()
        mock_logs = mock.MagicMock()
        mock_cfn = mock.MagicMock()
        mock_cfn.describe_stacks.return_value = describe_stacks_response
        mock_s3.delete_bucket.side_effect = \
            botocore.exceptions.ClientError({'Error': {}}, 'operation')
        client.side_effect = [mock_s3, mock_cfn, mock_logs]

        cli.main(['delete'])  # should still work in spite of error
Exemplo n.º 9
0
 def test_init_with_defaults(self):
     cli.main(['init', 'app_module:app'])
     with open('slam.yaml') as f:
         cfg = yaml.load(f)
     self.assertEqual(cfg['function']['module'], 'app_module')
     self.assertEqual(cfg['function']['app'], 'app')
     self.assertEqual(cfg['devstage'], 'dev')
     self.assertEqual(cfg['stage_environments'], {'dev': None})
     self.assertEqual(cfg['name'], 'app-module')
     self.assertEqual(cfg['aws']['s3_bucket'], 'app-module')
     self.assertEqual(cfg['requirements'], 'requirements.txt')
Exemplo n.º 10
0
    def test_deploy_rebuild(self, _load_config, client, _get_aws_region,
                            _build, _ensure_bucket_exists, get_cfn_template,
                            _print_status, remove):
        mock_s3 = mock.MagicMock()
        mock_cfn = mock.MagicMock()
        mock_cfn.describe_stacks.side_effect = \
            botocore.exceptions.ClientError({'Error': {}}, 'operation')
        client.side_effect = [mock_s3, mock_cfn]

        cli.main(['deploy', '--rebuild-deps'])
        _build.assert_called_once_with(config, rebuild_deps=True)
Exemplo n.º 11
0
    def test_tailed_logs(self, _load_config, client, time, mock_print,
                         mock_sleep):
        mock_cfn = mock.MagicMock()
        mock_logs = mock.MagicMock()
        mock_cfn.describe_stacks.return_value = describe_stacks_response
        mock_logs.filter_log_events.side_effect = [{
            'events': [
                {
                    'logStreamName': 'abc[$LATEST]',
                    'timestamp': 990000,
                    'message': 'foo'
                },
                {
                    'logStreamName': 'abc[$LATEST]',
                    'timestamp': 990050,
                    'message': 'bar'
                },
            ],
        }, {
            'events': []
        }, {
            'events': [
                {
                    'logStreamName': 'abc[$LATEST]',
                    'timestamp': 990075,
                    'message': 'baz'
                },
            ]
        }, {
            'events': [
                {
                    'logStreamName': 'abc[$LATEST]',
                    'timestamp': 990074,
                    'message': 'api'
                },
            ]
        }, RuntimeError]
        client.side_effect = [mock_cfn, mock_logs]

        try:
            cli.main(['logs', '--tail'])
        except RuntimeError:
            pass
        mock_logs.filter_log_events.assert_any_call(
            logGroupName='/aws/lambda/foo', startTime=940000, interleaved=True)
        mock_logs.filter_log_events.assert_any_call(
            logGroupName='/aws/lambda/foo', startTime=990051, interleaved=True)
        self.assertEqual(mock_sleep.call_count, 2)
        mock_sleep.assert_any_call(5)
        self.assertIn(' foo', mock_print.call_args_list[0][0][0])
        self.assertIn(' bar', mock_print.call_args_list[1][0][0])
        self.assertIn(' api', mock_print.call_args_list[2][0][0])
        self.assertIn(' baz', mock_print.call_args_list[3][0][0])
Exemplo n.º 12
0
 def test_init_with_defaults(self):
     cli.main(['init', 'app_module:app'])
     with open('slam.yaml') as f:
         cfg = yaml.load(f)
     self.assertEqual(cfg['function']['module'], 'app_module')
     self.assertEqual(cfg['function']['app'], 'app')
     self.assertEqual(cfg['devstage'], 'dev')
     self.assertEqual(cfg['stage_environments'], {'dev': None})
     self.assertEqual(cfg['name'], 'app-module')
     self.assertEqual(cfg['aws']['s3_bucket'], 'app-module')
     self.assertEqual(cfg['requirements'], 'requirements.txt')
     if sys.version_info[0] == 2:
         self.assertEqual(cfg['aws']['lambda_runtime'], 'python2.7')
     else:
         self.assertEqual(cfg['aws']['lambda_runtime'], 'python3.6')
Exemplo n.º 13
0
    def test_invoke_dry_run(self, _load_config, client):
        mock_cfn = mock.MagicMock()
        mock_lmb = mock.MagicMock()
        mock_cfn.describe_stacks.return_value = describe_stacks_response
        mock_lmb.invoke.return_value = {
            'StatusCode': 200,
            'Payload': BytesIO(b'{"foo":"bar"}')
        }
        client.side_effect = [mock_cfn, mock_lmb]

        cli.main(['invoke', '--dry-run'])
        mock_cfn.describe_stacks.assert_called_once_with(StackName='foo')
        mock_lmb.invoke.assert_called_once_with(FunctionName='arn:lambda:foo',
                                                InvocationType='DryRun',
                                                Payload='{"kwargs": {}}',
                                                Qualifier='dev')
Exemplo n.º 14
0
    def test_delete_no_logs(self, _load_config, client):
        mock_s3 = mock.MagicMock()
        mock_logs = mock.MagicMock()
        mock_cfn = mock.MagicMock()
        mock_cfn.describe_stacks.return_value = describe_stacks_response
        client.side_effect = [mock_s3, mock_cfn, mock_logs]

        cli.main(['delete', '--no-logs'])
        mock_cfn.describe_stacks.assert_called_once_with(StackName='foo')
        mock_cfn.delete_stack.assert_called_once_with(StackName='foo')
        mock_cfn.get_waiter.assert_called_once_with('stack_delete_complete')
        mock_cfn.get_waiter().wait.assert_called_once_with(StackName='foo')
        mock_logs.delete_log_group.assert_not_called()
        mock_s3.delete_object.assert_called_once_with(Bucket='bucket',
                                                      Key='lambda-old.zip')
        mock_s3.delete_bucket(Bucket='bucket')
Exemplo n.º 15
0
    def test_paginated_logs(self, _load_config, client, time, mock_print):
        mock_cfn = mock.MagicMock()
        mock_logs = mock.MagicMock()
        mock_cfn.describe_stacks.return_value = describe_stacks_response
        mock_logs.filter_log_events.side_effect = [{
            'events': [
                {
                    'logStreamName': 'abc[$LATEST]',
                    'timestamp': 990000,
                    'message': 'foo'
                },
                {
                    'logStreamName': 'abc[$LATEST]',
                    'timestamp': 990050,
                    'message': 'bar'
                },
            ],
            'nextToken':
            'foo-token'
        }, {
            'events': [
                {
                    'logStreamName': 'abc[$LATEST]',
                    'timestamp': 990075,
                    'message': 'baz'
                },
            ]
        }, {
            'events': []
        }]
        client.side_effect = [mock_cfn, mock_logs]

        cli.main(['logs'])
        mock_logs.filter_log_events.assert_any_call(
            logGroupName='/aws/lambda/foo', startTime=940000, interleaved=True)
        mock_logs.filter_log_events.assert_any_call(
            logGroupName='/aws/lambda/foo',
            startTime=990051,
            interleaved=True,
            nextToken='foo-token')
        self.assertEqual(mock_print.call_count, 3)
        self.assertIn(' foo', mock_print.call_args_list[0][0][0])
        self.assertIn(' bar', mock_print.call_args_list[1][0][0])
        self.assertIn(' baz', mock_print.call_args_list[2][0][0])
Exemplo n.º 16
0
    def test_deploy_with_package(self, _load_config, client, _get_aws_region,
                                 _build, _ensure_bucket_exists,
                                 get_cfn_template, _print_status, remove):
        mock_s3 = mock.MagicMock()
        mock_cfn = mock.MagicMock()
        mock_cfn.describe_stacks.side_effect = \
            botocore.exceptions.ClientError({'Error': {}}, 'operation')
        client.side_effect = [mock_s3, mock_cfn]

        cli.main(['deploy', '--lambda-package', 'my-lambda.zip'])
        _build.assert_not_called()
        mock_s3.upload_file.assert_called_with('my-lambda.zip', 'bucket',
                                               'my-lambda.zip')
        try:
            remove.assert_called_once_with('my-lambda.zip')
        except AssertionError:
            pass
        else:
            raise AssertionError('file should not have been deleted')
Exemplo n.º 17
0
    def test_invoke_with_args(self, _load_config, client):
        mock_cfn = mock.MagicMock()
        mock_lmb = mock.MagicMock()
        mock_cfn.describe_stacks.return_value = describe_stacks_response
        mock_lmb.invoke.return_value = {
            'StatusCode': 200,
            'Payload': BytesIO(b'{"foo":"bar"}')
        }
        client.side_effect = [mock_cfn, mock_lmb]

        cli.main([
            'invoke', 'arg=string', 'arg2:=true', 'arg3:=123',
            'arg4:={"foo":"bar"}'
        ])
        mock_cfn.describe_stacks.assert_called_once_with(StackName='foo')
        mock_lmb.invoke.assert_called_once_with(
            FunctionName='arn:lambda:foo',
            InvocationType='RequestResponse',
            Payload='{"kwargs": {"arg": "string", "arg2": true, "arg3": 123, '
            '"arg4": {"foo": "bar"}}}',
            Qualifier='dev')
Exemplo n.º 18
0
    def test_not_deployed(self, _load_config, client):
        mock_cfn = mock.MagicMock()
        mock_logs = mock.MagicMock()
        mock_cfn.describe_stacks.side_effect = \
            botocore.exceptions.ClientError({'Error': {}}, 'operation')
        mock_logs.filter_log_events.return_value = {
            'events': [
                {
                    'timestamp': 1000,
                    'message': 'foo'
                },
                {
                    'timestamp': 1001,
                    'message': 'bar'
                },
            ]
        }
        client.side_effect = [mock_cfn, mock_logs]

        cli.main(['logs'])
        mock_logs.filter_log_events.assert_not_called()
Exemplo n.º 19
0
    def test_publish_version_dev(self, _load_config, client, get_cfn_template,
                                 _print_status):
        mock_cfn = mock.MagicMock()
        mock_lmb = mock.MagicMock()
        mock_cfn.describe_stacks.return_value = describe_stacks_response
        mock_lmb.publish_version.return_value = {'Version': '3'}
        client.side_effect = [mock_cfn, mock_lmb]

        cli.main(['publish', 'dev', '--version', '42'])
        mock_lmb.publish_version.assert_not_called()
        mock_cfn.update_stack.assert_called_once_with(
            StackName='foo', TemplateBody='cfn-template',
            Parameters=[
                {'ParameterKey': 'LambdaS3Bucket', 'ParameterValue': 'bucket'},
                {'ParameterKey': 'LambdaS3Key',
                 'ParameterValue': 'lambda-old.zip'},
                {'ParameterKey': 'DevVersion', 'ParameterValue': '42'},
                {'ParameterKey': 'ProdVersion', 'ParameterValue': '2'},
                {'ParameterKey': 'StagingVersion', 'ParameterValue': '1'}
            ],
            Capabilities=['CAPABILITY_IAM'])
Exemplo n.º 20
0
    def test_stage_logs(self, _load_config, client, time, mock_print):
        mock_cfn = mock.MagicMock()
        mock_logs = mock.MagicMock()
        mock_cfn.describe_stacks.return_value = describe_stacks_response
        mock_logs.filter_log_events.side_effect = [{
            'events': [
                {
                    'logStreamName': 'abc[$LATEST]',
                    'timestamp': 990000,
                    'message': 'foo'
                },
                {
                    'logStreamName': 'abc[2]',
                    'timestamp': 990000,
                    'message': 'foo'
                },
                {
                    'logStreamName': 'abc[$LATEST]',
                    'timestamp': 990050,
                    'message': 'baz'
                },
            ]
        }, {
            'events': [{
                'timestamp': 990025,
                'message': 'bar'
            }]
        }]
        client.side_effect = [mock_cfn, mock_logs]

        cli.main(['logs', '--stage', 'prod'])
        mock_logs.filter_log_events.assert_any_call(
            logGroupName='/aws/lambda/foo', startTime=940000, interleaved=True)
        mock_logs.filter_log_events.assert_any_call(
            logGroupName='API-Gateway-Execution-Logs_123abc/prod',
            startTime=940000,
            interleaved=True)
        self.assertEqual(mock_print.call_count, 2)
        self.assertIn(' foo', mock_print.call_args_list[0][0][0])
        self.assertIn(' bar', mock_print.call_args_list[1][0][0])
Exemplo n.º 21
0
    def test_deploy_update(self, _load_config, client, _get_aws_region, _build,
                           _ensure_bucket_exists, get_cfn_template,
                           _print_status, remove):
        mock_s3 = mock.MagicMock()
        mock_cfn = mock.MagicMock()
        mock_cfn.describe_stacks.return_value = describe_stacks_response
        client.side_effect = [mock_s3, mock_cfn]

        cli.main(['deploy'])
        mock_cfn.describe_stacks.assert_called_once_with(StackName='foo')
        _build.assert_called_once_with(config, rebuild_deps=False)
        _ensure_bucket_exists.assert_called_once_with(mock_s3, 'bucket',
                                                      'us-east-1')
        mock_s3.upload_file.assert_called_with('lambda.zip', 'bucket',
                                               'lambda.zip')
        remove.assert_called_once_with('lambda.zip')
        mock_cfn.update_stack.assert_called_once_with(
            StackName='foo',
            TemplateBody='cfn-template',
            Parameters=[{
                'ParameterKey': 'LambdaS3Bucket',
                'ParameterValue': 'bucket'
            }, {
                'ParameterKey': 'LambdaS3Key',
                'ParameterValue': 'lambda.zip'
            }, {
                'ParameterKey': 'DevVersion',
                'ParameterValue': '$LATEST'
            }, {
                'ParameterKey': 'ProdVersion',
                'ParameterValue': '2'
            }, {
                'ParameterKey': 'StagingVersion',
                'ParameterValue': '1'
            }],
            Capabilities=['CAPABILITY_IAM'])
        mock_s3.delete_object(Bucket='bucket', Key='lambda-old.zip')
        mock_cfn.get_waiter.assert_called_once_with('stack_update_complete')
        mock_cfn.get_waiter().wait.assert_called_once_with(StackName='foo')
        _print_status.assert_called_once_with(config)
Exemplo n.º 22
0
    def test_deploy_first(self, _load_config, client, _get_aws_region, _build,
                          _ensure_bucket_exists, get_cfn_template,
                          _print_status, remove):
        mock_s3 = mock.MagicMock()
        mock_cfn = mock.MagicMock()
        mock_cfn.describe_stacks.side_effect = \
            botocore.exceptions.ClientError({'Error': {}}, 'operation')
        client.side_effect = [mock_s3, mock_cfn]

        cli.main(['deploy'])
        mock_cfn.describe_stacks.assert_called_once_with(StackName='foo')
        _build.assert_called_once_with(config, rebuild_deps=False)
        _ensure_bucket_exists.assert_called_once_with(mock_s3, 'bucket',
                                                      'us-east-1')
        mock_s3.upload_file.assert_called_with('lambda.zip', 'bucket',
                                               'lambda.zip')
        remove.assert_called_once_with('lambda.zip')
        mock_cfn.create_stack.assert_called_once_with(
            StackName='foo',
            TemplateBody='cfn-template',
            Parameters=[{
                'ParameterKey': 'LambdaS3Bucket',
                'ParameterValue': 'bucket'
            }, {
                'ParameterKey': 'LambdaS3Key',
                'ParameterValue': 'lambda.zip'
            }, {
                'ParameterKey': 'DevVersion',
                'ParameterValue': '$LATEST'
            }, {
                'ParameterKey': 'ProdVersion',
                'ParameterValue': '$LATEST'
            }, {
                'ParameterKey': 'StagingVersion',
                'ParameterValue': '$LATEST'
            }],
            Capabilities=['CAPABILITY_IAM'])
        mock_cfn.get_waiter.assert_called_once_with('stack_create_complete')
        mock_cfn.get_waiter().wait.assert_called_once_with(StackName='foo')
        _print_status.assert_called_once_with(config)
Exemplo n.º 23
0
    def test_dev_no_api_logs(self, _load_config, client, time, mock_print):
        mock_cfn = mock.MagicMock()
        mock_logs = mock.MagicMock()
        r = copy.deepcopy(describe_stacks_response)
        r['Stacks'][0]['Outputs'] = [
            {
                'OutputKey': 'FunctionArn',
                'OutputValue': 'arn:lambda:foo'
            },
        ]
        mock_cfn.describe_stacks.return_value = r
        mock_logs.filter_log_events.side_effect = [{
            'events': [
                {
                    'logStreamName': 'abc[$LATEST]',
                    'timestamp': 990000,
                    'message': 'foo'
                },
                {
                    'logStreamName': 'abc[1]',
                    'timestamp': 990000,
                    'message': 'foo'
                },
                {
                    'logStreamName': 'abc[$LATEST]',
                    'timestamp': 990050,
                    'message': 'baz'
                },
            ]
        }]
        client.side_effect = [mock_cfn, mock_logs]

        cli.main(['logs'])
        mock_logs.filter_log_events.assert_called_once_with(
            logGroupName='/aws/lambda/foo', startTime=940000, interleaved=True)
        self.assertEqual(mock_print.call_count, 2)
        self.assertIn(' foo', mock_print.call_args_list[0][0][0])
        self.assertIn(' baz', mock_print.call_args_list[1][0][0])
Exemplo n.º 24
0
    def test_register_plugins(self, iter_entry_points):
        @climax.command()
        @climax.argument('--foo-option')
        def plugin_init(config, foo_option):
            return {'x': 'y'}

        def plugin_cfn_template(config, template):
            template['Resources']['foo'] = config['foo']
            return template

        plugin_module = mock.MagicMock(spec='init')
        plugin_module.__doc__ = 'test plugin\n'
        plugin_module.init = plugin_init
        plugin_module.cfn_template = plugin_cfn_template
        plugin = mock.MagicMock()
        plugin.name = 'foo'
        plugin.load.return_value = plugin_module
        plugin_without_init = mock.MagicMock()
        plugin_without_init.name = 'bar'
        plugin_without_init.load.return_value = mock.MagicMock(spec=[])
        iter_entry_points.return_value = [plugin, plugin_without_init]
        cli.register_plugins()
        iter_entry_points.assert_called_once_with('slam_plugins')
        self.assertEqual(cli.init._arguments[-1], (('--foo-option', ), {}))
        self.assertEqual(cli.init._argnames[-1], 'foo_option')
        self.assertEqual(cli.plugins['foo'], plugin_module)

        cli.main(['init', '--foo-option', 'abc', 'app_module:app'])
        with open('slam.yaml') as f:
            cfg = f.read()
        self.assertIn('# test plugin\n# \nfoo:\n  x: y\n', cfg)

        cfg = yaml.load(BytesIO(cfg.encode('utf-8')))
        self.assertEqual(cfg['foo'], {'x': 'y'})

        tpl = cfn.get_cfn_template(cfg)
        tpl = json.loads(tpl)
        self.assertEqual(tpl['Resources']['foo'], {'x': 'y'})
Exemplo n.º 25
0
    def test_delete(self, _load_config, client):
        mock_s3 = mock.MagicMock()
        mock_logs = mock.MagicMock()
        mock_cfn = mock.MagicMock()
        mock_cfn.describe_stacks.return_value = describe_stacks_response
        client.side_effect = [mock_s3, mock_cfn, mock_logs]

        cli.main(['delete'])
        mock_cfn.describe_stacks.assert_called_once_with(StackName='foo')
        mock_cfn.delete_stack.assert_called_once_with(StackName='foo')
        mock_cfn.get_waiter.assert_called_once_with('stack_delete_complete')
        mock_cfn.get_waiter().wait.assert_called_once_with(StackName='foo')
        mock_logs.delete_log_group.assert_any_call(
            logGroupName='/aws/lambda/foo')
        mock_logs.delete_log_group.assert_any_call(
            logGroupName='API-Gateway-Execution-Logs_123abc/dev')
        mock_logs.delete_log_group.assert_any_call(
            logGroupName='API-Gateway-Execution-Logs_123abc/staging')
        mock_logs.delete_log_group.assert_any_call(
            logGroupName='API-Gateway-Execution-Logs_123abc/prod')
        mock_s3.delete_object.assert_called_once_with(Bucket='bucket',
                                                      Key='lambda-old.zip')
        mock_s3.delete_bucket(Bucket='bucket')
Exemplo n.º 26
0
    def test_no_group(self, _load_config, client, time, mock_print):
        mock_cfn = mock.MagicMock()
        mock_logs = mock.MagicMock()
        mock_cfn.describe_stacks.return_value = describe_stacks_response
        mock_logs.filter_log_events.side_effect = [
            botocore.exceptions.ClientError({'Error': {}}, 'operation'), {
                'events': [{
                    'timestamp': 990025,
                    'message': 'bar'
                }]
            }
        ]
        client.side_effect = [mock_cfn, mock_logs]

        cli.main(['logs'])
        mock_logs.filter_log_events.assert_any_call(
            logGroupName='/aws/lambda/foo', startTime=940000, interleaved=True)
        mock_logs.filter_log_events.assert_any_call(
            logGroupName='API-Gateway-Execution-Logs_123abc/dev',
            startTime=940000,
            interleaved=True)
        self.assertEqual(mock_print.call_count, 1)
        self.assertIn(' bar', mock_print.call_args_list[0][0][0])
Exemplo n.º 27
0
    def test_delete_no_api_gateway(self, _load_config, client):
        mock_s3 = mock.MagicMock()
        mock_logs = mock.MagicMock()
        mock_cfn = mock.MagicMock()
        mock_cfn.describe_stacks.return_value = {
            'Stacks': [{
                'Parameters': [
                    {
                        'ParameterKey': 'LambdaS3Bucket',
                        'ParameterValue': 'bucket'
                    },
                    {
                        'ParameterKey': 'LambdaS3Key',
                        'ParameterValue': 'lambda-old.zip'
                    },
                ],
                'Outputs': [
                    {
                        'OutputKey': 'FunctionArn',
                        'OutputValue': 'arn:lambda:foo'
                    },
                ]
            }]
        }
        client.side_effect = [mock_s3, mock_cfn, mock_logs]

        cli.main(['delete'])
        mock_cfn.describe_stacks.assert_called_once_with(StackName='foo')
        mock_cfn.delete_stack.assert_called_once_with(StackName='foo')
        mock_cfn.get_waiter.assert_called_once_with('stack_delete_complete')
        mock_cfn.get_waiter().wait.assert_called_once_with(StackName='foo')
        mock_logs.delete_log_group.assert_called_once_with(
            logGroupName='/aws/lambda/foo')
        mock_s3.delete_object.assert_called_once_with(Bucket='bucket',
                                                      Key='lambda-old.zip')
        mock_s3.delete_bucket(Bucket='bucket')
Exemplo n.º 28
0
 def test_init_with_runtime(self):
     cli.main(['init', '--runtime', 'foo', 'app_module:app'])
     with open('slam.yaml') as f:
         cfg = yaml.load(f)
     self.assertEqual(cfg['aws']['lambda_runtime'], 'foo')
Exemplo n.º 29
0
 def test_cli_build(self, _build, _load_config):
     cli.main(['build'])
     _build.assert_called_once_with({'requirements': 'r'},
                                    rebuild_deps=False)
Exemplo n.º 30
0
 def test_cli_build_rebuild_deps(self, _build, _load_config):
     cli.main(['build', '--rebuild-deps'])
     _build.assert_called_once_with({'requirements': 'r'},
                                    rebuild_deps=True)