Skip to content

Commit

Permalink
Make flake8 happy (#3060)
Browse files Browse the repository at this point in the history
  • Loading branch information
zippolyte authored and nmuesch committed Jan 31, 2019
1 parent e2449e1 commit d62b1e1
Show file tree
Hide file tree
Showing 14 changed files with 137 additions and 172 deletions.
18 changes: 9 additions & 9 deletions ceph/datadog_checks/ceph/ceph.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,15 +246,15 @@ def _extract_metrics(self, raw, tags):
self.log.debug('Error retrieving df_detail metrics')

def _osd_pct_used(self, health):
"""Take a single health check string, return (OSD name, percentage used)"""
# Full string looks like: osd.2 is full at 95%
# Near full string: osd.1 is near full at 94%
pct = re.compile(r'\d+%').findall(health)
osd = re.compile(r'osd.\d+').findall(health)
if len(pct) > 0 and len(osd) > 0:
return osd[0], int(pct[0][:-1])
else:
return None, None
"""Take a single health check string, return (OSD name, percentage used)"""
# Full string looks like: osd.2 is full at 95%
# Near full string: osd.1 is near full at 94%
pct = re.compile(r'\d+%').findall(health)
osd = re.compile(r'osd.\d+').findall(health)
if len(pct) > 0 and len(osd) > 0:
return osd[0], int(pct[0][:-1])
else:
return None, None

def _perform_service_checks(self, raw, tags, health_checks):
if 'status' in raw:
Expand Down
2 changes: 1 addition & 1 deletion cisco_aci/datadog_checks/cisco_aci/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ def get_attributes(obj):
# if the object is not a dict
# it is probably already scoped to attributes
return obj
if key is not "attributes":
if key != "attributes":
attrs = key_obj.get('attributes')
if type(attrs) is not dict:
# if the attributes doesn't exist,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -519,12 +519,14 @@ def _get_hostname(self, hostname, sample, scraper_config):
"""
If hostname is None, look at label_to_hostname setting
"""
if (hostname is None and scraper_config['label_to_hostname'] is not None and
scraper_config['label_to_hostname'] in sample[self.SAMPLE_LABELS]):
hostname = sample[self.SAMPLE_LABELS][scraper_config['label_to_hostname']]
suffix = scraper_config['label_to_hostname_suffix']
if suffix is not None:
hostname += suffix
if (
hostname is None and scraper_config['label_to_hostname'] is not None and
scraper_config['label_to_hostname'] in sample[self.SAMPLE_LABELS]
):
hostname = sample[self.SAMPLE_LABELS][scraper_config['label_to_hostname']]
suffix = scraper_config['label_to_hostname_suffix']
if suffix is not None:
hostname += suffix

return hostname

Expand Down
55 changes: 22 additions & 33 deletions datadog_checks_base/tests/test_pdhbasecheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,16 @@
# Licensed under a 3-clause BSD style license (see LICENSE)

import pytest
from datadog_checks.stubs import aggregator

try:
from datadog_checks.checks.win.winpdh_base import PDHBaseCheck

# for reasons unknown, flake8 says that pdh_mocks_fixture is unused, even though
# it's used below. noqa to suppress that error.
from datadog_test_libs.win.pdh_mocks import pdh_mocks_fixture, initialize_pdh_tests # noqa: F401

except ImportError: # noqa: E722
from datadog_test_libs.win.pdh_mocks import initialize_pdh_tests, pdh_mocks_fixture
except ImportError:
pass

from .utils import requires_windows


@pytest.fixture
def Aggregator():
aggregator.reset()
return aggregator


DEFAULT_INSTANCE = {'host': '.'}

SINGLE_INSTANCE_COUNTER = [["Memory", None, "Available Bytes", "test.system.mem.available", "gauge"]]
Expand All @@ -38,48 +27,48 @@ def Aggregator():
]


@requires_windows # noqa: F811
def test_single_instance_counter(Aggregator, pdh_mocks_fixture):
@requires_windows
def test_single_instance_counter(aggregator, pdh_mocks_fixture): # noqa F811
initialize_pdh_tests()
instance = DEFAULT_INSTANCE
c = PDHBaseCheck("testcheck", {}, {}, [instance], SINGLE_INSTANCE_COUNTER)
c.check(instance)

Aggregator.assert_metric("test.system.mem.available", tags=None, count=1)
aggregator.assert_metric("test.system.mem.available", tags=None, count=1)


@requires_windows # noqa: F811
def test_single_instance_counter_with_instance(Aggregator, pdh_mocks_fixture):
@requires_windows
def test_single_instance_counter_with_instance(aggregator, pdh_mocks_fixture): # noqa F811
initialize_pdh_tests()
instance = DEFAULT_INSTANCE
with pytest.raises(AttributeError):
PDHBaseCheck("testcheck", {}, {}, [instance], INSTANCE_OF_SINGLE_INSTANCE_COUNTER)


@requires_windows # noqa: F811
def test_multi_instance_counter(Aggregator, pdh_mocks_fixture):
@requires_windows
def test_multi_instance_counter(aggregator, pdh_mocks_fixture): # noqa F811
initialize_pdh_tests()
instance = DEFAULT_INSTANCE
c = PDHBaseCheck("testcheck", {}, {}, [instance], MULTI_INSTANCE_COUNTER)
c.check(instance)
for t in ['instance:0', 'instance:1', 'instance:_Total']:
Aggregator.assert_metric("test.processor_time", tags=['%s' % t], count=1)
assert Aggregator.metrics_asserted_pct == 100.0
aggregator.assert_metric("test.processor_time", tags=['%s' % t], count=1)
assert aggregator.metrics_asserted_pct == 100.0


@requires_windows # noqa: F811
def test_multi_instance_counter_specific_instances(Aggregator, pdh_mocks_fixture):
@requires_windows
def test_multi_instance_counter_specific_instances(aggregator, pdh_mocks_fixture): # noqa F811
initialize_pdh_tests()
instance = DEFAULT_INSTANCE
c = PDHBaseCheck("testcheck", {}, {}, [instance], MULTI_INSTANCE_COUNTER_WITH_INSTANCES)
c.check(instance)
for t in ['test.processor_time_0', 'test.processor_time_1']:
Aggregator.assert_metric(t, tags=None, count=1)
assert Aggregator.metrics_asserted_pct == 100.0
aggregator.assert_metric(t, tags=None, count=1)
assert aggregator.metrics_asserted_pct == 100.0


@requires_windows # noqa: F811
def test_returns_partial_metrics(Aggregator, pdh_mocks_fixture):
@requires_windows
def test_returns_partial_metrics(aggregator, pdh_mocks_fixture): # noqa F811
COUNTER_LIST = [
["NTDS", None, "LDAP Client Sessions", "active_directory.ldap.client_sessions", "gauge"],
["NTDS", None, "LDAP Bind Time", "active_directory.ldap.bind_time", "gauge"],
Expand All @@ -94,8 +83,8 @@ def test_returns_partial_metrics(Aggregator, pdh_mocks_fixture):
c = PDHBaseCheck("testcheck", {}, {}, [instance], COUNTER_LIST)
c.check(instance)

Aggregator.assert_metric("active_directory.ldap.client_sessions", tags=None, count=1)
Aggregator.assert_metric("active_directory.ldap.bind_time", tags=None, count=1)
Aggregator.assert_metric("active_directory.ldap.successful_binds_persec", tags=None, count=1)
Aggregator.assert_metric("active_directory.ldap.searches_persec", tags=None, count=1)
assert Aggregator.metrics_asserted_pct == 100.0
aggregator.assert_metric("active_directory.ldap.client_sessions", tags=None, count=1)
aggregator.assert_metric("active_directory.ldap.bind_time", tags=None, count=1)
aggregator.assert_metric("active_directory.ldap.successful_binds_persec", tags=None, count=1)
aggregator.assert_metric("active_directory.ldap.searches_persec", tags=None, count=1)
assert aggregator.metrics_asserted_pct == 100.0
38 changes: 14 additions & 24 deletions datadog_checks_base/tests/test_winpdh.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,13 @@
import pytest
import datadog_checks.stubs.datadog_agent as logger
from collections import defaultdict
from datadog_checks.stubs import aggregator

try:
from datadog_checks.checks.win.winpdh import WinPDHCounter, SINGLE_INSTANCE_KEY

# for reasons unknown, flake8 says that pdh_mocks_fixture is unused, even though
# it's used below. noqa to suppress that error.
from datadog_test_libs.win.pdh_mocks import pdh_mocks_fixture, initialize_pdh_tests # noqa: F401
from datadog_test_libs.win.pdh_mocks import pdh_mocks_fixture_bad_perf_strings # noqa: F401

except: # noqa: E722
from datadog_test_libs.win.pdh_mocks import (
initialize_pdh_tests, pdh_mocks_fixture_bad_perf_strings, pdh_mocks_fixture
)
except ImportError:
import platform

if platform.system() != 'Windows':
Expand All @@ -24,12 +20,6 @@
from .utils import requires_windows


@pytest.fixture
def Aggregator():
aggregator.reset()
return aggregator


'''
WinPDHCounter tests.
Expand All @@ -38,8 +28,8 @@ def Aggregator():
'''


@requires_windows # noqa: F811
def test_winpdhcounter_bad_strings_english(pdh_mocks_fixture_bad_perf_strings):
@requires_windows
def test_winpdhcounter_bad_strings_english(pdh_mocks_fixture_bad_perf_strings): # noqa F811
initialize_pdh_tests()
counter = WinPDHCounter('System', 'Processor Queue Length', logger)

Expand All @@ -48,8 +38,8 @@ def test_winpdhcounter_bad_strings_english(pdh_mocks_fixture_bad_perf_strings):
assert SINGLE_INSTANCE_KEY in vals


@requires_windows # noqa: F811
def test_winpdhcounter_throws_on_bad_input(pdh_mocks_fixture):
@requires_windows
def test_winpdhcounter_throws_on_bad_input(pdh_mocks_fixture): # noqa F811
initialize_pdh_tests()
with pytest.raises(AttributeError):
WinPDHCounter('Ssystem', 'Processor Queue Length', logger)
Expand All @@ -58,8 +48,8 @@ def test_winpdhcounter_throws_on_bad_input(pdh_mocks_fixture):
WinPDHCounter('System', 'PProcessor Queue Length', logger)


@requires_windows # noqa: F811
def test_winpdhcounter_throws_on_bad_input_with_bad_strings(pdh_mocks_fixture_bad_perf_strings):
@requires_windows
def test_winpdhcounter_throws_on_bad_input_with_bad_strings(pdh_mocks_fixture_bad_perf_strings): # noqa F811
initialize_pdh_tests()
with pytest.raises(AttributeError):
WinPDHCounter('Ssystem', 'Processor Queue Length', logger)
Expand All @@ -68,8 +58,8 @@ def test_winpdhcounter_throws_on_bad_input_with_bad_strings(pdh_mocks_fixture_ba
WinPDHCounter('System', 'PProcessor Queue Length', logger)


@requires_windows # noqa: F811
def test_winpdhcounter_bad_strings_not_english(pdh_mocks_fixture_bad_perf_strings):
@requires_windows
def test_winpdhcounter_bad_strings_not_english(pdh_mocks_fixture_bad_perf_strings): # noqa F811
WinPDHCounter._use_en_counter_names = False
WinPDHCounter.pdh_counter_dict = defaultdict(list)

Expand All @@ -83,8 +73,8 @@ def test_winpdhcounter_bad_strings_not_english(pdh_mocks_fixture_bad_perf_string
WinPDHCounter('System', 'Processor Queue Length', logger)


@requires_windows # noqa: F811
def test_winpdhcounter_non_english(pdh_mocks_fixture):
@requires_windows
def test_winpdhcounter_non_english(pdh_mocks_fixture): # noqa F811
WinPDHCounter._use_en_counter_names = False
WinPDHCounter.pdh_counter_dict = defaultdict(list)
initialize_pdh_tests(lang="se-sv")
Expand Down
25 changes: 6 additions & 19 deletions iis/tests/test_iis.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
# (C) Datadog, Inc. 2010-2017
# All rights reserved
# Licensed under Simplified BSD License (see LICENSE)
import pytest
import re

from datadog_checks.iis import IIS
from datadog_checks.iis.iis import DEFAULT_COUNTERS
# for reasons unknown, flake8 says that pdh_mocks_fixture is unused, even though
# it's used below. noqa to suppress that error.
from datadog_test_libs.win.pdh_mocks import pdh_mocks_fixture # noqa: F401
from datadog_test_libs.win.pdh_mocks import pdh_mocks_fixture # noqa F401

from .common import (
CHECK_NAME,
Expand All @@ -20,9 +17,7 @@
)


# flake8 then says this is a redefinition of unused, which it's not.
@pytest.mark.usefixtures("pdh_mocks_fixture") # noqa: F811
def test_basic_check(aggregator, pdh_mocks_fixture):
def test_basic_check(aggregator, pdh_mocks_fixture): # noqa: F811
instance = MINIMAL_INSTANCE
c = IIS(CHECK_NAME, {}, {}, [instance])
c.check(instance)
Expand All @@ -40,9 +35,7 @@ def test_basic_check(aggregator, pdh_mocks_fixture):
aggregator.assert_all_metrics_covered()


# flake8 then says this is a redefinition of unused, which it's not.
@pytest.mark.usefixtures("pdh_mocks_fixture") # noqa: F811
def test_check_on_specific_websites(aggregator, pdh_mocks_fixture):
def test_check_on_specific_websites(aggregator, pdh_mocks_fixture): # noqa: F811
instance = INSTANCE
c = IIS(CHECK_NAME, {}, {}, [instance])
c.check(instance)
Expand All @@ -63,19 +56,15 @@ def test_check_on_specific_websites(aggregator, pdh_mocks_fixture):
aggregator.assert_all_metrics_covered()


# flake8 then says this is a redefinition of unused, which it's not.
@pytest.mark.usefixtures("pdh_mocks_fixture") # noqa: F811
def test_service_check_with_invalid_host(aggregator, pdh_mocks_fixture):
def test_service_check_with_invalid_host(aggregator, pdh_mocks_fixture): # noqa: F811
instance = INVALID_HOST_INSTANCE
c = IIS(CHECK_NAME, {}, {}, [instance])
c.check(instance)

aggregator.assert_service_check('iis.site_up', IIS.CRITICAL, tags=["site:{0}".format('Total')])


# flake8 then says this is a redefinition of unused, which it's not.
@pytest.mark.usefixtures("pdh_mocks_fixture") # noqa: F811
def test_check(aggregator, pdh_mocks_fixture):
def test_check(aggregator, pdh_mocks_fixture): # noqa: F811
"""
Returns the right metrics and service checks
"""
Expand Down Expand Up @@ -106,9 +95,7 @@ def test_check(aggregator, pdh_mocks_fixture):
aggregator.assert_all_metrics_covered()


# flake8 then says this is a redefinition of unused, which it's not.
@pytest.mark.usefixtures("pdh_mocks_fixture") # noqa: F811
def test_check_without_sites_specified(aggregator, pdh_mocks_fixture):
def test_check_without_sites_specified(aggregator, pdh_mocks_fixture): # noqa: F811
"""
Returns the right metrics and service checks for the `_Total` site
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def _get_group_coordinator(self, client, group):
try:
coord_resp = self._make_blocking_req(client, request, node_id=broker.nodeId)
# 0 means that there is no error
if coord_resp and coord_resp.error_code is 0:
if coord_resp and coord_resp.error_code == 0:
client.cluster.add_group_coordinator(group, coord_resp)
coord_id = client.cluster.coordinator_for_group(group)
if coord_id is not None and coord_id >= 0:
Expand Down Expand Up @@ -478,7 +478,7 @@ def _get_consumer_offsets(self, client, consumer_group, topic_partitions, coord_
response = self._make_blocking_req(client, request, node_id=broker_id)
for (topic, partition_offsets) in response.topics:
for partition, offset, _, error_code in partition_offsets:
if error_code is not 0:
if error_code != 0:
continue
consumer_offsets[(topic, partition)] = offset

Expand Down
2 changes: 1 addition & 1 deletion kafka_consumer/tests/test_kafka_consumer_zk.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def test_check_nogroups_zk(aggregator, zk_instance):
kafka_consumer_check.check(nogroup_instance)

for topic in TOPICS:
if topic is not '__consumer_offsets':
if topic != '__consumer_offsets':
for partition in PARTITIONS:
tags = ["topic:{}".format(topic),
"partition:{}".format(partition)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def check(self, instance):
queues = self.DEFAULT_QUEUES + instance.get("extra_queues", [])
for queue in queues:
for metric, func in iteritems(self.QUEUE_METRICS_TRANSFORMERS):
transformers[queue + metric] = func
transformers[queue + metric] = func

self.process(scraper_config, metric_transformers=transformers)

Expand Down
2 changes: 1 addition & 1 deletion kubelet/datadog_checks/kubelet/prometheus.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def _is_container_metric(labels):
if lbl == 'container_name':
if lbl in labels:
if labels[lbl] == '' or labels[lbl] == 'POD':
return False
return False
if lbl not in labels:
return False
return True
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ def _create_kubernetes_state_prometheus_instance(self, instance):
ksm_instance['label_to_hostname_suffix'] = "-" + clustername

if 'labels_mapper' in ksm_instance and not isinstance(ksm_instance['labels_mapper'], dict):
self.log.warning("Option labels_mapper should be a dictionary for {}".format(endpoint))
self.log.warning("Option labels_mapper should be a dictionary for {}".format(endpoint))

return ksm_instance

Expand Down
8 changes: 4 additions & 4 deletions network/tests/test_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,10 @@ def test_cx_state(aggregator, network_check):
@mock.patch('datadog_checks.network.network.Platform.is_solaris', return_value=False)
@mock.patch('datadog_checks.network.network.Platform.is_windows', return_value=True)
def test_win_uses_psutil(is_linux, is_bsd, is_solaris, is_windows, network_check):
with mock.patch.object(network_check, '_check_psutil') as _check_psutil:
network_check.check({})
network_check._check_psutil = mock.MagicMock()
_check_psutil.assert_called_once_with({})
with mock.patch.object(network_check, '_check_psutil') as _check_psutil:
network_check.check({})
network_check._check_psutil = mock.MagicMock()
_check_psutil.assert_called_once_with({})


def test_check_psutil(aggregator, network_check):
Expand Down
Loading

0 comments on commit d62b1e1

Please sign in to comment.