Skip to content

Commit

Permalink
tests: integration tests for sam deploy (#1565)
Browse files Browse the repository at this point in the history
- integration tests for `sam deploy` and `sam deploy --guided`
- fix for regression tests
- `stack-name` is required
- additional error cases for when a s3 bucket is needed to be looked up
  during `sam deploy`
- move parameter prompts logic to its own function
  • Loading branch information
sriram-mv authored Nov 21, 2019
1 parent 16cbbea commit e93897a
Show file tree
Hide file tree
Showing 6 changed files with 339 additions and 28 deletions.
49 changes: 27 additions & 22 deletions samcli/commands/deploy/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@
@template_click_option(include_build=True)
@click.option(
"--stack-name",
required=False,
default="sam-app",
required=True,
help="The name of the AWS CloudFormation stack you're deploying to. "
"If you specify an existing stack, the command updates the stack. "
"If you specify a new stack, the command creates it.",
Expand Down Expand Up @@ -288,10 +287,10 @@ def do_cli(
def guided_deploy(
stack_name, s3_bucket, region, profile, confirm_changeset, parameter_override_keys, parameter_overrides
):
default_stack_name = stack_name or "sam-app"
default_region = region or "us-east-1"
default_capabilities = ("CAPABILITY_IAM",)
input_capabilities = None
input_parameter_overrides = {}

color = Colored()
start_bold = "\033[1m"
Expand All @@ -301,25 +300,9 @@ def guided_deploy(
color.yellow("\n\tSetting default arguments for 'sam deploy'\n\t=========================================")
)

stack_name = click.prompt(f"\t{start_bold}Stack Name{end_bold}", default=stack_name, type=click.STRING)
stack_name = click.prompt(f"\t{start_bold}Stack Name{end_bold}", default=default_stack_name, type=click.STRING)
region = click.prompt(f"\t{start_bold}AWS Region{end_bold}", default=default_region, type=click.STRING)
if parameter_override_keys:
for parameter_key, parameter_properties in parameter_override_keys.items():
no_echo = parameter_properties.get("NoEcho", False)
if no_echo:
parameter = click.prompt(
f"\t{start_bold}Parameter {parameter_key}{end_bold}", type=click.STRING, hide_input=True
)
input_parameter_overrides[parameter_key] = {"Value": parameter, "Hidden": True}
else:
parameter = click.prompt(
f"\t{start_bold}Parameter {parameter_key}{end_bold}",
default=parameter_overrides.get(
parameter_key, parameter_properties.get("Default", "No default specified")
),
type=click.STRING,
)
input_parameter_overrides[parameter_key] = {"Value": parameter, "Hidden": False}
input_parameter_overrides = prompt_parameters(parameter_override_keys, start_bold, end_bold)

click.secho("\t#Shows you resources changes to be deployed and require a 'Y' to initiate deploy")
confirm_changeset = click.confirm(
Expand All @@ -331,7 +314,7 @@ def guided_deploy(
if not capabilities_confirm:
input_capabilities = click.prompt(
f"\t{start_bold}Capabilities{end_bold}",
default=default_capabilities,
default=default_capabilities[0],
type=FuncParamType(func=_space_separated_list_func_type),
)

Expand All @@ -354,6 +337,28 @@ def guided_deploy(
)


def prompt_parameters(parameter_override_keys, start_bold, end_bold):
_prompted_param_overrides = {}
if parameter_override_keys:
for parameter_key, parameter_properties in parameter_override_keys.items():
no_echo = parameter_properties.get("NoEcho", False)
if no_echo:
parameter = click.prompt(
f"\t{start_bold}Parameter {parameter_key}{end_bold}", type=click.STRING, hide_input=True
)
_prompted_param_overrides[parameter_key] = {"Value": parameter, "Hidden": True}
else:
parameter = click.prompt(
f"\t{start_bold}Parameter {parameter_key}{end_bold}",
default=_prompted_param_overrides.get(
parameter_key, parameter_properties.get("Default", "No default specified")
),
type=click.STRING,
)
_prompted_param_overrides[parameter_key] = {"Value": parameter, "Hidden": False}
return _prompted_param_overrides


def print_deploy_args(stack_name, s3_bucket, region, capabilities, parameter_overrides, confirm_changeset):

_parameters = parameter_overrides.copy()
Expand Down
3 changes: 2 additions & 1 deletion samcli/commands/deploy/deploy_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,15 @@ def run(self):
session = boto3.Session(profile_name=self.profile if self.profile else None)
cloudformation_client = session.client("cloudformation", region_name=self.region if self.region else None)

s3_client = None
if self.s3_bucket:
s3_client = session.client("s3", region_name=self.region if self.region else None)

self.s3_uploader = S3Uploader(s3_client, self.s3_bucket, self.s3_prefix, self.kms_key_id, self.force_upload)

self.deployer = Deployer(cloudformation_client)

region = s3_client._client_config.region_name # pylint: disable=W0212
region = s3_client._client_config.region_name if s3_client else self.region # pylint: disable=W0212

return self.deploy(
self.stack_name,
Expand Down
4 changes: 4 additions & 0 deletions samcli/lib/package/s3_uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ def upload(self, file_name, remote_path):
additional_args["Metadata"] = self.artifact_metadata

print_progress_callback = ProgressPercentage(file_name, remote_path)
if not self.bucket_name:
raise BucketNotSpecifiedError()
future = self.transfer_manager.upload(
file_name, self.bucket_name, remote_path, additional_args, [print_progress_callback]
)
Expand Down Expand Up @@ -144,6 +146,8 @@ def file_exists(self, remote_path):
return False

def make_url(self, obj_path):
if not self.bucket_name:
raise BucketNotSpecifiedError()
return "s3://{0}/{1}".format(self.bucket_name, obj_path)

def file_checksum(self, file_name):
Expand Down
6 changes: 6 additions & 0 deletions tests/integration/deploy/deploy_integ_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,20 @@ def get_deploy_command_list(
force_upload=False,
notification_arns=None,
fail_on_empty_changeset=False,
confirm_changeset=False,
no_execute_changeset=False,
parameter_overrides=None,
role_arn=None,
kms_key_id=None,
tags=None,
profile=None,
region=None,
guided=False,
):
command_list = [self.base_command(), "deploy"]

if guided:
command_list = command_list + ["--guided"]
if s3_bucket:
command_list = command_list + ["--s3-bucket", str(s3_bucket)]
if capabilities:
Expand All @@ -73,6 +77,8 @@ def get_deploy_command_list(
command_list = command_list + ["--force-upload"]
if fail_on_empty_changeset:
command_list = command_list + ["--fail-on-empty-changeset"]
if confirm_changeset:
command_list = command_list + ["--confirm-changeset"]
if tags:
command_list = command_list + ["--tags", str(tags)]
if region:
Expand Down
Loading

0 comments on commit e93897a

Please sign in to comment.