2016-09-16 16:24:23 -04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2016-12-19 19:59:49 -05:00
|
|
|
# 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.
|
|
|
|
|
2016-09-16 16:24:23 -04:00
|
|
|
import os
|
|
|
|
from os import path
|
|
|
|
import test_inventory
|
|
|
|
import unittest
|
|
|
|
|
2017-02-13 23:06:14 +00:00
|
|
|
TARGET_DIR = path.join(os.getcwd(), 'tests', 'inventory')
|
2016-09-16 16:24:23 -04:00
|
|
|
|
Use python import machinery for inventory code
Change Idb7780f55e4a1fd77dd76becbf67c1ccbf220db7 restructured the python
inventory generation code so that it would be possible to install it
with pip. This change removes (most) of the import path hacks and
switches to using a pip-installed version of osa_toolkit.
Of note, the path hacks are left in place for the dynamic_inventory.py
file for now, as that file is really more of an endpoint, but is tested.
Also, the bootstrap-ansible.sh script was modified to install the code;
this is unnecessary with the tox environments because the tox directive
'usedevelop=True' does that already. Production environments still need
this, though.
Finally, to maintain usability when called directly, the interpreter for
dynamic_inventory.py was updated from `/usr/bin/env` to
`/opt/ansible-runime/python`. This change ensures that in a full
deployment the user is using the exact same code paths whether Ansible
invokes the script, or it is called directly. This also means that using
the script locally on a development machine, it must be invoked as an
argument to Python, unless the ansible-runtime directory exists.
Change-Id: Iafa573b1b144f98528d5e0aceb3f36e9de2a22a2
2017-07-06 16:32:05 -04:00
|
|
|
from osa_toolkit import manage as mi
|
2016-09-16 16:24:23 -04:00
|
|
|
|
|
|
|
|
Create complete AIO inventory config for tests
With commit f7a50a24b9d68866da615939136e84e460f4df62 (Change ID:
I99541d36aed6d4b9f83746c1cd1a5521b310f1f6,
https://review.openstack.org/#/c/369485/) the inventory configuration
was split into multiple files to enable scenario AIO testing. This had
the side effect of no longer populating inventory with a full config
file - the tests/inventory/openstack_user_config.yml file was a symlink
to etc/openstack_deploy/openstack_user_config.yml.aio, which only had a
handful of groups.
The result was that only 7 host entries appeared in inventory: aio1,
galera, rabbitmq, memcache, log, repo, and haproxy containers. These
coincide with the groups defined only in the
openstack_user_config.yml.aio file.
To make the inventory tests more robust against changes like these, this
patch adds some module-level fixtures that generate the test
configuration file from all the files ending with '.aio'. This file is
dynamically constructed at the beginning of each test run, and deleted
afterwards. The symlink to the openstack_user_config.yml.aio file is
removed in favor of this dynamically created file, though a placeholder
file remains to keep the directory in git.
A test was added to ensure any configured groups have hosts added; this
should serve as a safeguard against similar reconstructions. No check is
made for groups defined by the environment but unpopulated by config,
such as cinder_volumes_containers.
The original file could have been kept as a placeholder, but it would be
marked as changed by git after every test run.
The management tests were also updated to make use of the dynamic
configuration creation.
Change-Id: Ie4ba9c50315736a0b86e0caa2cccb0908c452a49
2016-09-22 16:48:49 -04:00
|
|
|
def setUpModule():
|
|
|
|
test_inventory.make_config()
|
|
|
|
|
|
|
|
|
|
|
|
def tearDownModule():
|
|
|
|
os.remove(test_inventory.USER_CONFIG_FILE)
|
|
|
|
|
|
|
|
|
2016-09-16 16:24:23 -04:00
|
|
|
class TestExportFunction(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.inv = test_inventory.get_inventory()
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
test_inventory.cleanup()
|
|
|
|
|
|
|
|
def test_host_is_present(self):
|
|
|
|
host_inv = mi.export_host_info(self.inv)['hosts']
|
|
|
|
self.assertIn('aio1', host_inv.keys())
|
|
|
|
|
|
|
|
def test_groups_added(self):
|
|
|
|
host_inv = mi.export_host_info(self.inv)['hosts']
|
|
|
|
self.assertIn('groups', host_inv['aio1'].keys())
|
|
|
|
|
|
|
|
def test_variables_added(self):
|
|
|
|
host_inv = mi.export_host_info(self.inv)['hosts']
|
|
|
|
self.assertIn('hostvars', host_inv['aio1'].keys())
|
|
|
|
|
|
|
|
def test_number_of_hosts(self):
|
|
|
|
host_inv = mi.export_host_info(self.inv)['hosts']
|
|
|
|
|
|
|
|
self.assertEqual(len(self.inv['_meta']['hostvars']),
|
|
|
|
len(host_inv))
|
|
|
|
|
|
|
|
def test_all_information_added(self):
|
|
|
|
all_info = mi.export_host_info(self.inv)['all']
|
|
|
|
self.assertIn('provider_networks', all_info)
|
|
|
|
|
|
|
|
def test_all_lb_information(self):
|
|
|
|
all_info = mi.export_host_info(self.inv)['all']
|
|
|
|
inv_all = self.inv['all']['vars']
|
|
|
|
self.assertEqual(inv_all['internal_lb_vip_address'],
|
|
|
|
all_info['internal_lb_vip_address'])
|
|
|
|
|
|
|
|
|
2016-10-24 11:44:55 -04:00
|
|
|
class TestRemoveIpfunction(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.inv = test_inventory.get_inventory()
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
test_inventory.cleanup()
|
|
|
|
|
|
|
|
def test_ips_removed(self):
|
|
|
|
mi.remove_ip_addresses(self.inv)
|
2017-02-13 23:06:14 +00:00
|
|
|
mi.remove_ip_addresses(self.inv, TARGET_DIR)
|
2016-10-24 11:44:55 -04:00
|
|
|
hostvars = self.inv['_meta']['hostvars']
|
|
|
|
|
|
|
|
for host, variables in hostvars.items():
|
|
|
|
has_networks = 'container_networks' in variables
|
|
|
|
if variables.get('is_metal', False):
|
|
|
|
continue
|
|
|
|
self.assertFalse(has_networks)
|
|
|
|
|
2017-02-13 23:06:14 +00:00
|
|
|
def test_inventory_item_removed(self):
|
|
|
|
inventory = self.inv
|
|
|
|
|
|
|
|
# Make sure we have log_hosts in the original inventory
|
|
|
|
self.assertIn('log_hosts', inventory)
|
|
|
|
|
|
|
|
mi.remove_inventory_item("log_hosts", inventory)
|
|
|
|
mi.remove_inventory_item("log_hosts", inventory, TARGET_DIR)
|
|
|
|
|
|
|
|
# No make sure it's gone
|
|
|
|
self.assertIn('log_hosts', inventory)
|
|
|
|
|
2016-10-24 11:44:55 -04:00
|
|
|
def test_metal_ips_kept(self):
|
|
|
|
mi.remove_ip_addresses(self.inv)
|
|
|
|
hostvars = self.inv['_meta']['hostvars']
|
|
|
|
|
|
|
|
for host, variables in hostvars.items():
|
|
|
|
has_networks = 'container_networks' in variables
|
|
|
|
if not variables.get('is_metal', False):
|
|
|
|
continue
|
|
|
|
self.assertTrue(has_networks)
|
|
|
|
|
|
|
|
def test_ansible_host_vars_removed(self):
|
|
|
|
mi.remove_ip_addresses(self.inv)
|
|
|
|
hostvars = self.inv['_meta']['hostvars']
|
|
|
|
|
|
|
|
for host, variables in hostvars.items():
|
|
|
|
has_host = 'ansible_host' in variables
|
|
|
|
if variables.get('is_metal', False):
|
|
|
|
continue
|
|
|
|
self.assertFalse(has_host)
|
|
|
|
|
|
|
|
def test_multiple_calls(self):
|
|
|
|
"""Removal should fail silently if keys are absent."""
|
|
|
|
mi.remove_ip_addresses(self.inv)
|
|
|
|
mi.remove_ip_addresses(self.inv)
|
|
|
|
|
|
|
|
|
2016-09-16 16:24:23 -04:00
|
|
|
if __name__ == '__main__':
|
2017-07-06 15:37:45 -04:00
|
|
|
unittest.main(catchbreak=True)
|