def test_happy_path(valid_template, namespace): template_path, obj, metadata = handler.load(valid_template, namespace) assert obj assert metadata['Version'] == '1.0' assert metadata['DeployBucket'] == 'sample-deploy-bucket' assert metadata['StackName'] == 'MyStackName' assert metadata['Variables'] == [ OrderedDict({'PreparedVar': 'PreparedValue'}), {'SAMWise::AccountId': "{SAMWise::AccountId}"}, {'SAMWise::Namespace': namespace}, {'SAMWise::StackName': 'MyStackName'} ] assert obj['Parameters']['Namespace']['Default'] == namespace
def test_get_python_runtime_image(valid_template, namespace): template_path, obj, metadata = handler.load(valid_template, namespace) runtime, image = get_python_runtime_image(obj) assert runtime == "python3.7" assert image == "lambci/lambda:build-python3.7"
def main(): doc_string = string.Template(__doc__) doc_with_version = doc_string.safe_substitute(VERSION=__version__) arguments = docopt(doc_with_version) if arguments.get('--version'): print(f"SAMWise v{__version__}") sys.exit() aws_profile = arguments.get('--profile') deploy_region = arguments.get('--region') namespace = arguments.get('--namespace') parameter_overrides = arguments.get('--parameter-overrides') or "" parameter_overrides += f" {constants.NAMESPACE_KEY}={namespace}" if aws_profile: print(f"SAMWise v{__version__} | AWS Profile: {aws_profile}") else: print(f"SAMWise v{__version__}| AWS Profile via environment") print('-' * 100) input_file = arguments.get('--in') output_path = arguments.get( '--out') or constants.DEFAULT_TEMPLATE_FILE_PATH aws_creds = get_aws_credentials(aws_profile) aws_account_id = str(aws_creds['AWS_ACCOUNT_ID']) print(f"{Fore.LIGHTCYAN_EX} - Looking for a SAMWise template{Fore.RESET}") try: template_path, template_obj, metadata = load(input_file, namespace, aws_account_id) except (TemplateNotFound, InlineIncludeNotFound, UnsupportedSAMWiseVersion, InvalidSAMWiseTemplate) as error: print(f"{Fore.RED} - ERROR: {error}{Fore.RESET}") sys.exit(2) stack_name = metadata['StackName'] print(f" - Stack {stack_name} loaded") save(template_obj, output_path) output_file_path = f"{os.path.abspath(output_path)}/{AWS_SAM_TEMPLATE_FILE_NAME}" print(f" - CloudFormation Template rendered to {output_file_path}") if arguments.get('lint'): print( f"{Fore.LIGHTCYAN_EX} - Running cfn-lint against generated template{Fore.RESET}" ) results = lint_template(output_file_path) if results: print(textwrap.indent(results, " - ")) sys.exit(1) elif arguments.get('generate'): if arguments.get('--print'): print("-" * 100) yaml_print(template_obj) elif arguments.get('package') or arguments.get('deploy'): base_dir = os.path.dirname(template_path) s3_bucket = arguments.get('--s3-bucket') or metadata[ constants.DEPLOYBUCKET_NAME_KEY] force = bool(arguments.get('package')) or arguments.get( '--force') # if packaging was requested or forced package(stack_name, template_obj, output_path, base_dir, aws_creds, s3_bucket, parameter_overrides, force, arguments.get('--cache-dir')) if arguments.get('deploy'): upload(aws_creds, output_path, s3_bucket, stack_name) tags = metadata[constants.TAGS_KEY] deploy(aws_creds, aws_profile, deploy_region, output_path, stack_name, tags, parameter_overrides, confirm=bool(not arguments.get('--yes'))) else: print('Invalid Option')
def test_missing_samwise_template(missing_template, namespace): with pytest.raises(Exception) as err: handler.load(missing_template, namespace) assert 'No SAM or SAMWise template file could be found' in str(err.value)
def test_invalid_samwise_template(invalid_template, namespace): with pytest.raises(Exception) as err: handler.load(invalid_template, namespace) assert 'SAMWise metadata is invalid' in str(err.value)
def test_non_samwise_template(non_samwise_template, namespace): with pytest.raises(Exception) as err: handler.load(non_samwise_template, namespace) assert 'SAMWise metadata not found' in str(err.value)
def test_include_samwise_template(include_template, include_data, namespace): template_path, obj, metadata = handler.load(include_template, namespace) assert obj yaml_string = yaml_dumps(obj['Resources']['MyStateMachine']['Properties']['DefinitionString']) assert yaml_string == f"!Sub |\n{include_data}\n"
def test_account_id(accountid_template, namespace): template_path, obj, metadata = handler.load(accountid_template, namespace, '123456789012') assert obj['Metadata']['SAMWise']['DeployBucket'] == 'sample-deploy-bucket-123456789012'