def test_generate_subconfiguration_k8s(expected_sub_config): with patch( 'nerve_tools.configure_nerve.get_current_location', side_effect=get_current_location ), patch('nerve_tools.configure_nerve.convert_location_type', side_effect=convert_location_type), patch( 'nerve_tools.configure_nerve.get_named_zookeeper_topology', side_effect=get_named_zookeeper_topology ), patch( 'nerve_tools.configure_nerve.get_labels_by_service_and_port', side_effect=get_labels_by_service_and_port): for k, v in expected_sub_config.items(): expected_sub_config[k]['host'] = '10.4.5.6' for check in expected_sub_config[k]['checks']: check['host'] = '10.1.2.3' new_expected_sub_config = {} for k, v in expected_sub_config.items(): new_expected_sub_config[k.replace( '10.0.0.1', '10.4.5.6')] = expected_sub_config[k] mock_service_info = { 'port': 1234, 'routes': [('remote_location', 'local_location')], 'healthcheck_timeout_s': 2.0, 'healthcheck_mode': 'http', 'healthcheck_port': 1234, 'hacheck_ip': '10.1.2.3', 'service_ip': '10.4.5.6', 'advertise': ['region', 'superregion'], 'extra_advertise': [ ('habitat:my_habitat', 'region:another_region'), ('habitat:your_habitat', 'region:another_region'), # Ignored ], 'deploy_group': 'prod.canary', 'paasta_instance': 'canary', } actual_config = configure_nerve.generate_subconfiguration( service_name='test_service', service_info=mock_service_info, host_ip='10.4.5.6', hacheck_port=6666, weight=mock.sentinel.weight, zk_topology_dir='/fake/path', zk_location_type='superregion', zk_cluster_type='infrastructure', labels_dir='/dev/null', envoy_service_info=None, ) assert new_expected_sub_config == actual_config
def test_generate_subconfiguration_with_envoy_listeners(expected_sub_config_with_envoy_listeners): with mock.patch( 'nerve_tools.configure_nerve.get_current_location', side_effect=get_current_location ), mock.patch( 'nerve_tools.configure_nerve.convert_location_type', side_effect=convert_location_type ), mock.patch( 'nerve_tools.configure_nerve.get_named_zookeeper_topology', side_effect=get_named_zookeeper_topology ), mock.patch( 'nerve_tools.configure_nerve.get_labels_by_service_and_port', side_effect=get_labels_by_service_and_port ): mock_service_info = { 'port': 1234, 'routes': [('remote_location', 'local_location')], 'healthcheck_timeout_s': 2.0, 'healthcheck_mode': 'http', 'healthcheck_port': 1234, 'advertise': ['region', 'superregion'], 'extra_advertise': [ ('habitat:my_habitat', 'region:another_region'), ('habitat:your_habitat', 'region:another_region'), # Ignored ], 'deploy_group': 'prod.canary', 'paasta_instance': 'canary', } mock_envoy_service_info = copy.deepcopy(mock_service_info) mock_envoy_service_info.update({ 'port': 35000, 'healthcheck_port': 35000, 'extra_healthcheck_headers': {'Host': 'test_service'}, }) actual_config = configure_nerve.generate_subconfiguration( service_name='test_service', service_info=mock_service_info, ip_address='ip_address', hacheck_port=6666, weight=mock.sentinel.weight, zk_topology_dir='/fake/path', zk_location_type='superregion', zk_cluster_type='infrastructure', labels_dir='/dev/null', envoy_service_info=mock_envoy_service_info, ) assert expected_sub_config_with_envoy_listeners == actual_config
def test_generate_subconfiguration(): expected_config = { 'test_service.my_superregion.region:my_region.1234.new': { 'zk_hosts': ['1.2.3.4', '2.3.4.5'], 'zk_path': '/nerve/region:my_region/test_service', 'checks': [{ 'rise': 1, 'uri': '/http/test_service/1234/status', 'host': '127.0.0.1', 'timeout': 2.0, 'open_timeout': 2.0, 'fall': 2, 'type': 'http', 'port': 6666, 'headers': {}, }], 'host': 'ip_address', 'check_interval': 3.0, 'port': 1234, 'weight': CPUS, }, 'test_service.another_superregion.region:another_region.1234.new': { 'zk_hosts': ['3.4.5.6', '4.5.6.7'], 'zk_path': '/nerve/region:another_region/test_service', 'checks': [{ 'rise': 1, 'uri': '/http/test_service/1234/status', 'host': '127.0.0.1', 'timeout': 2.0, 'open_timeout': 2.0, 'fall': 2, 'type': 'http', 'port': 6666, 'headers': {}, }], 'host': 'ip_address', 'check_interval': 3.0, 'port': 1234, 'weight': CPUS, } } def get_current_location(typ): return { 'superregion': 'my_superregion', 'habitat': 'my_habitat', 'region': 'my_region', }[typ] def convert_location_type(src_typ, src_loc, dst_typ): return { ('my_superregion', 'superregion', 'region'): ['my_region'], ('my_region', 'region', 'superregion'): ['my_superregion'], ('another_region', 'region', 'region'): ['another_region'], ('another_region', 'region', 'superregion'): ['another_superregion'], }[(src_typ, src_loc, dst_typ)] def get_named_zookeeper_topology(cluster_type, cluster_location): return { ('infrastructure', 'my_superregion'): ['1.2.3.4', '2.3.4.5'], ('infrastructure', 'another_superregion'): ['3.4.5.6', '4.5.6.7'] }[(cluster_type, cluster_location)] with contextlib.nested( mock.patch('nerve_tools.configure_nerve.get_current_location', side_effect=get_current_location), mock.patch('nerve_tools.configure_nerve.convert_location_type', side_effect=convert_location_type), mock.patch('nerve_tools.configure_nerve.get_named_zookeeper_topology', side_effect=get_named_zookeeper_topology)): actual_config = configure_nerve.generate_subconfiguration( service_name='test_service', advertise=['region'], extra_advertise=[ ('habitat:my_habitat', 'region:another_region'), ('habitat:your_habitat', 'region:another_region'), # Ignored ], port=1234, ip_address='ip_address', healthcheck_timeout_s=2.0, hacheck_uri='/http/test_service/1234/status', healthcheck_headers={}, ) assert expected_config == actual_config
def test_generate_subconfiguration(): expected_config = { 'test_service.my_superregion.region:my_region.1234.new': { 'zk_hosts': ['1.2.3.4', '2.3.4.5'], 'zk_path': '/nerve/region:my_region/test_service', 'checks': [{ 'rise': 1, 'uri': '/http/test_service/1234/status', 'host': '127.0.0.1', 'timeout': 2.0, 'open_timeout': 2.0, 'fall': 2, 'type': 'http', 'port': 6666, 'headers': {}, }], 'host': 'ip_address', 'check_interval': 3.0, 'port': 1234, 'weight': 1, }, 'test_service.another_superregion.region:another_region.1234.new': { 'zk_hosts': ['3.4.5.6', '4.5.6.7'], 'zk_path': '/nerve/region:another_region/test_service', 'checks': [{ 'rise': 1, 'uri': '/http/test_service/1234/status', 'host': '127.0.0.1', 'timeout': 2.0, 'open_timeout': 2.0, 'fall': 2, 'type': 'http', 'port': 6666, 'headers': {}, }], 'host': 'ip_address', 'check_interval': 3.0, 'port': 1234, 'weight': 1, } } def get_current_location(typ): return { 'superregion': 'my_superregion', 'habitat': 'my_habitat', 'region': 'my_region', }[typ] def convert_location_type(src_typ, src_loc, dst_typ): return { ('my_superregion', 'superregion', 'region'): ['my_region'], ('my_region', 'region', 'superregion'): ['my_superregion'], ('another_region', 'region', 'region'): ['another_region'], ('another_region', 'region', 'superregion'): ['another_superregion'], }[(src_typ, src_loc, dst_typ)] def get_named_zookeeper_topology(cluster_type, cluster_location, zk_topology_dir): return { ('infrastructure', 'my_superregion'): ['1.2.3.4', '2.3.4.5'], ('infrastructure', 'another_superregion'): ['3.4.5.6', '4.5.6.7'] }[(cluster_type, cluster_location)] with contextlib.nested( mock.patch('nerve_tools.configure_nerve.get_current_location', side_effect=get_current_location), mock.patch('nerve_tools.configure_nerve.convert_location_type', side_effect=convert_location_type), mock.patch('nerve_tools.configure_nerve.get_named_zookeeper_topology', side_effect=get_named_zookeeper_topology)): actual_config = configure_nerve.generate_subconfiguration( service_name='test_service', advertise=['region'], extra_advertise=[ ('habitat:my_habitat', 'region:another_region'), ('habitat:your_habitat', 'region:another_region'), # Ignored ], port=1234, ip_address='ip_address', healthcheck_timeout_s=2.0, hacheck_uri='/http/test_service/1234/status', healthcheck_headers={}, hacheck_port=6666, weight=1, zk_topology_dir='/fake/path', zk_location_type='superregion', zk_cluster_type='infrastructure', ) assert expected_config == actual_config
def test_generate_subconfiguration(): expected_config = { 'test_service.my_superregion.region:my_region.1234.new': { 'zk_hosts': ['1.2.3.4', '2.3.4.5'], 'zk_path': '/nerve/region:my_region/test_service', 'checks': [{ 'rise': 1, 'uri': '/http/test_service/1234/status', 'host': '127.0.0.1', 'timeout': 2.0, 'open_timeout': 2.0, 'fall': 2, 'type': 'http', 'port': 6666, 'headers': {}, }], 'host': 'ip_address', 'check_interval': 3.0, 'port': 1234, 'weight': mock.sentinel.weight, }, 'test_service.my_superregion.superregion:my_superregion.1234.new': { 'zk_hosts': ['1.2.3.4', '2.3.4.5'], 'zk_path': '/nerve/superregion:my_superregion/test_service', 'checks': [{ 'rise': 1, 'uri': '/http/test_service/1234/status', 'host': '127.0.0.1', 'timeout': 2.0, 'open_timeout': 2.0, 'fall': 2, 'type': 'http', 'port': 6666, 'headers': {}, }], 'host': 'ip_address', 'check_interval': 3.0, 'port': 1234, 'weight': mock.sentinel.weight, }, 'test_service.another_superregion.region:another_region.1234.new': { 'zk_hosts': ['3.4.5.6', '4.5.6.7'], 'zk_path': '/nerve/region:another_region/test_service', 'checks': [{ 'rise': 1, 'uri': '/http/test_service/1234/status', 'host': '127.0.0.1', 'timeout': 2.0, 'open_timeout': 2.0, 'fall': 2, 'type': 'http', 'port': 6666, 'headers': {}, }], 'host': 'ip_address', 'check_interval': 3.0, 'port': 1234, 'weight': mock.sentinel.weight, }, 'test_service.my_superregion:1234.v2.new': { 'zk_hosts': ['1.2.3.4', '2.3.4.5'], 'zk_path': '/smartstack/global/test_service', 'checks': [{ 'rise': 1, 'uri': '/http/test_service/1234/status', 'host': '127.0.0.1', 'timeout': 2.0, 'open_timeout': 2.0, 'fall': 2, 'type': 'http', 'port': 6666, 'headers': {}, }], 'host': 'ip_address', 'check_interval': 3.0, 'port': 1234, 'weight': mock.sentinel.weight, 'labels': { 'region:my_region': '', 'superregion:my_superregion': '', }, }, 'test_service.another_superregion:1234.v2.new': { 'zk_hosts': ['3.4.5.6', '4.5.6.7'], 'zk_path': '/smartstack/global/test_service', 'checks': [{ 'rise': 1, 'uri': '/http/test_service/1234/status', 'host': '127.0.0.1', 'timeout': 2.0, 'open_timeout': 2.0, 'fall': 2, 'type': 'http', 'port': 6666, 'headers': {}, }], 'host': 'ip_address', 'check_interval': 3.0, 'port': 1234, 'weight': mock.sentinel.weight, 'labels': { 'region:another_region': '', }, }, } def get_current_location(typ): return { 'ecosystem': 'my_ecosystem', 'superregion': 'my_superregion', 'habitat': 'my_habitat', 'region': 'my_region', }[typ] def convert_location_type(src_loc, src_typ, dst_typ): if src_typ == dst_typ: return [src_loc] return { ('my_superregion', 'superregion', 'superregion'): ['my_superregion'], ('another_superregion', 'superregion', 'region'): ['another_region'], ('my_region', 'region', 'superregion'): ['my_superregion'], ('another_region', 'region', 'region'): ['another_region'], ('another_region', 'region', 'superregion'): ['another_superregion'], }[(src_loc, src_typ, dst_typ)] def get_named_zookeeper_topology(cluster_type, cluster_location, zk_topology_dir): return { ('infrastructure', 'my_superregion'): ['1.2.3.4', '2.3.4.5'], ('infrastructure', 'another_superregion'): ['3.4.5.6', '4.5.6.7'] }[(cluster_type, cluster_location)] with contextlib.nested( mock.patch('nerve_tools.configure_nerve.get_current_location', side_effect=get_current_location), mock.patch('nerve_tools.configure_nerve.convert_location_type', side_effect=convert_location_type), mock.patch('nerve_tools.configure_nerve.get_named_zookeeper_topology', side_effect=get_named_zookeeper_topology)): mock_service_info = { 'port': 1234, 'routes': [('remote_location', 'local_location')], 'healthcheck_timeout_s': 2.0, 'healthcheck_mode': 'http', 'healthcheck_port': 1234, 'advertise': ['region', 'superregion'], 'extra_advertise': [ ('habitat:my_habitat', 'region:another_region'), ('habitat:your_habitat', 'region:another_region'), # Ignored ], } actual_config = configure_nerve.generate_subconfiguration( service_name='test_service', service_info=mock_service_info, ip_address='ip_address', hacheck_port=6666, weight=mock.sentinel.weight, zk_topology_dir='/fake/path', zk_location_type='superregion', zk_cluster_type='infrastructure', ) assert expected_config == actual_config
def test_generate_subconfiguration(): expected_config = { 'test_service.my_superregion.region:my_region.1234.new': { 'zk_hosts': ['1.2.3.4', '2.3.4.5'], 'zk_path': '/nerve/region:my_region/test_service', 'checks': [{ 'rise': 1, 'uri': '/http/test_service/1234/status', 'host': '127.0.0.1', 'timeout': 2.0, 'open_timeout': 2.0, 'fall': 2, 'type': 'http', 'port': 6666, 'headers': {}, }], 'host': 'ip_address', 'check_interval': 3.0, 'port': 1234, 'weight': mock.sentinel.weight, }, 'test_service.my_superregion.superregion:my_superregion.1234.new': { 'zk_hosts': ['1.2.3.4', '2.3.4.5'], 'zk_path': '/nerve/superregion:my_superregion/test_service', 'checks': [{ 'rise': 1, 'uri': '/http/test_service/1234/status', 'host': '127.0.0.1', 'timeout': 2.0, 'open_timeout': 2.0, 'fall': 2, 'type': 'http', 'port': 6666, 'headers': {}, }], 'host': 'ip_address', 'check_interval': 3.0, 'port': 1234, 'weight': mock.sentinel.weight, }, 'test_service.another_superregion.region:another_region.1234.new': { 'zk_hosts': ['3.4.5.6', '4.5.6.7'], 'zk_path': '/nerve/region:another_region/test_service', 'checks': [{ 'rise': 1, 'uri': '/http/test_service/1234/status', 'host': '127.0.0.1', 'timeout': 2.0, 'open_timeout': 2.0, 'fall': 2, 'type': 'http', 'port': 6666, 'headers': {}, }], 'host': 'ip_address', 'check_interval': 3.0, 'port': 1234, 'weight': mock.sentinel.weight, }, 'test_service.my_superregion:1234.v2.new': { 'zk_hosts': ['1.2.3.4', '2.3.4.5'], 'zk_path': '/smartstack/global/test_service', 'checks': [{ 'rise': 1, 'uri': '/http/test_service/1234/status', 'host': '127.0.0.1', 'timeout': 2.0, 'open_timeout': 2.0, 'fall': 2, 'type': 'http', 'port': 6666, 'headers': {}, }], 'host': 'ip_address', 'check_interval': 3.0, 'port': 1234, 'weight': mock.sentinel.weight, 'labels': { 'region:my_region': '', 'superregion:my_superregion': '', }, }, 'test_service.another_superregion:1234.v2.new': { 'zk_hosts': ['3.4.5.6', '4.5.6.7'], 'zk_path': '/smartstack/global/test_service', 'checks': [{ 'rise': 1, 'uri': '/http/test_service/1234/status', 'host': '127.0.0.1', 'timeout': 2.0, 'open_timeout': 2.0, 'fall': 2, 'type': 'http', 'port': 6666, 'headers': {}, }], 'host': 'ip_address', 'check_interval': 3.0, 'port': 1234, 'weight': mock.sentinel.weight, 'labels': { 'region:another_region': '', }, }, } def get_current_location(typ): return { 'ecosystem': 'my_ecosystem', 'superregion': 'my_superregion', 'habitat': 'my_habitat', 'region': 'my_region', }[typ] def convert_location_type(src_loc, src_typ, dst_typ): if src_typ == dst_typ: return [src_loc] return { ('my_superregion', 'superregion', 'superregion'): ['my_superregion'], ('another_superregion', 'superregion', 'region'): ['another_region'], ('my_region', 'region', 'superregion'): ['my_superregion'], ('another_region', 'region', 'region'): ['another_region'], ('another_region', 'region', 'superregion'): ['another_superregion'], }[(src_loc, src_typ, dst_typ)] def get_named_zookeeper_topology(cluster_type, cluster_location, zk_topology_dir): return { ('infrastructure', 'my_superregion'): ['1.2.3.4', '2.3.4.5'], ('infrastructure', 'another_superregion'): ['3.4.5.6', '4.5.6.7'] }[(cluster_type, cluster_location)] with contextlib.nested( mock.patch('nerve_tools.configure_nerve.get_current_location', side_effect=get_current_location), mock.patch('nerve_tools.configure_nerve.convert_location_type', side_effect=convert_location_type), mock.patch( 'nerve_tools.configure_nerve.get_named_zookeeper_topology', side_effect=get_named_zookeeper_topology)): mock_service_info = { 'port': 1234, 'routes': [('remote_location', 'local_location')], 'healthcheck_timeout_s': 2.0, 'healthcheck_mode': 'http', 'healthcheck_port': 1234, 'advertise': ['region', 'superregion'], 'extra_advertise': [ ('habitat:my_habitat', 'region:another_region'), ('habitat:your_habitat', 'region:another_region'), # Ignored ], } actual_config = configure_nerve.generate_subconfiguration( service_name='test_service', service_info=mock_service_info, ip_address='ip_address', hacheck_port=6666, weight=mock.sentinel.weight, zk_topology_dir='/fake/path', zk_location_type='superregion', zk_cluster_type='infrastructure', ) assert expected_config == actual_config