예제 #1
0
def test_generate_configuration(mock_get_current_location):
    actual_configuration = configure_synapse.generate_configuration(
        synapse_tools_config={'bind_addr': '0.0.0.0'},
        zookeeper_topology=['1.2.3.4', '2.3.4.5'],
        services=[('test_service', {
            'proxy_port': 1234,
            'healthcheck_uri': '/status',
            'retries': 2,
            'timeout_connect_ms': 2000,
            'timeout_server_ms': 3000,
            'extra_headers': {
                'X-Mode': 'ro'
            },
            'extra_healthcheck_headers': {
                'X-Mode': 'ro'
            },
            'balance': 'roundrobin',
        })])

    expected_configuration = configure_synapse.generate_base_config(
        synapse_tools_config={'bind_addr': '0.0.0.0'})
    expected_configuration['services'] = {
        'test_service': {
            'default_servers': [],
            'use_previous_backends': False,
            'discovery': {
                'hosts': ['1.2.3.4', '2.3.4.5'],
                'method': 'zookeeper',
                'path': '/nerve/region:my_region/test_service'
            },
            'haproxy': {
                'listen': [
                    'option httpchk GET /http/test_service/0/status HTTP/1.1\\r\\nX-Mode:\\ ro',
                    'http-check send-state',
                    'retries 2',
                    'timeout connect 2000ms',
                    'timeout server 3000ms',
                    'balance roundrobin',
                ],
                'frontend': [
                    'timeout client 3000ms',
                    'capture request header X-B3-SpanId len 64',
                    'capture request header X-B3-TraceId len 64',
                    'capture request header X-B3-ParentSpanId len 64',
                    'capture request header X-B3-Flags len 10',
                    'capture request header X-B3-Sampled len 10',
                    'option httplog',
                ],
                'backend': [
                    'reqadd X-Mode:\ ro',
                ],
                'port':
                '1234',
                'server_options':
                'check port 6666 observe layer7'
            }
        }
    }

    assert actual_configuration == expected_configuration
def test_generate_configuration_empty():
    actual_configuration = configure_synapse.generate_configuration(
        synapse_tools_config={'bind_addr': '0.0.0.0'},
        zookeeper_topology=['1.2.3.4', '2.3.4.5'],
        services=[])
    expected_configuration = configure_synapse.generate_base_config(
        synapse_tools_config={'bind_addr': '0.0.0.0'})
    assert actual_configuration == expected_configuration
def test_generate_configuration_empty():
    actual_configuration = configure_synapse.generate_configuration(
        synapse_tools_config={'bind_addr': '0.0.0.0'},
        zookeeper_topology=['1.2.3.4', '2.3.4.5'],
        services=[]
    )
    expected_configuration = configure_synapse.generate_base_config(
        synapse_tools_config={'bind_addr': '0.0.0.0'}
    )
    assert actual_configuration == expected_configuration
예제 #4
0
def test_generate_configuration_empty(mock_available_location_types):
    actual_configuration = configure_synapse.generate_configuration(
        synapse_tools_config=configure_synapse.set_defaults(
            {'bind_addr': '0.0.0.0'}),
        zookeeper_topology=['1.2.3.4', '2.3.4.5'],
        services=[])
    expected_configuration = configure_synapse.generate_base_config(
        synapse_tools_config=configure_synapse.set_defaults(
            {'bind_addr': '0.0.0.0'}))
    assert actual_configuration == expected_configuration
def test_chaos_error_503(mock_get_current_location):
    with mock.patch.object(configure_synapse, 'get_my_grouping') as grouping_mock:
        grouping_mock.return_value = 'my_ecosystem'
        actual_configuration = configure_synapse.generate_configuration(
            synapse_tools_config={'bind_addr': '0.0.0.0'},
            zookeeper_topology=['1.2.3.4'],
            services=[
                (
                    'test_service',
                    {
                        'proxy_port': 1234,
                        'chaos': {'ecosystem': {'my_ecosystem': {'fail': 'error_503'}}}
                    }
                )
            ]
        )
        assert actual_configuration['services']['test_service']['discovery']['method'] == 'base'
def test_chaos_drop(mock_get_current_location):
    with mock.patch.object(configure_synapse, 'get_my_grouping') as grouping_mock:
        grouping_mock.return_value = 'my_ecosystem'
        actual_configuration = configure_synapse.generate_configuration(
            synapse_tools_config={'bind_addr': '0.0.0.0'},
            zookeeper_topology=['1.2.3.4'],
            services=[
                (
                    'test_service',
                    {
                        'proxy_port': 1234,
                        'chaos': {'ecosystem': {'my_ecosystem': {'fail': 'drop'}}}
                    }
                )
            ]
        )
        grouping_mock.assert_called_once_with('ecosystem')
    frontend = actual_configuration['services']['test_service']['haproxy']['frontend']
    assert 'tcp-request content reject' in frontend
def test_chaos_error_503(mock_get_current_location):
    with mock.patch.object(configure_synapse,
                           'get_my_grouping') as grouping_mock:
        grouping_mock.return_value = 'my_ecosystem'
        actual_configuration = configure_synapse.generate_configuration(
            synapse_tools_config={'bind_addr': '0.0.0.0'},
            zookeeper_topology=['1.2.3.4'],
            services=[('test_service', {
                'proxy_port': 1234,
                'chaos': {
                    'ecosystem': {
                        'my_ecosystem': {
                            'fail': 'error_503'
                        }
                    }
                }
            })])
        assert actual_configuration['services']['test_service']['discovery'][
            'method'] == 'base'
def test_chaos_drop(mock_get_current_location):
    with mock.patch.object(configure_synapse,
                           'get_my_grouping') as grouping_mock:
        grouping_mock.return_value = 'my_ecosystem'
        actual_configuration = configure_synapse.generate_configuration(
            synapse_tools_config={'bind_addr': '0.0.0.0'},
            zookeeper_topology=['1.2.3.4'],
            services=[('test_service', {
                'proxy_port': 1234,
                'chaos': {
                    'ecosystem': {
                        'my_ecosystem': {
                            'fail': 'drop'
                        }
                    }
                }
            })])
        grouping_mock.assert_called_once_with('ecosystem')
    frontend = actual_configuration['services']['test_service']['haproxy'][
        'frontend']
    assert 'tcp-request content reject' in frontend
예제 #9
0
def test_generate_configuration_with_errorfiles(mock_get_current_location,
                                                mock_available_location_types):
    synapse_tools_config = configure_synapse.set_defaults({
        'bind_addr':
        '0.0.0.0',
        'haproxy.defaults.inter':
        '1234',
        'errorfiles': {
            '404': '/etc/haproxy-synapse/errors/404.http',
            '503': '/etc/haproxy-synapse/errors/503.http',
        }
    })

    actual_configuration = configure_synapse.generate_configuration(
        synapse_tools_config=synapse_tools_config,
        zookeeper_topology=['1.2.3.4', '2.3.4.5'],
        services=[])

    assert 'errorfile 404 /etc/haproxy-synapse/errors/404.http' in actual_configuration[
        'haproxy']['defaults']
    assert 'errorfile 503 /etc/haproxy-synapse/errors/503.http' in actual_configuration[
        'haproxy']['defaults']
def test_chaos_delay(mock_get_current_location):
    with mock.patch.object(configure_synapse,
                           'get_my_grouping') as grouping_mock:
        grouping_mock.return_value = 'my_ecosystem'
        actual_configuration = configure_synapse.generate_configuration(
            synapse_tools_config=configure_synapse.set_defaults(
                {'bind_addr': '0.0.0.0'}),
            zookeeper_topology=['1.2.3.4'],
            services=[('test_service', {
                'proxy_port': 1234,
                'chaos': {
                    'ecosystem': {
                        'my_ecosystem': {
                            'delay': '300ms'
                        }
                    }
                }
            })])
        grouping_mock.assert_called_once_with('ecosystem')
    frontend = actual_configuration['services']['test_service']['haproxy'][
        'frontend']
    assert 'tcp-request inspect-delay 300ms' in frontend
    assert 'tcp-request content accept if WAIT_END' in frontend
def test_generate_configuration(mock_get_current_location):
    actual_configuration = configure_synapse.generate_configuration(
        synapse_tools_config={'bind_addr': '0.0.0.0'},
        zookeeper_topology=['1.2.3.4', '2.3.4.5'],
        services=[
            (
                'test_service',
                {
                    'proxy_port': 1234,
                    'healthcheck_uri': '/status',
                    'retries': 2,
                    'timeout_connect_ms': 2000,
                    'timeout_server_ms': 3000,
                    'extra_headers': {
                        'X-Mode': 'ro'
                    },
                    'extra_healthcheck_headers': {
                        'X-Mode': 'ro'
                    }
                }
            )
        ]
    )

    expected_configuration = configure_synapse.generate_base_config(
        synapse_tools_config={'bind_addr': '0.0.0.0'}
    )
    expected_configuration['services'] = {
        'test_service': {
            'default_servers': [],
            'use_previous_backends': False,
            'discovery': {
                'hosts': ['1.2.3.4', '2.3.4.5'],
                'method': 'zookeeper',
                'path': '/nerve/region:my_region/test_service'},
            'haproxy': {
                'listen': [
                    'option httpchk GET /http/test_service/0/status HTTP/1.1\\r\\nX-Mode:\\ ro',
                    'http-check send-state',
                    'retries 2',
                    'timeout connect 2000ms',
                    'timeout server 3000ms',
                ],
                'frontend': [
                    'timeout client 3000ms',
                    'capture request header X-B3-SpanId len 64',
                    'capture request header X-B3-TraceId len 64',
                    'capture request header X-B3-ParentSpanId len 64',
                    'capture request header X-B3-Flags len 10',
                    'capture request header X-B3-Sampled len 10',
                    'option httplog',
                ],
                'backend': [
                    'reqadd X-Mode:\ ro',
                ],
                'port': '1234',
                'server_options': 'check port 6666 observe layer7'
            }
        }
    }

    assert actual_configuration == expected_configuration
예제 #12
0
def test_discovery_only_services(mock_get_current_location,
                                 mock_available_location_types):
    synapse_tools_config = configure_synapse.set_defaults({
        'bind_addr':
        '0.0.0.0',
        'listen_with_nginx':
        True,
        'listen_with_haproxy':
        True,
    })
    actual_configuration = configure_synapse.generate_configuration(
        synapse_tools_config=synapse_tools_config,
        zookeeper_topology=['1.2.3.4', '2.3.4.5'],
        services=[('test_service', {
            'proxy_port': None,
            'healthcheck_uri': '/status',
            'retries': 2,
            'timeout_connect_ms': 2000,
            'timeout_server_ms': 3000,
            'extra_headers': {
                'X-Mode': 'ro'
            },
            'extra_healthcheck_headers': {
                'X-Mode': 'ro'
            },
            'balance': 'roundrobin',
            'advertise': ['region', 'superregion'],
            'discover': 'region',
        })])

    expected_configuration = configure_synapse.generate_base_config(
        synapse_tools_config=synapse_tools_config)
    expected_configuration['services'] = {
        'test_service': {
            'default_servers': [],
            'use_previous_backends': False,
            'discovery': {
                'hosts': ['1.2.3.4', '2.3.4.5'],
                'method':
                'zookeeper',
                'path':
                '/smartstack/global/test_service',
                'label_filters': [
                    {
                        'label': 'region:my_region',
                        'value': '',
                        'condition': 'equals',
                    },
                ],
            },
            'haproxy': {
                'disabled': True
            },
            'nginx': {
                'disabled': True
            },
        },
        'test_service.superregion': {
            'default_servers': [],
            'use_previous_backends': False,
            'discovery': {
                'hosts': ['1.2.3.4', '2.3.4.5'],
                'method':
                'zookeeper',
                'path':
                '/smartstack/global/test_service',
                'label_filters': [
                    {
                        'label': 'superregion:my_superregion',
                        'value': '',
                        'condition': 'equals',
                    },
                ],
            },
            'haproxy': {
                'disabled': True
            },
            'nginx': {
                'disabled': True
            },
        },
    }

    assert actual_configuration == expected_configuration
예제 #13
0
def test_generate_configuration_only_nginx(mock_get_current_location,
                                           mock_available_location_types):
    synapse_tools_config = configure_synapse.set_defaults({
        'bind_addr':
        '0.0.0.0',
        'listen_with_nginx':
        True,
        'listen_with_haproxy':
        False,
    })
    actual_configuration = configure_synapse.generate_configuration(
        synapse_tools_config=synapse_tools_config,
        zookeeper_topology=['1.2.3.4', '2.3.4.5'],
        services=[('test_service', {
            'proxy_port': 1234,
            'healthcheck_uri': '/status',
            'retries': 2,
            'timeout_connect_ms': 2000,
            'timeout_server_ms': 3000,
            'extra_headers': {
                'X-Mode': 'ro'
            },
            'extra_healthcheck_headers': {
                'X-Mode': 'ro'
            },
            'balance': 'roundrobin',
            'advertise': ['region', 'superregion'],
            'discover': 'region',
        })])

    expected_configuration = configure_synapse.generate_base_config(
        synapse_tools_config=synapse_tools_config)
    expected_configuration['services'] = {
        'test_service': {
            'default_servers': [],
            'use_previous_backends': False,
            'discovery': {
                'hosts': ['1.2.3.4', '2.3.4.5'],
                'method':
                'zookeeper',
                'path':
                '/smartstack/global/test_service',
                'label_filters': [
                    {
                        'label': 'region:my_region',
                        'value': '',
                        'condition': 'equals',
                    },
                ],
            },
            'haproxy': {
                'listen': [],
                'frontend': [
                    'timeout client 3000ms',
                    'capture request header X-B3-SpanId len 64',
                    'capture request header X-B3-TraceId len 64',
                    'capture request header X-B3-ParentSpanId len 64',
                    'capture request header X-B3-Flags len 10',
                    'capture request header X-B3-Sampled len 10',
                    'option httplog',
                    'acl test_service_has_connslots connslots(test_service) gt 0',
                    'use_backend test_service if test_service_has_connslots',
                    'acl test_service.superregion_has_connslots connslots(test_service.superregion) gt 0',
                    'use_backend test_service.superregion if test_service.superregion_has_connslots',
                ],
                'backend': [
                    'balance roundrobin',
                    'reqidel ^X-Mode:.*',
                    'reqadd X-Mode:\ ro',
                    'option httpchk GET /http/test_service/0/status HTTP/1.1\\r\\nX-Mode:\\ ro',
                    'http-check send-state',
                    'retries 2',
                    'timeout connect 2000ms',
                    'timeout server 3000ms',
                ],
                'bind_address':
                '/var/run/synapse/sockets/test_service.sock',
                'server_options':
                'check port 6666 observe layer7 maxconn 50 maxqueue 10',
                'backend_name':
                'test_service',
                'port':
                None,
            },
            'nginx': {
                'disabled': True
            },
        },
        'test_service.superregion': {
            'default_servers': [],
            'use_previous_backends': False,
            'discovery': {
                'hosts': ['1.2.3.4', '2.3.4.5'],
                'method':
                'zookeeper',
                'path':
                '/smartstack/global/test_service',
                'label_filters': [
                    {
                        'label': 'superregion:my_superregion',
                        'value': '',
                        'condition': 'equals',
                    },
                ],
            },
            'haproxy': {
                'listen': [],
                'backend': [
                    'balance roundrobin',
                    'reqidel ^X-Mode:.*',
                    'reqadd X-Mode:\ ro',
                    'option httpchk GET /http/test_service/0/status HTTP/1.1\\r\\nX-Mode:\\ ro',
                    'http-check send-state',
                    'retries 2',
                    'timeout connect 2000ms',
                    'timeout server 3000ms',
                ],
                'server_options':
                'check port 6666 observe layer7 maxconn 50 maxqueue 10',
                'backend_name':
                'test_service.superregion',
            },
            'nginx': {
                'disabled': True
            },
        },
        'test_service.nginx_listener': {
            'default_servers': [{
                'host':
                'unix',
                'port':
                '/var/run/synapse/sockets/test_service.sock',
            }],
            'discovery': {
                'method': 'base'
            },
            'haproxy': {
                'disabled': True
            },
            'file_output': {
                'disabled': True
            },
            'nginx': {
                'mode': 'tcp',
                'port': 1234,
                'server': [
                    'proxy_timeout 3610s',
                ]
            },
            'use_previous_backends':
            True
        },
    }

    assert actual_configuration == expected_configuration
예제 #14
0
def test_generate_configuration_with_proxied_through(
        mock_get_current_location, mock_available_location_types):
    actual_configuration = configure_synapse.generate_configuration(
        synapse_tools_config=configure_synapse.set_defaults(
            {'bind_addr': '0.0.0.0'}),
        zookeeper_topology=['1.2.3.4', '2.3.4.5'],
        services=[('test_service', {
            'proxy_port': 1234,
            'healthcheck_uri': '/status',
            'retries': 2,
            'timeout_connect_ms': 2000,
            'timeout_server_ms': 3000,
            'extra_headers': {
                'X-Mode': 'ro'
            },
            'extra_healthcheck_headers': {
                'X-Mode': 'ro'
            },
            'balance': 'roundrobin',
            'advertise': ['region'],
            'discover': 'region',
            'proxied_through': 'proxy_service',
        }),
                  ('proxy_service', {
                      'proxy_port': 5678,
                      'balance': 'roundrobin',
                      'advertise': ['region'],
                      'discover': 'region',
                  })])

    expected_configuration = configure_synapse.generate_base_config(
        synapse_tools_config=configure_synapse.set_defaults(
            {'bind_addr': '0.0.0.0'}))
    expected_configuration['services'] = {
        'proxy_service': {
            'default_servers': [],
            'use_previous_backends': False,
            'discovery': {
                'hosts': ['1.2.3.4', '2.3.4.5'],
                'method':
                'zookeeper',
                'path':
                '/smartstack/global/proxy_service',
                'label_filters': [
                    {
                        'label': 'region:my_region',
                        'value': '',
                        'condition': 'equals',
                    },
                ],
            },
            'haproxy': {
                'listen': [],
                'frontend': [
                    'capture request header X-B3-SpanId len 64',
                    'capture request header X-B3-TraceId len 64',
                    'capture request header X-B3-ParentSpanId len 64',
                    'capture request header X-B3-Flags len 10',
                    'capture request header X-B3-Sampled len 10',
                    'option httplog',
                    'bind /var/run/synapse/sockets/proxy_service.sock',
                    'acl proxy_service_has_connslots connslots(proxy_service) gt 0',
                    'use_backend proxy_service if proxy_service_has_connslots',
                ],
                'backend': [
                    'balance roundrobin',
                    'acl is_status_request path /status',
                    'reqadd X-Smartstack-Source:\\ proxy_service if !is_status_request',
                    'option httpchk GET /http/proxy_service/0/status',
                    'http-check send-state',
                ],
                'port':
                '5678',
                'server_options':
                'check port 6666 observe layer7 maxconn 50 maxqueue 10',
                'backend_name':
                'proxy_service',
            },
        },
        'test_service': {
            'default_servers': [],
            'use_previous_backends': False,
            'discovery': {
                'hosts': ['1.2.3.4', '2.3.4.5'],
                'method':
                'zookeeper',
                'path':
                '/smartstack/global/test_service',
                'label_filters': [
                    {
                        'label': 'region:my_region',
                        'value': '',
                        'condition': 'equals',
                    },
                ],
            },
            'haproxy': {
                'listen': [],
                'frontend': [
                    'timeout client 3000ms',
                    'capture request header X-B3-SpanId len 64',
                    'capture request header X-B3-TraceId len 64',
                    'capture request header X-B3-ParentSpanId len 64',
                    'capture request header X-B3-Flags len 10',
                    'capture request header X-B3-Sampled len 10',
                    'option httplog',
                    'bind /var/run/synapse/sockets/test_service.sock',
                    'acl is_status_request path /status',
                    'acl request_from_proxy hdr_beg(X-Smartstack-Source) -i proxy_service',
                    'acl proxied_through_backend_has_connslots connslots(proxy_service) gt 0',
                    'use_backend proxy_service if !is_status_request !request_from_proxy proxied_through_backend_has_connslots',
                    'reqadd X-Smartstack-Destination:\\ test_service if !is_status_request !request_from_proxy proxied_through_backend_has_connslots',
                    'acl test_service_has_connslots connslots(test_service) gt 0',
                    'use_backend test_service if test_service_has_connslots',
                ],
                'backend': [
                    'balance roundrobin',
                    'reqidel ^X-Mode:.*',
                    'reqadd X-Mode:\ ro',
                    'option httpchk GET /http/test_service/0/status HTTP/1.1\\r\\nX-Mode:\\ ro',
                    'http-check send-state',
                    'retries 2',
                    'timeout connect 2000ms',
                    'timeout server 3000ms',
                ],
                'port':
                '1234',
                'server_options':
                'check port 6666 observe layer7 maxconn 50 maxqueue 10',
                'backend_name':
                'test_service',
            },
        },
    }

    assert actual_configuration == expected_configuration