Skip to content

Commit

Permalink
Support Python 3
Browse files Browse the repository at this point in the history
  • Loading branch information
ofek committed Nov 22, 2018
1 parent c3fe75d commit 2003f92
Show file tree
Hide file tree
Showing 10 changed files with 194 additions and 255 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ jobs:
- stage: test
env: CHECK=mongo PYTHON3=true
- stage: test
env: CHECK=mysql
env: CHECK=mysql PYTHON3=true
- stage: test
env: CHECK=nagios
- stage: test
Expand Down
144 changes: 71 additions & 73 deletions mysql/datadog_checks/mysql/mysql.py

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion mysql/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def get_requirements(fpath):
return f.readlines()


CHECKS_BASE_REQ = 'datadog_checks_base'
CHECKS_BASE_REQ = 'datadog-checks-base>=4.2.0'

setup(
name='datadog-mysql',
Expand Down
7 changes: 3 additions & 4 deletions mysql/tests/common.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
# (C) Datadog, Inc. 2010-2017
# (C) Datadog, Inc. 2018
# All rights reserved
# Licensed under Simplified BSD License (see LICENSE)

# Licensed under a 3-clause BSD style license (see LICENSE)
import os

from datadog_checks.utils.common import get_docker_hostname
from datadog_checks.dev import get_docker_hostname

HERE = os.path.dirname(os.path.abspath(__file__))
ROOT = os.path.dirname(os.path.dirname(HERE))
Expand Down
48 changes: 0 additions & 48 deletions mysql/tests/common_config.py

This file was deleted.

153 changes: 81 additions & 72 deletions mysql/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,87 +1,96 @@
# (C) Datadog, Inc. 2010-2017
# (C) Datadog, Inc. 2018
# All rights reserved
# Licensed under Simplified BSD License (see LICENSE)
import subprocess
import time
# Licensed under a 3-clause BSD style license (see LICENSE)
import os
import sys

import pytest
import pymysql
import pytest

from . import common
from datadog_checks.dev import WaitFor, docker_run
from . import common, tags


MYSQL_FLAVOR = os.getenv('MYSQL_FLAVOR', 'mysql')
COMPOSE_FILE = '{}.yaml'.format(MYSQL_FLAVOR)


@pytest.fixture(scope="session")
def spin_up_mysql():
"""
Start a cluster with one master, one replica and one unhealthy replica and
stop it after the tests are done.
If there's any problem executing docker-compose, let the exception bubble
up.
"""
env = os.environ
env['MYSQL_DOCKER_REPO'] = _mysql_docker_repo()
env['MYSQL_PORT'] = str(common.PORT)
env['MYSQL_SLAVE_PORT'] = str(common.SLAVE_PORT)
env['WAIT_FOR_IT_SCRIPT_PATH'] = _wait_for_it_script()

args = [
"docker-compose",
"-f", os.path.join(common.HERE, 'compose', COMPOSE_FILE)
]

subprocess.check_call(args + ["up", "-d"], env=env)

# wait for the master and setup the database
started = False
for _ in xrange(15):
try:
passw = common.MARIA_ROOT_PASS if MYSQL_FLAVOR == 'mariadb' else ''
conn = pymysql.connect(host=common.HOST, port=common.PORT, user='root', password=passw)
_setup_master(conn)
sys.stderr.write("Master connected!\n")
started = True
break
except Exception as e:
sys.stderr.write("Exception starting master: {}\n".format(e))
time.sleep(2)

if not started:
subprocess.check_call(args + ["logs", "mysql-master"], env=env)
subprocess.check_call(args + ["down"], env=env)
raise Exception("Timeout starting master")

# wait for the slave
started = False
for _ in xrange(60):
try:
pymysql.connect(host=common.HOST, port=common.SLAVE_PORT, user=common.USER, passwd=common.PASS)
sys.stderr.write("Slave connected!\n")
started = True
break
except Exception as e:
sys.stderr.write("Exception starting slave: {}\n".format(e))
time.sleep(2)

if not started:
subprocess.check_call(args + ["logs", "mysql-slave"], env=env)
subprocess.check_call(args + ["down"], env=env)
raise Exception("Timeout starting slave")

yield
subprocess.check_call(args + ["down"], env=env)
@pytest.fixture(scope='session')
def dd_environment(instance_basic):
with docker_run(
os.path.join(common.HERE, 'compose', COMPOSE_FILE),
env_vars={
'MYSQL_DOCKER_REPO': _mysql_docker_repo(),
'MYSQL_PORT': str(common.PORT),
'MYSQL_SLAVE_PORT': str(common.SLAVE_PORT),
'WAIT_FOR_IT_SCRIPT_PATH': _wait_for_it_script(),
},
conditions=[
WaitFor(connect_master, wait=2),
WaitFor(connect_slave, wait=2),
],
):
yield instance_basic


@pytest.fixture(scope='session')
def instance_basic():
return {
'server': common.HOST,
'user': common.USER,
'pass': common.PASS,
'port': common.PORT,
}


@pytest.fixture
def aggregator():
from datadog_checks.stubs import aggregator
aggregator.reset()
return aggregator
def instance_complex():
return {
'server': common.HOST,
'user': common.USER,
'pass': common.PASS,
'port': common.PORT,
'options': {
'replication': True,
'extra_status_metrics': True,
'extra_innodb_metrics': True,
'extra_performance_metrics': True,
'schema_size_metrics': True,
},
'tags': tags.METRIC_TAGS,
'queries': [
{
'query': "SELECT * from testdb.users where name='Alice' limit 1;",
'metric': 'alice.age',
'type': 'gauge',
'field': 'age'
},
{
'query': "SELECT * from testdb.users where name='Bob' limit 1;",
'metric': 'bob.age',
'type': 'gauge',
'field': 'age'
},
],
}


@pytest.fixture(scope='session')
def instance_error():
return {
'server': common.HOST,
'user': 'unknown',
'pass': common.PASS,
}


def connect_master():
passw = common.MARIA_ROOT_PASS if MYSQL_FLAVOR == 'mariadb' else ''
conn = pymysql.connect(host=common.HOST, port=common.PORT, user='root', password=passw)
_setup_master(conn)


def connect_slave():
pymysql.connect(host=common.HOST, port=common.SLAVE_PORT, user=common.USER, passwd=common.PASS)


def _setup_master(conn):
Expand All @@ -103,8 +112,8 @@ def _wait_for_it_script():
FIXME: relying on the filesystem layout is a bad idea, the testing helper
should expose its path through the api instead
"""
dir = os.path.join(common.TESTS_HELPER_DIR, 'scripts', 'wait-for-it.sh')
return os.path.abspath(dir)
script = os.path.join(common.TESTS_HELPER_DIR, 'scripts', 'wait-for-it.sh')
return os.path.abspath(script)


def _mysql_docker_repo():
Expand Down
7 changes: 3 additions & 4 deletions mysql/tests/tags.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# (C) Datadog, Inc. 2010-2017
# (C) Datadog, Inc. 2018
# All rights reserved
# Licensed under Simplified BSD License (see LICENSE)

import common
# Licensed under a 3-clause BSD style license (see LICENSE)
from . import common

METRIC_TAGS = ['tag1', 'tag2']
SC_TAGS = ['server:' + common.HOST, 'port:' + str(common.PORT), 'tag1', 'tag2']
Expand Down
44 changes: 21 additions & 23 deletions mysql/tests/test_mysql.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
# (C) Datadog, Inc. 2010-2017
# (C) Datadog, Inc. 2018
# All rights reserved
# Licensed under Simplified BSD License (see LICENSE)
from os import environ
import logging
# Licensed under a 3-clause BSD style license (see LICENSE)
import copy
import subprocess
from os import environ

import mock
import pytest
import psutil
from datadog_checks.mysql import MySql
from datadog_checks.utils.platform import Platform

from . import common, variables, tags, common_config
import pytest

log = logging.getLogger('test_mysql')
from datadog_checks.base.utils.platform import Platform
from datadog_checks.mysql import MySql
from . import common, tags, variables


def test_minimal_config(aggregator, spin_up_mysql):
@pytest.mark.usefixtures('dd_environment')
def test_minimal_config(aggregator, instance_basic):
mysql_check = MySql(common.CHECK_NAME, {}, {})
mysql_check.check(common_config.MYSQL_MINIMAL_CONFIG)
mysql_check.check(instance_basic)

# Test service check
aggregator.assert_service_check('mysql.can_connect', status=MySql.OK,
Expand All @@ -33,9 +31,10 @@ def test_minimal_config(aggregator, spin_up_mysql):
aggregator.assert_metric(mname, at_least=0)


def test_complex_config(aggregator, spin_up_mysql):
mysql_check = MySql(common.CHECK_NAME, {}, {}, instances=[common_config.MYSQL_COMPLEX_CONFIG])
mysql_check.check(common_config.MYSQL_COMPLEX_CONFIG)
@pytest.mark.usefixtures('dd_environment')
def test_complex_config(aggregator, instance_complex):
mysql_check = MySql(common.CHECK_NAME, {}, {}, instances=[instance_complex])
mysql_check.check(instance_complex)

# Test service check
aggregator.assert_service_check('mysql.can_connect', status=MySql.OK,
Expand Down Expand Up @@ -108,24 +107,26 @@ def test_complex_config(aggregator, spin_up_mysql):
aggregator.assert_all_metrics_covered()


def test_connection_failure(aggregator, spin_up_mysql):
@pytest.mark.usefixtures('dd_environment')
def test_connection_failure(aggregator, instance_error):
"""
Service check reports connection failure
"""
mysql_check = MySql(common.CHECK_NAME, {}, {}, instances=[common_config.CONNECTION_FAILURE])
mysql_check = MySql(common.CHECK_NAME, {}, {}, instances=[instance_error])

with pytest.raises(Exception):
mysql_check.check(common_config.CONNECTION_FAILURE)
mysql_check.check(instance_error)

aggregator.assert_service_check('mysql.can_connect', status=MySql.CRITICAL,
tags=tags.SC_FAILURE_TAGS, count=1)

aggregator.assert_all_metrics_covered()


def test_complex_config_replica(aggregator, spin_up_mysql):
@pytest.mark.usefixtures('dd_environment')
def test_complex_config_replica(aggregator, instance_complex):
mysql_check = MySql(common.CHECK_NAME, {}, {})
config = copy.deepcopy(common_config.MYSQL_COMPLEX_CONFIG)
config = copy.deepcopy(instance_complex)
config['port'] = common.SLAVE_PORT
mysql_check.check(config)

Expand All @@ -139,9 +140,6 @@ def test_complex_config_replica(aggregator, spin_up_mysql):
aggregator.assert_service_check('mysql.replication.slave_running', status=MySql.OK,
tags=tags.SC_TAGS_REPLICA, at_least=1)

ver = map(lambda x: int(x), mysql_check.mysql_version[mysql_check._get_host_key()])
ver = tuple(ver)

testable_metrics = (variables.STATUS_VARS + variables.VARIABLES_VARS +
variables.INNODB_VARS + variables.BINLOG_VARS +
variables.SYSTEM_METRICS + variables.SCHEMA_VARS +
Expand Down
5 changes: 2 additions & 3 deletions mysql/tests/variables.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# (C) Datadog, Inc. 2010-2017
# (C) Datadog, Inc. 2018
# All rights reserved
# Licensed under Simplified BSD License (see LICENSE)

# Licensed under a 3-clause BSD style license (see LICENSE)
STATUS_VARS = [
# Command Metrics
'mysql.performance.slow_queries',
Expand Down
Loading

0 comments on commit 2003f92

Please sign in to comment.