def test_wait_for_deployment(mock_sleep, mock_instances_deployed, mock__log,
                             mock_get_cluster_instance_map_for_service):
    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_sleeper = MockSleep()
    mock_sleep.side_effect = mock_sleeper.mock_sleep_side_effect

    with raises(TimeoutError):
        mark_for_deployment.wait_for_deployment('service', 'deploy_group_1', 'somesha', '/nail/soa', 1)

    mock_get_cluster_instance_map_for_service.assert_called_with('/nail/soa', 'service', 'deploy_group_1')
    mock_instances_deployed.assert_called_with(cluster='cluster1',
                                               service='service',
                                               instances=mock_cluster_map['cluster1']['instances'],
                                               git_sha='somesha')

    mock_cluster_map = {'cluster1': {'instances': ['instance1', 'instance2']},
                        'cluster2': {'instances': ['instance1', 'instance2']}}
    mock_get_cluster_instance_map_for_service.return_value = mock_cluster_map
    assert mark_for_deployment.wait_for_deployment('service', 'deploy_group_1', 'somesha', '/nail/soa', 1)

    mock_cluster_map = {'cluster1': {'instances': ['instance1', 'instance2']},
                        'cluster2': {'instances': ['instance1', 'instance3']}}
    mock_get_cluster_instance_map_for_service.return_value = mock_cluster_map
    mock_sleeper.call_count = 0
    with raises(TimeoutError):
        mark_for_deployment.wait_for_deployment('service', 'deploy_group_1', 'somesha', '/nail/soa', 1)
Example #2
0
def paasta_wait_for_deployment(args):
    """Wrapping wait_for_deployment"""
    if args.verbose:
        log.setLevel(level=logging.DEBUG)
    else:
        log.setLevel(level=logging.INFO)

    service = args.service
    if service and service.startswith('services-'):
        service = service.split('services-', 1)[1]

    if args.git_url is None:
        args.git_url = get_git_url(service=service, soa_dir=args.soa_dir)

    try:
        validate_full_git_sha(args.commit)
    except ArgumentTypeError:
        refs = remote_git.list_remote_refs(args.git_url)
        commits = short_to_full_git_sha(short=args.commit, refs=refs)
        if len(commits) != 1:
            raise ValueError(
                "%s matched %d git shas (with refs pointing at them). Must match exactly 1."
                % (args.commit, len(commits)), )
        args.commit = commits[0]

    try:
        validate_service_name(service, soa_dir=args.soa_dir)
        validate_deploy_group(args.deploy_group, service, args.soa_dir)
        validate_git_sha(
            args.commit,
            args.git_url,
            args.deploy_group,
            service,
        )
    except (GitShaError, DeployGroupError, NoSuchService) as e:
        paasta_print(PaastaColors.red('{}'.format(e)))
        return 1

    try:
        wait_for_deployment(
            service=service,
            deploy_group=args.deploy_group,
            git_sha=args.commit,
            soa_dir=args.soa_dir,
            timeout=args.timeout,
        )
        _log(
            service=service,
            component='deploy',
            line=("Deployment of {} for {} complete".format(
                args.commit, args.deploy_group)),
            level='event',
        )

    except (KeyboardInterrupt, TimeoutError, NoSuchCluster):
        report_waiting_aborted(service, args.deploy_group)
        return 1

    return 0
def test_wait_for_deployment(
    mock_instances_deployed,
    mock__log,
    mock_paasta_service_config_loader,
    mock_load_system_paasta_config,
):
    mock_paasta_service_config_loader.return_value.clusters = ['cluster1']
    mock_paasta_service_config_loader.return_value.instance_configs.return_value = [
        mock_marathon_instance_config('instance1'),
        mock_marathon_instance_config('instance2'),
        mock_marathon_instance_config('instance3'),
    ]
    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):
                mark_for_deployment.wait_for_deployment(
                    'service', 'fake_deploy_group', 'somesha', '/nail/soa', 1)

    mock_paasta_service_config_loader.return_value.clusters = [
        'cluster1', 'cluster2'
    ]
    mock_paasta_service_config_loader.return_value.instance_configs.side_effect = [
        [
            mock_marathon_instance_config('instance1'),
            mock_marathon_instance_config('instance2')
        ],
        [
            mock_marathon_instance_config('instance1'),
            mock_marathon_instance_config('instance2')
        ],
    ]
    with patch('sys.stdout', autospec=True, flush=Mock()):
        assert mark_for_deployment.wait_for_deployment('service',
                                                       'fake_deploy_group',
                                                       'somesha', '/nail/soa',
                                                       5) == 0

    mock_paasta_service_config_loader.return_value.clusters = [
        'cluster1', 'cluster2'
    ]
    mock_paasta_service_config_loader.return_value.instance_configs.side_effect = [
        [
            mock_marathon_instance_config('instance1'),
            mock_marathon_instance_config('instance2')
        ],
        [
            mock_marathon_instance_config('instance1'),
            mock_marathon_instance_config('instance3')
        ],
    ]
    with raises(TimeoutError):
        mark_for_deployment.wait_for_deployment('service', 'fake_deploy_group',
                                                'somesha', '/nail/soa', 0)
def test_wait_for_deployment(
    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):
                mark_for_deployment.wait_for_deployment(
                    'service', 'deploy_group_1', 'somesha', '/nail/soa', 1)
    mock_get_cluster_instance_map_for_service.assert_called_with(
        '/nail/soa', 'service', 'deploy_group_1')

    mock_cluster_map = {
        'cluster1': {
            'instances': ['instance1', 'instance2']
        },
        'cluster2': {
            'instances': ['instance1', 'instance2']
        },
    }
    mock_get_cluster_instance_map_for_service.return_value = mock_cluster_map
    with patch('sys.stdout', autospec=True, flush=Mock()):
        assert mark_for_deployment.wait_for_deployment('service',
                                                       'deploy_group_2',
                                                       'somesha', '/nail/soa',
                                                       5) == 0

    mock_cluster_map = {
        'cluster1': {
            'instances': ['instance1', 'instance2']
        },
        'cluster2': {
            'instances': ['instance1', 'instance3']
        },
    }
    mock_get_cluster_instance_map_for_service.return_value = mock_cluster_map
    with raises(TimeoutError):
        mark_for_deployment.wait_for_deployment('service', 'deploy_group_3',
                                                'somesha', '/nail/soa', 0)
Example #5
0
def test_wait_for_deployment_raise_no_such_cluster(
    mock_instances_deployed,
    mock__log,
    mock_paasta_service_config,
    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_paasta_service_config.return_value.clusters = ['cluster3']
    with raises(NoSuchCluster):
        mark_for_deployment.wait_for_deployment('service', 'deploy_group_3',
                                                'somesha', '/nail/soa', 0)
def paasta_wait_for_deployment(args):
    """Wrapping wait_for_deployment"""
    if args.verbose:
        log.setLevel(level=logging.DEBUG)
    else:
        log.setLevel(level=logging.INFO)

    service = args.service
    if service and service.startswith('services-'):
        service = service.split('services-', 1)[1]

    if args.git_url is None:
        args.git_url = get_git_url(service=service, soa_dir=args.soa_dir)

    args.commit = validate_git_sha(sha=args.commit, git_url=args.git_url)

    try:
        validate_service_name(service, soa_dir=args.soa_dir)
        validate_deploy_group(args.deploy_group, service, args.soa_dir)
        validate_git_sha_is_latest(
            args.commit,
            args.git_url,
            args.deploy_group,
            service,
        )
    except (GitShaError, DeployGroupError, NoSuchService) as e:
        paasta_print(PaastaColors.red(f'{e}'))
        return 1

    try:
        wait_for_deployment(
            service=service,
            deploy_group=args.deploy_group,
            git_sha=args.commit,
            soa_dir=args.soa_dir,
            timeout=args.timeout,
        )
        _log(
            service=service,
            component='deploy',
            line=("Deployment of {} for {} complete".format(
                args.commit, args.deploy_group)),
            level='event',
        )

    except (KeyboardInterrupt, TimeoutError, NoSuchCluster):
        report_waiting_aborted(service, args.deploy_group)
        return 1

    return 0
def test_wait_for_deployment(mock_sleep, mock_instances_deployed, mock__log,
                             mock_get_cluster_instance_map_for_service):
    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_sleeper = MockSleep()
    mock_sleep.side_effect = mock_sleeper.mock_sleep_side_effect

    with raises(TimeoutError):
        mark_for_deployment.wait_for_deployment('service', 'deploy_group_1',
                                                'somesha', '/nail/soa', 1)

    mock_get_cluster_instance_map_for_service.assert_called_with(
        '/nail/soa', 'service', 'deploy_group_1')
    mock_instances_deployed.assert_called_with(
        cluster='cluster1',
        service='service',
        instances=mock_cluster_map['cluster1']['instances'],
        git_sha='somesha')

    mock_cluster_map = {
        'cluster1': {
            'instances': ['instance1', 'instance2']
        },
        'cluster2': {
            'instances': ['instance1', 'instance2']
        }
    }
    mock_get_cluster_instance_map_for_service.return_value = mock_cluster_map
    assert mark_for_deployment.wait_for_deployment('service', 'deploy_group_1',
                                                   'somesha', '/nail/soa', 1)

    mock_cluster_map = {
        'cluster1': {
            'instances': ['instance1', 'instance2']
        },
        'cluster2': {
            'instances': ['instance1', 'instance3']
        }
    }
    mock_get_cluster_instance_map_for_service.return_value = mock_cluster_map
    mock_sleeper.call_count = 0
    with raises(TimeoutError):
        mark_for_deployment.wait_for_deployment('service', 'deploy_group_1',
                                                'somesha', '/nail/soa', 1)
Example #8
0
def test_wait_for_deployment_raise_no_such_cluster(
    mock_instances_deployed,
    mock__log,
    mock_paasta_service_config_loader,
    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_paasta_service_config_loader.return_value.clusters = ["cluster3"]
    with raises(NoSuchCluster):
        mark_for_deployment.wait_for_deployment("service", "deploy_group_3",
                                                "somesha", "/nail/soa", 0)
Example #9
0
def paasta_wait_for_deployment(args):
    """Wrapping wait_for_deployment"""
    if args.verbose:
        log.setLevel(level=logging.DEBUG)
    else:
        log.setLevel(level=logging.INFO)

    service = args.service
    if service and service.startswith('services-'):
        service = service.split('services-', 1)[1]

    if args.git_url is None:
        args.git_url = get_git_url(service=service, soa_dir=args.soa_dir)

    try:
        validate_service_name(service, soa_dir=args.soa_dir)
        validate_deploy_group(args.deploy_group, service, args.soa_dir)
        validate_git_sha(args.commit, args.git_url, args.deploy_group, service)
    except (GitShaError, DeployGroupError, NoSuchService) as e:
        paasta_print(PaastaColors.red('{}'.format(e)))
        return 1

    try:
        wait_for_deployment(
            service=service,
            deploy_group=args.deploy_group,
            git_sha=args.commit,
            soa_dir=args.soa_dir,
            timeout=args.timeout)
        _log(
            service=service,
            component='deploy',
            line=("Deployment of {0} for {1} complete".format(
                args.commit, args.deploy_group)),
            level='event')

    except (KeyboardInterrupt, TimeoutError):
        paasta_print("Waiting for deployment aborted.")
        return 1
    except NoInstancesFound:
        return 1

    return 0
Example #10
0
def paasta_wait_for_deployment(args):
    """Wrapping wait_for_deployment"""
    if args.verbose:
        log.setLevel(level=logging.DEBUG)
    else:
        log.setLevel(level=logging.INFO)

    service = args.service
    if service and service.startswith('services-'):
        service = service.split('services-', 1)[1]

    if args.git_url is None:
        args.git_url = get_git_url(service=service, soa_dir=args.soa_dir)

    try:
        validate_service_name(service, soa_dir=args.soa_dir)
        validate_deploy_group(args.deploy_group, service, args.soa_dir)
        validate_git_sha(args.commit, args.git_url,
                         args.deploy_group, service)
    except (GitShaError, DeployGroupError, NoSuchService) as e:
        paasta_print(PaastaColors.red('{}'.format(e)))
        return 1

    try:
        wait_for_deployment(service=service,
                            deploy_group=args.deploy_group,
                            git_sha=args.commit,
                            soa_dir=args.soa_dir,
                            timeout=args.timeout)
        _log(service=service,
             component='deploy',
             line=("Deployment of {0} for {1} complete".
                   format(args.commit, args.deploy_group)),
             level='event')

    except (KeyboardInterrupt, TimeoutError):
        paasta_print("Waiting for deployment aborted.")
        return 1
    except NoInstancesFound:
        return 1

    return 0
def test_wait_for_deployment(mock_instances_deployed, mock__log,
                             mock_get_cluster_instance_map_for_service):
    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

    with raises(TimeoutError):
        mark_for_deployment.wait_for_deployment('service', 'deploy_group_1',
                                                'somesha', '/nail/soa', 1)
    mock_get_cluster_instance_map_for_service.assert_called_with(
        '/nail/soa', 'service', 'deploy_group_1')

    mock_cluster_map = {
        'cluster1': {
            'instances': ['instance1', 'instance2']
        },
        'cluster2': {
            'instances': ['instance1', 'instance2']
        }
    }
    mock_get_cluster_instance_map_for_service.return_value = mock_cluster_map
    assert mark_for_deployment.wait_for_deployment('service', 'deploy_group_2',
                                                   'somesha', '/nail/soa',
                                                   5) == 0

    mock_cluster_map = {
        'cluster1': {
            'instances': ['instance1', 'instance2']
        },
        'cluster2': {
            'instances': ['instance1', 'instance3']
        }
    }
    mock_get_cluster_instance_map_for_service.return_value = mock_cluster_map
    with raises(TimeoutError):
        mark_for_deployment.wait_for_deployment('service', 'deploy_group_3',
                                                'somesha', '/nail/soa', 1)
Example #12
0
def paasta_wait_for_deployment(args):
    """Wrapping wait_for_deployment"""
    if args.verbose:
        log.setLevel(level=logging.DEBUG)
    else:
        log.setLevel(level=logging.INFO)

    service = args.service
    if service and service.startswith("services-"):
        service = service.split("services-", 1)[1]

    if args.git_url is None:
        args.git_url = get_git_url(service=service, soa_dir=args.soa_dir)

    args.commit = validate_git_sha(sha=args.commit, git_url=args.git_url)

    version = DeploymentVersion(sha=args.commit,
                                image_version=args.image_version)

    try:
        validate_service_name(service, soa_dir=args.soa_dir)
        validate_deploy_group(args.deploy_group, service, args.soa_dir)
        validate_version_is_latest(version, args.git_url, args.deploy_group,
                                   service)
    except (VersionError, DeployGroupError, NoSuchService) as e:
        print(PaastaColors.red(f"{e}"))
        return 1

    try:
        asyncio.run(
            wait_for_deployment(
                service=service,
                deploy_group=args.deploy_group,
                git_sha=args.commit,
                image_version=args.image_version,
                soa_dir=args.soa_dir,
                timeout=args.timeout,
                polling_interval=args.polling_interval,
                diagnosis_interval=args.diagnosis_interval,
                time_before_first_diagnosis=args.time_before_first_diagnosis,
            ))
        _log(
            service=service,
            component="deploy",
            line=(f"Deployment of {version} for {args.deploy_group} complete"),
            level="event",
        )

    except (KeyboardInterrupt, TimeoutError, NoSuchCluster):
        report_waiting_aborted(service, args.deploy_group)
        return 1

    return 0
def test_wait_for_deployment(
    mock_check_if_instance_is_done,
    mock__log,
    mock_get_instance_configs_for_service_in_deploy_group_all_clusters,
    mock_load_system_paasta_config,
):
    mock_get_instance_configs_for_service_in_deploy_group_all_clusters.return_value = {
        "cluster1": [
            mock_marathon_instance_config("instance1"),
            mock_marathon_instance_config("instance2"),
            mock_marathon_instance_config("instance3"),
        ],
    }

    def check_if_instance_is_done_side_effect(service,
                                              instance,
                                              cluster,
                                              version,
                                              instance_config,
                                              api=None):
        return instance in ["instance1", "instance2"]

    mock_check_if_instance_is_done.side_effect = check_if_instance_is_done_side_effect

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

    mock_load_system_paasta_config.return_value.get_mark_for_deployment_max_polling_threads.return_value = (
        4)

    with raises(TimeoutError):
        with patch("asyncio.as_completed",
                   side_effect=[asyncio.TimeoutError],
                   autospec=True):
            asyncio.run(
                mark_for_deployment.wait_for_deployment(
                    "service", "fake_deploy_group", "somesha", "/nail/soa", 1))

    mock_get_instance_configs_for_service_in_deploy_group_all_clusters.return_value = {
        "cluster1": [
            mock_marathon_instance_config("instance1"),
            mock_marathon_instance_config("instance2"),
        ],
        "cluster2": [
            mock_marathon_instance_config("instance1"),
            mock_marathon_instance_config("instance2"),
        ],
    }
    with patch("sys.stdout", autospec=True, flush=Mock()):
        assert (asyncio.run(
            mark_for_deployment.wait_for_deployment("service",
                                                    "fake_deploy_group",
                                                    "somesha", "/nail/soa",
                                                    5)) == 0)

    mock_get_instance_configs_for_service_in_deploy_group_all_clusters.return_value = {
        "cluster1": [
            mock_marathon_instance_config("instance1"),
            mock_marathon_instance_config("instance2"),
        ],
        "cluster2": [
            mock_marathon_instance_config("instance1"),
            mock_marathon_instance_config("instance3"),
        ],
    }
    with raises(TimeoutError):
        asyncio.run(
            mark_for_deployment.wait_for_deployment("service",
                                                    "fake_deploy_group",
                                                    "somesha", "/nail/soa", 0))
Example #14
0
def test_wait_for_deployment(
    mock_instances_deployed,
    mock__log,
    mock_paasta_service_config_loader,
    mock_load_system_paasta_config,
):
    mock_paasta_service_config_loader.return_value.clusters = ["cluster1"]
    mock_paasta_service_config_loader.return_value.instance_configs.return_value = [
        mock_marathon_instance_config("instance1"),
        mock_marathon_instance_config("instance2"),
        mock_marathon_instance_config("instance3"),
    ]
    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):
                mark_for_deployment.wait_for_deployment(
                    "service", "fake_deploy_group", "somesha", "/nail/soa", 1)

    mock_paasta_service_config_loader.return_value.clusters = [
        "cluster1", "cluster2"
    ]
    mock_paasta_service_config_loader.return_value.instance_configs.side_effect = [
        [
            mock_marathon_instance_config("instance1"),
            mock_marathon_instance_config("instance2"),
        ],
        [
            mock_marathon_instance_config("instance1"),
            mock_marathon_instance_config("instance2"),
        ],
        [],
        [],
    ]
    with patch("sys.stdout", autospec=True, flush=Mock()):
        assert (mark_for_deployment.wait_for_deployment(
            "service", "fake_deploy_group", "somesha", "/nail/soa", 5) == 0)

    mock_paasta_service_config_loader.return_value.clusters = [
        "cluster1", "cluster2"
    ]
    mock_paasta_service_config_loader.return_value.instance_configs.side_effect = [
        [
            mock_marathon_instance_config("instance1"),
            mock_marathon_instance_config("instance2"),
        ],
        [
            mock_marathon_instance_config("instance1"),
            mock_marathon_instance_config("instance3"),
        ],
        [],
        [],
    ]
    with raises(TimeoutError):
        mark_for_deployment.wait_for_deployment("service", "fake_deploy_group",
                                                "somesha", "/nail/soa", 0)