Example #1
0
def test_new_check_test():
    check_path = os.path.join(CORE_ROOT, 'my_check')

    try:
        run_command(
            [sys.executable, '-m', 'datadog_checks.dev', 'create', '-q', '-l', CORE_ROOT, 'my-check'],
            capture=True,
            check=True
        )
        run_command(
            [sys.executable, '-m', 'pip', 'install', check_path],
            capture=True,
            check=True
        )

        with chdir(check_path):
            with EnvVars(ignore=[TESTING_PLUGIN]):
                run_command([sys.executable, '-m', 'pytest'], capture=True, check=True)

        run_command(
            [sys.executable, '-m', 'pip', 'uninstall', '-y', 'my-check'],
            capture=True,
            check=True
        )
    finally:
        remove_path(check_path)
Example #2
0
def test_new_check_test(integration_type, installable):
    check_path = os.path.join(CORE_ROOT, 'my_check')

    try:
        run_command(
            [
                sys.executable,
                '-m',
                'datadog_checks.dev',
                'create',
                '--type',
                integration_type,
                '--quiet',
                '--location',
                CORE_ROOT,
                'My Check',
            ],
            capture=True,
            check=True,
        )
        if installable:
            run_command([sys.executable, '-m', 'pip', 'install', check_path],
                        capture=True,
                        check=True)

            with chdir(check_path):
                ignored_env_vars = [TESTING_PLUGIN, 'PYTEST_ADDOPTS']
                ignored_env_vars.extend(ev for ev in os.environ
                                        if ev.startswith(E2E_PREFIX))

                with EnvVars(ignore=ignored_env_vars):
                    run_command([sys.executable, '-m', 'pytest'],
                                capture=True,
                                check=True)

            # We only run style checks on the generated integration. Running the entire test suite would result in tox
            # creating Python environments, which would be too slow with little benefits.
            result = run_command([
                sys.executable, '-m', 'datadog_checks.dev', 'test', '-s',
                'my_check'
            ],
                                 capture=True,
                                 check=True)
            # `ddev test` will not fail if the provided check name doesn't correspond to an existing integration.
            # Instead, it will log a message. So we test for that message to verify style checks ran at all.
            assert 'Nothing to test!' not in result.stdout

            result = run_command([
                sys.executable, '-m', 'pip', 'uninstall', '-y',
                'datadog-my-check'
            ],
                                 capture=True,
                                 check=True)
            # `pip uninstall` is idempotent, so it will not fail if `check_package_name` is incorrect (i.e. the package
            # could not be found). Instead, it will log a warning, so we test for that warning to verify the package was
            # successfully uninstalled.
            # See: https://github.com/pypa/pip/issues/3016
            assert 'WARNING: Skipping' not in result.stdout
    finally:
        remove_path(check_path)
def test_new_check_test():
    check_path = os.path.join(CORE_ROOT, 'my_check')

    try:
        run_command(
            [
                sys.executable, '-m', 'datadog_checks.dev', 'create', '-q',
                '-l', CORE_ROOT, 'my-check'
            ],
            capture=True,
            check=True,
        )
        run_command([sys.executable, '-m', 'pip', 'install', check_path],
                    capture=True,
                    check=True)

        with chdir(check_path):
            ignored_env_vars = [TESTING_PLUGIN]
            ignored_env_vars.extend(ev for ev in os.environ
                                    if ev.startswith(E2E_PREFIX))

            with EnvVars(ignore=ignored_env_vars):
                run_command([sys.executable, '-m', 'pytest'],
                            capture=True,
                            check=True)

        run_command(
            [sys.executable, '-m', 'pip', 'uninstall', '-y', 'my-check'],
            capture=True,
            check=True)
    finally:
        remove_path(check_path)
def test_set_default_driver_conf():
    # Docker Agent with ODBCSYSINI env var
    # The only case where we set ODBCSYSINI to the the default odbcinst.ini folder
    with EnvVars({'DOCKER_DD_AGENT': 'true'}, ignore=['ODBCSYSINI']):
        set_default_driver_conf()
        assert os.environ['ODBCSYSINI'].endswith(os.path.join('data', 'driver_config'))

    # `set_default_driver_conf` have no effect on the cases below
    with EnvVars({'ODBCSYSINI': 'ABC', 'DOCKER_DD_AGENT': 'true'}):
        set_default_driver_conf()
        assert os.environ['ODBCSYSINI'] == 'ABC'

    with EnvVars({}, ignore=['ODBCSYSINI']):
        set_default_driver_conf()
        assert 'ODBCSYSINI' not in os.environ

    with EnvVars({'ODBCSYSINI': 'ABC'}):
        set_default_driver_conf()
        assert os.environ['ODBCSYSINI'] == 'ABC'
Example #5
0
    def test_config_kerberos_cache_restores_rollback(self):
        instance = {'auth_type': 'kerberos', 'kerberos_cache': '/test/file'}
        init_config = {}

        http = RequestsWrapper(instance, init_config)

        with EnvVars({'KRB5CCNAME': 'old'}):
            with mock.patch('requests.get', side_effect=lambda *args, **kwargs: os.environ.get('KRB5CCNAME')):
                assert http.get('https://www.google.com') == '/test/file'

            assert os.environ.get('KRB5CCNAME') == 'old'
Example #6
0
    def test_config_kerberos_keytab_file_rollback(self):
        instance = {'kerberos_keytab': '/test/file'}
        init_config = {}

        http = RequestsWrapper(instance, init_config)

        with EnvVars({'KRB5_CLIENT_KTNAME': 'old'}):
            assert os.environ.get('KRB5_CLIENT_KTNAME') == 'old'

            with mock.patch('requests.get', side_effect=lambda *args, **kwargs: os.environ.get('KRB5_CLIENT_KTNAME')):
                assert http.get('https://www.google.com') == '/test/file'

            assert os.environ.get('KRB5_CLIENT_KTNAME') == 'old'
def test_not_eksfargate(dd_run_check, monkeypatch, aggregator):
    with EnvVars(
        {
            KUBELET_NODE_ENV_VAR: 'foo',
        }
    ):
        instance = {}
        check = EksFargateCheck('eks_fargate', {}, [instance])
        monkeypatch.setattr(check, 'retrieve_pod_list', mock.Mock(return_value=json.loads(mock_from_file('pods.json'))))
        dd_run_check(check)
        assert check.NAMESPACE + '.pods.running' not in aggregator._metrics
        assert check.NAMESPACE + '.cpu.capacity' not in aggregator._metrics
        assert check.NAMESPACE + '.memory.capacity' not in aggregator._metrics
def test_eksfargate(dd_run_check, monkeypatch, aggregator):
    with EnvVars(
        {
            KUBELET_NODE_ENV_VAR: 'fargate-foo',
        }
    ):
        instance = {'tags': ['foo:bar']}
        check = EksFargateCheck('eks_fargate', {}, [instance])
        monkeypatch.setattr(check, 'retrieve_pod_list', mock.Mock(return_value=json.loads(mock_from_file('pods.json'))))
        dd_run_check(check)
        aggregator.assert_metric(
            check.NAMESPACE + '.pods.running',
            value=1,
            tags=['virtual_node:fargate-foo', 'foo:bar'],
        )
        aggregator.assert_metric(
            check.NAMESPACE + '.cpu.capacity', value=0.25, tags=['virtual_node:fargate-foo', 'foo:bar']
        )
        aggregator.assert_metric(
            check.NAMESPACE + '.memory.capacity',
            value=536870912.0,
            tags=['virtual_node:fargate-foo', 'foo:bar'],
        )
Example #9
0
def test_not_running_ci():
    with EnvVars(ignore=['SYSTEM_TEAMFOUNDATIONCOLLECTIONURI']):
        with mock.patch.dict(os.environ, {'AGENT_OS': 'Linux'}):
            assert running_on_ci() is False
            assert running_on_linux_ci() is False
def test_get_net_proc_base_location(aggregator, check, proc_location, envs,
                                    expected_net_proc_base_location):
    with EnvVars(envs):
        actual = check._get_net_proc_base_location(proc_location)
        assert expected_net_proc_base_location == actual
Example #11
0
def test_set_up_env_default_true():
    with EnvVars(ignore=[E2E_SET_UP]):
        assert set_up_env() is True
Example #12
0
def test_tear_down_env_false():
    with EnvVars({E2E_TEAR_DOWN: 'false'}):
        assert tear_down_env() is False
Example #13
0
def test_tear_down_env_default_true():
    with EnvVars(ignore=[E2E_TEAR_DOWN]):
        assert tear_down_env() is True
Example #14
0
def test_set_up_env_false():
    with EnvVars({E2E_SET_UP: 'false'}):
        assert set_up_env() is False