def test_status_smartstack_backends_multiple_locations_expected_count():
    service = "my_service"
    instance = "my_instance"
    service_instance = compose_job_id(service, instance)
    cluster = "fake_cluster"
    normal_count = 10

    good_task = mock.Mock()
    other_task = mock.Mock()
    fake_backend = {
        "status": "UP",
        "lastchg": "1",
        "last_chk": "OK",
        "check_code": "200",
        "svname": "ipaddress1:1001_hostname1",
        "check_status": "L7OK",
        "check_duration": 1,
    }
    with contextlib.nested(
        mock.patch("paasta_tools.marathon_tools.load_service_namespace_config", autospec=True),
        mock.patch("paasta_tools.marathon_tools.read_namespace_for_service_instance"),
        mock.patch("paasta_tools.marathon_serviceinit.get_mesos_slaves_grouped_by_attribute"),
        mock.patch("paasta_tools.marathon_serviceinit.get_backends", autospec=True),
        mock.patch("paasta_tools.marathon_serviceinit.match_backends_and_tasks", autospec=True),
        mock.patch("paasta_tools.marathon_serviceinit.haproxy_backend_report", autospec=True),
    ) as (
        mock_load_service_namespace_config,
        mock_read_ns,
        mock_get_mesos_slaves_grouped_by_attribute,
        mock_get_backends,
        mock_match_backends_and_tasks,
        mock_haproxy_backend_report,
    ):
        mock_load_service_namespace_config.return_value.get_discover.return_value = "fake_discover"
        mock_read_ns.return_value = instance
        mock_get_backends.return_value = [fake_backend]
        mock_match_backends_and_tasks.return_value = [(fake_backend, good_task)]
        tasks = [good_task, other_task]
        mock_get_mesos_slaves_grouped_by_attribute.return_value = {
            "fake_location1": ["fakehost1"],
            "fake_location2": ["fakehost2"],
        }
        marathon_serviceinit.status_smartstack_backends(
            service=service,
            instance=instance,
            cluster=cluster,
            job_config=fake_marathon_job_config,
            tasks=tasks,
            expected_count=normal_count,
            soa_dir=None,
            verbose=False,
        )
        mock_get_backends.assert_any_call(service_instance, synapse_host="fakehost1", synapse_port=3212)
        mock_get_backends.assert_any_call(service_instance, synapse_host="fakehost2", synapse_port=3212)
        expected_count_per_location = int(normal_count / len(mock_get_mesos_slaves_grouped_by_attribute.return_value))
        mock_haproxy_backend_report.assert_any_call(expected_count_per_location, 1)
示例#2
0
def test_status_smartstack_backends_no_smartstack_replication_info():
    service = 'my_service'
    instance = 'my_instance'
    service_instance = compose_job_id(service, instance)
    cluster = 'fake_cluster'
    tasks = mock.Mock()
    normal_count = 10
    with contextlib.nested(
        mock.patch('paasta_tools.marathon_tools.load_service_namespace_config', autospec=True),
        mock.patch('paasta_tools.marathon_tools.read_namespace_for_service_instance'),
        mock.patch('paasta_tools.marathon_serviceinit.get_mesos_slaves_grouped_by_attribute'),
    ) as (
        mock_load_service_namespace_config,
        mock_read_ns,
        mock_get_mesos_slaves_grouped_by_attribute,
    ):
        mock_load_service_namespace_config.return_value.get_discover.return_value = 'fake_discover'
        mock_read_ns.return_value = instance
        mock_get_mesos_slaves_grouped_by_attribute.return_value = {}
        actual = marathon_serviceinit.status_smartstack_backends(
            service=service,
            instance=instance,
            cluster=cluster,
            job_config=fake_marathon_job_config,
            tasks=tasks,
            expected_count=normal_count,
            soa_dir=None,
            verbose=False,
            synapse_port=123456,
            synapse_haproxy_url_format=DEFAULT_SYNAPSE_HAPROXY_URL_FORMAT,
        )
        assert "%s is NOT in smartstack" % service_instance in actual
示例#3
0
def test_status_smartstack_backends_multiple_locations():
    service = 'my_service'
    instance = 'my_instance'
    service_instance = compose_job_id(service, instance)
    cluster = 'fake_cluster'
    good_task = mock.Mock()
    other_task = mock.Mock()
    fake_backend = {'status': 'UP', 'lastchg': '1', 'last_chk': 'OK',
                    'check_code': '200', 'svname': 'ipaddress1:1001_hostname1',
                    'check_status': 'L7OK', 'check_duration': 1}
    with contextlib.nested(
        mock.patch('paasta_tools.marathon_tools.load_service_namespace_config', autospec=True),
        mock.patch('paasta_tools.marathon_tools.read_namespace_for_service_instance'),
        mock.patch('paasta_tools.marathon_serviceinit.get_mesos_slaves_grouped_by_attribute'),
        mock.patch('paasta_tools.marathon_serviceinit.get_backends', autospec=True),
        mock.patch('paasta_tools.marathon_serviceinit.match_backends_and_tasks', autospec=True),
    ) as (
        mock_load_service_namespace_config,
        mock_read_ns,
        mock_get_mesos_slaves_grouped_by_attribute,
        mock_get_backends,
        mock_match_backends_and_tasks,
    ):
        mock_load_service_namespace_config.return_value.get_discover.return_value = 'fake_discover'
        mock_read_ns.return_value = instance
        mock_get_backends.return_value = [fake_backend]
        mock_match_backends_and_tasks.return_value = [
            (fake_backend, good_task),
        ]
        tasks = [good_task, other_task]
        mock_get_mesos_slaves_grouped_by_attribute.return_value = {
            'fake_location1': ['fakehost1'],
            'fake_location2': ['fakehost2'],
        }
        actual = marathon_serviceinit.status_smartstack_backends(
            service=service,
            instance=instance,
            cluster=cluster,
            job_config=fake_marathon_job_config,
            tasks=tasks,
            expected_count=len(mock_get_backends.return_value),
            soa_dir=None,
            verbose=False,
            synapse_port=123456,
            synapse_haproxy_url_format=DEFAULT_SYNAPSE_HAPROXY_URL_FORMAT,
        )
        mock_get_backends.assert_any_call(
            service_instance,
            synapse_host='fakehost1',
            synapse_port=123456,
            synapse_haproxy_url_format=DEFAULT_SYNAPSE_HAPROXY_URL_FORMAT,
        )
        mock_get_backends.assert_any_call(
            service_instance,
            synapse_host='fakehost2',
            synapse_port=123456,
            synapse_haproxy_url_format=DEFAULT_SYNAPSE_HAPROXY_URL_FORMAT,
        )
        assert "fake_location1 - %s" % PaastaColors.green('Healthy') in actual
        assert "fake_location2 - %s" % PaastaColors.green('Healthy') in actual
示例#4
0
def test_status_smartstack_backends_no_smartstack_replication_info():
    service = "servicename"
    instance = "instancename"

    cluster = "fake_cluster"
    tasks = mock.Mock()
    normal_count = 10
    with mock.patch("paasta_tools.marathon_serviceinit.get_slaves",
                    autospec=True) as mock_get_slaves:
        fake_service_namespace_config = mock.Mock()
        fake_service_namespace_config.get_discover.return_value = "fake_discover"
        mock_get_slaves.return_value = {}
        actual = marathon_serviceinit.status_smartstack_backends(
            service=service,
            instance=instance,
            cluster=cluster,
            job_config=fake_marathon_job_config,
            service_namespace_config=fake_service_namespace_config,
            tasks=tasks,
            expected_count=normal_count,
            soa_dir=None,
            verbose=False,
            synapse_port=123456,
            synapse_haproxy_url_format=DEFAULT_SYNAPSE_HAPROXY_URL_FORMAT,
            system_deploy_blacklist=[],
            system_deploy_whitelist=[],
        )
        assert "servicename.fake_nerve_ns is NOT in smartstack" in actual
def test_status_smartstack_backends_no_smartstack_replication_info():
    service = 'my_service'
    instance = 'my_instance'
    service_instance = compose_job_id(service, instance)
    cluster = 'fake_cluster'
    tasks = mock.Mock()
    normal_count = 10
    with mock.patch(
            'paasta_tools.marathon_tools.load_service_namespace_config',
            autospec=True,
    ) as mock_load_service_namespace_config, mock.patch(
            'paasta_tools.marathon_tools.read_registration_for_service_instance',
            autospec=True,
    ) as mock_read_reg, mock.patch(
            'paasta_tools.marathon_serviceinit.get_all_slaves_for_blacklist_whitelist',
            autospec=True,
    ) as mock_get_all_slaves_for_blacklist_whitelist:
        mock_load_service_namespace_config.return_value.get_discover.return_value = 'fake_discover'
        mock_read_reg.return_value = service_instance
        mock_get_all_slaves_for_blacklist_whitelist.return_value = {}
        actual = marathon_serviceinit.status_smartstack_backends(
            service=service,
            instance=instance,
            cluster=cluster,
            job_config=fake_marathon_job_config,
            tasks=tasks,
            expected_count=normal_count,
            soa_dir=None,
            verbose=False,
            synapse_port=123456,
            synapse_haproxy_url_format=DEFAULT_SYNAPSE_HAPROXY_URL_FORMAT,
            system_deploy_blacklist=[],
            system_deploy_whitelist=[],
        )
        assert "%s is NOT in smartstack" % service_instance in actual
示例#6
0
def test_status_smartstack_backends_different_nerve_ns():
    service = 'my_service'
    instance = 'my_instance'
    cluster = 'fake_cluster'
    different_ns = 'other_instance'
    normal_count = 10
    tasks = mock.Mock()
    with mock.patch(
            'paasta_tools.marathon_tools.read_namespace_for_service_instance'
    ) as read_ns_mock:
        read_ns_mock.return_value = different_ns
        actual = marathon_serviceinit.status_smartstack_backends(
            service=service,
            instance=instance,
            cluster=cluster,
            job_config=fake_marathon_job_config,
            tasks=tasks,
            expected_count=normal_count,
            soa_dir=None,
            verbose=False,
            synapse_port=123456,
            synapse_haproxy_url_format=DEFAULT_SYNAPSE_HAPROXY_URL_FORMAT,
        )
        assert "is announced in the" in actual
        assert different_ns in actual
示例#7
0
def test_status_smartstack_backends_no_smartstack_replication_info():
    service = 'my_service'
    instance = 'my_instance'
    service_instance = compose_job_id(service, instance)
    cluster = 'fake_cluster'
    tasks = mock.Mock()
    normal_count = 10
    with contextlib.nested(
        mock.patch('paasta_tools.marathon_tools.load_service_namespace_config', autospec=True),
        mock.patch('paasta_tools.marathon_tools.read_registration_for_service_instance', autospec=True),
        mock.patch('paasta_tools.marathon_serviceinit.get_all_slaves_for_blacklist_whitelist', autospec=True),
    ) as (
        mock_load_service_namespace_config,
        mock_read_reg,
        mock_get_all_slaves_for_blacklist_whitelist,
    ):
        mock_load_service_namespace_config.return_value.get_discover.return_value = 'fake_discover'
        mock_read_reg.return_value = service_instance
        mock_get_all_slaves_for_blacklist_whitelist.return_value = {}
        actual = marathon_serviceinit.status_smartstack_backends(
            service=service,
            instance=instance,
            cluster=cluster,
            job_config=fake_marathon_job_config,
            tasks=tasks,
            expected_count=normal_count,
            soa_dir=None,
            verbose=False,
            synapse_port=123456,
            synapse_haproxy_url_format=DEFAULT_SYNAPSE_HAPROXY_URL_FORMAT,
        )
        assert "%s is NOT in smartstack" % service_instance in actual
def test_status_smartstack_backends_no_smartstack_replication_info():
    service = 'my_service'
    instance = 'my_instance'
    service_instance = compose_job_id(service, instance)
    cluster = 'fake_cluster'
    tasks = mock.Mock()
    normal_count = 10
    with contextlib.nested(
        mock.patch('paasta_tools.marathon_tools.load_service_namespace_config', autospec=True),
        mock.patch('paasta_tools.marathon_tools.read_namespace_for_service_instance'),
        mock.patch('paasta_tools.marathon_serviceinit.get_mesos_slaves_grouped_by_attribute'),
    ) as (
        mock_load_service_namespace_config,
        mock_read_ns,
        mock_get_mesos_slaves_grouped_by_attribute,
    ):
        mock_load_service_namespace_config.return_value.get_discover.return_value = 'fake_discover'
        mock_read_ns.return_value = instance
        mock_get_mesos_slaves_grouped_by_attribute.return_value = {}
        actual = marathon_serviceinit.status_smartstack_backends(
            service=service,
            instance=instance,
            cluster=cluster,
            job_config=fake_marathon_job_config,
            tasks=tasks,
            expected_count=normal_count,
            soa_dir=None,
            verbose=False,
        )
        assert "%s is NOT in smartstack" % service_instance in actual
示例#9
0
def test_status_smartstack_backends_verbose_emphasizes_maint_instances():
    service = 'my_service'
    instance = 'my_instance'
    cluster = 'fake_cluster'
    normal_count = 10
    good_task = mock.Mock()
    other_task = mock.Mock()
    fake_backend = {
        'status': 'MAINT',
        'lastchg': '1',
        'last_chk': 'OK',
        'check_code': '200',
        'svname': 'ipaddress1:1001_hostname1',
        'check_status': 'L7OK',
        'check_duration': 1
    }
    with contextlib.nested(
            mock.patch(
                'paasta_tools.marathon_tools.load_service_namespace_config',
                autospec=True),
            mock.patch(
                'paasta_tools.marathon_tools.read_namespace_for_service_instance'
            ),
            mock.patch(
                'paasta_tools.marathon_serviceinit.get_mesos_slaves_grouped_by_attribute'
            ),
            mock.patch('paasta_tools.marathon_serviceinit.get_backends',
                       autospec=True),
            mock.patch(
                'paasta_tools.marathon_serviceinit.match_backends_and_tasks',
                autospec=True),
    ) as (
            mock_load_service_namespace_config,
            mock_read_ns,
            mock_get_mesos_slaves_grouped_by_attribute,
            mock_get_backends,
            mock_match_backends_and_tasks,
    ):
        mock_load_service_namespace_config.return_value.get_discover.return_value = 'fake_discover'
        mock_read_ns.return_value = instance
        mock_get_backends.return_value = [fake_backend]
        mock_match_backends_and_tasks.return_value = [
            (fake_backend, good_task),
        ]
        tasks = [good_task, other_task]
        mock_get_mesos_slaves_grouped_by_attribute.return_value = {
            'fake_location1': ['fakehost1']
        }
        actual = marathon_serviceinit.status_smartstack_backends(
            service=service,
            instance=instance,
            cluster=cluster,
            job_config=fake_marathon_job_config,
            tasks=tasks,
            expected_count=normal_count,
            soa_dir=None,
            verbose=True,
        )
        assert PaastaColors.red('MAINT') in actual
示例#10
0
def test_status_smartstack_backends_verbose_multiple_apps():
    service = 'my_service'
    instance = 'my_instance'
    service_instance = compose_job_id(service, instance)
    cluster = 'fake_cluster'

    good_task = mock.Mock()
    bad_task = mock.Mock()
    other_task = mock.Mock()
    haproxy_backends_by_task = {
        good_task: {'status': 'UP', 'lastchg': '1', 'last_chk': 'OK',
                    'check_code': '200', 'svname': 'ipaddress1:1001_hostname1',
                    'check_status': 'L7OK', 'check_duration': 1},
        bad_task: {'status': 'UP', 'lastchg': '1', 'last_chk': 'OK',
                   'check_code': '200', 'svname': 'ipaddress2:1002_hostname2',
                   'check_status': 'L7OK', 'check_duration': 1},
    }

    with contextlib.nested(
        mock.patch('paasta_tools.marathon_tools.load_service_namespace_config', autospec=True),
        mock.patch('paasta_tools.marathon_tools.read_namespace_for_service_instance'),
        mock.patch('paasta_tools.marathon_serviceinit.get_mesos_slaves_grouped_by_attribute'),
        mock.patch('paasta_tools.marathon_serviceinit.get_backends', autospec=True),
        mock.patch('paasta_tools.marathon_serviceinit.match_backends_and_tasks', autospec=True),
    ) as (
        mock_load_service_namespace_config,
        mock_read_ns,
        mock_get_mesos_slaves_grouped_by_attribute,
        mock_get_backends,
        mock_match_backends_and_tasks,
    ):
        mock_load_service_namespace_config.return_value.get_discover.return_value = 'fake_discover'
        mock_read_ns.return_value = instance
        mock_get_backends.return_value = haproxy_backends_by_task.values()
        mock_match_backends_and_tasks.return_value = [
            (haproxy_backends_by_task[good_task], good_task),
            (haproxy_backends_by_task[bad_task], None),
            (None, other_task),
        ]
        tasks = [good_task, other_task]
        mock_get_mesos_slaves_grouped_by_attribute.return_value = {'fake_location1': ['fakehost1']}
        actual = marathon_serviceinit.status_smartstack_backends(
            service=service,
            instance=instance,
            cluster=cluster,
            job_config=fake_marathon_job_config,
            tasks=tasks,
            expected_count=len(haproxy_backends_by_task),
            soa_dir=None,
            verbose=True,
        )
        mock_get_backends.assert_called_once_with(
            service_instance,
            synapse_host='fakehost1',
            synapse_port=3212,
        )
        assert "fake_location1" in actual
        assert "hostname1:1001" in actual
        assert re.search(r"%s[^\n]*hostname2:1002" % re.escape(PaastaColors.GREY), actual)
def test_status_smartstack_backends_verbose_multiple_apps():
    service = 'my_service'
    instance = 'my_instance'
    service_instance = compose_job_id(service, instance)
    cluster = 'fake_cluster'

    good_task = mock.Mock()
    bad_task = mock.Mock()
    other_task = mock.Mock()
    haproxy_backends_by_task = {
        good_task: {'status': 'UP', 'lastchg': '1', 'last_chk': 'OK',
                    'check_code': '200', 'svname': 'ipaddress1:1001_hostname1',
                    'check_status': 'L7OK', 'check_duration': 1},
        bad_task: {'status': 'UP', 'lastchg': '1', 'last_chk': 'OK',
                   'check_code': '200', 'svname': 'ipaddress2:1002_hostname2',
                   'check_status': 'L7OK', 'check_duration': 1},
    }

    with contextlib.nested(
        mock.patch('paasta_tools.marathon_tools.load_service_namespace_config', autospec=True),
        mock.patch('paasta_tools.marathon_tools.read_namespace_for_service_instance'),
        mock.patch('paasta_tools.marathon_serviceinit.get_mesos_slaves_grouped_by_attribute'),
        mock.patch('paasta_tools.marathon_serviceinit.get_backends', autospec=True),
        mock.patch('paasta_tools.marathon_serviceinit.match_backends_and_tasks', autospec=True),
    ) as (
        mock_load_service_namespace_config,
        mock_read_ns,
        mock_get_mesos_slaves_grouped_by_attribute,
        mock_get_backends,
        mock_match_backends_and_tasks,
    ):
        mock_load_service_namespace_config.return_value.get_discover.return_value = 'fake_discover'
        mock_read_ns.return_value = instance
        mock_get_backends.return_value = haproxy_backends_by_task.values()
        mock_match_backends_and_tasks.return_value = [
            (haproxy_backends_by_task[good_task], good_task),
            (haproxy_backends_by_task[bad_task], None),
            (None, other_task),
        ]
        tasks = [good_task, other_task]
        mock_get_mesos_slaves_grouped_by_attribute.return_value = {'fake_location1': ['fakehost1']}
        actual = marathon_serviceinit.status_smartstack_backends(
            service=service,
            instance=instance,
            cluster=cluster,
            job_config=fake_marathon_job_config,
            tasks=tasks,
            expected_count=len(haproxy_backends_by_task),
            soa_dir=None,
            verbose=True,
        )
        mock_get_backends.assert_called_once_with(
            service_instance,
            synapse_host='fakehost1',
            synapse_port=3212,
        )
        assert "fake_location1" in actual
        assert "hostname1:1001" in actual
        assert re.search(r"%s[^\n]*hostname2:1002" % re.escape(PaastaColors.GREY), actual)
def test_status_smartstack_backends_verbose_demphasizes_maint_instances_for_unrelated_tasks(
):
    service = "servicename"
    instance = "instancename"
    cluster = "fake_cluster"
    normal_count = 10
    good_task = mock.Mock()
    other_task = mock.Mock()
    fake_backend = {
        "status": "MAINT",
        "lastchg": "1",
        "last_chk": "OK",
        "check_code": "200",
        "svname": "ipaddress1:1001_hostname1",
        "check_status": "L7OK",
        "check_duration": 1,
    }
    with mock.patch(
            "paasta_tools.marathon_serviceinit.get_all_slaves_for_blacklist_whitelist",
            autospec=True,
    ) as mock_get_all_slaves_for_blacklist_whitelist, mock.patch(
            "paasta_tools.marathon_serviceinit.get_backends",
            autospec=True) as mock_get_backends, mock.patch(
                "paasta_tools.marathon_serviceinit.match_backends_and_tasks",
                autospec=True) as mock_match_backends_and_tasks:
        mock_get_all_slaves_for_blacklist_whitelist.return_value = [{
            "hostname":
            "fake",
            "attributes": {
                "fake_discover": "fake_location_1"
            }
        }]
        fake_service_namespace_config = mock.Mock()
        fake_service_namespace_config.get_discover.return_value = "fake_discover"
        mock_get_backends.return_value = [fake_backend]
        mock_match_backends_and_tasks.return_value = [(fake_backend, None)]
        tasks = [good_task, other_task]
        actual = marathon_serviceinit.status_smartstack_backends(
            service=service,
            instance=instance,
            cluster=cluster,
            job_config=fake_marathon_job_config,
            service_namespace_config=fake_service_namespace_config,
            tasks=tasks,
            expected_count=normal_count,
            soa_dir=None,
            verbose=True,
            synapse_port=123456,
            synapse_haproxy_url_format=DEFAULT_SYNAPSE_HAPROXY_URL_FORMAT,
            system_deploy_blacklist=[],
            system_deploy_whitelist=[],
        )
        assert PaastaColors.red("MAINT") not in actual
        assert re.search(
            r"%s[^\n]*hostname1:1001" % re.escape(PaastaColors.GREY), actual)
def test_status_smartstack_backends_verbose_demphasizes_maint_instances_for_unrelated_tasks():
    service = 'my_service'
    instance = 'my_instance'
    cluster = 'fake_cluster'
    normal_count = 10
    good_task = mock.Mock()
    other_task = mock.Mock()
    fake_backend = {
        'status': 'MAINT', 'lastchg': '1', 'last_chk': 'OK',
        'check_code': '200', 'svname': 'ipaddress1:1001_hostname1',
        'check_status': 'L7OK', 'check_duration': 1,
    }
    with mock.patch(
        'paasta_tools.marathon_tools.load_service_namespace_config', autospec=True,
    ) as mock_load_service_namespace_config, mock.patch(
        'paasta_tools.marathon_tools.read_registration_for_service_instance', autospec=True,
    ) as mock_read_reg, mock.patch(
        'paasta_tools.marathon_serviceinit.get_all_slaves_for_blacklist_whitelist', autospec=True,
    ) as mock_get_all_slaves_for_blacklist_whitelist, mock.patch(
        'paasta_tools.marathon_serviceinit.get_backends', autospec=True,
    ) as mock_get_backends, mock.patch(
        'paasta_tools.marathon_serviceinit.match_backends_and_tasks', autospec=True,
    ) as mock_match_backends_and_tasks:
        mock_get_all_slaves_for_blacklist_whitelist.return_value = [
            {
                'hostname': 'fake',
                'attributes': {
                    'fake_discover': 'fake_location_1',
                },
            },
        ]
        mock_load_service_namespace_config.return_value.get_discover.return_value = 'fake_discover'
        mock_read_reg.return_value = compose_job_id(service, instance)
        mock_get_backends.return_value = [fake_backend]
        mock_match_backends_and_tasks.return_value = [
            (fake_backend, None),
        ]
        tasks = [good_task, other_task]
        actual = marathon_serviceinit.status_smartstack_backends(
            service=service,
            instance=instance,
            cluster=cluster,
            job_config=fake_marathon_job_config,
            tasks=tasks,
            expected_count=normal_count,
            soa_dir=None,
            verbose=True,
            synapse_port=123456,
            synapse_haproxy_url_format=DEFAULT_SYNAPSE_HAPROXY_URL_FORMAT,
            system_deploy_blacklist=[],
            system_deploy_whitelist=[],
        )
        assert PaastaColors.red('MAINT') not in actual
        assert re.search(r"%s[^\n]*hostname1:1001" % re.escape(PaastaColors.GREY), actual)
示例#14
0
def test_status_smartstack_backends_verbose_demphasizes_maint_instances_for_unrelated_tasks():
    service = 'my_service'
    instance = 'my_instance'
    cluster = 'fake_cluster'
    normal_count = 10
    good_task = mock.Mock()
    other_task = mock.Mock()
    fake_backend = {'status': 'MAINT', 'lastchg': '1', 'last_chk': 'OK',
                    'check_code': '200', 'svname': 'ipaddress1:1001_hostname1',
                    'check_status': 'L7OK', 'check_duration': 1}
    with contextlib.nested(
        mock.patch('paasta_tools.marathon_tools.load_service_namespace_config', autospec=True),
        mock.patch('paasta_tools.marathon_tools.read_registration_for_service_instance', autospec=True),
        mock.patch('paasta_tools.marathon_serviceinit.get_all_slaves_for_blacklist_whitelist', autospec=True),
        mock.patch('paasta_tools.marathon_serviceinit.get_backends', autospec=True),
        mock.patch('paasta_tools.marathon_serviceinit.match_backends_and_tasks', autospec=True),
    ) as (
        mock_load_service_namespace_config,
        mock_read_reg,
        mock_get_all_slaves_for_blacklist_whitelist,
        mock_get_backends,
        mock_match_backends_and_tasks,
    ):
        mock_get_all_slaves_for_blacklist_whitelist.return_value = [
            {
                'hostname': 'fake',
                'attributes': {
                    'fake_discover': 'fake_location_1'
                }
            }
        ]
        mock_load_service_namespace_config.return_value.get_discover.return_value = 'fake_discover'
        mock_read_reg.return_value = compose_job_id(service, instance)
        mock_get_backends.return_value = [fake_backend]
        mock_match_backends_and_tasks.return_value = [
            (fake_backend, None),
        ]
        tasks = [good_task, other_task]
        actual = marathon_serviceinit.status_smartstack_backends(
            service=service,
            instance=instance,
            cluster=cluster,
            job_config=fake_marathon_job_config,
            tasks=tasks,
            expected_count=normal_count,
            soa_dir=None,
            verbose=True,
            synapse_port=123456,
            synapse_haproxy_url_format=DEFAULT_SYNAPSE_HAPROXY_URL_FORMAT,
        )
        assert PaastaColors.red('MAINT') not in actual
        assert re.search(r"%s[^\n]*hostname1:1001" % re.escape(PaastaColors.GREY), actual)
def test_status_smartstack_backends_verbose_demphasizes_maint_instances_for_unrelated_tasks():
    service = "my_service"
    instance = "my_instance"
    cluster = "fake_cluster"
    normal_count = 10
    good_task = mock.Mock()
    other_task = mock.Mock()
    fake_backend = {
        "status": "MAINT",
        "lastchg": "1",
        "last_chk": "OK",
        "check_code": "200",
        "svname": "ipaddress1:1001_hostname1",
        "check_status": "L7OK",
        "check_duration": 1,
    }
    with contextlib.nested(
        mock.patch("paasta_tools.marathon_tools.load_service_namespace_config", autospec=True),
        mock.patch("paasta_tools.marathon_tools.read_namespace_for_service_instance"),
        mock.patch("paasta_tools.marathon_serviceinit.get_mesos_slaves_grouped_by_attribute"),
        mock.patch("paasta_tools.marathon_serviceinit.get_backends", autospec=True),
        mock.patch("paasta_tools.marathon_serviceinit.match_backends_and_tasks", autospec=True),
    ) as (
        mock_load_service_namespace_config,
        mock_read_ns,
        mock_get_mesos_slaves_grouped_by_attribute,
        mock_get_backends,
        mock_match_backends_and_tasks,
    ):
        mock_load_service_namespace_config.return_value.get_discover.return_value = "fake_discover"
        mock_read_ns.return_value = instance
        mock_get_backends.return_value = [fake_backend]
        mock_match_backends_and_tasks.return_value = [(fake_backend, None)]
        tasks = [good_task, other_task]
        mock_get_mesos_slaves_grouped_by_attribute.return_value = {"fake_location1": ["fakehost1"]}
        actual = marathon_serviceinit.status_smartstack_backends(
            service=service,
            instance=instance,
            cluster=cluster,
            job_config=fake_marathon_job_config,
            tasks=tasks,
            expected_count=normal_count,
            soa_dir=None,
            verbose=True,
        )
        assert PaastaColors.red("MAINT") not in actual
        assert re.search(r"%s[^\n]*hostname1:1001" % re.escape(PaastaColors.GREY), actual)
示例#16
0
def test_status_smartstack_backends_verbose_emphasizes_maint_instances():
    service = 'my_service'
    instance = 'my_instance'
    cluster = 'fake_cluster'
    normal_count = 10
    good_task = mock.Mock()
    other_task = mock.Mock()
    fake_backend = {'status': 'MAINT', 'lastchg': '1', 'last_chk': 'OK',
                    'check_code': '200', 'svname': 'ipaddress1:1001_hostname1',
                    'check_status': 'L7OK', 'check_duration': 1}
    with contextlib.nested(
        mock.patch('paasta_tools.marathon_tools.load_service_namespace_config', autospec=True),
        mock.patch('paasta_tools.marathon_tools.read_namespace_for_service_instance'),
        mock.patch('paasta_tools.marathon_serviceinit.get_mesos_slaves_grouped_by_attribute'),
        mock.patch('paasta_tools.marathon_serviceinit.get_backends', autospec=True),
        mock.patch('paasta_tools.marathon_serviceinit.match_backends_and_tasks', autospec=True),
    ) as (
        mock_load_service_namespace_config,
        mock_read_ns,
        mock_get_mesos_slaves_grouped_by_attribute,
        mock_get_backends,
        mock_match_backends_and_tasks,
    ):
        mock_load_service_namespace_config.return_value.get_discover.return_value = 'fake_discover'
        mock_read_ns.return_value = instance
        mock_get_backends.return_value = [fake_backend]
        mock_match_backends_and_tasks.return_value = [
            (fake_backend, good_task),
        ]
        tasks = [good_task, other_task]
        mock_get_mesos_slaves_grouped_by_attribute.return_value = {'fake_location1': ['fakehost1']}
        actual = marathon_serviceinit.status_smartstack_backends(
            service=service,
            instance=instance,
            cluster=cluster,
            job_config=fake_marathon_job_config,
            tasks=tasks,
            expected_count=normal_count,
            soa_dir=None,
            verbose=True,
            synapse_port=123456,
            synapse_haproxy_url_format=DEFAULT_SYNAPSE_HAPROXY_URL_FORMAT,
        )
        assert PaastaColors.red('MAINT') in actual
def test_status_smartstack_backends_different_nerve_ns():
    service = 'my_service'
    instance = 'my_instance'
    cluster = 'fake_cluster'
    different_ns = 'other_instance'
    normal_count = 10
    tasks = mock.Mock()
    with mock.patch('paasta_tools.marathon_tools.read_namespace_for_service_instance') as read_ns_mock:
        read_ns_mock.return_value = different_ns
        actual = marathon_serviceinit.status_smartstack_backends(
            service=service,
            instance=instance,
            cluster=cluster,
            job_config=fake_marathon_job_config,
            tasks=tasks,
            expected_count=normal_count,
            soa_dir=None,
            verbose=False,
        )
        assert "is announced in the" in actual
        assert different_ns in actual
def test_status_smartstack_backends_multiple_locations_expected_count():
    service = 'my_service'
    instance = 'my_instance'
    service_instance = compose_job_id(service, instance)
    cluster = 'fake_cluster'
    normal_count = 10

    good_task = mock.Mock()
    other_task = mock.Mock()
    fake_backend = {
        'status': 'UP',
        'lastchg': '1',
        'last_chk': 'OK',
        'check_code': '200',
        'svname': 'ipaddress1:1001_hostname1',
        'check_status': 'L7OK',
        'check_duration': 1
    }
    with contextlib.nested(
            mock.patch(
                'paasta_tools.marathon_tools.load_service_namespace_config',
                autospec=True),
            mock.patch(
                'paasta_tools.marathon_tools.read_registration_for_service_instance',
                autospec=True),
            mock.patch(
                'paasta_tools.marathon_serviceinit.get_all_slaves_for_blacklist_whitelist',
                autospec=True),
            mock.patch('paasta_tools.marathon_serviceinit.get_backends',
                       autospec=True),
            mock.patch(
                'paasta_tools.marathon_serviceinit.match_backends_and_tasks',
                autospec=True),
            mock.patch(
                'paasta_tools.marathon_serviceinit.haproxy_backend_report',
                autospec=True),
    ) as (
            mock_load_service_namespace_config,
            mock_read_reg,
            mock_get_all_slaves_for_blacklist_whitelist,
            mock_get_backends,
            mock_match_backends_and_tasks,
            mock_haproxy_backend_report,
    ):
        mock_load_service_namespace_config.return_value.get_discover.return_value = 'fake_discover'
        mock_read_reg.return_value = service_instance
        mock_get_backends.return_value = [fake_backend]
        mock_match_backends_and_tasks.return_value = [
            (fake_backend, good_task),
        ]
        tasks = [good_task, other_task]
        mock_get_all_slaves_for_blacklist_whitelist.return_value = [{
            'hostname':
            'hostname1',
            'attributes': {
                'fake_discover': 'fakelocation'
            }
        }, {
            'hostname':
            'hostname2',
            'attributes': {
                'fake_discover': 'fakelocation2'
            }
        }]
        marathon_serviceinit.status_smartstack_backends(
            service=service,
            instance=instance,
            cluster=cluster,
            job_config=fake_marathon_job_config,
            tasks=tasks,
            expected_count=normal_count,
            soa_dir=None,
            verbose=False,
            synapse_port=123456,
            synapse_haproxy_url_format=DEFAULT_SYNAPSE_HAPROXY_URL_FORMAT,
        )
        mock_get_backends.assert_any_call(
            service_instance,
            synapse_host='hostname1',
            synapse_port=123456,
            synapse_haproxy_url_format=DEFAULT_SYNAPSE_HAPROXY_URL_FORMAT,
        )
        mock_get_backends.assert_any_call(
            service_instance,
            synapse_host='hostname2',
            synapse_port=123456,
            synapse_haproxy_url_format=DEFAULT_SYNAPSE_HAPROXY_URL_FORMAT,
        )
        expected_count_per_location = int(normal_count / 2)
        mock_haproxy_backend_report.assert_any_call(
            expected_count_per_location, 1)
def test_status_smartstack_backends_different_nerve_ns():
    service = 'my_service'
    instance = 'my_instance'
    different_ns = 'different_ns'
    service_instance = compose_job_id(service, different_ns)

    cluster = 'fake_cluster'
    good_task = mock.Mock()
    bad_task = mock.Mock()
    other_task = mock.Mock()
    haproxy_backends_by_task = {
        good_task: {
            'status': 'UP',
            'lastchg': '1',
            'last_chk': 'OK',
            'check_code': '200',
            'svname': 'ipaddress1:1001_hostname1',
            'check_status': 'L7OK',
            'check_duration': 1
        },
        bad_task: {
            'status': 'UP',
            'lastchg': '1',
            'last_chk': 'OK',
            'check_code': '200',
            'svname': 'ipaddress2:1002_hostname2',
            'check_status': 'L7OK',
            'check_duration': 1
        },
    }

    with contextlib.nested(
            mock.patch(
                'paasta_tools.marathon_tools.load_service_namespace_config',
                autospec=True),
            mock.patch(
                'paasta_tools.marathon_tools.read_registration_for_service_instance',
                autospec=True),
            mock.patch(
                'paasta_tools.marathon_serviceinit.get_all_slaves_for_blacklist_whitelist',
                autospec=True),
            mock.patch('paasta_tools.marathon_serviceinit.get_backends',
                       autospec=True),
            mock.patch(
                'paasta_tools.marathon_serviceinit.match_backends_and_tasks',
                autospec=True),
    ) as (
            mock_load_service_namespace_config,
            mock_read_reg,
            mock_get_all_slaves_for_blacklist_whitelist,
            mock_get_backends,
            mock_match_backends_and_tasks,
    ):
        mock_load_service_namespace_config.return_value.get_discover.return_value = 'fake_discover'
        mock_get_all_slaves_for_blacklist_whitelist.return_value = [{
            'hostname':
            'fakehost',
            'attributes': {
                'fake_discover': 'fakelocation'
            }
        }]

        mock_read_reg.return_value = service_instance
        mock_get_backends.return_value = haproxy_backends_by_task.values()
        mock_match_backends_and_tasks.return_value = [
            (haproxy_backends_by_task[good_task], good_task),
            (haproxy_backends_by_task[bad_task], None),
            (None, other_task),
        ]
        mock_read_reg.return_value = compose_job_id(service, different_ns)
        tasks = [good_task, other_task]
        actual = marathon_serviceinit.status_smartstack_backends(
            service=service,
            instance=instance,
            cluster=cluster,
            job_config=fake_marathon_job_config,
            tasks=tasks,
            expected_count=len(haproxy_backends_by_task),
            soa_dir=None,
            verbose=False,
            synapse_port=123456,
            synapse_haproxy_url_format=DEFAULT_SYNAPSE_HAPROXY_URL_FORMAT,
        )
        mock_get_backends.assert_called_once_with(
            service_instance,
            synapse_host='fakehost',
            synapse_port=123456,
            synapse_haproxy_url_format=DEFAULT_SYNAPSE_HAPROXY_URL_FORMAT,
        )
        assert "fakelocation" in actual
        assert "Healthy" in actual
示例#20
0
def test_status_smartstack_backends_verbose_multiple_locations():
    service = "servicename"
    instance = "instancename"
    cluster = "fake_cluster"
    good_task = mock.Mock()
    other_task = mock.Mock()
    fake_backend = {
        "status": "UP",
        "lastchg": "1",
        "last_chk": "OK",
        "check_code": "200",
        "svname": "ipaddress1:1001_hostname1",
        "check_status": "L7OK",
        "check_duration": 1,
    }
    fake_other_backend = {
        "status": "UP",
        "lastchg": "1",
        "last_chk": "OK",
        "check_code": "200",
        "svname": "ipaddress1:1002_hostname2",
        "check_status": "L7OK",
        "check_duration": 1,
    }
    with mock.patch(
            "paasta_tools.marathon_serviceinit.get_slaves",
            autospec=True) as mock_get_slaves, mock.patch(
                "paasta_tools.marathon_serviceinit.get_backends",
                autospec=True,
                side_effect=[[fake_backend], [fake_other_backend]],
            ) as mock_get_backends, mock.patch(
                "paasta_tools.marathon_serviceinit.match_backends_and_tasks",
                autospec=True,
                side_effect=[[(fake_backend, good_task)],
                             [(fake_other_backend, good_task)]],
            ):
        fake_service_namespace_config = mock.Mock()
        fake_service_namespace_config.get_discover.return_value = "fake_discover"
        mock_get_slaves.return_value = [
            {
                "hostname": "hostname1",
                "attributes": {
                    "fake_discover": "fakelocation"
                }
            },
            {
                "hostname": "hostname2",
                "attributes": {
                    "fake_discover": "fakeotherlocation"
                },
            },
        ]
        tasks = [good_task, other_task]
        actual = marathon_serviceinit.status_smartstack_backends(
            service=service,
            instance=instance,
            cluster=cluster,
            job_config=fake_marathon_job_config,
            service_namespace_config=fake_service_namespace_config,
            tasks=tasks,
            expected_count=1,
            soa_dir=None,
            verbose=True,
            synapse_port=123456,
            synapse_haproxy_url_format=DEFAULT_SYNAPSE_HAPROXY_URL_FORMAT,
            system_deploy_blacklist=[],
            system_deploy_whitelist=[],
        )
        mock_get_backends.assert_any_call(
            "servicename.fake_nerve_ns",
            synapse_host="hostname1",
            synapse_port=123456,
            synapse_haproxy_url_format=DEFAULT_SYNAPSE_HAPROXY_URL_FORMAT,
        )
        mock_get_backends.assert_any_call(
            "servicename.fake_nerve_ns",
            synapse_host="hostname2",
            synapse_port=123456,
            synapse_haproxy_url_format=DEFAULT_SYNAPSE_HAPROXY_URL_FORMAT,
        )
        mock_get_slaves.assert_called_once_with()
        assert "fakelocation - %s" % PaastaColors.green("Healthy") in actual
        assert "hostname1:1001" in actual
        assert "fakeotherlocation - %s" % PaastaColors.green(
            "Healthy") in actual
        assert "hostname2:1002" in actual
示例#21
0
def test_status_smartstack_backends_verbose_multiple_apps():
    service = "servicename"
    instance = "instancename"
    cluster = "fake_cluster"

    good_task = mock.Mock()
    bad_task = mock.Mock()
    other_task = mock.Mock()
    haproxy_backends_by_task = {
        good_task: {
            "status": "UP",
            "lastchg": "1",
            "last_chk": "OK",
            "check_code": "200",
            "svname": "ipaddress1:1001_hostname1",
            "check_status": "L7OK",
            "check_duration": 1,
        },
        bad_task: {
            "status": "UP",
            "lastchg": "1",
            "last_chk": "OK",
            "check_code": "200",
            "svname": "ipaddress2:1002_hostname2",
            "check_status": "L7OK",
            "check_duration": 1,
        },
    }

    with mock.patch(
            "paasta_tools.marathon_serviceinit.get_slaves",
            autospec=True) as mock_get_slaves, mock.patch(
                "paasta_tools.marathon_serviceinit.get_backends",
                autospec=True
            ) as mock_get_backends, mock.patch(
                "paasta_tools.marathon_serviceinit.match_backends_and_tasks",
                autospec=True) as mock_match_backends_and_tasks:
        fake_service_namespace_config = mock.Mock()
        fake_service_namespace_config.get_discover.return_value = "fake_discover"
        mock_get_backends.return_value = haproxy_backends_by_task.values()
        mock_match_backends_and_tasks.return_value = [
            (haproxy_backends_by_task[good_task], good_task),
            (haproxy_backends_by_task[bad_task], None),
            (None, other_task),
        ]
        tasks = [good_task, other_task]
        mock_get_slaves.return_value = [{
            "hostname": "hostname1",
            "attributes": {
                "fake_discover": "fakelocation"
            }
        }]
        actual = marathon_serviceinit.status_smartstack_backends(
            service=service,
            instance=instance,
            cluster=cluster,
            job_config=fake_marathon_job_config,
            service_namespace_config=fake_service_namespace_config,
            tasks=tasks,
            expected_count=len(haproxy_backends_by_task),
            soa_dir=None,
            verbose=True,
            synapse_port=123456,
            synapse_haproxy_url_format=DEFAULT_SYNAPSE_HAPROXY_URL_FORMAT,
            system_deploy_blacklist=[],
            system_deploy_whitelist=[],
        )
        mock_get_backends.assert_called_once_with(
            "servicename.fake_nerve_ns",
            synapse_host="hostname1",
            synapse_port=123456,
            synapse_haproxy_url_format=DEFAULT_SYNAPSE_HAPROXY_URL_FORMAT,
        )
        assert "fakelocation" in actual
        assert "hostname1:1001" in actual
        assert re.search(
            r"%s[^\n]*hostname2:1002" % re.escape(PaastaColors.GREY), actual)
示例#22
0
def test_status_smartstack_backends_multiple_locations_expected_count():
    service = "servicename"
    instance = "instancename"
    cluster = "fake_cluster"
    normal_count = 10

    good_task = mock.Mock()
    other_task = mock.Mock()
    fake_backend = {
        "status": "UP",
        "lastchg": "1",
        "last_chk": "OK",
        "check_code": "200",
        "svname": "ipaddress1:1001_hostname1",
        "check_status": "L7OK",
        "check_duration": 1,
    }
    with mock.patch(
            "paasta_tools.marathon_serviceinit.get_slaves",
            autospec=True) as mock_get_slaves, mock.patch(
                "paasta_tools.marathon_serviceinit.get_backends",
                autospec=True
            ) as mock_get_backends, mock.patch(
                "paasta_tools.marathon_serviceinit.match_backends_and_tasks",
                autospec=True) as mock_match_backends_and_tasks, mock.patch(
                    "paasta_tools.marathon_serviceinit.haproxy_backend_report",
                    autospec=True) as mock_haproxy_backend_report:
        fake_service_namespace_config = mock.Mock()
        fake_service_namespace_config.get_discover.return_value = "fake_discover"
        mock_get_backends.return_value = [fake_backend]
        mock_match_backends_and_tasks.return_value = [(fake_backend, good_task)
                                                      ]
        tasks = [good_task, other_task]
        mock_get_slaves.return_value = [
            {
                "hostname": "hostname1",
                "attributes": {
                    "fake_discover": "fakelocation"
                }
            },
            {
                "hostname": "hostname2",
                "attributes": {
                    "fake_discover": "fakelocation2"
                }
            },
        ]
        marathon_serviceinit.status_smartstack_backends(
            service=service,
            instance=instance,
            cluster=cluster,
            job_config=fake_marathon_job_config,
            service_namespace_config=fake_service_namespace_config,
            tasks=tasks,
            expected_count=normal_count,
            soa_dir=None,
            verbose=False,
            synapse_port=123456,
            synapse_haproxy_url_format=DEFAULT_SYNAPSE_HAPROXY_URL_FORMAT,
            system_deploy_blacklist=[],
            system_deploy_whitelist=[],
        )
        mock_get_backends.assert_any_call(
            "servicename.fake_nerve_ns",
            synapse_host="hostname1",
            synapse_port=123456,
            synapse_haproxy_url_format=DEFAULT_SYNAPSE_HAPROXY_URL_FORMAT,
        )
        mock_get_backends.assert_any_call(
            "servicename.fake_nerve_ns",
            synapse_host="hostname2",
            synapse_port=123456,
            synapse_haproxy_url_format=DEFAULT_SYNAPSE_HAPROXY_URL_FORMAT,
        )
        expected_count_per_location = int(normal_count / 2)
        mock_haproxy_backend_report.assert_any_call(
            expected_count_per_location, 1)
示例#23
0
def test_status_smartstack_backends_verbose_emphasizes_maint_instances():
    service = 'my_service'
    instance = 'my_instance'
    cluster = 'fake_cluster'
    normal_count = 10
    good_task = mock.Mock()
    other_task = mock.Mock()
    fake_backend = {
        'status': 'MAINT',
        'lastchg': '1',
        'last_chk': 'OK',
        'check_code': '200',
        'svname': 'ipaddress1:1001_hostname1',
        'check_status': 'L7OK',
        'check_duration': 1
    }
    with contextlib.nested(
            mock.patch(
                'paasta_tools.marathon_tools.load_service_namespace_config',
                autospec=True),
            mock.patch(
                'paasta_tools.marathon_tools.read_namespace_for_service_instance',
                autospec=True),
            mock.patch(
                'paasta_tools.marathon_serviceinit.get_all_slaves_for_blacklist_whitelist',
                autospec=True),
            mock.patch('paasta_tools.marathon_serviceinit.get_backends',
                       autospec=True),
            mock.patch(
                'paasta_tools.marathon_serviceinit.match_backends_and_tasks',
                autospec=True),
    ) as (
            mock_load_service_namespace_config,
            mock_read_ns,
            mock_get_mesos_slaves_for_blacklist_whitelist,
            mock_get_backends,
            mock_match_backends_and_tasks,
    ):
        mock_get_mesos_slaves_for_blacklist_whitelist.return_value = [{
            'hostname':
            'fake',
            'attributes': {
                'fake_discover': 'fake_location_1'
            }
        }]
        mock_load_service_namespace_config.return_value.get_discover.return_value = 'fake_discover'
        mock_read_ns.return_value = instance
        mock_get_backends.return_value = [fake_backend]
        mock_match_backends_and_tasks.return_value = [
            (fake_backend, good_task),
        ]
        tasks = [good_task, other_task]
        actual = marathon_serviceinit.status_smartstack_backends(
            service=service,
            instance=instance,
            cluster=cluster,
            job_config=fake_marathon_job_config,
            tasks=tasks,
            expected_count=normal_count,
            soa_dir=None,
            verbose=True,
            synapse_port=123456,
            synapse_haproxy_url_format=DEFAULT_SYNAPSE_HAPROXY_URL_FORMAT,
        )
        assert PaastaColors.red('MAINT') in actual
示例#24
0
def test_status_smartstack_backends_verbose_multiple_locations():
    service = 'my_service'
    instance = 'my_instance'
    service_instance = compose_job_id(service, instance)
    cluster = 'fake_cluster'
    good_task = mock.Mock()
    other_task = mock.Mock()
    fake_backend = {
        'status': 'UP',
        'lastchg': '1',
        'last_chk': 'OK',
        'check_code': '200',
        'svname': 'ipaddress1:1001_hostname1',
        'check_status': 'L7OK',
        'check_duration': 1
    }
    fake_other_backend = {
        'status': 'UP',
        'lastchg': '1',
        'last_chk': 'OK',
        'check_code': '200',
        'svname': 'ipaddress1:1002_hostname2',
        'check_status': 'L7OK',
        'check_duration': 1
    }
    with contextlib.nested(
            mock.patch(
                'paasta_tools.marathon_tools.load_service_namespace_config',
                autospec=True),
            mock.patch(
                'paasta_tools.marathon_tools.read_namespace_for_service_instance'
            ),
            mock.patch(
                'paasta_tools.marathon_serviceinit.get_mesos_slaves_grouped_by_attribute'
            ),
            mock.patch('paasta_tools.marathon_serviceinit.get_backends',
                       autospec=True,
                       side_effect=[[fake_backend], [fake_other_backend]]),
            mock.patch(
                'paasta_tools.marathon_serviceinit.match_backends_and_tasks',
                autospec=True,
                side_effect=[[(fake_backend, good_task)],
                             [(fake_other_backend, good_task)]]),
    ) as (
            mock_load_service_namespace_config,
            mock_read_ns,
            mock_get_mesos_slaves_grouped_by_attribute,
            mock_get_backends,
            mock_match_backends_and_tasks,
    ):
        mock_load_service_namespace_config.return_value.get_discover.return_value = 'fake_discover'
        mock_read_ns.return_value = instance
        tasks = [good_task, other_task]
        mock_get_mesos_slaves_grouped_by_attribute.return_value = {
            'fake_location1': ['fakehost1'],
            'fake_location2': ['fakehost2'],
        }
        actual = marathon_serviceinit.status_smartstack_backends(
            service=service,
            instance=instance,
            cluster=cluster,
            job_config=fake_marathon_job_config,
            tasks=tasks,
            expected_count=1,
            soa_dir=None,
            verbose=True,
        )
        mock_get_backends.assert_any_call(
            service_instance,
            synapse_host='fakehost1',
            synapse_port=3212,
        )
        mock_get_backends.assert_any_call(
            service_instance,
            synapse_host='fakehost2',
            synapse_port=3212,
        )
        mock_get_mesos_slaves_grouped_by_attribute.assert_called_once_with(
            attribute='fake_discover',
            blacklist=[],
        )
        assert "fake_location1 - %s" % PaastaColors.green('Healthy') in actual
        assert "hostname1:1001" in actual
        assert "fake_location2 - %s" % PaastaColors.green('Healthy') in actual
        assert "hostname2:1002" in actual
def test_status_smartstack_backends_verbose_multiple_apps():
    service = 'my_service'
    instance = 'my_instance'
    service_instance = compose_job_id(service, instance)
    cluster = 'fake_cluster'

    good_task = mock.Mock()
    bad_task = mock.Mock()
    other_task = mock.Mock()
    haproxy_backends_by_task = {
        good_task: {
            'status': 'UP',
            'lastchg': '1',
            'last_chk': 'OK',
            'check_code': '200',
            'svname': 'ipaddress1:1001_hostname1',
            'check_status': 'L7OK',
            'check_duration': 1,
        },
        bad_task: {
            'status': 'UP',
            'lastchg': '1',
            'last_chk': 'OK',
            'check_code': '200',
            'svname': 'ipaddress2:1002_hostname2',
            'check_status': 'L7OK',
            'check_duration': 1,
        },
    }

    with mock.patch(
            'paasta_tools.marathon_tools.load_service_namespace_config',
            autospec=True,
    ) as mock_load_service_namespace_config, mock.patch(
            'paasta_tools.marathon_tools.read_registration_for_service_instance',
            autospec=True,
    ) as mock_read_reg, mock.patch(
            'paasta_tools.marathon_serviceinit.get_all_slaves_for_blacklist_whitelist',
            autospec=True,
    ) as mock_get_all_slaves_for_blacklist_whitelist, mock.patch(
            'paasta_tools.marathon_serviceinit.get_backends',
            autospec=True,
    ) as mock_get_backends, mock.patch(
            'paasta_tools.marathon_serviceinit.match_backends_and_tasks',
            autospec=True,
    ) as mock_match_backends_and_tasks:
        mock_load_service_namespace_config.return_value.get_discover.return_value = 'fake_discover'
        mock_read_reg.return_value = service_instance
        mock_get_backends.return_value = haproxy_backends_by_task.values()
        mock_match_backends_and_tasks.return_value = [
            (haproxy_backends_by_task[good_task], good_task),
            (haproxy_backends_by_task[bad_task], None),
            (None, other_task),
        ]
        tasks = [good_task, other_task]
        mock_get_all_slaves_for_blacklist_whitelist.return_value = [
            {
                'hostname': 'hostname1',
                'attributes': {
                    'fake_discover': 'fakelocation',
                },
            },
        ]
        actual = marathon_serviceinit.status_smartstack_backends(
            service=service,
            instance=instance,
            cluster=cluster,
            job_config=fake_marathon_job_config,
            tasks=tasks,
            expected_count=len(haproxy_backends_by_task),
            soa_dir=None,
            verbose=True,
            synapse_port=123456,
            synapse_haproxy_url_format=DEFAULT_SYNAPSE_HAPROXY_URL_FORMAT,
            system_deploy_blacklist=[],
            system_deploy_whitelist=[],
        )
        mock_get_backends.assert_called_once_with(
            service_instance,
            synapse_host='hostname1',
            synapse_port=123456,
            synapse_haproxy_url_format=DEFAULT_SYNAPSE_HAPROXY_URL_FORMAT,
        )
        assert "fakelocation" in actual
        assert "hostname1:1001" in actual
        assert re.search(
            r"%s[^\n]*hostname2:1002" % re.escape(PaastaColors.GREY), actual)
def test_status_smartstack_backends_verbose_multiple_apps():
    service = "my_service"
    instance = "my_instance"
    service_instance = compose_job_id(service, instance)
    cluster = "fake_cluster"

    good_task = mock.Mock()
    bad_task = mock.Mock()
    other_task = mock.Mock()
    haproxy_backends_by_task = {
        good_task: {
            "status": "UP",
            "lastchg": "1",
            "last_chk": "OK",
            "check_code": "200",
            "svname": "ipaddress1:1001_hostname1",
            "check_status": "L7OK",
            "check_duration": 1,
        },
        bad_task: {
            "status": "UP",
            "lastchg": "1",
            "last_chk": "OK",
            "check_code": "200",
            "svname": "ipaddress2:1002_hostname2",
            "check_status": "L7OK",
            "check_duration": 1,
        },
    }

    with contextlib.nested(
        mock.patch("paasta_tools.marathon_tools.load_service_namespace_config", autospec=True),
        mock.patch("paasta_tools.marathon_tools.read_namespace_for_service_instance"),
        mock.patch("paasta_tools.marathon_serviceinit.get_mesos_slaves_grouped_by_attribute"),
        mock.patch("paasta_tools.marathon_serviceinit.get_backends", autospec=True),
        mock.patch("paasta_tools.marathon_serviceinit.match_backends_and_tasks", autospec=True),
    ) as (
        mock_load_service_namespace_config,
        mock_read_ns,
        mock_get_mesos_slaves_grouped_by_attribute,
        mock_get_backends,
        mock_match_backends_and_tasks,
    ):
        mock_load_service_namespace_config.return_value.get_discover.return_value = "fake_discover"
        mock_read_ns.return_value = instance
        mock_get_backends.return_value = haproxy_backends_by_task.values()
        mock_match_backends_and_tasks.return_value = [
            (haproxy_backends_by_task[good_task], good_task),
            (haproxy_backends_by_task[bad_task], None),
            (None, other_task),
        ]
        tasks = [good_task, other_task]
        mock_get_mesos_slaves_grouped_by_attribute.return_value = {"fake_location1": ["fakehost1"]}
        actual = marathon_serviceinit.status_smartstack_backends(
            service=service,
            instance=instance,
            cluster=cluster,
            job_config=fake_marathon_job_config,
            tasks=tasks,
            expected_count=len(haproxy_backends_by_task),
            soa_dir=None,
            verbose=True,
        )
        mock_get_backends.assert_called_once_with(service_instance, synapse_host="fakehost1", synapse_port=3212)
        assert "fake_location1" in actual
        assert "hostname1:1001" in actual
        assert re.search(r"%s[^\n]*hostname2:1002" % re.escape(PaastaColors.GREY), actual)
def test_status_smartstack_backends_verbose_multiple_locations():
    service = "my_service"
    instance = "my_instance"
    service_instance = compose_job_id(service, instance)
    cluster = "fake_cluster"
    good_task = mock.Mock()
    other_task = mock.Mock()
    fake_backend = {
        "status": "UP",
        "lastchg": "1",
        "last_chk": "OK",
        "check_code": "200",
        "svname": "ipaddress1:1001_hostname1",
        "check_status": "L7OK",
        "check_duration": 1,
    }
    fake_other_backend = {
        "status": "UP",
        "lastchg": "1",
        "last_chk": "OK",
        "check_code": "200",
        "svname": "ipaddress1:1002_hostname2",
        "check_status": "L7OK",
        "check_duration": 1,
    }
    with contextlib.nested(
        mock.patch("paasta_tools.marathon_tools.load_service_namespace_config", autospec=True),
        mock.patch("paasta_tools.marathon_tools.read_namespace_for_service_instance"),
        mock.patch("paasta_tools.marathon_serviceinit.get_mesos_slaves_grouped_by_attribute"),
        mock.patch(
            "paasta_tools.marathon_serviceinit.get_backends",
            autospec=True,
            side_effect=[[fake_backend], [fake_other_backend]],
        ),
        mock.patch(
            "paasta_tools.marathon_serviceinit.match_backends_and_tasks",
            autospec=True,
            side_effect=[[(fake_backend, good_task)], [(fake_other_backend, good_task)]],
        ),
    ) as (
        mock_load_service_namespace_config,
        mock_read_ns,
        mock_get_mesos_slaves_grouped_by_attribute,
        mock_get_backends,
        mock_match_backends_and_tasks,
    ):
        mock_load_service_namespace_config.return_value.get_discover.return_value = "fake_discover"
        mock_read_ns.return_value = instance
        tasks = [good_task, other_task]
        mock_get_mesos_slaves_grouped_by_attribute.return_value = {
            "fake_location1": ["fakehost1"],
            "fake_location2": ["fakehost2"],
        }
        actual = marathon_serviceinit.status_smartstack_backends(
            service=service,
            instance=instance,
            cluster=cluster,
            job_config=fake_marathon_job_config,
            tasks=tasks,
            expected_count=1,
            soa_dir=None,
            verbose=True,
        )
        mock_get_backends.assert_any_call(service_instance, synapse_host="fakehost1", synapse_port=3212)
        mock_get_backends.assert_any_call(service_instance, synapse_host="fakehost2", synapse_port=3212)
        mock_get_mesos_slaves_grouped_by_attribute.assert_called_once_with(attribute="fake_discover", blacklist=[])
        assert "fake_location1 - %s" % PaastaColors.green("Healthy") in actual
        assert "hostname1:1001" in actual
        assert "fake_location2 - %s" % PaastaColors.green("Healthy") in actual
        assert "hostname2:1002" in actual
示例#28
0
def test_status_smartstack_backends_verbose_multiple_locations():
    service = 'my_service'
    instance = 'my_instance'
    service_instance = compose_job_id(service, instance)
    cluster = 'fake_cluster'
    good_task = mock.Mock()
    other_task = mock.Mock()
    fake_backend = {'status': 'UP', 'lastchg': '1', 'last_chk': 'OK',
                    'check_code': '200', 'svname': 'ipaddress1:1001_hostname1',
                    'check_status': 'L7OK', 'check_duration': 1}
    fake_other_backend = {'status': 'UP', 'lastchg': '1', 'last_chk': 'OK',
                          'check_code': '200', 'svname': 'ipaddress1:1002_hostname2',
                          'check_status': 'L7OK', 'check_duration': 1}
    with contextlib.nested(
        mock.patch('paasta_tools.marathon_tools.load_service_namespace_config', autospec=True),
        mock.patch('paasta_tools.marathon_tools.read_registration_for_service_instance', autospec=True),
        mock.patch('paasta_tools.marathon_serviceinit.get_all_slaves_for_blacklist_whitelist', autospec=True),
        mock.patch('paasta_tools.marathon_serviceinit.get_backends', autospec=True,
                   side_effect=[[fake_backend], [fake_other_backend]]),
        mock.patch('paasta_tools.marathon_serviceinit.match_backends_and_tasks',
                   autospec=True, side_effect=[[(fake_backend, good_task)], [(fake_other_backend, good_task)]]),
    ) as (
        mock_load_service_namespace_config,
        mock_read_reg,
        mock_get_all_slaves_for_blacklist_whitelist,
        mock_get_backends,
        mock_match_backends_and_tasks,
    ):
        mock_load_service_namespace_config.return_value.get_discover.return_value = 'fake_discover'
        mock_read_reg.return_value = service_instance
        mock_get_all_slaves_for_blacklist_whitelist.return_value = [
            {
                'hostname': 'hostname1',
                'attributes': {
                    'fake_discover': 'fakelocation'
                }
            },
            {
                'hostname': 'hostname2',
                'attributes': {
                    'fake_discover': 'fakeotherlocation'
                }
            }
        ]
        tasks = [good_task, other_task]
        actual = marathon_serviceinit.status_smartstack_backends(
            service=service,
            instance=instance,
            cluster=cluster,
            job_config=fake_marathon_job_config,
            tasks=tasks,
            expected_count=1,
            soa_dir=None,
            verbose=True,
            synapse_port=123456,
            synapse_haproxy_url_format=DEFAULT_SYNAPSE_HAPROXY_URL_FORMAT,
        )
        mock_get_backends.assert_any_call(
            service_instance,
            synapse_host='hostname1',
            synapse_port=123456,
            synapse_haproxy_url_format=DEFAULT_SYNAPSE_HAPROXY_URL_FORMAT,
        )
        mock_get_backends.assert_any_call(
            service_instance,
            synapse_host='hostname2',
            synapse_port=123456,
            synapse_haproxy_url_format=DEFAULT_SYNAPSE_HAPROXY_URL_FORMAT,
        )
        mock_get_all_slaves_for_blacklist_whitelist.assert_called_once_with(
            blacklist=[],
            whitelist=[],
        )
        assert "fakelocation - %s" % PaastaColors.green('Healthy') in actual
        assert "hostname1:1001" in actual
        assert "fakeotherlocation - %s" % PaastaColors.green('Healthy') in actual
        assert "hostname2:1002" in actual
def test_status_smartstack_backends_multiple_locations_expected_count():
    service = 'my_service'
    instance = 'my_instance'
    service_instance = compose_job_id(service, instance)
    cluster = 'fake_cluster'
    normal_count = 10

    good_task = mock.Mock()
    other_task = mock.Mock()
    fake_backend = {'status': 'UP', 'lastchg': '1', 'last_chk': 'OK',
                    'check_code': '200', 'svname': 'ipaddress1:1001_hostname1',
                    'check_status': 'L7OK', 'check_duration': 1}
    with contextlib.nested(
        mock.patch('paasta_tools.marathon_tools.load_service_namespace_config', autospec=True),
        mock.patch('paasta_tools.marathon_tools.read_namespace_for_service_instance'),
        mock.patch('paasta_tools.marathon_serviceinit.get_mesos_slaves_grouped_by_attribute'),
        mock.patch('paasta_tools.marathon_serviceinit.get_backends', autospec=True),
        mock.patch('paasta_tools.marathon_serviceinit.match_backends_and_tasks', autospec=True),
        mock.patch('paasta_tools.marathon_serviceinit.haproxy_backend_report', autospec=True),
    ) as (
        mock_load_service_namespace_config,
        mock_read_ns,
        mock_get_mesos_slaves_grouped_by_attribute,
        mock_get_backends,
        mock_match_backends_and_tasks,
        mock_haproxy_backend_report,
    ):
        mock_load_service_namespace_config.return_value.get_discover.return_value = 'fake_discover'
        mock_read_ns.return_value = instance
        mock_get_backends.return_value = [fake_backend]
        mock_match_backends_and_tasks.return_value = [
            (fake_backend, good_task),
        ]
        tasks = [good_task, other_task]
        mock_get_mesos_slaves_grouped_by_attribute.return_value = {
            'fake_location1': ['fakehost1'],
            'fake_location2': ['fakehost2'],
        }
        marathon_serviceinit.status_smartstack_backends(
            service=service,
            instance=instance,
            cluster=cluster,
            job_config=fake_marathon_job_config,
            tasks=tasks,
            expected_count=normal_count,
            soa_dir=None,
            verbose=False,
        )
        mock_get_backends.assert_any_call(
            service_instance,
            synapse_host='fakehost1',
            synapse_port=3212,
        )
        mock_get_backends.assert_any_call(
            service_instance,
            synapse_host='fakehost2',
            synapse_port=3212,
        )
        expected_count_per_location = int(
            normal_count / len(mock_get_mesos_slaves_grouped_by_attribute.return_value))
        mock_haproxy_backend_report.assert_any_call(expected_count_per_location, 1)
def test_status_smartstack_backends_verbose_multiple_locations():
    service = 'my_service'
    instance = 'my_instance'
    service_instance = compose_job_id(service, instance)
    cluster = 'fake_cluster'
    good_task = mock.Mock()
    other_task = mock.Mock()
    fake_backend = {
        'status': 'UP',
        'lastchg': '1',
        'last_chk': 'OK',
        'check_code': '200',
        'svname': 'ipaddress1:1001_hostname1',
        'check_status': 'L7OK',
        'check_duration': 1
    }
    fake_other_backend = {
        'status': 'UP',
        'lastchg': '1',
        'last_chk': 'OK',
        'check_code': '200',
        'svname': 'ipaddress1:1002_hostname2',
        'check_status': 'L7OK',
        'check_duration': 1
    }
    with contextlib.nested(
            mock.patch(
                'paasta_tools.marathon_tools.load_service_namespace_config',
                autospec=True),
            mock.patch(
                'paasta_tools.marathon_tools.read_registration_for_service_instance',
                autospec=True),
            mock.patch(
                'paasta_tools.marathon_serviceinit.get_all_slaves_for_blacklist_whitelist',
                autospec=True),
            mock.patch('paasta_tools.marathon_serviceinit.get_backends',
                       autospec=True,
                       side_effect=[[fake_backend], [fake_other_backend]]),
            mock.patch(
                'paasta_tools.marathon_serviceinit.match_backends_and_tasks',
                autospec=True,
                side_effect=[[(fake_backend, good_task)],
                             [(fake_other_backend, good_task)]]),
    ) as (
            mock_load_service_namespace_config,
            mock_read_reg,
            mock_get_all_slaves_for_blacklist_whitelist,
            mock_get_backends,
            mock_match_backends_and_tasks,
    ):
        mock_load_service_namespace_config.return_value.get_discover.return_value = 'fake_discover'
        mock_read_reg.return_value = service_instance
        mock_get_all_slaves_for_blacklist_whitelist.return_value = [{
            'hostname':
            'hostname1',
            'attributes': {
                'fake_discover': 'fakelocation'
            }
        }, {
            'hostname':
            'hostname2',
            'attributes': {
                'fake_discover': 'fakeotherlocation'
            }
        }]
        tasks = [good_task, other_task]
        actual = marathon_serviceinit.status_smartstack_backends(
            service=service,
            instance=instance,
            cluster=cluster,
            job_config=fake_marathon_job_config,
            tasks=tasks,
            expected_count=1,
            soa_dir=None,
            verbose=True,
            synapse_port=123456,
            synapse_haproxy_url_format=DEFAULT_SYNAPSE_HAPROXY_URL_FORMAT,
        )
        mock_get_backends.assert_any_call(
            service_instance,
            synapse_host='hostname1',
            synapse_port=123456,
            synapse_haproxy_url_format=DEFAULT_SYNAPSE_HAPROXY_URL_FORMAT,
        )
        mock_get_backends.assert_any_call(
            service_instance,
            synapse_host='hostname2',
            synapse_port=123456,
            synapse_haproxy_url_format=DEFAULT_SYNAPSE_HAPROXY_URL_FORMAT,
        )
        mock_get_all_slaves_for_blacklist_whitelist.assert_called_once_with(
            blacklist=[],
            whitelist=[],
        )
        assert "fakelocation - %s" % PaastaColors.green('Healthy') in actual
        assert "hostname1:1001" in actual
        assert "fakeotherlocation - %s" % PaastaColors.green(
            'Healthy') in actual
        assert "hostname2:1002" in actual
示例#31
0
def test_status_smartstack_backends_different_nerve_ns():
    service = 'my_service'
    instance = 'my_instance'
    different_ns = 'different_ns'
    service_instance = compose_job_id(service, different_ns)

    cluster = 'fake_cluster'
    good_task = mock.Mock()
    bad_task = mock.Mock()
    other_task = mock.Mock()
    haproxy_backends_by_task = {
        good_task: {'status': 'UP', 'lastchg': '1', 'last_chk': 'OK',
                    'check_code': '200', 'svname': 'ipaddress1:1001_hostname1',
                    'check_status': 'L7OK', 'check_duration': 1},
        bad_task: {'status': 'UP', 'lastchg': '1', 'last_chk': 'OK',
                   'check_code': '200', 'svname': 'ipaddress2:1002_hostname2',
                   'check_status': 'L7OK', 'check_duration': 1},
    }

    with contextlib.nested(
        mock.patch('paasta_tools.marathon_tools.load_service_namespace_config', autospec=True),
        mock.patch('paasta_tools.marathon_tools.read_registration_for_service_instance', autospec=True),
        mock.patch('paasta_tools.marathon_serviceinit.get_all_slaves_for_blacklist_whitelist', autospec=True),
        mock.patch('paasta_tools.marathon_serviceinit.get_backends', autospec=True),
        mock.patch('paasta_tools.marathon_serviceinit.match_backends_and_tasks', autospec=True),
    ) as (
        mock_load_service_namespace_config,
        mock_read_reg,
        mock_get_all_slaves_for_blacklist_whitelist,
        mock_get_backends,
        mock_match_backends_and_tasks,
    ):
        mock_load_service_namespace_config.return_value.get_discover.return_value = 'fake_discover'
        mock_get_all_slaves_for_blacklist_whitelist.return_value = [{
            'hostname': 'fakehost',
            'attributes': {
                'fake_discover': 'fakelocation'
            }
        }]

        mock_read_reg.return_value = service_instance
        mock_get_backends.return_value = haproxy_backends_by_task.values()
        mock_match_backends_and_tasks.return_value = [
            (haproxy_backends_by_task[good_task], good_task),
            (haproxy_backends_by_task[bad_task], None),
            (None, other_task),
        ]
        mock_read_reg.return_value = compose_job_id(service, different_ns)
        tasks = [good_task, other_task]
        actual = marathon_serviceinit.status_smartstack_backends(
            service=service,
            instance=instance,
            cluster=cluster,
            job_config=fake_marathon_job_config,
            tasks=tasks,
            expected_count=len(haproxy_backends_by_task),
            soa_dir=None,
            verbose=False,
            synapse_port=123456,
            synapse_haproxy_url_format=DEFAULT_SYNAPSE_HAPROXY_URL_FORMAT,
        )
        mock_get_backends.assert_called_once_with(
            service_instance,
            synapse_host='fakehost',
            synapse_port=123456,
            synapse_haproxy_url_format=DEFAULT_SYNAPSE_HAPROXY_URL_FORMAT,
        )
        assert "fakelocation" in actual
        assert "Healthy" in actual
示例#32
0
def test_status_smartstack_backends_different_nerve_ns():
    service = "servicename"
    instance = "instancename"
    different_ns = "different_ns"
    service_instance = compose_job_id(service, different_ns)

    cluster = "fake_cluster"
    good_task = mock.Mock()
    bad_task = mock.Mock()
    other_task = mock.Mock()
    haproxy_backends_by_task = {
        good_task: {
            "status": "UP",
            "lastchg": "1",
            "last_chk": "OK",
            "check_code": "200",
            "svname": "ipaddress1:1001_hostname1",
            "check_status": "L7OK",
            "check_duration": 1,
        },
        bad_task: {
            "status": "UP",
            "lastchg": "1",
            "last_chk": "OK",
            "check_code": "200",
            "svname": "ipaddress2:1002_hostname2",
            "check_status": "L7OK",
            "check_duration": 1,
        },
    }

    with mock.patch(
            "paasta_tools.marathon_serviceinit.get_slaves",
            autospec=True) as mock_get_slaves, mock.patch(
                "paasta_tools.marathon_serviceinit.get_backends",
                autospec=True
            ) as mock_get_backends, mock.patch(
                "paasta_tools.marathon_serviceinit.match_backends_and_tasks",
                autospec=True) as mock_match_backends_and_tasks:
        fake_service_namespace_config = mock.Mock()
        fake_service_namespace_config.get_discover.return_value = "fake_discover"
        mock_get_slaves.return_value = [{
            "hostname": "fakehost",
            "attributes": {
                "fake_discover": "fakelocation"
            }
        }]

        mock_get_backends.return_value = haproxy_backends_by_task.values()
        mock_match_backends_and_tasks.return_value = [
            (haproxy_backends_by_task[good_task], good_task),
            (haproxy_backends_by_task[bad_task], None),
            (None, other_task),
        ]

        tasks = [good_task, other_task]
        with mock.patch.object(
                fake_marathon_job_config,
                "get_registrations",
                return_value=[compose_job_id(service, different_ns)],
        ):
            actual = marathon_serviceinit.status_smartstack_backends(
                service=service,
                instance=instance,
                cluster=cluster,
                job_config=fake_marathon_job_config,
                service_namespace_config=fake_service_namespace_config,
                tasks=tasks,
                expected_count=len(haproxy_backends_by_task),
                soa_dir=None,
                verbose=False,
                synapse_port=123456,
                synapse_haproxy_url_format=DEFAULT_SYNAPSE_HAPROXY_URL_FORMAT,
                system_deploy_blacklist=[],
                system_deploy_whitelist=[],
            )
        mock_get_backends.assert_called_once_with(
            service_instance,
            synapse_host="fakehost",
            synapse_port=123456,
            synapse_haproxy_url_format=DEFAULT_SYNAPSE_HAPROXY_URL_FORMAT,
        )
        assert "fakelocation" in actual
        assert "Healthy" in actual
示例#33
0
def test_status_smartstack_backends_multiple_locations_expected_count():
    service = 'my_service'
    instance = 'my_instance'
    service_instance = compose_job_id(service, instance)
    cluster = 'fake_cluster'
    normal_count = 10

    good_task = mock.Mock()
    other_task = mock.Mock()
    fake_backend = {'status': 'UP', 'lastchg': '1', 'last_chk': 'OK',
                    'check_code': '200', 'svname': 'ipaddress1:1001_hostname1',
                    'check_status': 'L7OK', 'check_duration': 1}
    with contextlib.nested(
        mock.patch('paasta_tools.marathon_tools.load_service_namespace_config', autospec=True),
        mock.patch('paasta_tools.marathon_tools.read_registration_for_service_instance', autospec=True),
        mock.patch('paasta_tools.marathon_serviceinit.get_all_slaves_for_blacklist_whitelist', autospec=True),
        mock.patch('paasta_tools.marathon_serviceinit.get_backends', autospec=True),
        mock.patch('paasta_tools.marathon_serviceinit.match_backends_and_tasks', autospec=True),
        mock.patch('paasta_tools.marathon_serviceinit.haproxy_backend_report', autospec=True),
    ) as (
        mock_load_service_namespace_config,
        mock_read_reg,
        mock_get_all_slaves_for_blacklist_whitelist,
        mock_get_backends,
        mock_match_backends_and_tasks,
        mock_haproxy_backend_report,
    ):
        mock_load_service_namespace_config.return_value.get_discover.return_value = 'fake_discover'
        mock_read_reg.return_value = service_instance
        mock_get_backends.return_value = [fake_backend]
        mock_match_backends_and_tasks.return_value = [
            (fake_backend, good_task),
        ]
        tasks = [good_task, other_task]
        mock_get_all_slaves_for_blacklist_whitelist.return_value = [
            {
                'hostname': 'hostname1',
                'attributes': {
                    'fake_discover': 'fakelocation'
                }
            },
            {
                'hostname': 'hostname2',
                'attributes': {
                    'fake_discover': 'fakelocation2'
                }
            }
        ]
        marathon_serviceinit.status_smartstack_backends(
            service=service,
            instance=instance,
            cluster=cluster,
            job_config=fake_marathon_job_config,
            tasks=tasks,
            expected_count=normal_count,
            soa_dir=None,
            verbose=False,
            synapse_port=123456,
            synapse_haproxy_url_format=DEFAULT_SYNAPSE_HAPROXY_URL_FORMAT,
        )
        mock_get_backends.assert_any_call(
            service_instance,
            synapse_host='hostname1',
            synapse_port=123456,
            synapse_haproxy_url_format=DEFAULT_SYNAPSE_HAPROXY_URL_FORMAT,
        )
        mock_get_backends.assert_any_call(
            service_instance,
            synapse_host='hostname2',
            synapse_port=123456,
            synapse_haproxy_url_format=DEFAULT_SYNAPSE_HAPROXY_URL_FORMAT,
        )
        expected_count_per_location = int(normal_count / 2)
        mock_haproxy_backend_report.assert_any_call(expected_count_per_location, 1)
示例#34
0
def test_status_smartstack_backends_multiple_locations_expected_count():
    service = 'my_service'
    instance = 'my_instance'
    service_instance = compose_job_id(service, instance)
    cluster = 'fake_cluster'
    normal_count = 10

    good_task = mock.Mock()
    other_task = mock.Mock()
    fake_backend = {
        'status': 'UP',
        'lastchg': '1',
        'last_chk': 'OK',
        'check_code': '200',
        'svname': 'ipaddress1:1001_hostname1',
        'check_status': 'L7OK',
        'check_duration': 1
    }
    with contextlib.nested(
            mock.patch(
                'paasta_tools.marathon_tools.load_service_namespace_config',
                autospec=True),
            mock.patch(
                'paasta_tools.marathon_tools.read_namespace_for_service_instance'
            ),
            mock.patch(
                'paasta_tools.marathon_serviceinit.get_mesos_slaves_grouped_by_attribute'
            ),
            mock.patch('paasta_tools.marathon_serviceinit.get_backends',
                       autospec=True),
            mock.patch(
                'paasta_tools.marathon_serviceinit.match_backends_and_tasks',
                autospec=True),
            mock.patch(
                'paasta_tools.marathon_serviceinit.haproxy_backend_report',
                autospec=True),
    ) as (
            mock_load_service_namespace_config,
            mock_read_ns,
            mock_get_mesos_slaves_grouped_by_attribute,
            mock_get_backends,
            mock_match_backends_and_tasks,
            mock_haproxy_backend_report,
    ):
        mock_load_service_namespace_config.return_value.get_discover.return_value = 'fake_discover'
        mock_read_ns.return_value = instance
        mock_get_backends.return_value = [fake_backend]
        mock_match_backends_and_tasks.return_value = [
            (fake_backend, good_task),
        ]
        tasks = [good_task, other_task]
        mock_get_mesos_slaves_grouped_by_attribute.return_value = {
            'fake_location1': ['fakehost1'],
            'fake_location2': ['fakehost2'],
        }
        marathon_serviceinit.status_smartstack_backends(
            service=service,
            instance=instance,
            cluster=cluster,
            job_config=fake_marathon_job_config,
            tasks=tasks,
            expected_count=normal_count,
            soa_dir=None,
            verbose=False,
        )
        mock_get_backends.assert_any_call(
            service_instance,
            synapse_host='fakehost1',
            synapse_port=3212,
        )
        mock_get_backends.assert_any_call(
            service_instance,
            synapse_host='fakehost2',
            synapse_port=3212,
        )
        expected_count_per_location = int(
            normal_count /
            len(mock_get_mesos_slaves_grouped_by_attribute.return_value))
        mock_haproxy_backend_report.assert_any_call(
            expected_count_per_location, 1)
def test_status_smartstack_backends_verbose_multiple_locations():
    service = 'my_service'
    instance = 'my_instance'
    service_instance = compose_job_id(service, instance)
    cluster = 'fake_cluster'
    good_task = mock.Mock()
    other_task = mock.Mock()
    fake_backend = {'status': 'UP', 'lastchg': '1', 'last_chk': 'OK',
                    'check_code': '200', 'svname': 'ipaddress1:1001_hostname1',
                    'check_status': 'L7OK', 'check_duration': 1}
    fake_other_backend = {'status': 'UP', 'lastchg': '1', 'last_chk': 'OK',
                          'check_code': '200', 'svname': 'ipaddress1:1002_hostname2',
                          'check_status': 'L7OK', 'check_duration': 1}
    with contextlib.nested(
        mock.patch('paasta_tools.marathon_tools.load_service_namespace_config', autospec=True),
        mock.patch('paasta_tools.marathon_tools.read_namespace_for_service_instance'),
        mock.patch('paasta_tools.marathon_serviceinit.get_mesos_slaves_grouped_by_attribute'),
        mock.patch('paasta_tools.marathon_serviceinit.get_backends', autospec=True,
                   side_effect=[[fake_backend], [fake_other_backend]]),
        mock.patch('paasta_tools.marathon_serviceinit.match_backends_and_tasks',
                   autospec=True, side_effect=[[(fake_backend, good_task)], [(fake_other_backend, good_task)]]),
    ) as (
        mock_load_service_namespace_config,
        mock_read_ns,
        mock_get_mesos_slaves_grouped_by_attribute,
        mock_get_backends,
        mock_match_backends_and_tasks,
    ):
        mock_load_service_namespace_config.return_value.get_discover.return_value = 'fake_discover'
        mock_read_ns.return_value = instance
        tasks = [good_task, other_task]
        mock_get_mesos_slaves_grouped_by_attribute.return_value = {
            'fake_location1': ['fakehost1'],
            'fake_location2': ['fakehost2'],
        }
        actual = marathon_serviceinit.status_smartstack_backends(
            service=service,
            instance=instance,
            cluster=cluster,
            job_config=fake_marathon_job_config,
            tasks=tasks,
            expected_count=1,
            soa_dir=None,
            verbose=True,
        )
        mock_get_backends.assert_any_call(
            service_instance,
            synapse_host='fakehost1',
            synapse_port=3212,
        )
        mock_get_backends.assert_any_call(
            service_instance,
            synapse_host='fakehost2',
            synapse_port=3212,
        )
        mock_get_mesos_slaves_grouped_by_attribute.assert_called_once_with(
            attribute='fake_discover',
            blacklist=[],
        )
        assert "fake_location1 - %s" % PaastaColors.green('Healthy') in actual
        assert "hostname1:1001" in actual
        assert "fake_location2 - %s" % PaastaColors.green('Healthy') in actual
        assert "hostname2:1002" in actual
示例#36
0
def test_status_smartstack_backends_multiple_locations():
    service = 'servicename'
    instance = 'instancename'
    cluster = 'fake_cluster'
    good_task = mock.Mock()
    other_task = mock.Mock()
    fake_backend = {
        'status': 'UP', 'lastchg': '1', 'last_chk': 'OK',
        'check_code': '200', 'svname': 'ipaddress1:1001_hostname1',
        'check_status': 'L7OK', 'check_duration': 1,
    }
    with mock.patch(
        'paasta_tools.marathon_serviceinit.get_all_slaves_for_blacklist_whitelist', autospec=True,
    ) as mock_get_all_slaves_for_blacklist_whitelist, mock.patch(
        'paasta_tools.marathon_serviceinit.get_backends', autospec=True,
    ) as mock_get_backends, mock.patch(
        'paasta_tools.marathon_serviceinit.match_backends_and_tasks', autospec=True,
    ) as mock_match_backends_and_tasks:
        fake_service_namespace_config = mock.Mock()
        fake_service_namespace_config.get_discover.return_value = 'fake_discover'
        mock_get_backends.return_value = [fake_backend]
        mock_match_backends_and_tasks.return_value = [
            (fake_backend, good_task),
        ]
        tasks = [good_task, other_task]
        mock_get_all_slaves_for_blacklist_whitelist.return_value = [
            {
                'hostname': 'fakehost',
                'attributes': {
                    'fake_discover': 'fakelocation',
                },
            },
            {
                'hostname': 'fakeotherhost',
                'attributes': {
                    'fake_discover': 'fakeotherlocation',
                },
            },
        ]
        actual = marathon_serviceinit.status_smartstack_backends(
            service=service,
            instance=instance,
            cluster=cluster,
            job_config=fake_marathon_job_config,
            service_namespace_config=fake_service_namespace_config,
            tasks=tasks,
            expected_count=len(mock_get_backends.return_value),
            soa_dir=None,
            verbose=False,
            synapse_port=123456,
            synapse_haproxy_url_format=DEFAULT_SYNAPSE_HAPROXY_URL_FORMAT,
            system_deploy_blacklist=[],
            system_deploy_whitelist=[],
        )
        mock_get_backends.assert_any_call(
            'servicename.fake_nerve_ns',
            synapse_host='fakehost',
            synapse_port=123456,
            synapse_haproxy_url_format=DEFAULT_SYNAPSE_HAPROXY_URL_FORMAT,
        )
        mock_get_backends.assert_any_call(
            'servicename.fake_nerve_ns',
            synapse_host='fakeotherhost',
            synapse_port=123456,
            synapse_haproxy_url_format=DEFAULT_SYNAPSE_HAPROXY_URL_FORMAT,
        )
        assert "fakelocation - %s" % PaastaColors.green('Healthy') in actual
        assert "fakeotherlocation - %s" % PaastaColors.green('Healthy') in actual