Skip to content

Commit

Permalink
Fail wait-for-deployment if a cluster has no configured API endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Dudko committed Aug 1, 2017
1 parent 4fff48e commit 93f8bf8
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 8 deletions.
34 changes: 29 additions & 5 deletions paasta_tools/cli/cmds/mark_for_deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
from paasta_tools.utils import format_tag
from paasta_tools.utils import get_git_url
from paasta_tools.utils import get_paasta_tag_from_deploy_group
from paasta_tools.utils import load_system_paasta_config
from paasta_tools.utils import paasta_print
from paasta_tools.utils import PaastaColors
from paasta_tools.utils import TimeoutError
Expand Down Expand Up @@ -175,6 +176,17 @@ def mark_for_deployment(git_url, deploy_group, service, commit):
return return_code


def report_waiting_aborted(service, deploy_group):
paasta_print(PaastaColors.red(
"Waiting for deployment aborted."
" PaaSTA will continue trying to deploy this code.",
))
paasta_print("If you wish to see the status, run:")
paasta_print()
paasta_print(" paasta status -s %s -l %s -v" % (service, deploy_group))
paasta_print()


def paasta_mark_for_deployment(args):
"""Wrapping mark_for_deployment"""
if args.verbose:
Expand Down Expand Up @@ -264,12 +276,10 @@ def paasta_mark_for_deployment(args):
commit=old_git_sha,
)
else:
paasta_print("Waiting for deployment aborted. PaaSTA will continue to try to deploy this code.")
paasta_print("If you wish to see the status, run:")
paasta_print()
paasta_print(" paasta status -s %s -v" % service)
paasta_print()
report_waiting_aborted(service, args.deploy_group)
ret = 1
except NoSuchCluster:
report_waiting_aborted(service, args.deploy_group)
except NoInstancesFound:
return 1
if old_git_sha is not None and old_git_sha != args.commit and not args.auto_rollback:
Expand Down Expand Up @@ -527,7 +537,14 @@ def wait_for_deployment(service, deploy_group, git_sha, soa_dir, timeout):

total_instances = 0
clusters_data = []
api_endpoints = load_system_paasta_config().get_api_endpoints()
for cluster in cluster_map:
if cluster not in api_endpoints:
paasta_print(PaastaColors.red(
'Cluster %s is NOT in paasta-api endpoints config.' %
cluster,
))
raise NoSuchCluster
clusters_data.append(ClusterData(
cluster=cluster, service=service,
git_sha=git_sha,
Expand Down Expand Up @@ -615,3 +632,10 @@ def compose_timeout_message(clusters_data, timeout, deploy_group, service, git_s

class NoInstancesFound(Exception):
pass


class NoSuchCluster(Exception):
"""To be raised by wait_for_deployment() when a service has a marathon config for
a cluster that is not listed in /etc/paasta/api_endpoints.json.
"""
pass
6 changes: 4 additions & 2 deletions paasta_tools/cli/cmds/wait_for_deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

from paasta_tools import remote_git
from paasta_tools.cli.cmds.mark_for_deployment import NoInstancesFound
from paasta_tools.cli.cmds.mark_for_deployment import NoSuchCluster
from paasta_tools.cli.cmds.mark_for_deployment import report_waiting_aborted
from paasta_tools.cli.cmds.mark_for_deployment import wait_for_deployment
from paasta_tools.cli.utils import lazy_choices_completer
from paasta_tools.cli.utils import list_deploy_groups
Expand Down Expand Up @@ -238,8 +240,8 @@ def paasta_wait_for_deployment(args):
level='event',
)

except (KeyboardInterrupt, TimeoutError):
paasta_print("Waiting for deployment aborted.")
except (KeyboardInterrupt, TimeoutError, NoSuchCluster):
report_waiting_aborted(service, args.deploy_group)
return 1
except NoInstancesFound:
return 1
Expand Down
27 changes: 26 additions & 1 deletion tests/cli/test_cmds_wait_for_deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from six.moves.queue import Queue

from paasta_tools.cli.cmds import mark_for_deployment
from paasta_tools.cli.cmds.mark_for_deployment import NoSuchCluster
from paasta_tools.cli.cmds.wait_for_deployment import get_latest_marked_sha
from paasta_tools.cli.cmds.wait_for_deployment import paasta_wait_for_deployment
from paasta_tools.cli.cmds.wait_for_deployment import validate_git_sha
Expand Down Expand Up @@ -222,17 +223,23 @@ def instances_deployed_side_effect(cluster_data, instances_out, green_light): #
cluster_data.instances_queue.task_done()


@patch('paasta_tools.cli.cmds.mark_for_deployment.load_system_paasta_config', autospec=True)
@patch('paasta_tools.cli.cmds.mark_for_deployment.get_cluster_instance_map_for_service', autospec=True)
@patch('paasta_tools.cli.cmds.mark_for_deployment._log', autospec=True)
@patch('paasta_tools.cli.cmds.mark_for_deployment.instances_deployed', autospec=True)
def test_wait_for_deployment(
mock_instances_deployed, mock__log,
mock_instances_deployed,
mock__log,
mock_get_cluster_instance_map_for_service,
mock_load_system_paasta_config,
):
mock_cluster_map = {'cluster1': {'instances': ['instance1', 'instance2', 'instance3']}}
mock_get_cluster_instance_map_for_service.return_value = mock_cluster_map
mock_instances_deployed.side_effect = instances_deployed_side_effect

mock_load_system_paasta_config.return_value.get_api_endpoints.return_value = \
{'cluster1': 'some_url_1', 'cluster2': 'some_url_2'}

with raises(TimeoutError):
with patch('time.time', side_effect=[0, 0, 2], autospec=True):
with patch('time.sleep', autospec=True):
Expand All @@ -256,6 +263,24 @@ def test_wait_for_deployment(
mark_for_deployment.wait_for_deployment('service', 'deploy_group_3', 'somesha', '/nail/soa', 0)


@patch('paasta_tools.cli.cmds.mark_for_deployment.load_system_paasta_config', autospec=True)
@patch('paasta_tools.cli.cmds.mark_for_deployment.get_cluster_instance_map_for_service', autospec=True)
@patch('paasta_tools.cli.cmds.mark_for_deployment._log', autospec=True)
@patch('paasta_tools.cli.cmds.mark_for_deployment.instances_deployed', autospec=True)
def test_wait_for_deployment_raise_no_such_cluster(
mock_instances_deployed,
mock__log,
mock_get_cluster_instance_map_for_service,
mock_load_system_paasta_config,
):
mock_load_system_paasta_config.return_value.get_api_endpoints.return_value = \
{'cluster1': 'some_url_1', 'cluster2': 'some_url_2'}

mock_get_cluster_instance_map_for_service.return_value = {'cluster3': {'instances': ['instance3']}}
with raises(NoSuchCluster):
mark_for_deployment.wait_for_deployment('service', 'deploy_group_3', 'somesha', '/nail/soa', 0)


@patch('paasta_tools.cli.cmds.wait_for_deployment.validate_service_name', autospec=True)
@patch('paasta_tools.cli.cmds.mark_for_deployment.wait_for_deployment', autospec=True)
def test_paasta_wait_for_deployment_return_1_when_no_such_service(
Expand Down

0 comments on commit 93f8bf8

Please sign in to comment.