Summary: add initial unittest structure and first unittest

Description:
 - add new unit test base class (KollaClientTest), add init methods and methods
   to run cli commands and capture output
 - add basic host add/remove/list tests to exercise unit test env
 - ensure load_etc_yamll never returns a null value
This commit is contained in:
Steve Noyes 2015-08-03 15:04:42 -04:00
parent 1185203093
commit 0aa3d280c0
4 changed files with 306 additions and 1 deletions

View File

@ -51,12 +51,16 @@ def get_pk_bits():
def load_etc_yaml(fileName):
try:
with open(get_client_etc() + fileName, 'r') as f:
return yaml.load(f)
contents = yaml.load(f)
except Exception:
# TODO(bmace) if file doesn't exist on a load we don't
# want to blow up, some better behavior here?
return {}
if not contents:
contents = {}
return contents
def save_etc_yaml(fileName, contents):
with open(get_client_etc() + fileName, 'w') as f:

0
tests/__init__.py Normal file
View File

151
tests/common.py Normal file
View File

@ -0,0 +1,151 @@
# 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 os
import sys
import subprocess
import traceback
import testtools
import logging
import kollaclient.utils as utils
TEST_SUFFIX = '/test/'
ENV_ETC = 'KOLLA_CLIENT_ETC'
VENV_PY_PATH = '/.venv/bin/python'
class KollaClientTest(testtools.TestCase):
cmd_prefix = ''
log = logging.getLogger(__name__)
def setUp(self):
super(KollaClientTest, self).setUp()
logging.basicConfig(stream=sys.stderr)
logging.getLogger(__name__).setLevel(logging.DEBUG)
# switch to test path
self._setup_env_var()
self._set_cmd_prefix()
# make sure hosts and zones yaml files exist
# and clear them out
etc_path = os.getenv('KOLLA_CLIENT_ETC')
self._init_dir(etc_path)
self.log.debug('$KOLLA_CLIENT_ETC for tests: %s'
% os.getenv('KOLLA_CLIENT_ETC'))
hosts_path = etc_path + '/hosts.yml'
self._init_file(hosts_path)
zones_path = etc_path + '/zones.yml'
self._init_file(zones_path)
def tearDown(self):
self._restore_env_var()
super(KollaClientTest, self).tearDown()
def run_client_cmd(self, cmd):
full_cmd = ('%s %s' % (self.cmd_prefix, cmd))
self.log.debug('running command: %s' % cmd)
(retval, msg) = self._run_command(full_cmd)
self.assertEqual(0, retval, ('command [%s] failed: (%s): %s'
% (full_cmd, retval, msg)))
return msg
# PRIVATE FUNCTIONS ----------------------------------------------------
def _setup_env_var(self):
new_etc_path = utils.get_client_etc() + TEST_SUFFIX
os.environ[ENV_ETC] = new_etc_path
def _restore_env_var(self):
etc_path = utils.get_client_etc()
if etc_path.endswith(TEST_SUFFIX):
etc_path = etc_path.rsplit('/', 1)[0]
os.environ[ENV_ETC] = etc_path
def _run_command(self, cmd):
# self.log.debug('run cmd: %s' % cmd)
retval = 0
msg = ''
try:
msg = subprocess.check_output(cmd.split(),
stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
retval = e.returncode
msg = e.output
except Exception as e:
retval = e.errno
msg = 'Unexpected exception: %s' % traceback.format_exc()
# the py dev debugger adds a string at the line start, remove it
if msg.startswith('pydev debugger'):
msg = msg.split('\n', 1)[1]
return (retval, msg)
def _init_file(self, filepath):
with open(filepath, 'w') as f:
f.close()
def _init_dir(self, path):
if not os.path.isdir(path):
os.makedirs(path)
def _set_cmd_prefix(self):
""" The kolla-client can be run:
1) from the command line via $ kollaclient, or
2) if that doesn't work, this assumes that we're operating
in a dev't debug environment, which means that the kollaclient
was installed in a virtualenv. So then we have to use the python
version in virtualenv and the tests will have to be run
from the tests directory.
"""
self._run_command('which python')
self.cmd_prefix = 'kollaclient'
(retval, msg) = self._run_command('%s host add -h' % self.cmd_prefix)
if retval == 0:
self.log.debug('kollaclient found, will use as the test command')
return
self.log.debug('kollaclient exec failed: %s' % msg)
self.log.debug('look for kollaclient shell in virtual env')
# try to see if this is a debug virtual environment
# will run the tests via kollaclient/shell.sh and
# use the python in .venv/bin/python
cwd = os.getcwd()
if cwd.endswith('tests'):
os_kolla_dir = cwd.rsplit('/', 1)[0]
shell_dir = os_kolla_dir + '/kollaclient/'
shell_path = shell_dir + 'shell.py'
python_path = os_kolla_dir + VENV_PY_PATH
self.log.debug('shell_path: %s' % shell_path)
self.log.debug('python_path: %s' % python_path)
if os.path.exists(shell_path) and os.path.exists(python_path):
self.cmd_prefix = '%s %s ' % (python_path, shell_path)
self.run_client_cmd('host add -h')
self.log.info('successfully ran command in venv environment')
return
self.assertEqual(0, 1,
'no kollaclient shell command found. Aborting tests')

150
tests/host.py Normal file
View File

@ -0,0 +1,150 @@
# 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 unittest
import common
KEY_NET = 'NetworkAddress'
KEY_SERVICES = "Services"
KEY_ZONE = 'Zone'
class TestFunctional(common.KollaClientTest):
def setUp(self):
super(TestFunctional, self).setUp()
def tearDown(self):
super(TestFunctional, self).tearDown()
def test_host_add_remove(self):
# host file should be initialized to an empty dict {}
msg = self.run_client_cmd('host list')
self.assertEqual('', msg, 'hosts.yml is not empty: %s' % msg)
hosts = self.Hosts()
hostname = 'host_test1'
ip_addr = '1.1.1.1'
hosts.add(hostname, ip_addr)
self.run_client_cmd('host add %s %s' % (hostname, ip_addr))
msg = self.run_client_cmd('host list')
self._check_hosts_yml(hosts, msg)
hostname = 'host_test2'
ip_addr = '2.2.2.2'
hosts.add(hostname, ip_addr)
self.run_client_cmd('host add %s %s' % (hostname, ip_addr))
msg = self.run_client_cmd('host list')
self._check_hosts_yml(hosts, msg)
hostname = 'host_test2'
hosts.remove(hostname)
self.run_client_cmd('host remove %s' % hostname)
msg = self.run_client_cmd('host list')
self._check_hosts_yml(hosts, msg)
hostname = 'host_test1'
hosts.remove(hostname)
self.run_client_cmd('host remove %s' % hostname)
msg = self.run_client_cmd('host list')
self._check_hosts_yml(hosts, msg)
def _check_hosts_yml(self, hosts, hosts_yml):
"""
The yml is a string representation of a simple yml file,
that is returned by the host list command; of form:
\n
hostname1\n
{'NetworkAddress': 'ip', 'Services': '[]', 'Zone': 'zone'}\n
hostname2\n
{'NetworkAddress': 'ip', 'Services': '[]', 'Zone': 'zone'}\n
etc
"""
# check for any host in yml that shouldn't be there
yml_lines = hosts_yml.split('}\n')
exp_hosts = hosts.get_hostnames()
for yml_line in yml_lines:
yml_line = yml_line.strip()
if yml_line:
yml_host = yml_line.split('\n')[0]
self.assertIn(yml_host, exp_hosts,
'yml: %s, contains unexpected host: %s'
% (yml_lines, yml_host))
for hostname in exp_hosts:
exp_ip = hosts.get_ip(hostname)
exp_zone = hosts.get_zone(hostname)
hostname_found = False
for yml_line in yml_lines:
yml_line = yml_line.strip()
if yml_line.startswith(hostname):
hostname_found = True
# check network info
self.assertIn(KEY_NET, yml_line,
'host: %s, has no %s entry in yml'
% (hostname, KEY_NET))
ip_start = yml_line.find(KEY_NET) + len(KEY_NET) + 4
ip_end = yml_line.find(',', ip_start) - 1
ip = yml_line[ip_start: ip_end]
self.assertEqual(exp_ip, ip, 'incorrect ip address in yml')
# check zone
self.assertIn(KEY_NET, yml_line,
'host: %s, has no %s entry in yml'
% (hostname, KEY_ZONE))
zn_start = yml_line.find(KEY_ZONE) + len(KEY_ZONE) + 4
zn_end = yml_line.find(',', zn_start) - 1
zone = yml_line[zn_start: zn_end]
self.assertEqual(exp_zone, zone, 'incorrect zone in yml')
# check services (TODO)
self.assertTrue(hostname_found,
'hostname: %s not in yml: %s'
% (hostname, hosts_yml))
class Hosts():
info = {}
def remove(self, name):
del self.info[name]
def add(self, name, ip, zone='', services=[]):
if name not in self.info:
self.info[name] = {}
self.info[name][KEY_NET] = ip
self.info[name][KEY_ZONE] = zone
self.info[name][KEY_SERVICES] = services
def get_ip(self, name):
return self.info[name][KEY_NET]
def get_zone(self, name):
return self.info[name][KEY_ZONE]
def get_services(self, name):
return self.info[name][KEY_SERVICES]
def get_hostnames(self):
return self.info.keys()
if __name__ == '__main__':
unittest.main()