Exemple #1
0
def test_get_paasta_native_services_running_here_for_nerve():
    cluster = 'edelweiss'
    soa_dir = 'the_sound_of_music'
    fake_marathon_services = [
        ('no_test', 'left_behind', 1111),
        ('no_docstrings', 'forever_abandoned', 2222),
    ]
    registrations = [
        ['no_docstrings.dos'],
        ['no_test.uno'],
    ]
    nerve_dicts = [
        long_running_service_tools.ServiceNamespaceConfig({
            'binary': 1,
            'proxy_port': 6666
        }),
        long_running_service_tools.ServiceNamespaceConfig({
            'clock': 0,
            'proxy_port': 6666
        }),
    ]
    expected = [
        ('no_test.uno', {
            'clock': 0,
            'port': 1111,
            'proxy_port': 6666
        }),
        ('no_docstrings.dos', {
            'binary': 1,
            'port': 2222,
            'proxy_port': 6666
        }),
    ]
    with mock.patch(
            'paasta_tools.native_mesos_scheduler.paasta_native_services_running_here',
            autospec=True,
            return_value=fake_marathon_services,
    ) as pnsrh_patch, mock.patch(
            'paasta_tools.native_mesos_scheduler.read_all_registrations_for_service_instance',
            autospec=True,
            side_effect=lambda *args, **kwargs: registrations.pop(),
    ) as get_namespace_patch, mock.patch(
            'paasta_tools.native_mesos_scheduler.load_service_namespace_config',
            autospec=True,
            side_effect=lambda *args, **kwargs: nerve_dicts.pop(),
    ) as read_ns_config_patch:
        actual = native_mesos_scheduler.get_paasta_native_services_running_here_for_nerve(
            cluster, soa_dir)
        assert expected == actual
        pnsrh_patch.assert_called_once_with(hostname=None)
        get_namespace_patch.assert_any_call('no_test', 'left_behind', cluster,
                                            soa_dir)
        get_namespace_patch.assert_any_call('no_docstrings',
                                            'forever_abandoned', cluster,
                                            soa_dir)
        assert get_namespace_patch.call_count == 2
        read_ns_config_patch.assert_any_call('no_test', 'uno', soa_dir)
        read_ns_config_patch.assert_any_call('no_docstrings', 'dos', soa_dir)
        assert read_ns_config_patch.call_count == 2
def test_get_paasta_native_services_running_here_for_nerve_when_not_in_smartstack():
    cluster = "edelweiss"
    soa_dir = "the_sound_of_music"
    fake_marathon_services = [
        ("no_test", "left_behind", 1111),
        ("no_docstrings", "forever_abandoned", 2222),
    ]
    registrations = [["no_docstrings.dos"], ["no_test.uno"]]
    nerve_dicts = [
        long_running_service_tools.ServiceNamespaceConfig({"binary": 1}),
        long_running_service_tools.ServiceNamespaceConfig(
            {"clock": 0, "proxy_port": 6666}
        ),
    ]
    expected = [("no_test.uno", {"clock": 0, "port": 1111, "proxy_port": 6666})]
    with mock.patch(
        "paasta_tools.native_mesos_scheduler.paasta_native_services_running_here",
        autospec=True,
        return_value=fake_marathon_services,
    ) as pnsrh_patch, mock.patch(
        "paasta_tools.native_mesos_scheduler.load_paasta_native_job_config",
        autospec=True,
    ) as mock_load_paasta_native_job_config, mock.patch(
        "paasta_tools.native_mesos_scheduler.load_service_namespace_config",
        autospec=True,
        side_effect=lambda *args, **kwargs: nerve_dicts.pop(),
    ) as read_ns_config_patch:

        def mock_registrations_side_effect(*args, **kwargs):
            return registrations.pop()

        mock_load_paasta_native_job_config.return_value.get_registrations.side_effect = (
            mock_registrations_side_effect
        )
        actual = (
            native_mesos_scheduler.get_paasta_native_services_running_here_for_nerve(
                cluster, soa_dir
            )
        )
        assert expected == actual
        pnsrh_patch.assert_called_once_with(hostname=None)
        mock_load_paasta_native_job_config.assert_any_call(
            service="no_test",
            instance="left_behind",
            cluster=cluster,
            load_deployments=False,
            soa_dir=soa_dir,
        )
        mock_load_paasta_native_job_config.assert_any_call(
            service="no_docstrings",
            instance="forever_abandoned",
            cluster=cluster,
            load_deployments=False,
            soa_dir=soa_dir,
        )
        assert mock_load_paasta_native_job_config.call_count == 2
        read_ns_config_patch.assert_any_call("no_test", "uno", soa_dir)
        read_ns_config_patch.assert_any_call("no_docstrings", "dos", soa_dir)
        assert read_ns_config_patch.call_count == 2
Exemple #3
0
 def test_get_healthcheck_for_instance_cmd(self):
     fake_service = 'fake_service'
     fake_namespace = 'fake_namespace'
     fake_hostname = 'fake_hostname'
     fake_random_port = 666
     fake_cmd = '/bin/fake_command'
     fake_service_config = long_running_service_tools.LongRunningServiceConfig(
         service=fake_service,
         cluster='fake_cluster',
         instance=fake_namespace,
         config_dict={
             'healthcheck_mode': 'cmd',
             'healthcheck_cmd': fake_cmd
         },
         branch_dict={},
     )
     fake_service_namespace_config = long_running_service_tools.ServiceNamespaceConfig({})
     with contextlib.nested(
         mock.patch('paasta_tools.long_running_service_tools.load_service_namespace_config',
                    autospec=True,
                    return_value=fake_service_namespace_config),
         mock.patch('socket.getfqdn', autospec=True, return_value=fake_hostname),
     ) as (
         load_service_namespace_config_patch,
         hostname_patch
     ):
         expected = ('cmd', fake_cmd)
         actual = long_running_service_tools.get_healthcheck_for_instance(
             fake_service, fake_namespace, fake_service_config, fake_random_port)
         assert expected == actual
Exemple #4
0
    def test_get_healthcheck_for_instance_not_matching_mode(self):
        fake_service = "fake_service"
        fake_namespace = "fake_namespace"
        fake_hostname = "fake_hostname"
        fake_random_port = 666

        fake_service_config = long_running_service_tools.LongRunningServiceConfig(
            service=fake_service,
            cluster="fake_cluster",
            instance=fake_namespace,
            config_dict={},
            branch_dict=None,
        )
        fake_service_namespace_config = long_running_service_tools.ServiceNamespaceConfig(
            {"mode": "http"})
        with mock.patch(
                "paasta_tools.long_running_service_tools.load_service_namespace_config",
                autospec=True,
                return_value=fake_service_namespace_config,
        ), mock.patch("socket.getfqdn",
                      autospec=True,
                      return_value=fake_hostname):
            expected = ("http", "http://fake_hostname:666/status")
            actual = long_running_service_tools.get_healthcheck_for_instance(
                fake_service, fake_namespace, fake_service_config,
                fake_random_port)
            assert expected == actual
Exemple #5
0
 def test_get_healthcheck_for_instance_custom_soadir(self):
     fake_service = "fake_service"
     fake_namespace = "fake_namespace"
     fake_hostname = "fake_hostname"
     fake_random_port = 666
     fake_soadir = "/fake/soadir"
     fake_service_config = long_running_service_tools.LongRunningServiceConfig(
         service=fake_service,
         cluster="fake_cluster",
         instance=fake_namespace,
         config_dict={"healthcheck_mode": None},
         branch_dict=None,
     )
     fake_service_namespace_config = long_running_service_tools.ServiceNamespaceConfig(
         {})
     with mock.patch(
             "paasta_tools.long_running_service_tools.load_service_namespace_config",
             autospec=True,
             return_value=fake_service_namespace_config,
     ) as load_service_namespace_config_patch, mock.patch(
             "socket.getfqdn", autospec=True, return_value=fake_hostname):
         expected = (None, None)
         actual = long_running_service_tools.get_healthcheck_for_instance(
             fake_service,
             fake_namespace,
             fake_service_config,
             fake_random_port,
             soa_dir=fake_soadir,
         )
         assert expected == actual
         load_service_namespace_config_patch.assert_called_once_with(
             fake_service, fake_namespace, fake_soadir)
Exemple #6
0
 def test_get_healthcheck_for_instance_cmd(self):
     fake_service = "fake_service"
     fake_namespace = "fake_namespace"
     fake_hostname = "fake_hostname"
     fake_random_port = 666
     fake_cmd = "/bin/fake_command"
     fake_service_config = long_running_service_tools.LongRunningServiceConfig(
         service=fake_service,
         cluster="fake_cluster",
         instance=fake_namespace,
         config_dict={
             "instances": 1,
             "healthcheck_mode": "cmd",
             "healthcheck_cmd": fake_cmd,
         },
         branch_dict=None,
     )
     fake_service_namespace_config = long_running_service_tools.ServiceNamespaceConfig(
         {})
     with mock.patch(
             "paasta_tools.long_running_service_tools.load_service_namespace_config",
             autospec=True,
             return_value=fake_service_namespace_config,
     ), mock.patch("socket.getfqdn",
                   autospec=True,
                   return_value=fake_hostname):
         expected = ("cmd", fake_cmd)
         actual = long_running_service_tools.get_healthcheck_for_instance(
             fake_service, fake_namespace, fake_service_config,
             fake_random_port)
         assert expected == actual
Exemple #7
0
 def test_get_healthcheck_for_instance_custom_soadir(self):
     fake_service = 'fake_service'
     fake_namespace = 'fake_namespace'
     fake_hostname = 'fake_hostname'
     fake_random_port = 666
     fake_soadir = '/fake/soadir'
     fake_service_config = long_running_service_tools.LongRunningServiceConfig(
         service=fake_service,
         cluster='fake_cluster',
         instance=fake_namespace,
         config_dict={
             'healthcheck_mode': None,
         },
         branch_dict={},
     )
     fake_service_namespace_config = long_running_service_tools.ServiceNamespaceConfig({})
     with contextlib.nested(
         mock.patch('paasta_tools.long_running_service_tools.load_service_namespace_config',
                    autospec=True,
                    return_value=fake_service_namespace_config),
         mock.patch('socket.getfqdn', autospec=True, return_value=fake_hostname),
     ) as (
         load_service_namespace_config_patch,
         hostname_patch
     ):
         expected = (None, None)
         actual = long_running_service_tools.get_healthcheck_for_instance(
             fake_service, fake_namespace, fake_service_config, fake_random_port, soa_dir=fake_soadir)
         assert expected == actual
         load_service_namespace_config_patch.assert_called_once_with(fake_service, fake_namespace, fake_soadir)
    def test_get_healthcheck_for_instance_tcp(self):
        fake_service = 'fake_service'
        fake_namespace = 'fake_namespace'
        fake_hostname = 'fake_hostname'
        fake_random_port = 666

        fake_service_config = long_running_service_tools.LongRunningServiceConfig(
            service=fake_service,
            cluster='fake_cluster',
            instance=fake_namespace,
            config_dict={},
            branch_dict={},
        )
        fake_service_namespace_config = long_running_service_tools.ServiceNamespaceConfig(
            {
                'mode': 'tcp',
            })
        with mock.patch(
                'paasta_tools.long_running_service_tools.load_service_namespace_config',
                autospec=True,
                return_value=fake_service_namespace_config,
        ), mock.patch(
                'socket.getfqdn',
                autospec=True,
                return_value=fake_hostname,
        ):
            expected = ('tcp',
                        'tcp://%s:%d' % (fake_hostname, fake_random_port))
            actual = long_running_service_tools.get_healthcheck_for_instance(
                fake_service,
                fake_namespace,
                fake_service_config,
                fake_random_port,
            )
            assert expected == actual
Exemple #9
0
    def test_get_healthcheck_for_instance_http(self):
        fake_service = 'fake_service'
        fake_namespace = 'fake_namespace'
        fake_hostname = 'fake_hostname'
        fake_random_port = 666

        fake_path = '/fake_path'
        fake_service_config = long_running_service_tools.LongRunningServiceConfig(
            service=fake_service,
            cluster='fake_cluster',
            instance=fake_namespace,
            config_dict={},
            branch_dict={},
        )
        fake_service_namespace_config = long_running_service_tools.ServiceNamespaceConfig({
            'mode': 'http',
            'healthcheck_uri': fake_path,
        })
        with contextlib.nested(
            mock.patch('paasta_tools.long_running_service_tools.load_service_namespace_config',
                       autospec=True,
                       return_value=fake_service_namespace_config),
            mock.patch('socket.getfqdn', autospec=True, return_value=fake_hostname),
        ) as (
            load_service_namespace_config_patch,
            hostname_patch
        ):
            expected = ('http', 'http://%s:%d%s' % (fake_hostname, fake_random_port, fake_path))
            actual = long_running_service_tools.get_healthcheck_for_instance(
                fake_service, fake_namespace, fake_service_config, fake_random_port)
            assert expected == actual
 def test_get_discover_default(self):
     assert long_running_service_tools.ServiceNamespaceConfig(
     ).get_discover() == 'region'
 def test_get_mode_invalid(self):
     config = {'mode': 'paasta'}
     with raises(long_running_service_tools.InvalidSmartstackMode):
         long_running_service_tools.ServiceNamespaceConfig(
             config).get_mode()
 def test_get_healthcheck_uri_default(self):
     assert long_running_service_tools.ServiceNamespaceConfig(
     ).get_healthcheck_uri() == '/status'
 def test_get_mode_valid(self):
     config = {'mode': 'tcp'}
     assert long_running_service_tools.ServiceNamespaceConfig(
         config).get_mode() == 'tcp'
 def test_get_mode_default_when_port_specified(self):
     config = {'proxy_port': 1234}
     assert long_running_service_tools.ServiceNamespaceConfig(
         config).get_mode() == 'http'
 def test_get_mode_default(self):
     assert long_running_service_tools.ServiceNamespaceConfig().get_mode(
     ) is None
Exemple #16
0
 def test_get_mode_valid(self):
     config = {"mode": "tcp"}
     assert (long_running_service_tools.ServiceNamespaceConfig(
         config).get_mode() == "tcp")
Exemple #17
0
def test_get_paasta_native_services_running_here_for_nerve_multiple_namespaces(
):
    cluster = 'edelweiss'
    soa_dir = 'the_sound_of_music'
    fake_marathon_services = [
        ('no_test', 'left_behind', 1111),
        ('no_docstrings', 'forever_abandoned', 2222),
    ]
    namespaces = [
        ['no_docstrings.quatro'],
        ['no_test.uno', 'no_test.dos', 'no_test.tres'],
    ]
    nerve_dicts = {
        ('no_test', 'uno'):
        long_running_service_tools.ServiceNamespaceConfig({'proxy_port':
                                                           6666}),
        ('no_test', 'dos'):
        long_running_service_tools.ServiceNamespaceConfig({'proxy_port':
                                                           6667}),
        ('no_test', 'tres'):
        long_running_service_tools.ServiceNamespaceConfig({'proxy_port':
                                                           6668}),
        ('no_docstrings', 'quatro'):
        long_running_service_tools.ServiceNamespaceConfig({'proxy_port':
                                                           6669}),
    }
    expected = [
        ('no_test.uno', {
            'port': 1111,
            'proxy_port': 6666
        }),
        ('no_test.dos', {
            'port': 1111,
            'proxy_port': 6667
        }),
        ('no_test.tres', {
            'port': 1111,
            'proxy_port': 6668
        }),
        ('no_docstrings.quatro', {
            'port': 2222,
            'proxy_port': 6669
        }),
    ]
    with mock.patch(
            'paasta_tools.native_mesos_scheduler.paasta_native_services_running_here',
            autospec=True,
            return_value=fake_marathon_services,
    ) as pnsrh_patch, mock.patch(
            'paasta_tools.native_mesos_scheduler.load_paasta_native_job_config',
            autospec=True,
    ) as mock_load_paasta_native_job_config, mock.patch(
            'paasta_tools.native_mesos_scheduler.load_service_namespace_config',
            autospec=True,
            side_effect=lambda service, namespace, soa_dir: nerve_dicts.pop(
                (service, namespace)),
    ) as read_ns_config_patch:

        def mock_registrations_side_effect(*args, **kwargs):
            return namespaces.pop()

        mock_load_paasta_native_job_config.return_value.get_registrations.side_effect = mock_registrations_side_effect
        actual = native_mesos_scheduler.get_paasta_native_services_running_here_for_nerve(
            cluster, soa_dir)
        assert expected == actual
        pnsrh_patch.assert_called_once_with(hostname=None)
        mock_load_paasta_native_job_config.assert_any_call(
            service='no_test',
            instance='left_behind',
            cluster=cluster,
            load_deployments=False,
            soa_dir=soa_dir,
        )
        mock_load_paasta_native_job_config.assert_any_call(
            service='no_docstrings',
            instance='forever_abandoned',
            cluster=cluster,
            load_deployments=False,
            soa_dir=soa_dir,
        )
        assert mock_load_paasta_native_job_config.call_count == 2
        read_ns_config_patch.assert_any_call('no_test', 'uno', soa_dir)
        read_ns_config_patch.assert_any_call('no_test', 'dos', soa_dir)
        read_ns_config_patch.assert_any_call('no_test', 'tres', soa_dir)
        read_ns_config_patch.assert_any_call('no_docstrings', 'quatro',
                                             soa_dir)
        assert read_ns_config_patch.call_count == 4