Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix version detection for Postgres v10+ #2208

Merged
merged 4 commits into from
Sep 11, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions postgres/datadog_checks/postgres/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
import re
from contextlib import closing

import pg8000
from six.moves import zip_longest
try:
import psycopg2
except ImportError:
psycopg2 = None
import pg8000

from datadog_checks.checks import AgentCheck
from datadog_checks.errors import CheckException
Expand Down Expand Up @@ -418,9 +419,15 @@ def _get_version(self, key, db):
def _is_above(self, key, db, version_to_compare):
version = self._get_version(key, db)
if type(version) == list:
return version >= version_to_compare
# iterate from major down to bugfix
for v, vc in zip_longest(version, version_to_compare, fillvalue=0):
if v == vc:
continue

return False
return v > vc

# return True if version is the same
return True

def _is_8_3_or_above(self, key, db):
return self._is_above(key, db, [8, 3, 0])
Expand Down Expand Up @@ -569,8 +576,9 @@ def _get_archiver_metrics(self, key, db):
}

def _get_replication_metrics(self, key, db):
""" Use either REPLICATION_METRICS_9_1 or REPLICATION_METRICS_9_1 + REPLICATION_METRICS_9_2
depending on the postgres version.
""" Use either REPLICATION_METRICS_10, REPLICATION_METRICS_9_1, or
REPLICATION_METRICS_9_1 + REPLICATION_METRICS_9_2, depending on the
postgres version.
Uses a dictionnary to save the result for each instance
"""
metrics = self.replication_metrics.get(key)
Expand Down
21 changes: 15 additions & 6 deletions postgres/tests/test_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,17 +111,20 @@ def test_is_above(check):
"""
db = MagicMock()

# Test larger major versions
# Test major versions
db.cursor().fetchone.return_value = ['10.5.4']
assert check._is_above('larger major', db, [9, 5, 4])
assert check._is_above('smaller major', db, [9, 5, 4])
assert check._is_above('larger major', db, [11, 0, 0]) is False

# Test minor version larger
# Test minor versions
db.cursor().fetchone.return_value = ['10.5.4']
assert check._is_above('larger_minor', db, [9, 8, 4])
assert check._is_above('smaller minor', db, [10, 4, 4])
assert check._is_above('larger minor', db, [10, 6, 4]) is False

# Test patch version larger
# Test patch versions
db.cursor().fetchone.return_value = ['10.5.4']
assert check._is_above('larger_patch', db, [9, 5, 8])
assert check._is_above('smaller patch', db, [10, 5, 3])
assert check._is_above('larger patch', db, [10, 5, 5]) is False

# Test same version, _is_above() returns True for greater than or equal to
db.cursor().fetchone.return_value = ['10.5.4']
Expand All @@ -135,6 +138,12 @@ def test_is_above(check):
db.cursor().fetchone.return_value = ['11.0.0']
assert check._is_above('official_release', db, [11, -1, 3])

# Test versions of unequal length
db.cursor().fetchone.return_value = ['10.0']
assert check._is_above('unequal_length', db, [10, 0])
assert check._is_above('unequal_length', db, [10, 0, 0])
assert check._is_above('unequal_length', db, [10, 0, 1]) is False


def test_malformed_get_custom_queries(check):
"""
Expand Down