From 397cb44abb238619a1356ead11a5c44e2fb0c522 Mon Sep 17 00:00:00 2001 From: Nicholas Muesch Date: Fri, 14 Dec 2018 17:31:37 -0500 Subject: [PATCH] Support Py3 --- .travis.yml | 2 +- .../tests/compose/docker-compose.yaml | 2 +- cassandra_nodetool/tests/conftest.py | 84 +++++++++---------- cassandra_nodetool/tests/test_integration.py | 2 +- cassandra_nodetool/tests/test_unit.py | 2 +- cassandra_nodetool/tox.ini | 21 +++-- 6 files changed, 55 insertions(+), 58 deletions(-) diff --git a/.travis.yml b/.travis.yml index 6c46a103e8270..e45ff92a78f5e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -81,7 +81,7 @@ jobs: - stage: test env: CHECK=cacti PYTHON3=true - stage: test - env: CHECK=cassandra_nodetool + env: CHECK=cassandra_nodetool PYTHON3=true - stage: test env: CHECK=ceph - stage: test diff --git a/cassandra_nodetool/tests/compose/docker-compose.yaml b/cassandra_nodetool/tests/compose/docker-compose.yaml index f50cc04f2512a..17c96245a61e4 100644 --- a/cassandra_nodetool/tests/compose/docker-compose.yaml +++ b/cassandra_nodetool/tests/compose/docker-compose.yaml @@ -19,7 +19,7 @@ services: expose: - ${CONTAINER_PORT} volumes: - - ./jmxremote.password:/etc/cassandra/jmxremote.password + - ${JMX_PASS_FILE}:/etc/cassandra/jmxremote.password dd-test-cassandra2: image: cassandra:${CASSANDRA_VERSION} diff --git a/cassandra_nodetool/tests/conftest.py b/cassandra_nodetool/tests/conftest.py index 73ecd779bfd96..62f91c4b5fbee 100644 --- a/cassandra_nodetool/tests/conftest.py +++ b/cassandra_nodetool/tests/conftest.py @@ -9,7 +9,9 @@ import pytest import time -import common +from . import common +from datadog_checks.dev import TempDir +from datadog_checks.dev.utils import copy_path log = logging.getLogger(__file__) @@ -23,7 +25,7 @@ def wait_on_docker_logs(container_name, max_wait, sentences): log.info("Waiting for {} to come up".format(container_name)) for _ in range(max_wait): out = subprocess.check_output(args) - if any(s in out for s in sentences): + if any(str.encode(s) in out for s in sentences): log.info('{} is up!'.format(container_name)) return True time.sleep(1) @@ -53,46 +55,42 @@ def cassandra_cluster(): env['CONTAINER_PORT'] = common.PORT # We need to restrict permission on the password file - os.chmod(os.path.join(common.HERE, 'compose', 'jmxremote.password'), stat.S_IRUSR) - - docker_compose_args = [ - "docker-compose", - "-f", os.path.join(common.HERE, 'compose', 'docker-compose.yaml') - ] - subprocess.check_call(docker_compose_args + ["up", "-d", common.CASSANDRA_CONTAINER_NAME]) - - # wait for the cluster to be up before yielding - if not wait_on_docker_logs( - common.CASSANDRA_CONTAINER_NAME, - 20, - ['Listening for thrift clients', "Created default superuser role 'cassandra'"] - ): - raise Exception("Cassandra cluster dd-test-cassandra boot timed out!") - - cassandra_seed = get_container_ip("{}".format(common.CASSANDRA_CONTAINER_NAME)) - env['CASSANDRA_SEEDS'] = cassandra_seed - subprocess.check_call(docker_compose_args + ["up", "-d", common.CASSANDRA_CONTAINER_NAME_2]) - - if not wait_on_docker_logs( - common.CASSANDRA_CONTAINER_NAME_2, - 50, - ['Listening for thrift clients', 'Not starting RPC server as requested'] - ): - raise Exception("Cassandra cluster {} boot timed out!".format(common.CASSANDRA_CONTAINER_NAME_2)) - - subprocess.check_call([ - "docker", - "exec", common.CASSANDRA_CONTAINER_NAME, - "cqlsh", - "-e", "CREATE KEYSPACE test WITH REPLICATION={'class':'SimpleStrategy', 'replication_factor':2}" - ]) - yield + # Create a temporary file so if we have to run tests more than once on a machine + # the original file's perms aren't modified + with TempDir() as tmpdir: + jmx_pass_file = os.path.join(common.HERE, "compose", 'jmxremote.password') + copy_path(jmx_pass_file, tmpdir) + temp_jmx_file = os.path.join(tmpdir, 'jmxremote.password') + env['JMX_PASS_FILE'] = temp_jmx_file + os.chmod(temp_jmx_file, stat.S_IRWXU) + docker_compose_args = [ + "docker-compose", + "-f", os.path.join(common.HERE, 'compose', 'docker-compose.yaml') + ] + subprocess.check_call(docker_compose_args + ["up", "-d", common.CASSANDRA_CONTAINER_NAME]) + # wait for the cluster to be up before yielding + if not wait_on_docker_logs( + common.CASSANDRA_CONTAINER_NAME, + 20, + ['Listening for thrift clients', "Created default superuser role 'cassandra'"] + ): + raise Exception("Cassandra cluster dd-test-cassandra boot timed out!") + cassandra_seed = get_container_ip("{}".format(common.CASSANDRA_CONTAINER_NAME)) + env['CASSANDRA_SEEDS'] = cassandra_seed.decode('utf-8') + subprocess.check_call(docker_compose_args + ["up", "-d", common.CASSANDRA_CONTAINER_NAME_2]) + if not wait_on_docker_logs( + common.CASSANDRA_CONTAINER_NAME_2, + 50, + ['Listening for thrift clients', 'Not starting RPC server as requested'] + ): + raise Exception("Cassandra cluster {} boot timed out!".format(common.CASSANDRA_CONTAINER_NAME_2)) + subprocess.check_call([ + "docker", + "exec", common.CASSANDRA_CONTAINER_NAME, + "cqlsh", + "-e", + "CREATE KEYSPACE IF NOT EXISTS test WITH REPLICATION={'class':'SimpleStrategy', 'replication_factor':2}" + ]) + yield subprocess.check_call(docker_compose_args + ["down"]) - - -@pytest.fixture -def aggregator(): - from datadog_checks.stubs import aggregator - aggregator.reset() - return aggregator diff --git a/cassandra_nodetool/tests/test_integration.py b/cassandra_nodetool/tests/test_integration.py index 7de588d09fc35..2a56277c6200b 100644 --- a/cassandra_nodetool/tests/test_integration.py +++ b/cassandra_nodetool/tests/test_integration.py @@ -4,7 +4,7 @@ import pytest -import common +from . import common from datadog_checks.cassandra_nodetool import CassandraNodetoolCheck diff --git a/cassandra_nodetool/tests/test_unit.py b/cassandra_nodetool/tests/test_unit.py index abc0d72631884..3b1c3feb6e2b2 100644 --- a/cassandra_nodetool/tests/test_unit.py +++ b/cassandra_nodetool/tests/test_unit.py @@ -4,7 +4,7 @@ from mock import patch from os import path -import common +from . import common from datadog_checks.cassandra_nodetool import CassandraNodetoolCheck diff --git a/cassandra_nodetool/tox.ini b/cassandra_nodetool/tox.ini index 6c9db5d702a89..5e74000e5916b 100644 --- a/cassandra_nodetool/tox.ini +++ b/cassandra_nodetool/tox.ini @@ -2,37 +2,36 @@ minversion = 2.0 basepython = py27 envlist = + {py27,py36} unit - integration flake8 [testenv] usedevelop = true platform = linux|darwin|win32 - -[testenv:unit] deps = -e../datadog_checks_base[deps] -rrequirements-dev.txt setenv = + CASSANDRA_VERSION=2.1.14 CONTAINER_PORT=7199 +passenv = + DOCKER* + COMPOSE* + JMX_* commands = pip install --require-hashes -r requirements.txt - pytest -m"not integration" -v + pytest -m"integration" -v -[testenv:integration] +[testenv:unit] deps = -e../datadog_checks_base[deps] -rrequirements-dev.txt setenv = - CASSANDRA_VERSION=2.1.14 CONTAINER_PORT=7199 -passenv = - DOCKER* - COMPOSE* commands = pip install --require-hashes -r requirements.txt - pytest -m"integration" -v + pytest -m"not integration" -v [testenv:flake8] skip_install = true @@ -41,4 +40,4 @@ commands = flake8 . [flake8] exclude = .eggs,.tox -max-line-length = 120 +max-line-length = 120 \ No newline at end of file