Skip to content
This repository has been archived by the owner on Feb 14, 2024. It is now read-only.

Commit

Permalink
#12: Fix tests (fixes #10
Browse files Browse the repository at this point in the history
* Sync charm-helpers for tests
* Add Travis CI configuration
* Add yakkety support
* Clean up pre-existing PEP8 sins
* Fix functional tests
  • Loading branch information
fnordahl authored and marcoceppi committed Apr 4, 2017
1 parent 1a82778 commit cefb77f
Show file tree
Hide file tree
Showing 19 changed files with 1,260 additions and 63 deletions.
40 changes: 40 additions & 0 deletions .travis.yml
@@ -0,0 +1,40 @@
sudo: required
dist: trusty
language: python
env:
- UNIT_TEST=1
- AMULET_TEST=gate-basic-precise
- AMULET_TEST=gate-basic-trusty
- AMULET_TEST=gate-basic-xenial
- AMULET_TEST=gate-basic-yakkety
script:
- if [ ! -z ${UNIT_TEST} ]; then make lint; fi
- if [ ! -z ${UNIT_TEST} ]; then make test; fi
# setup dependencies here to save time on simpler test envs
# upgrade python 2.7 to mitigate juju-deployer ssl.SSLError
# sudo back to ourself to activate lxd group membership
- if [ ! -z ${AMULET_TEST} ]; then
sudo add-apt-repository -y ppa:ubuntu-lxc/lxd-stable;
sudo add-apt-repository -y ppa:juju/stable;
sudo add-apt-repository -y ppa:fkrull/deadsnakes-python2.7;
sudo apt-get -qq update;
sudo apt-get -y install lxd juju python-tox libpython2.7 libpython2.7-dev libpython2.7-minimal libpython2.7-stdlib python-crypto python-pkg-resources python-yaml python2.7 python2.7-dev python2.7-minimal;
sudo lxd init --auto;
sudo usermod -a -G lxd travis;
sudo su travis -c 'lxc network create lxdbr0';
sudo su travis -c 'lxc network attach-profile lxdbr0 default eth0';
sudo su travis -c 'juju bootstrap localhost';
make virtualenv;
fi
- if [ ! -z ${AMULET_TEST} ]; then
sudo su travis -c '. .venv/bin/activate && bundletester -vl DEBUG
--test-pattern "${AMULET_TEST}*"';
fi
- if [ ! -z ${AMULET_TEST} ]; then sudo su travis -c 'juju status'; fi
- if [ ! -z ${AMULET_TEST} ]; then
sudo su travis -c \
'juju ssh mysql/0 sudo cat /var/log/juju/unit-mysql-0.log';
fi
- if [ ! -z ${AMULET_TEST} ]; then
sudo su travis -c 'juju ssh mysql/0 sudo cat /var/log/mysql/error.log';
fi
27 changes: 17 additions & 10 deletions Makefile
Expand Up @@ -3,23 +3,30 @@ PYTHON := /usr/bin/env python
export PYTHONPATH := hooks

virtualenv:
virtualenv .venv
.venv/bin/pip install flake8 nose coverage mock six pyyaml \
netifaces netaddr pymysql
@if [ ! -d .venv ]; then \
virtualenv .venv; \
. .venv/bin/activate && \
.venv/bin/pip install -U pip; \
. .venv/bin/activate && \
.venv/bin/pip install -r test-requirements.txt; \
fi


lint: virtualenv
.venv/bin/flake8 --exclude hooks/charmhelpers,tests/charmhelpers \
hooks unit_tests tests
@charm proof
@. .venv/bin/activate && \
.venv/bin/flake8 --exclude hooks/charmhelpers,tests/charmhelpers \
hooks unit_tests tests
@. .venv/bin/activate && .venv/bin/charm-proof

test: virtualenv
@echo Starting tests...
@.venv/bin/nosetests --nologcapture --with-coverage unit_tests
@. .venv/bin/activate && \
.venv/bin/nosetests --nologcapture --with-coverage unit_tests

functional_test:
functional_test: virtualenv
@echo Starting Amulet tests...
@tests/setup/00-setup
@juju test -v -p AMULET_HTTP_PROXY,AMULET_OS_VIP --timeout 2700
@. .venv/bin/activate && \
.venv/bin/bundletester -vl DEBUG --test-pattern gate-basic-*

bin/charm_helpers_sync.py:
@mkdir -p bin
Expand Down
1 change: 0 additions & 1 deletion charm-helpers-tests.yaml
Expand Up @@ -2,4 +2,3 @@ branch: lp:charm-helpers
destination: tests/charmhelpers
include:
- contrib.amulet
- contrib.openstack.amulet
1 change: 1 addition & 0 deletions hooks/lib/utils.py
Expand Up @@ -40,6 +40,7 @@ def install(*pkgs):
cmd.append(pkg)
subprocess.check_call(cmd)


try:
import dns.resolver
except ImportError:
Expand Down
1 change: 1 addition & 0 deletions metadata.yaml
Expand Up @@ -10,6 +10,7 @@ tags:
- databases
- openstack
series:
- yakkety
- xenial
- trusty
- precise
Expand Down
13 changes: 13 additions & 0 deletions test-requirements.txt
@@ -0,0 +1,13 @@
path.py<=8.1.2
PyYAML<=3.11
flake8
nose
coverage
mock
six
netifaces
netaddr
pymysql
bundletester
amulet
charm-tools
52 changes: 0 additions & 52 deletions tests/010-configs

This file was deleted.

101 changes: 101 additions & 0 deletions tests/basic_deployment.py
@@ -0,0 +1,101 @@
#!/usr/bin/python3

import amulet
from charmhelpers.contrib.amulet.deployment import (
AmuletDeployment
)
import logging
import sys

try:
import pymysql
except ImportError:
import pymysql3 # noqa


class MySQLBasicDeployment(AmuletDeployment):
def __init__(self, series=None):
self.log = self._get_logger()
self.max_connections = '30'
# the default of 80% eats too much resources in a CI environment
self.dataset_size = '25%'

self.d = amulet.Deployment(series=series)

self.d.add('mysql')
self.d.configure(
'mysql', {
'max-connections': self.max_connections,
'dataset-size': self.dataset_size,
},
)
self.d.expose('mysql')

try:
self.d.setup(timeout=900)
self.d.sentry.wait()
except amulet.helpers.TimeoutError:
amulet.raise_status(amulet.SKIP,
msg="Environment wasn't stood up in time")

# Allow connections from outside
mysqlmaster = self.d.sentry['mysql'][0]
mysqlmaster.run(
"echo 'GRANT ALL ON *.* to root@\"%\"'\""
" IDENTIFIED BY '`cat /var/lib/mysql/mysql.passwd`'\"|"
"mysql -u root --password=`cat /var/lib/mysql/mysql.passwd`"
)

def _get_logger(self, name="deployment-logger", level=logging.DEBUG):
"""Get a logger object that will log to stdout."""
log = logging
logger = log.getLogger(name)
fmt = log.Formatter("%(asctime)s %(funcName)s "
"%(levelname)s: %(message)s")

handler = log.StreamHandler(stream=sys.stdout)
handler.setLevel(level)
handler.setFormatter(fmt)

logger.addHandler(handler)
logger.setLevel(level)

return logger

def test_100_connect(self):
"""Verify that we can connect to the deployed MySQL instance"""
self.log.info('Verify that we can connect to the deployed MySQL '
'instance')
mysql_server = self.d.sentry['mysql'][0]
mysql_password = mysql_server.file_contents(
'/var/lib/mysql/mysql.passwd')
try:
self.mysql_conn = pymysql.connect(
user='root',
password=mysql_password,
host=mysql_server.info['public-address'],
database='mysql')
except:
amulet.raise_status(amulet.FAIL, msg='Unable to connect to MySQL')
self.log.info('OK')

def test_110_check_variables(self):
"""Verify that configured variables are reflected back to us when
asking MySQL server about them."""
self.log.info('Verify that configured variables are reflected back to '
'us when asking MySQL server about them.')
cur = self.mysql_conn.cursor()
cur.execute("SHOW VARIABLES LIKE 'max_connections'")
for row in cur:
if row[0] == 'max_connections':
if row[1] == self.max_connections:
self.log.info('OK')
return
else:
amulet.raise_status(
amulet.FAIL,
msg="max_connections='{}', expected '{}'"
"".format(row[1], self.max_connections))
amulet.raise_status(
amulet.FAIL,
msg='Unable to retrieve value for max_connections from server')
36 changes: 36 additions & 0 deletions tests/charmhelpers/__init__.py
@@ -0,0 +1,36 @@
# Copyright 2014-2015 Canonical Limited.
#
# 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.

# Bootstrap charm-helpers, installing its dependencies if necessary using
# only standard libraries.
import subprocess
import sys

try:
import six # flake8: noqa
except ImportError:
if sys.version_info.major == 2:
subprocess.check_call(['apt-get', 'install', '-y', 'python-six'])
else:
subprocess.check_call(['apt-get', 'install', '-y', 'python3-six'])
import six # flake8: noqa

try:
import yaml # flake8: noqa
except ImportError:
if sys.version_info.major == 2:
subprocess.check_call(['apt-get', 'install', '-y', 'python-yaml'])
else:
subprocess.check_call(['apt-get', 'install', '-y', 'python3-yaml'])
import yaml # flake8: noqa
13 changes: 13 additions & 0 deletions tests/charmhelpers/contrib/__init__.py
@@ -0,0 +1,13 @@
# Copyright 2014-2015 Canonical Limited.
#
# 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.
13 changes: 13 additions & 0 deletions tests/charmhelpers/contrib/amulet/__init__.py
@@ -0,0 +1,13 @@
# Copyright 2014-2015 Canonical Limited.
#
# 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.

0 comments on commit cefb77f

Please sign in to comment.