def test_cli_utility(self): zappa_cli = ZappaCLI() zappa_cli.api_stage = 'ttt888' zappa_cli.load_settings('test_settings.json') zappa_cli.create_package() zappa_cli.remove_local_zip() logs = [ { 'timestamp': '12345', 'message': '[START RequestId] test' }, { 'timestamp': '12345', 'message': '[REPORT RequestId] test' }, { 'timestamp': '12345', 'message': '[END RequestId] test' }, { 'timestamp': '12345', 'message': 'test' } ] zappa_cli.print_logs(logs)
def test_cli_error_exit_code(self): # Discussion: https://github.com/Miserlou/Zappa/issues/407 zappa_cli = ZappaCLI() # Sanity argv = '-s test_settings.json status devor'.split() with self.assertRaises(SystemExit) as system_exit: zappa_cli.handle(argv) self.assertEqual(system_exit.exception.code, 1)
def test_cli_init(self): zappa_cli = ZappaCLI() # Via http://stackoverflow.com/questions/2617057/how-to-supply-stdin-files-and-environment-variable-inputs-to-python-unit-tests inputs = ['dev', 'lmbda', 'test_settings', ''] input_generator = (i for i in inputs) with mock.patch('__builtin__.raw_input', lambda prompt: next(input_generator)): zappa_cli.init()
def test_cli_cognito_triggers(self, session): zappa_cli = ZappaCLI() zappa_cli.api_stage = 'ttt888' zappa_cli.api_key_required = True zappa_cli.load_settings('test_settings.json', session) zappa_cli.lambda_arn = 'arn:aws:lambda:us-east-1:12345:function:Zappa-Trigger-Test' zappa_cli.update_cognito_triggers()
def test_lets_encrypt_sanity(self): # We need a fake account key and crt import subprocess proc = subprocess.Popen(["openssl genrsa 2048 > /tmp/account.key"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) out, err = proc.communicate() if proc.returncode != 0: raise IOError("OpenSSL Error: {0}".format(err)) proc = subprocess.Popen(["openssl req -x509 -newkey rsa:2048 -subj '/C=US/ST=Denial/L=Springfield/O=Dis/CN=www.example.com' -passout pass:foo -keyout /tmp/key.key -out test_signed.crt -days 1 > /tmp/signed.crt"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) out, err = proc.communicate() if proc.returncode != 0: raise IOError("OpenSSL Error: {0}".format(err)) DEFAULT_CA = "https://acme-staging.api.letsencrypt.org" CA = "https://acme-staging.api.letsencrypt.org" try: result = register_account() except ValueError as e: pass # that's fine. create_domain_key() create_domain_csr('herp.derp.wtf') parse_account_key() parse_csr() create_chained_certificate() try: result = sign_certificate() except ValueError as e: pass # that's fine. result = verify_challenge('http://echo.jsontest.com/status/valid') try: result = verify_challenge('http://echo.jsontest.com/status/fail') except ValueError as e: pass # that's fine. try: result = verify_challenge('http://bing.com') except ValueError as e: pass # that's fine. encode_certificate(b'123') # without domain testing.. zappa_cli = ZappaCLI() zappa_cli.api_stage = 'ttt888' zappa_cli.load_settings('test_settings.json') get_cert_and_update_domain(zappa_cli, 'kerplah', 'zzzz', domain=None, clean_up=True) os.remove('test_signed.crt') cleanup()
def test_cli_init(self): if os.path.isfile('zappa_settings.json'): os.remove('zappa_settings.json') # Test directly zappa_cli = ZappaCLI() # Via http://stackoverflow.com/questions/2617057/how-to-supply-stdin-files-and-environment-variable-inputs-to-python-unit-tests inputs = ['dev', 'lmbda', 'test_settings', ''] input_generator = (i for i in inputs) with mock.patch('__builtin__.raw_input', lambda prompt: next(input_generator)): zappa_cli.init() if os.path.isfile('zappa_settings.json'): os.remove('zappa_settings.json') # Test via handle() input_generator = (i for i in inputs) with mock.patch('__builtin__.raw_input', lambda prompt: next(input_generator)): zappa_cli = ZappaCLI() argv = ['init'] zappa_cli.handle(argv) if os.path.isfile('zappa_settings.json'): os.remove('zappa_settings.json')
def test_load_settings_toml(self): zappa_cli = ZappaCLI() settings_file = zappa_cli.get_json_or_yaml_settings("test_settings") zappa_cli = ZappaCLI() zappa_cli.api_stage = 'ttt888' zappa_cli.load_settings('tests/test_settings.toml') self.assertEqual(False, zappa_cli.stage_config['touch'])
def test_cli_aws_status(self, session): zappa_cli = ZappaCLI() zappa_cli.api_stage = "ttt888" zappa_cli.load_settings("test_settings.json", session) zappa_cli.api_stage = "devor" zappa_cli.lambda_name = "baby-flask-devor" zappa_cli.zappa.credentials_arn = "arn:aws:iam::12345:role/ZappaLambdaExecution" resp = zappa_cli.status()
def test_cli_aws_status(self, session): zappa_cli = ZappaCLI() zappa_cli.api_stage = 'ttt888' zappa_cli.load_settings('test_settings.json', session) zappa_cli.api_stage = 'devor' zappa_cli.lambda_name = 'baby-flask-devor' zappa_cli.zappa.credentials_arn = 'arn:aws:iam::12345:role/ZappaLambdaExecution' resp = zappa_cli.status()
def update_certificate(self): """ Call 'certify' locally. """ import boto3 session = boto3.Session() z_cli = ZappaCLI() z_cli.api_stage = self.settings.API_STAGE z_cli.load_settings(session=session) z_cli.certify() return
def test_cli_aws(self, session): zappa_cli = ZappaCLI() zappa_cli.api_stage = 'ttt888' zappa_cli.api_key_required = True zappa_cli.authorization_type = 'NONE' zappa_cli.load_settings('test_settings.json', session) zappa_cli.zappa.credentials_arn = 'arn:aws:iam::12345:role/ZappaLambdaExecution' zappa_cli.deploy() zappa_cli.update() zappa_cli.rollback(1) zappa_cli.tail(since=0, filter_pattern='', keep_open=False) zappa_cli.schedule() zappa_cli.unschedule() zappa_cli.undeploy(no_confirm=True, remove_logs=True)
def test_cli_utility(self): zappa_cli = ZappaCLI() zappa_cli.api_stage = 'ttt888' zappa_cli.load_settings('test_settings.json') zappa_cli.create_package() zappa_cli.remove_local_zip() logs = [ { 'timestamp': '12345', 'message': '[START RequestId] test' }, { 'timestamp': '12345', 'message': '[REPORT RequestId] test' }, { 'timestamp': '12345', 'message': '[END RequestId] test' }, { 'timestamp': '12345', 'message': 'test' }, { 'timestamp': '1480001341214', 'message': '[INFO] 2016-11-24T15:29:13.326Z c0cb52d1-b25a-11e6-9b73-f940ce24319a 59.111.125.48 - - [24/Nov/2016:15:29:13 +0000] "GET / HTTP/1.1" 200 2590 "" "python-requests/2.11.0" 0/4.672' }, { 'timestamp': '1480001341214', 'message': '[INFO] 2016-11-24T15:29:13.326Z c0cb52d1-b25a-11e6-9b73-f940ce24319a 59.111.125.48 - - [24/Nov/2016:15:29:13 +0000] "GET / HTTP/1.1" 400 2590 "" "python-requests/2.11.0" 0/4.672' }, { 'timestamp': '1480001341215', 'message': '[1480001341258] [DEBUG] 2016-11-24T15:29:01.258Z b890d8f6-b25a-11e6-b6bc-718f7ec807df Zappa Event: {}' } ] zappa_cli.print_logs(logs) zappa_cli.print_logs(logs, colorize=False) zappa_cli.print_logs(logs, colorize=False, http=True) zappa_cli.print_logs(logs, colorize=True, http=True) zappa_cli.print_logs(logs, colorize=True, http=False) zappa_cli.check_for_update()
def test_cli_args(self): zappa_cli = ZappaCLI() # Sanity argv = '-s test_settings.json derp ttt888'.split() zappa_cli.handle(argv)
def test_cli_aws(self, session): zappa_cli = ZappaCLI() zappa_cli.api_stage = 'ttt888' zappa_cli.api_key_required = True zappa_cli.load_settings('test_settings.json', session) zappa_cli.zappa.credentials_arn = 'arn:aws:iam::12345:role/ZappaLambdaExecution' zappa_cli.deploy() zappa_cli.update() zappa_cli.rollback(1) zappa_cli.tail(False) zappa_cli.schedule() zappa_cli.unschedule() zappa_cli.undeploy(noconfirm=True, remove_logs=True)
def test_cli_sanity(self): zappa_cli = ZappaCLI() return
def test_cli_utility(self): zappa_cli = ZappaCLI() zappa_cli.api_stage = 'ttt888' zappa_cli.load_settings('test_settings.json') zappa_cli.create_package() zappa_cli.remove_local_zip() logs = [{ 'timestamp': '12345', 'message': '[START RequestId] test' }, { 'timestamp': '12345', 'message': '[REPORT RequestId] test' }, { 'timestamp': '12345', 'message': '[END RequestId] test' }, { 'timestamp': '12345', 'message': 'test' }] zappa_cli.print_logs(logs)
def test_cli_aws(self, session): zappa_cli = ZappaCLI() zappa_cli.api_stage = 'ttt333' zappa_cli.load_settings('test_settings.json', session) zappa_cli.zappa.credentials_arn = 'arn:aws:iam::724336686645:role/ZappaLambdaExecution' zappa_cli.deploy() zappa_cli.update() zappa_cli.rollback(1) zappa_cli.tail(False)
def test_bad_stage_name_catch(self): zappa_cli = ZappaCLI() self.assertRaises(ValueError, zappa_cli.load_settings, 'tests/test_bad_stage_name_settings.json')
def test_load_settings(self): zappa_cli = ZappaCLI() zappa_cli.api_stage = 'ttt888' zappa_cli.load_settings('test_settings.json') self.assertEqual(False, zappa_cli.stage_config['touch'])
def test_load_extended_settings(self): zappa_cli = ZappaCLI() zappa_cli.api_stage = 'extendo' zappa_cli.load_settings('test_settings.json') self.assertEqual('lmbda', zappa_cli.stage_config['s3_bucket']) self.assertEqual(True, zappa_cli.stage_config['touch']) zappa_cli = ZappaCLI() zappa_cli.api_stage = 'extendofail' with self.assertRaises(ClickException): zappa_cli.load_settings('test_settings.json') zappa_cli = ZappaCLI() zappa_cli.api_stage = 'ttt888' with self.assertRaises(RuntimeError): zappa_cli.load_settings('tests/test_bad_circular_extends_settings.json') zappa_cli = ZappaCLI() zappa_cli.api_stage = 'extendo2' zappa_cli.load_settings('test_settings.json') self.assertEqual('lmbda2', zappa_cli.stage_config['s3_bucket']) # Second Extension self.assertTrue(zappa_cli.stage_config['touch']) # First Extension self.assertTrue(zappa_cli.stage_config['delete_local_zip']) # The base
from zappa.cli import ZappaCLI zappa = ZappaCLI() zappa.handle(["deploy"])
def test_bad_environment_vars_catch(self): zappa_cli = ZappaCLI() zappa_cli.api_stage = 'ttt888' self.assertRaises(ValueError, zappa_cli.load_settings, 'tests/test_bad_environment_vars.json')
from zappa.cli import ZappaCLI zappa = ZappaCLI() zappa.handle(["update"])
def test_cli_default(self): # Discussion: https://github.com/Miserlou/Zappa/issues/422 zappa_cli = ZappaCLI() argv = '-s tests/test_one_env.json status'.split() # It'll fail, but at least it'll cover it. with self.assertRaises(SystemExit) as system_exit: zappa_cli.handle(argv) self.assertEqual(system_exit.exception.code, 1) zappa_cli = ZappaCLI() argv = '-s tests/test_one_env.json status --all'.split() # It'll fail, but at least it'll cover it. with self.assertRaises(SystemExit) as system_exit: zappa_cli.handle(argv) self.assertEqual(system_exit.exception.code, 1) zappa_cli = ZappaCLI() argv = '-s test_settings.json status'.split() with self.assertRaises(SystemExit) as system_exit: zappa_cli.handle(argv) self.assertEqual(system_exit.exception.code, 2)
def test_load_settings_yaml(self): zappa_cli = ZappaCLI() settings_file = zappa_cli.get_json_or_yaml_settings("test_settings") zappa_cli = ZappaCLI() zappa_cli.api_stage = 'ttt888' zappa_cli.load_settings('tests/test_settings.yml') self.assertEqual(False, zappa_cli.stage_config['touch']) zappa_cli = ZappaCLI() zappa_cli.api_stage = 'extendo' zappa_cli.load_settings('tests/test_settings.yml') self.assertEqual('lmbda', zappa_cli.stage_config['s3_bucket']) self.assertEqual(True, zappa_cli.stage_config['touch'])
def test_remote_env_package(self): zappa_cli = ZappaCLI() zappa_cli.api_stage = 'depricated_remote_env' zappa_cli.load_settings('test_settings.json') self.assertEqual('lmbda-env', zappa_cli.stage_config['remote_env_bucket']) self.assertEqual('dev/env.json', zappa_cli.stage_config['remote_env_file']) zappa_cli.create_package() with zipfile.ZipFile(zappa_cli.zip_path, 'r') as lambda_zip: content = lambda_zip.read('zappa_settings.py') zappa_cli.remove_local_zip() m = re.search("REMOTE_ENV='(.*)'", content) self.assertEqual(m.group(1), 's3://lmbda-env/dev/env.json') zappa_cli = ZappaCLI() zappa_cli.api_stage = 'remote_env' zappa_cli.load_settings('test_settings.json') self.assertEqual('s3://lmbda-env/prod/env.json', zappa_cli.stage_config['remote_env']) zappa_cli.create_package() with zipfile.ZipFile(zappa_cli.zip_path, 'r') as lambda_zip: content = lambda_zip.read('zappa_settings.py') zappa_cli.remove_local_zip() m = re.search("REMOTE_ENV='(.*)'", content) self.assertEqual(m.group(1), 's3://lmbda-env/prod/env.json')
def test_cli_aws(self, session): zappa_cli = ZappaCLI() zappa_cli.api_stage = 'ttt888' zappa_cli.api_key_required = True zappa_cli.authorization_type = 'NONE' zappa_cli.load_settings('test_settings.json', session) zappa_cli.zappa.credentials_arn = 'arn:aws:iam::12345:role/ZappaLambdaExecution' zappa_cli.deploy() zappa_cli.update() zappa_cli.rollback(1) zappa_cli.tail(False) zappa_cli.schedule() zappa_cli.unschedule() zappa_cli.undeploy(noconfirm=True, remove_logs=True)
def test_bad_json_catch(self): zappa_cli = ZappaCLI() self.assertRaises(ValueError, zappa_cli.load_settings_file('tests/test_bad_settings.json'))
def test_cli_aws(self, session): zappa_cli = ZappaCLI() zappa_cli.api_stage = 'ttt888' zappa_cli.load_settings('test_settings.json', session) zappa_cli.zappa.credentials_arn = 'arn:aws:iam::724336686645:role/ZappaLambdaExecution' zappa_cli.deploy() zappa_cli.update() zappa_cli.rollback(1) zappa_cli.tail(False) zappa_cli.schedule() zappa_cli.unschedule() zappa_cli.undeploy(noconfirm=True)
def test_cli_schedule_async_resources(self, session): zappa_cli = ZappaCLI() zappa_cli.api_stage = 'schedule_async_resources_sqs' zappa_cli.load_settings('test_settings.json', session) zappa_cli.schedule() zappa_cli.unschedule()