Exemplo n.º 1
0
def test_will_report_route_ids():
    config = Config(ParsedConfig('ReportRouteIDs true'))
    assert config.will_report_route_ids
    config = Config(ParsedConfig('ReportRouteIDs false'))
    assert not config.will_report_route_ids
    config = Config(ParsedConfig('ReportRouteIDs false\nRouteIDs'))
    assert not config.will_report_route_ids
    config = Config(ParsedConfig('ReportRouteIDs true\nRouteIDs "808-808"'))
    assert config.will_report_route_ids
    config = Config(ParsedConfig('ReportRouteIDs false\nRouteIDs "808-808"'))
    assert config.will_report_route_ids
Exemplo n.º 2
0
def test_will_report_http_methods():
    config = Config(ParsedConfig('ReportHTTPMethods true'))
    assert config.will_report_http_methods
    config = Config(ParsedConfig('ReportHTTPMethods false'))
    assert not config.will_report_http_methods
    config = Config(ParsedConfig('ReportHTTPMethods false\nHTTPMethods'))
    assert not config.will_report_http_methods
    config = Config(ParsedConfig('ReportHTTPMethods true\nHTTPMethods "GET"'))
    assert config.will_report_http_methods
    config = Config(ParsedConfig('ReportHTTPMethods false\nHTTPMethods "GET"'))
    assert config.will_report_http_methods
Exemplo n.º 3
0
    def new_func(*args, **kwargs):
        c = Config()

        if not c.exists():
            value = prompt('Please enter the base url for your Kong Admin interface')
            c.save(base_url=value)

        if args[0].obj is not {}:
            args[0].obj = {}

        args[0].obj['base_url'] = c.load()['base_url']

        return f(*args, **kwargs)
Exemplo n.º 4
0
def test_driver_create(state, monkeypatch):
    # set some config values
    data = state.config.data.copy()
    data["htcondor_driver"] = dict()
    state.config = Config(data)

    monkeypatch.setattr(HTCondorInterface, "__abstractmethods__", set())
    monkeypatch.setattr(ShellHTCondorInterface, "__init__",
                        Mock(return_value=None))

    sif = ShellHTCondorInterface()
    sif.config = state.config.htcondor_driver

    monkeypatch.setattr("os.path.exists", Mock(return_value=True))
    monkeypatch.setattr("os.path.getsize", Mock(return_value=40 * 1e6))
    warning = Mock()
    monkeypatch.setattr("kong.drivers.htcondor_driver.logger.warning", warning)

    assert warning.call_count == 0

    driver = HTCondorDriver(state.config, sif)

    monkeypatch.setattr("os.path.getsize", Mock(return_value=60 * 1e6))
    driver = HTCondorDriver(state.config, sif)
    warning.assert_called_once()
Exemplo n.º 5
0
def test_will_report_api_names():
    config = Config(ParsedConfig('ReportAPINames true'))
    assert config.will_report_api_names
    assert config.will_report_apis
    config = Config(ParsedConfig('ReportAPINames false'))
    assert not config.will_report_api_names
    assert config.will_report_apis  # default
    config = Config(ParsedConfig('ReportAPINames false\nAPINames'))
    assert not config.will_report_api_names
    assert config.will_report_apis  # default
    config = Config(ParsedConfig('ReportAPINames true\nAPINames "MyAPI"'))
    assert config.will_report_api_names
    assert config.will_report_apis
    config = Config(ParsedConfig('ReportAPINames false\nAPINames "MyAPI"'))
    assert config.will_report_api_names
    assert config.will_report_apis
Exemplo n.º 6
0
def test_will_report_api_ids():
    config = Config(ParsedConfig('ReportAPIIDs true'))
    assert config.will_report_api_ids
    assert config.will_report_apis
    config = Config(ParsedConfig('ReportAPIIDs false'))
    assert not config.will_report_api_ids
    assert config.will_report_apis  # default
    config = Config(ParsedConfig('ReportAPIIDs false\nAPIIDs'))
    assert not config.will_report_api_ids
    assert config.will_report_apis  # default
    config = Config(ParsedConfig('ReportAPIIDs true\nAPIIDs "808-808"'))
    assert config.will_report_api_ids
    assert config.will_report_apis
    config = Config(ParsedConfig('ReportAPIIDs false\nAPIIDs "808-808"'))
    assert config.will_report_api_ids
    assert config.will_report_apis
Exemplo n.º 7
0
 def load_config_and_register_read(self, config):
     self.config = Config(config)
     read_kwargs = {}
     if self.config.interval:
         read_kwargs['interval'] = self.config.interval
     if self.config.name:
         read_kwargs['name'] = self.config.name
     collectd.register_read(self.update_and_report, **read_kwargs)
Exemplo n.º 8
0
def test_will_report_service_names():
    config = Config(ParsedConfig('ReportServiceNames true'))
    assert config.will_report_service_names
    assert config.will_report_services
    config = Config(ParsedConfig('ReportServiceNames false'))
    assert not config.will_report_service_names
    assert config.will_report_services  # default
    config = Config(ParsedConfig('ReportServiceNames false\nServiceNames'))
    assert not config.will_report_service_names
    assert config.will_report_services  # default
    config = Config(
        ParsedConfig('ReportServiceNames true\nServiceNames "MyService"'))
    assert config.will_report_service_names
    assert config.will_report_services
    config = Config(
        ParsedConfig('ReportServiceNames false\nServiceNames "MyService"'))
    assert config.will_report_service_names
    assert config.will_report_services
Exemplo n.º 9
0
def test_will_report_service_ids():
    config = Config(ParsedConfig('ReportServiceIDs true'))
    assert config.will_report_service_ids
    assert config.will_report_services
    config = Config(ParsedConfig('ReportServiceIDs false'))
    assert not config.will_report_service_ids
    assert config.will_report_services  # default
    config = Config(ParsedConfig('ReportServiceIDs false\nServiceIDs'))
    assert not config.will_report_service_ids
    assert config.will_report_services  # default
    config = Config(
        ParsedConfig('ReportServiceIDs true\nServiceIDs "808-808"'))
    assert config.will_report_service_ids
    assert config.will_report_services
    config = Config(
        ParsedConfig('ReportServiceIDs false\nServiceIDs "808-808"'))
    assert config.will_report_service_ids
    assert config.will_report_services
Exemplo n.º 10
0
def test_confirm_metrics_source_extra_dimensions(reporter, metric):
    cfg_str = '''
    ExtraDimension "test_dimension" "test_val"
    ExtraDimension "another_dimension" "another_val"
    '''
    reporter.config = Config(ParsedConfig(cfg_str))
    reporter.update_http_method_scope_groups()
    metrics = reporter.calculate_status_code_scope_metrics(metric)
    assert metrics
    for met in metrics:
        assert met.dimensions['test_dimension'] == 'test_val'
        assert met.dimensions['another_dimension'] == 'another_val'
Exemplo n.º 11
0
def test_https_settings():
    assert config.auth_header == [
        'Authorization', 'Basic YWRtaW46cGFzc3dvcmQ='
    ]
    assert config.verify_certs is True
    assert config.ca_bundle == '/etc/certs'
    assert config.client_cert == '/usr/local/kong/cert.pem'
    assert config.client_cert_key == '/usr/local/kong/cert.key'
    cfg = Config(ParsedConfig('VerifyCerts false'))
    assert cfg.auth_header is None
    assert cfg.verify_certs is False
    assert cfg.ca_bundle is None
    assert cfg.client_cert is None
    assert cfg.client_cert_key is None
Exemplo n.º 12
0
def driver(monkeypatch, state):
    # set some config values
    data = state.config.data.copy()
    data["htcondor_driver"] = dict()
    state.config = Config(data)

    monkeypatch.setattr(HTCondorInterface, "__abstractmethods__", set())
    monkeypatch.setattr(ShellHTCondorInterface, "__init__",
                        Mock(return_value=None))

    sif = ShellHTCondorInterface()
    sif.config = state.config.htcondor_driver

    return HTCondorDriver(state.config, sif)
Exemplo n.º 13
0
def driver(monkeypatch, state):
    # set some config values
    data = state.config.data.copy()
    data["slurm_driver"] = dict(
        account="pseudo_account", node_size=42, default_queue="somequeue"
    )
    state.config = Config(data)

    monkeypatch.setattr(SlurmInterface, "__abstractmethods__", set())
    monkeypatch.setattr(ShellSlurmInterface, "__init__", Mock(return_value=None))

    sif = ShellSlurmInterface()

    return SlurmDriver(state.config, sif)
Exemplo n.º 14
0
def test_slurm_schema_file(app_env):
    app_dir, config_path, tmp_path = app_env
    os.makedirs(app_dir)

    with open(config_path, "w") as fh:
        fh.write(
            """
slurm_driver:
    sacct_delta: blablurz
        """.strip()
        )

    with pytest.raises(SchemaError):
        Config()

    with open(config_path, "w") as fh:
        fh.write(
            """
slurm_driver:
    sacct_delta: 10 weeks
        """.strip()
        )

    cfg = Config()
    assert cfg.data["slurm_driver"]["sacct_delta"] == timedelta(weeks=10)

    with open(config_path, "w") as fh:
        fh.write(
            """
slurm_driver:
    sacct_delta: 50 weeks
        """.strip()
        )

    cfg = Config()
    print(cfg.data)
    assert cfg.data["slurm_driver"]["sacct_delta"] == timedelta(weeks=50)
Exemplo n.º 15
0
def test_default_descriptor_attributes_sanity():
    config = Config()
    for attr, value in descriptors.values():
        if 'whitelist' in attr or 'blacklist' in attr:
            pattern_list = getattr(config, attr)
            assert isinstance(pattern_list, PatternList)
        else:
            assert getattr(config, attr) == value
            if 'report' not in attr or attr in ('report_status_code_groups', ):
                continue
            whitelist = getattr(
                config, '{0}_whitelist'.format(attr.split('report_')[1]))
            if value:
                assert whitelist.elements == ['*']
            else:
                assert whitelist.elements == []
Exemplo n.º 16
0
    def new_func(*args, **kwargs):
        c = Config()

        if not c.exists():
            value = prompt(
                'Please enter the base url for your Kong Admin interface')
            c.save(base_url=value)

        if args[0].obj is not {}:
            args[0].obj = {}

        args[0].obj['base_url'] = c.load()['base_url']

        return f(*args, **kwargs)
Exemplo n.º 17
0
def test_will_report_status_codes():
    with pytest.raises(Exception) as e:
        config = Config(
            ParsedConfig(
                'ReportStatusCodes true\nReportStatusCodeGroups true'))
    assert 'Cannot simultaneously' in str(e)
    config = Config(
        ParsedConfig('ReportStatusCodes true\nReportStatusCodeGroups false'))
    assert config.will_report_status_codes
    config = Config(
        ParsedConfig('ReportStatusCodes false\nReportStatusCodeGroups true'))
    assert config.will_report_status_codes
    config = Config(
        ParsedConfig('ReportStatusCodes false\nReportStatusCodeGroups false'))
    assert not config.will_report_status_codes
    with pytest.raises(Exception) as e:
        config = Config(
            ParsedConfig(
                'ReportStatusCodes true\nReportStatusCodeGroups true\n'
                'ReportStatusCodes 200'))
    assert 'Cannot simultaneously' in str(e)
    config = Config(
        ParsedConfig(
            'ReportStatusCodes true\nReportStatusCodeGroups false\nStatusCodes 200'
        ))
    assert config.will_report_status_codes
    assert config.status_codes_whitelist.elements == ['200']
    config = Config(
        ParsedConfig(
            'ReportStatusCodes false\nReportStatusCodeGroups true\nStatusCodes 200'
        ))
    assert config.will_report_status_codes
    config = Config(
        ParsedConfig(
            'ReportStatusCodes false\nReportStatusCodeGroups false\nStatusCodes 200'
        ))
    assert config.will_report_status_codes
Exemplo n.º 18
0
def test_attribute_access(state):
    config = Config({"exist": 42})
    assert config.exist == 42

    with pytest.raises(AttributeError):
        config.noexist
Exemplo n.º 19
0
def test_default_metric_attributes_sanity():
    config = Config()
    for attr, info in metrics.items():
        assert getattr(config, attr) == info[2]
Exemplo n.º 20
0
def plugin_config(resource_types=None,
                  report_id=False,
                  report_name=False,
                  id_whitelist=None,
                  id_blacklist=None,
                  name_whitelist=None,
                  name_blacklist=None,
                  report_api_id=False,
                  api_id_whitelist=None,
                  api_id_blacklist=None,
                  report_api_name=False,
                  api_name_whitelist=None,
                  api_name_blacklist=None,
                  report_service_id=False,
                  service_id_whitelist=None,
                  service_id_blacklist=None,
                  report_service_name=False,
                  service_name_whitelist=None,
                  service_name_blacklist=None,
                  report_route_id=False,
                  route_id_whitelist=None,
                  route_id_blacklist=None,
                  report_http_method=False,
                  http_whitelist=None,
                  http_blacklist=None,
                  report_status_code=False,
                  report_status_code_group=False,
                  status_code_whitelist=None,
                  status_code_blacklist=None):
    def bool_to_str(b):
        return str(b).lower()

    def format_pattern_list(pattern_list):
        return ' '.join('"{0}"'.format(item) for item in pattern_list)

    if not isinstance(resource_types, list):
        resource_types = [resource_types]

    config_str = ''
    if resource_types[0]:
        for resource_type in resource_types:
            formatted_type = dict(api='API', service='Service')[resource_type]
            config_str += 'Report{0}IDs {1}\nReport{0}Names {2}\n'.format(
                formatted_type, bool_to_str(report_id),
                bool_to_str(report_name))
            if id_whitelist:
                config_str += '\n{0}IDs {1}'.format(
                    formatted_type, format_pattern_list(id_whitelist))
            if id_blacklist:
                config_str += '\n{0}IDsBlacklist {1}'.format(
                    formatted_type, format_pattern_list(id_blacklist))
            if name_whitelist:
                config_str += '\n{0}Names {1}'.format(
                    formatted_type, format_pattern_list(name_whitelist))
            if name_blacklist:
                config_str += '\n{0}NamesBlacklist {1}'.format(
                    formatted_type, format_pattern_list(name_blacklist))
    else:
        config_str += '\nReportAPIIDs {0}'.format(bool_to_str(report_api_id))
        if api_id_whitelist:
            config_str += '\nAPIIDs {0}'.format(
                format_pattern_list(api_id_whitelist))
        if api_id_blacklist:
            config_str += '\nAPIIDsBlacklist {0}'.format(
                format_pattern_list(api_id_blacklist))
        config_str += '\nReportAPINames {0}'.format(
            bool_to_str(report_api_name))
        if api_name_whitelist:
            config_str += '\nAPINames {0}'.format(
                format_pattern_list(api_name_whitelist))
        if api_name_blacklist:
            config_str += '\nAPINamesBlacklist {0}'.format(
                format_pattern_list(api_name_blacklist))
        config_str += '\nReportServiceIDs {0}'.format(
            bool_to_str(report_service_id))
        if service_id_whitelist:
            config_str += '\nServiceIDs {0}'.format(
                format_pattern_list(service_id_whitelist))
        if service_id_blacklist:
            config_str += '\nServiceIDsBlacklist {0}'.format(
                format_pattern_list(service_id_blacklist))
        config_str += '\nReportServiceNames {0}'.format(
            bool_to_str(report_service_name))
        if service_name_whitelist:
            config_str += '\nServiceNames {0}'.format(
                format_pattern_list(service_name_whitelist))
        if service_name_blacklist:
            config_str += '\nServiceNamesBlacklist {0}'.format(
                format_pattern_list(service_name_blacklist))

    config_str += '\nReportRouteIDs {0}'.format(bool_to_str(report_route_id))
    if route_id_whitelist:
        config_str += '\nRouteIDs {0}'.format(
            format_pattern_list(route_id_whitelist))
    if route_id_blacklist:
        config_str += '\nRouteIDsBlacklist {0}'.format(
            format_pattern_list(route_id_blacklist))

    config_str += '\nReportHTTPMethods {0}'.format(
        bool_to_str(report_http_method))
    if http_whitelist:
        config_str += '\nHTTPMethods {0}'.format(
            format_pattern_list(http_whitelist))
    if http_blacklist:
        config_str += '\nHTTPMethodsBlacklist {0}'.format(
            format_pattern_list(http_blacklist))

    config_str += '\nReportStatusCodes {0}'.format(
        bool_to_str(report_status_code))
    config_str += '\nReportStatusCodeGroups {0}'.format(
        bool_to_str(report_status_code_group))
    if status_code_whitelist:
        config_str += '\nStatusCodes {0}'.format(
            format_pattern_list(status_code_whitelist))
    if status_code_blacklist:
        config_str += '\nStatusCodesBlacklist {0}'.format(
            format_pattern_list(status_code_blacklist))

    return Config(utils.ParsedConfig(config_str))
Exemplo n.º 21
0
def test_missing_host():
    config = Config()
    assert config.host is None
Exemplo n.º 22
0
ServiceNamesBlacklist "MyOtherService"
ReportServiceIDs false
ServiceIDs "808-808"
ServiceIDs "101-101"
ServiceIDsBlacklist "102-102"
ServiceIDsBlacklist "202-202"
ReportRouteIDs false
RouteIDs "808-808"
RouteIDs "101-101"
RouteIDsBlacklist "102-102"
RouteIDsBlacklist "202-202"
Verbose true
ExtraDimension "some_dimension" "some_val"
"""
parsed_config = ParsedConfig(config_string)
config = Config(parsed_config)


def test_metric_attributes():
    assert config.total_requests is True
    assert config.request_size is True
    assert config.request_latency is True
    assert config.kong_latency is True
    assert config.upstream_latency is True
    assert config.response_size is False
    assert config.response_count is False


def test_boolean_report_attributes():
    assert config.interval == 10
    assert config.report_http_methods is False
Exemplo n.º 23
0
def test_config_creation(state):
    config = Config({})