def test_profile_error(): instance = common.generate_instance_config([]) instance['profile'] = 'profile1' with pytest.raises(ConfigurationError): SnmpCheck('snmp', {}, [instance]) init_config = { 'profiles': { 'profile1': { 'definition_file': 'doesntexistfile' } } } with pytest.raises(ConfigurationError): SnmpCheck('snmp', init_config, [instance]) with temp_dir() as tmp: profile_file = os.path.join(tmp, 'profile1.yaml') with open(profile_file, 'w') as f: f.write("not yaml: {") init_config = { 'profiles': { 'profile1': { 'definition_file': profile_file } } } with pytest.raises(ConfigurationError): SnmpCheck('snmp', init_config, [instance])
def test_profile_by_file(aggregator): instance = common.generate_instance_config([]) instance['profile'] = 'profile1' with temp_dir() as tmp: profile_file = os.path.join(tmp, 'profile1.yaml') with open(profile_file, 'w') as f: f.write(yaml.safe_dump({'metrics': common.SUPPORTED_METRIC_TYPES})) init_config = { 'profiles': { 'profile1': { 'definition_file': profile_file } } } check = SnmpCheck('snmp', init_config, [instance]) check.check(instance) common_tags = common.CHECK_TAGS + ['snmp_profile:profile1'] for metric in common.SUPPORTED_METRIC_TYPES: metric_name = "snmp." + metric['name'] aggregator.assert_metric(metric_name, tags=common_tags, count=1) aggregator.assert_metric('snmp.sysUpTimeInstance', count=1) common.assert_common_metrics(aggregator) aggregator.assert_all_metrics_covered()
def setup_gunicorn(request): with temp_dir() as tmpdir: app_dir, venv_dir = create_dirs(tmpdir) create_venv(venv_dir) venv_bin_path = get_venv_bin_path(venv_dir) install_pip_packages(venv_bin_path) conf_file = os.path.join(tmpdir, 'conf.py') copy_config_files(conf_file, app_dir) gunicorn_bin_path = os.path.join(venv_bin_path, 'gunicorn') proc = start_gunicorn(gunicorn_bin_path, conf_file) def fin(): proc.terminate() request.addfinalizer(fin) time.sleep(15) yield {'gunicorn_bin_path': gunicorn_bin_path}
def test_profile_override(): profile = { 'metrics': [{'MIB': 'TCP-MIB', 'symbol': 'tcpPassiveOpens', 'forced_type': 'monotonic_count'}], } with temp_dir() as tmp: with mock_profiles_confd_root(tmp): profile_file = os.path.join(tmp, 'generic-router.yaml') with open(profile_file, 'w') as f: f.write(yaml.safe_dump(profile)) profiles = _load_default_profiles() assert profiles['generic-router'] == {'definition': profile}
def setup_rabbitmq(): with temp_dir() as tmpdir: url = 'http://{}:{}/cli/rabbitmqadmin'.format(HOST, PORT) res = requests.get(url) res.raise_for_status() rabbitmq_admin_script = os.path.join(tmpdir, 'rabbitmqadmin') with open(rabbitmq_admin_script, 'w+') as f: f.write(res.text) setup_vhosts(rabbitmq_admin_script) setup_more(rabbitmq_admin_script) setup_more_with_vhosts(rabbitmq_admin_script) yield
def openldap_server(): with temp_dir() as d: host_socket_path = os.path.join(d, "ldapi") os.chmod(d, 0o777) create_file(host_socket_path) os.chmod(host_socket_path, 0o777) with docker_run( compose_file=os.path.join(HERE, "compose", "docker-compose.yaml"), env_vars={"HOST_SOCKET_DIR": d}, log_patterns="slapd starting", ): yield host_socket_path
def setup_rabbitmq(): with temp_dir() as tmpdir: url = 'http://{}:{}/cli/rabbitmqadmin'.format(HOST, PORT) res = requests.get(url) res.raise_for_status() rabbitmq_admin_script = os.path.join(tmpdir, 'rabbitmqadmin') with open(rabbitmq_admin_script, 'w+') as f: f.write(res.text) setup_vhosts(rabbitmq_admin_script) setup_more(rabbitmq_admin_script) setup_more_with_vhosts(rabbitmq_admin_script) # Set cluster name url = "http://{}:{}/api/cluster-name".format(HOST, PORT) res = requests.put( url, data='{"name": "rabbitmqtest"}', auth=("guest", "guest"), headers={"Content-Type": "application/json"} ) res.raise_for_status()
def test__get_ca_certs_paths(embedded_dir): with mock.patch('datadog_checks.http_check.utils.os.path.dirname') as dirname: # create a tmp `embedded` folder with temp_dir() as tmp: target = os.path.join(tmp, embedded_dir) os.mkdir(target) # point `dirname()` there dirname.return_value = target # tornado not found paths = _get_ca_certs_paths() assert len(paths) == 2 assert paths[0].startswith(target) assert paths[1] == '/etc/ssl/certs/ca-certificates.crt' # mock tornado's presence sys.modules['tornado'] = mock.MagicMock(__file__='.') paths = _get_ca_certs_paths() assert len(paths) == 3 assert paths[1].endswith('ca-certificates.crt') assert paths[2] == '/etc/ssl/certs/ca-certificates.crt'
def test_profile_extends(): # type: () -> None base = { 'metrics': [ {'MIB': 'TCP-MIB', 'symbol': 'tcpActiveOpens', 'forced_type': 'monotonic_count'}, {'MIB': 'UDP-MIB', 'symbol': 'udpHCInDatagrams', 'forced_type': 'monotonic_count'}, ], 'metric_tags': [{'MIB': 'SNMPv2-MIB', 'symbol': 'sysName', 'tag': 'snmp_host'}], } profile1 = { 'extends': ['base.yaml'], 'metrics': [{'MIB': 'TCP-MIB', 'symbol': 'tcpPassiveOpens', 'forced_type': 'monotonic_count'}], } with temp_dir() as tmp: with mock_profiles_confd_root(tmp): with open(os.path.join(tmp, 'base.yaml'), 'w') as f: f.write(yaml.safe_dump(base)) with open(os.path.join(tmp, 'profile1.yaml'), 'w') as f: f.write(yaml.safe_dump(profile1)) definition = {'extends': ['profile1.yaml']} recursively_expand_base_profiles(definition) assert definition == { 'extends': ['profile1.yaml'], 'metrics': [ {'MIB': 'TCP-MIB', 'symbol': 'tcpActiveOpens', 'forced_type': 'monotonic_count'}, {'MIB': 'UDP-MIB', 'symbol': 'udpHCInDatagrams', 'forced_type': 'monotonic_count'}, {'MIB': 'TCP-MIB', 'symbol': 'tcpPassiveOpens', 'forced_type': 'monotonic_count'}, ], 'metric_tags': [{'MIB': 'SNMPv2-MIB', 'symbol': 'sysName', 'tag': 'snmp_host'}], }