Skip to content

Commit

Permalink
Add python 3 compatability
Browse files Browse the repository at this point in the history
  • Loading branch information
Ravid Brown committed Jul 10, 2017
1 parent 96ed798 commit 863f257
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 37 deletions.
1 change: 1 addition & 0 deletions Dockerfile.build
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
FROM python:2.7
#FROM python:3.6

# Install Docker
ENV DOCKER_CONFIG=/tmp/
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# Get docker api version and set the compose file version accordingly
DOCKER_API_VERSION = $(shell python docker_test_tools/api_version.py)
COMPOSE_FILE_VERSION = $(shell python -c 'print "2.1" if $(DOCKER_API_VERSION) >= "1.24" else "2"')
COMPOSE_FILE_VERSION = $(shell python -c 'print("2.1" if "$(DOCKER_API_VERSION)" >= "1.24" else "2")')
DTT_COMPOSE_PATH=tests/resources/docker-compose-v$(COMPOSE_FILE_VERSION).yml

all: coverage nose2 pytest dist/docker-test-tools-*.tar.gz
Expand Down
2 changes: 1 addition & 1 deletion docker_test_tools/api_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ def get_server_api_version():
return server_versions['ApiVersion']

if __name__ == '__main__':
print get_server_api_version()
print(get_server_api_version())
4 changes: 2 additions & 2 deletions docker_test_tools/config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
import ConfigParser
from six.moves import configparser


class Config(object):
Expand Down Expand Up @@ -87,7 +87,7 @@ def get_file_config(self, config_path):
if not os.path.exists(config_path):
raise RuntimeError("Invalid configuration path: %s" % config_path)

config_reader = ConfigParser.ConfigParser()
config_reader = configparser.ConfigParser()
config_reader.read(config_path)
read_options = config_reader.options(self.SECTION_NAME)

Expand Down
24 changes: 21 additions & 3 deletions docker_test_tools/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def get_services(self):
except subprocess.CalledProcessError as error:
raise RuntimeError("Failed getting environment services, reason: %s" % error.output)

return services_output.strip().split('\n')
return self._to_str(services_output).strip().split('\n')

def setup(self):
"""Sets up the environment using docker commands.
Expand Down Expand Up @@ -148,7 +148,7 @@ def split_logs(self):

services_log_files[service_name].write(message)
finally:
for services_log_file in services_log_files.itervalues():
for services_log_file in services_log_files.values():
services_log_file.close()

def remove_containers(self):
Expand Down Expand Up @@ -270,13 +270,15 @@ def get_container_id(self, name):
"""
self.validate_service_name(name)
try:
return subprocess.check_output(
output = subprocess.check_output(
['docker-compose', '-f', self.compose_path, '-p', self.project_name, 'ps', '-q', name],
stderr=subprocess.STDOUT, env=self.environment_variables
)
except subprocess.CalledProcessError as error:
raise RuntimeError("Failed getting container %s id, reason: %s" % (name, error.output))

return self._to_str(output)

def is_container_ready(self, name):
""""Return True if the container is in ready state.
Expand All @@ -298,6 +300,7 @@ def is_container_ready(self, name):
log.warning("Failed getting container %s state, reason: %s", name, error.output)
return False

status_output = self._to_str(status_output)
if '"Health":' in status_output:
is_ready = '"Status":"healthy"' in status_output
else:
Expand Down Expand Up @@ -433,3 +436,18 @@ def _get_environment_variables():
env = os.environ.copy()
env['COMPOSE_API_VERSION'] = env['DOCKER_API_VERSION'] = server_api_version
return env

@staticmethod
def _to_str(value):
"""
For each value, return the value as string
For python3 compatibility
"""

if isinstance(value, str):
return value

if isinstance(value, bytes):
return value.decode('utf-8')

raise Exception("Type {} was not converted to string".format(type(value)))
6 changes: 3 additions & 3 deletions docker_test_tools/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import logging
import httplib
from six.moves import http_client
import waiting
import requests

Expand Down Expand Up @@ -27,7 +27,7 @@ def run_health_checks(checks, interval=1, timeout=60):
return False


def is_responsive(address, expected_status=httplib.OK):
def is_responsive(address, expected_status=http_client.OK):
"""Return True if the address is responsive.
:param string address: url address 'hostname:port'.
Expand All @@ -40,7 +40,7 @@ def is_responsive(address, expected_status=httplib.OK):
return False


def get_health_check(service_name, url, expected_status=httplib.OK):
def get_health_check(service_name, url, expected_status=http_client.OK):
"""Return a function used to determine if the given service is responsive.
:param string service_name: service name.
Expand Down
4 changes: 2 additions & 2 deletions docker_test_tools/wiremock.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import os
import json
import glob
import httplib
from six.moves import http_client
import logging

import requests
Expand Down Expand Up @@ -100,7 +100,7 @@ def get_request_journal(self):
:raise ValueError: on failure to retrieve journal from Wiremock admin API.
"""
response = requests.get(self.requests_url)
if response.status_code != httplib.OK:
if response.status_code != http_client.OK:
raise ValueError(response.text, response.status_code)
response_body = json.loads(response.text)
return response_body["requests"]
Expand Down
22 changes: 11 additions & 11 deletions tests/integration/test_example.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
import httplib
from six.moves import http_client
import logging
import requests

Expand Down Expand Up @@ -34,61 +34,61 @@ def setUp(self):
def test_services_sanity(self):
"""Validate services are responsive once the test start."""
log.info('Validating consul container is responsive')
self.assertEquals(requests.get('http://consul.service:8500').status_code, httplib.OK)
self.assertEquals(requests.get('http://consul.service:8500').status_code, http_client.OK)

log.info('Validating wiremock container is responsive')
self.assertEquals(requests.get('http://mocked.service:9999/__admin').status_code, httplib.OK)
self.assertEquals(requests.get('http://mocked.service:9999/__admin').status_code, http_client.OK)

def test_service_down(self):
"""Validate service down scenario."""
log.info('Validating consul container is responsive')
self.assertEquals(requests.get('http://consul.service:8500').status_code, httplib.OK)
self.assertEquals(requests.get('http://consul.service:8500').status_code, http_client.OK)

log.info('Validating consul container is unresponsive while in `container_down` context')
with self.controller.container_down(name='consul.service', health_check=consul_health_check):
with self.assertRaises(requests.ConnectionError):
requests.get('http://consul.service:8500')

log.info('Validating consul container has recovered and is responsive')
self.assertEquals(requests.get('http://consul.service:8500').status_code, httplib.OK)
self.assertEquals(requests.get('http://consul.service:8500').status_code, http_client.OK)

def test_service_stopped(self):
"""Validate service stopped scenario."""
log.info('Validating consul container is responsive')
self.assertEquals(requests.get('http://consul.service:8500').status_code, httplib.OK)
self.assertEquals(requests.get('http://consul.service:8500').status_code, http_client.OK)

log.info('Validating consul container is unresponsive while in `container_stopped` context')
with self.controller.container_stopped(name='consul.service', health_check=consul_health_check):
with self.assertRaises(requests.ConnectionError):
requests.get('http://consul.service:8500')

log.info('Validating consul container has recovered and is responsive')
self.assertEquals(requests.get('http://consul.service:8500').status_code, httplib.OK)
self.assertEquals(requests.get('http://consul.service:8500').status_code, http_client.OK)

def test_service_paused(self):
"""Validate service paused scenario."""
log.info('Validating consul container is responsive')
self.assertEquals(requests.get('http://consul.service:8500', timeout=2).status_code, httplib.OK)
self.assertEquals(requests.get('http://consul.service:8500', timeout=2).status_code, http_client.OK)

log.info('Validating consul container is unresponsive while in `container_paused` context')
with self.controller.container_paused(name='consul.service', health_check=consul_health_check):
with self.assertRaises(requests.Timeout):
requests.get('http://consul.service:8500', timeout=2)

log.info('Validating consul container has recovered and is responsive')
self.assertEquals(requests.get('http://consul.service:8500', timeout=2).status_code, httplib.OK)
self.assertEquals(requests.get('http://consul.service:8500', timeout=2).status_code, http_client.OK)

def test_mocked_service_configuration(self):
"""Validate wiremock service."""
log.info('Validating mocked service fail to find `test` endpoint')
self.assertEquals(requests.post('http://mocked.service:9999/test').status_code, httplib.NOT_FOUND)
self.assertEquals(requests.post('http://mocked.service:9999/test').status_code, http_client.NOT_FOUND)

log.info('Use WiremockController to stub the service `test` endpoint')
stubs_dir_path = os.path.join(os.path.dirname(__file__), '..', 'resources', 'wiremock_stubs')
self.wiremock.set_mapping_from_dir(stubs_dir_path)

log.info('Validating mocked service response on `test` endpoint')
self.assertEquals(requests.post('http://mocked.service:9999/test').status_code, httplib.OK)
self.assertEquals(requests.post('http://mocked.service:9999/test').status_code, http_client.OK)

def test_mocked_service_request_journal(self):
"""Validate wiremock request journal."""
Expand Down
4 changes: 0 additions & 4 deletions tests/ut/nose2.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ plugins = nose2.plugins.mp
nose2.plugins.layers
nose2.plugins.junitxml

[multiprocess]
always-on = True
processes = 4

[coverage]
always-on = True
coverage = docker_test_tools
Expand Down
10 changes: 5 additions & 5 deletions tests/ut/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import tempfile
import unittest

import ConfigParser
from six.moves import configparser

from docker_test_tools.config import Config

Expand Down Expand Up @@ -84,19 +84,19 @@ def test_bad_config_path(self):
def test_bad_section_name(self):
"""Try parsing an invalid config file path and validate operation failure."""
test_config_path = self.create_config_file(config_input={}, section='bad')
with self.assertRaises(ConfigParser.NoSectionError):
with self.assertRaises(configparser.NoSectionError):
Config(config_path=test_config_path)

def create_config_file(self, config_input, section='environment'):
"""Create a config file based on the given dictionary."""
test_config_path = os.path.join(self.test_dir, 'test.txt')

parser = ConfigParser.ConfigParser()
parser = configparser.ConfigParser()

with open(test_config_path, 'w') as cfgfile:
parser.add_section(section)
for option, value in config_input.iteritems():
parser.set(section, option, value)
for option, value in config_input.items():
parser.set(section, option, str(value))
parser.write(cfgfile)

return test_config_path
4 changes: 3 additions & 1 deletion tests/ut/test_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import mock
import unittest
import subprocess
from six.moves import builtins

from waiting import TimeoutExpired
from docker_test_tools import environment
Expand Down Expand Up @@ -74,6 +75,7 @@ def test_container_methods_happy_flow(self, mocked_check_output):
stderr=subprocess.STDOUT, env=self.ENVIRONMENT_VARIABLES
)

mocked_check_output.return_value = "DOCKER_SERVICE"
self.controller.get_container_id(service_name)
mocked_check_output.assert_called_with(
['docker-compose', '-f', self.compose_path, '-p', self.project_name, 'ps', '-q', service_name],
Expand Down Expand Up @@ -353,7 +355,7 @@ def test_stop_log_collection(self, split_logs_mock):
def test_start_log_collection(self, mocked_popen):
""""Validate the environment start_log_collection method."""
controller = self.get_controller()
with mock.patch("__builtin__.open", mock.mock_open()):
with mock.patch("{}.open".format(builtins.__name__), mock.mock_open()):
controller.start_log_collection()

mocked_popen.assert_called_with(
Expand Down
8 changes: 4 additions & 4 deletions tests/ut/test_wiremock.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import mock
import httplib
from six.moves import http_client
import unittest

from docker_test_tools import wiremock
Expand Down Expand Up @@ -104,7 +104,7 @@ def test_get_request_journal(self):
"""Test 'get_request_journal' method."""

mock_response = mock.Mock()
mock_response.status_code = httplib.OK
mock_response.status_code = http_client.OK
mock_response.text = self.journal_json
mock_get = mock.Mock(return_value=mock_response)

Expand All @@ -118,7 +118,7 @@ def test_error_getting_request_journal(self):
"""Test HTTP error while getting request journal."""

mock_response = mock.Mock()
mock_response.status_code = httplib.NOT_FOUND
mock_response.status_code = http_client.NOT_FOUND
mock_get = mock.Mock(return_value=mock_response)

with mock.patch("requests.get", mock_get):
Expand All @@ -128,7 +128,7 @@ def test_get_matching_requests(self):
"""Test 'get_matching_requests' method."""

mock_response = mock.Mock()
mock_response.status_code = httplib.OK
mock_response.status_code = http_client.OK
mock_response.text = self.journal_json
mock_get = mock.Mock(return_value=mock_response)

Expand Down

0 comments on commit 863f257

Please sign in to comment.