Add mysql service image test

Change-Id: I4dca8441bdd3dab31a01798bd6bfe488a7ef5eeb
This commit is contained in:
Dmitry Tyzhnenko 2016-05-25 14:07:17 +03:00 committed by Dmitry Tyzhnenko
parent 9e7594899c
commit 4f37eff0e9
6 changed files with 144 additions and 20 deletions

View File

@ -17,6 +17,13 @@ from __future__ import division
from mcp_tests.logger import logger
def exec_in_container(container, cmd):
command = container.create_exec(cmd)
stdout = container.start_exec(command)
inspect = container.client.exec_inspect(command['Id'])
return stdout, inspect['ExitCode']
class ContainerEngine(object):
def __init__(self,
remote=None,

View File

@ -3,4 +3,5 @@ paramiko
six
requests>=2.2.0
pytest>=2.9
docker-py
docker-py
docker-compose==1.7.1

View File

@ -0,0 +1,71 @@
# Copyright 2016 Mirantis, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import logging
import time
import docker
from compose.project import Project
from compose.service import Service
from mcp_tests.logger import logger
from mcp_tests.logger import console
logging.getLogger('compose.service').addHandler(console)
class ServiceBaseTest(object):
"""ServiceBaseTest contains setup/teardown for services up and down"""
@classmethod
def setup_class(cls):
"""Start container from image
Scenario:
1. Get image from private registry
2. Start container with it
"""
logger.info("Up services")
cli = docker.Client()
project_name = cls.__name__
services = []
for s in cls.services:
services.append(
Service(
# name=s['name'],
project=project_name,
client=cli,
**s))
cls.project = Project(
name=project_name,
services=services,
client=cli)
cls.containers = cls.project.up()
wait_services = getattr(cls, 'wait_services', 5)
logger.info("Sleep {} sec until MariDB is setting up".format(
wait_services))
time.sleep(wait_services)
logger.info("Start tests")
@classmethod
def teardown_class(cls):
"""Down service
Scenario:
5. Kill container
6. Remove volumes
"""
logger.info("Down service and remove volume")
cls.project.down(remove_image_type=False,
include_volumes=True)

View File

@ -11,34 +11,76 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import pytest
import traceback
from mcp_tests.helpers import mcp_tests_exceptions
from mcp_tests.logger import logger
from devops.helpers.helpers import tcp_ping
from devops.helpers.helpers import wait
from mcp_tests import settings
from mcp_tests.helpers.containers import exec_in_container
from mcp_tests.logger import logger
from mcp_tests.service_tests import ServiceBaseTest
class TestMysqlImage(object):
@pytest.mark.skipif(settings.PRIVATE_REGISTRY is None,
reason="PRIVATE_REGISTRY isn't set")
class TestMysqlImage(ServiceBaseTest):
"""Test class consits simple tests for mysql container"""
services = [
{
"name": "mariadb",
"image": "{}/nextgen/mariadb".format(settings.PRIVATE_REGISTRY),
"environment": {
"DB_ROOT_PASSWORD": "r00tme"
},
"ports": ["33306:3306"]
}
]
wait_services = 30
@pytest.mark.mysql_base
def test_mysql_check_mysqld(self):
"""Start container from image, check if mysql is running
Scenario:
4. Check access from root user
"""
logger.info("Trying check daemon")
container = self.containers[0]
cmd = 'pgrep mysqld'
out, exit_code = exec_in_container(container, cmd)
assert exit_code == 0
@pytest.mark.mysql_base
def test_mysql_is_running(self):
"""Start container from image, check if mysql is running
Scenario:
1. Get image from private registry
2. Start container with it
3. Check if mysql is running
4. Destroy container
3. Check port 3306
"""
logger.info('Check if registry set {0}'.format(
settings.PRIVATE_REGISTRY))
try:
if not settings.PRIVATE_REGISTRY:
raise mcp_tests_exceptions.VariableNotSet(
settings.PRIVATE_REGISTRY, 'localhost:5002/registry')
except mcp_tests_exceptions.VariableNotSet:
logger.error(traceback.format_exc())
logger.info("Trying to reach port 3306")
wait(lambda: tcp_ping('localhost', 33306),
timeout=30,
timeout_msg="MySQL port in not reacheble.")
@pytest.mark.mysql_base
def test_mysql_is_accessible(self):
"""Start container from image, check if mysql is running
Scenario:
4. Check access from root user
"""
logger.info("Trying fetch databases list")
container = self.containers[0]
cmd = 'mysql -Ns -uroot -pr00tme -e "SHOW DATABASES"'
out, exit_code = exec_in_container(container, cmd)
assert exit_code == 0
out = filter(bool, out.split('\n'))
logger.info("Databases in DB - {}".format(out))
assert set(out) == \
set(['information_schema', 'mysql', 'performance_schema'])

View File

@ -31,4 +31,4 @@ TIMESTAT_PATH_YAML = os.environ.get(
SSH_NODE_CREDENTIALS = os.environ.get('SSH_NODE_CREDENTIALS',
{'login': 'test', 'password': 'test'})
PRIVATE_REGISTRY = os.environ.get('PRIVATE_REGISTRY')
PRIVATE_REGISTRY = os.environ.get('PRIVATE_REGISTRY', None)

View File

@ -8,7 +8,10 @@ skipsdist = True
envlist = pep8, py27eters = True
[testenv]
deps = -r{toxinidir}/mcp_tests/requirements.txt
install_command = pip install --allow-external -U {opts} {packages}
deps =
setuptools
-r{toxinidir}/mcp_tests/requirements.txt
usedevelop = False
commands = py.test