From ad7536d6b593947bc10bab3d69d9d9736ddfc84b Mon Sep 17 00:00:00 2001 From: Steve Noyes Date: Fri, 20 May 2016 17:07:05 -0400 Subject: [PATCH] add version to api, misc fixes - add version to api - update cli version - ignore property files found in host and var directories that do not have corresponding host/groups in the inventory. - clear out saved test property files prior to running the test, as they could unexpectedly be put back if they didn't exist in the var directories. Jira-Issue: None --- kollacli/api/client.py | 5 +++ kollacli/common/properties.py | 52 ++++++++++++---------- kollacli/shell.py | 4 +- setup.cfg | 6 +-- tests/common.py | 6 ++- tests/versions.py | 84 +++++++++++++++++++++++++++++++++++ 6 files changed, 128 insertions(+), 29 deletions(-) create mode 100644 tests/versions.py diff --git a/kollacli/api/client.py b/kollacli/api/client.py index 8c144f9..0730540 100644 --- a/kollacli/api/client.py +++ b/kollacli/api/client.py @@ -34,6 +34,8 @@ LOG_FILE_MESSAGE_FORMAT = \ '[%(asctime)s] %(levelname)-8s %(name)s %(message)s' LOG = None +VERSION = '1.0' + class ClientApi( AsyncApi, @@ -49,6 +51,9 @@ class ClientApi( def __init__(self): self._configure_logging() + def get_version(self): + return VERSION + def base_call(self): LOG.info('base call') diff --git a/kollacli/common/properties.py b/kollacli/common/properties.py index fa8a613..b1a53c4 100644 --- a/kollacli/common/properties.py +++ b/kollacli/common/properties.py @@ -56,8 +56,10 @@ class AnsibleProperties(object): self.group_props = {} self.host_props = {} self.properties_loaded = False + self._inventory = None def _load_properties(self): + self._load_inventory() if not self.properties_loaded: self._load_properties_roles() self._load_properties_all() @@ -121,7 +123,11 @@ class AnsibleProperties(object): def _load_properties_hostvars(self): host_dir = get_host_vars_dir() + hostnames = self._inventory.get_hostnames() for hostfile in os.listdir(host_dir): + if hostfile not in hostnames: + # skip any host files that don't match existing hosts + continue self.host_props[hostfile] = [] with open(os.path.join(host_dir, hostfile)) as host_data: host_contents = yaml.safe_load(host_data) @@ -148,12 +154,10 @@ class AnsibleProperties(object): def _load_properties_groupvars(self): group_dir = get_group_vars_dir() + groupnames = self._inventory.get_groupnames() for groupfile in os.listdir(group_dir): - if (groupfile == 'all.yml'): - continue - self.group_props[groupfile] = [] - # don't load __GLOBAL__ as a group property list as it is globals - if groupfile == '__GLOBAL__': + if groupfile not in groupnames: + # skip any files that don't match existing groups continue with open(os.path.join(group_dir, groupfile)) as group_data: group_contents = yaml.safe_load(group_data) @@ -178,19 +182,22 @@ class AnsibleProperties(object): props.append(ansible_prop) self.group_props[groupfile] = props + def _load_inventory(self): + if not self._inventory: + self._inventory = Inventory.load() # nosec + def get_host_list(self, host_list): self._load_properties() prop_list = [] - inventory = Inventory.load() # nosec if host_list is not None: for host_name in host_list: - host = inventory.get_host(host_name) + host = self._inventory.get_host(host_name) if host is None: raise NotInInventory(u._('Host'), host_name) if host_name in self.host_props: prop_list += self.host_props[host_name] else: - hosts = inventory.get_hosts() + hosts = self._inventory.get_hosts() for host in hosts: if host.name in self.host_props: prop_list += self.host_props[host.name] @@ -199,16 +206,15 @@ class AnsibleProperties(object): def get_group_list(self, group_list): self._load_properties() prop_list = [] - inventory = Inventory.load() # nosec if group_list is not None: for group_name in group_list: - group = inventory.get_group(group_name) + group = self._inventory.get_group(group_name) if group is None: raise NotInInventory(u._('Group'), group_name) if group_name in self.group_props: prop_list += self.group_props[group_name] else: - groups = inventory.get_groups() + groups = self._inventory.get_groups() for group in groups: if group.name in self.group_props: prop_list += self.group_props[group.name] @@ -257,13 +263,13 @@ class AnsibleProperties(object): def set_host_property(self, property_dict, hosts): # if hosts is None set the property on all hosts - inventory = Inventory.load() # nosec + self._load_inventory() host_list = [] if hosts is None: - host_list = inventory.get_hosts() + host_list = self._inventory.get_hosts() else: for host_name in hosts: - host = inventory.get_host(host_name) + host = self._inventory.get_host(host_name) if host is None: raise NotInInventory(u._('Host'), host_name) host_list.append(host) @@ -277,13 +283,13 @@ class AnsibleProperties(object): def set_group_property(self, property_dict, groups): # if groups is None set the property on all hosts - inventory = Inventory.load() # nosec + self._load_inventory() group_list = [] if groups is None: - group_list = inventory.get_groups() + group_list = self._inventory.get_groups() else: for group_name in groups: - group = inventory.get_group(group_name) + group = self._inventory.get_group(group_name) if group is None: raise NotInInventory(u._('Group'), group_name) group_list.append(group) @@ -306,13 +312,13 @@ class AnsibleProperties(object): def clear_host_property(self, property_list, hosts): # if hosts is None set the property on all hosts - inventory = Inventory.load() # nosec + self._load_inventory() host_list = [] if hosts is None: - host_list = inventory.get_hosts() + host_list = self._inventory.get_hosts() else: for host_name in hosts: - host = inventory.get_host(host_name) + host = self._inventory.get_host(host_name) if host is None: raise NotInInventory(u._('Host'), host_name) host_list.append(host) @@ -326,13 +332,13 @@ class AnsibleProperties(object): def clear_group_property(self, property_list, groups): # if hosts is None set the property on all hosts - inventory = Inventory.load() # nosec + self._load_inventory() group_list = [] if groups is None: - group_list = inventory.get_groups() + group_list = self._inventory.get_groups() else: for group_name in groups: - group = inventory.get_group(group_name) + group = self._inventory.get_group(group_name) if group is None: raise NotInInventory(u._('Group'), group_name) group_list.append(group) diff --git a/kollacli/shell.py b/kollacli/shell.py index 27d5cb8..00aaa14 100755 --- a/kollacli/shell.py +++ b/kollacli/shell.py @@ -28,12 +28,14 @@ from kollacli.common.utils import get_kollacli_etc LOG = logging.getLogger(__name__) +VERSION = '0.3' + class KollaCli(App): def __init__(self): super(KollaCli, self).__init__( description=u._('Command-Line Client for OpenStack Kolla'), - version='0.2', + version=VERSION, command_manager=CommandManager('kolla.cli'), ) diff --git a/setup.cfg b/setup.cfg index 41b7023..44fce64 100644 --- a/setup.cfg +++ b/setup.cfg @@ -5,13 +5,13 @@ version = 3.0.1 description-file = README.rst -author = Borne Mace -author-email = borne.mace@oracle.com +author = Steve Noyes +author-email = steve.noyes@oracle.com url = https://ca-git.us.oracle.com/openstack-kollaclient classifier = - Development Status :: 3 - Alpha + Development Status :: 5 - Production/Stable Environment :: Console Intended Audience :: Developers Intended Audience :: Information Technology diff --git a/tests/common.py b/tests/common.py index 345cb7d..9923150 100644 --- a/tests/common.py +++ b/tests/common.py @@ -14,6 +14,7 @@ # import logging import os +import shutil import subprocess import sys import testtools @@ -213,8 +214,9 @@ class KollaCliTest(testtools.TestCase): def _save_dir(self, src_dir): dirname = os.path.basename(src_dir) save_dir = os.path.join('/tmp', dirname + '.utest.save') - if not os.path.exists(save_dir): - os.mkdir(save_dir) + if os.path.exists(save_dir): + shutil.rmtree(save_dir) + os.mkdir(save_dir) fnames = os.listdir(src_dir) for fname in fnames: src_path = os.path.join(src_dir, fname) diff --git a/tests/versions.py b/tests/versions.py new file mode 100644 index 0000000..f34970d --- /dev/null +++ b/tests/versions.py @@ -0,0 +1,84 @@ +# Copyright(c) 2016, Oracle and/or its affiliates. All Rights Reserved. +# +# 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. +# +from common import KollaCliTest + +from kollacli.api.client import ClientApi + +import os +import unittest + +CLIENT = ClientApi() + +PKG_VERSION_TAG = '%global package_version' +SETUP_VERSION_TAG = 'version =' + +RPM_SPEC_PATH = 'buildrpm/openstack-kollacli.spec' +SETUP_PATH = 'setup.cfg' + + +class TestFunctional(KollaCliTest): + + def test_versions(self): + """test versions + + cli versions are located in these files: + - setup.cfg + - buildrpm/openstack-kollacli.spec + + This will verify that they all match what is in the rpm spec. + + This test only runs if the cwd is either the root dev directory or the + tools directory. This should work in both tox and eclipse environments. + """ + cwd = os.getcwd() + prefix = None + if os.path.exists(os.path.join(cwd, RPM_SPEC_PATH)): + prefix = '' + elif os.path.exists(os.path.join(cwd, '../' + RPM_SPEC_PATH)): + prefix = '../' + if prefix is None: + # skip test, can't find rpm spec file + self.log.info('No rpm spec file found. cwd is %s. ' % cwd, + 'Skipping test.') + return + + pkg_version = self._get_version_from_file(prefix + RPM_SPEC_PATH, + PKG_VERSION_TAG) + setup_version = self._get_version_from_file(prefix + SETUP_PATH, + SETUP_VERSION_TAG) + self.assertEqual(pkg_version, setup_version, + 'rpm_spec vs setup.cfg mis-match') + + # check that the client version is readble + version = CLIENT.get_version() + self.assertIsNotNone(version, 'version is None') + + def _get_version_from_file(self, path, tag): + version = None + with open(path, 'r') as vfile: + for line in vfile: + if line.startswith(tag): + version = line.split(tag)[1] + version = version.strip() + version = version.replace("'", "") + break + self.assertIsNotNone(version, + 'version tag: [%s], not found in file: %s' + % (tag, path)) + return version + + +if __name__ == '__main__': + unittest.main()