From df57eb800d981017b26e9129f983b595676981f2 Mon Sep 17 00:00:00 2001 From: Alexander Chadin Date: Fri, 14 Dec 2018 18:54:12 +0300 Subject: [PATCH] Pass API microversion to Client class Change-Id: If6ac21330cb6c8303f62eec6ee5082f523ff73a1 --- watcherclient/client.py | 7 ++++ watcherclient/shell.py | 1 + watcherclient/tests/functional/v1/base.py | 5 ++- .../tests/functional/v1/test_audit.py | 35 +++++++++++++++++-- 4 files changed, 45 insertions(+), 3 deletions(-) diff --git a/watcherclient/client.py b/watcherclient/client.py index d283995..0494543 100644 --- a/watcherclient/client.py +++ b/watcherclient/client.py @@ -182,4 +182,11 @@ def Client(version, *args, **kwargs): python-watcherclient's doc. """ api_version, client_class = _get_client_class_and_version(version) + + kw_api = kwargs.get('os_infra_optim_api_version') + endpoint = kwargs.get('endpoint') + # If both os_infra_optim_api_version and endpoint are not provided, get + # API version from arg. + if not kw_api and not endpoint: + kwargs['os_infra_optim_api_version'] = api_version.get_string() return client_class(*args, **kwargs) diff --git a/watcherclient/shell.py b/watcherclient/shell.py index 729e733..4f2e113 100644 --- a/watcherclient/shell.py +++ b/watcherclient/shell.py @@ -207,5 +207,6 @@ def main(argv=sys.argv[1:]): watcher_app = WatcherShell() return watcher_app.run(argv) + if __name__ == '__main__': # pragma: no cover sys.exit(main(sys.argv[1:])) diff --git a/watcherclient/tests/functional/v1/base.py b/watcherclient/tests/functional/v1/base.py index dbdd72d..53f521f 100644 --- a/watcherclient/tests/functional/v1/base.py +++ b/watcherclient/tests/functional/v1/base.py @@ -64,10 +64,13 @@ class TestCase(testtools.TestCase): delimiter_line = re.compile('^\+\-[\+\-]+\-\+$') + api_version = 1.0 + @classmethod def watcher(cls, cmd, fail_ok=False): """Executes watcherclient command for the given action.""" - return execute('watcher {0}'.format(cmd), fail_ok=fail_ok) + return execute('watcher --os-infra-optim-api-version {0} {1}'.format( + cls.api_version, cmd), fail_ok=fail_ok) @classmethod def get_opts(cls, fields, format='value'): diff --git a/watcherclient/tests/functional/v1/test_audit.py b/watcherclient/tests/functional/v1/test_audit.py index 25200fa..67d2da1 100644 --- a/watcherclient/tests/functional/v1/test_audit.py +++ b/watcherclient/tests/functional/v1/test_audit.py @@ -13,10 +13,11 @@ # See the License for the specific language governing permissions and # limitations under the License. -from oslo_utils import uuidutils - +from datetime import datetime +from dateutil import tz import functools +from oslo_utils import uuidutils from tempest.lib.common.utils import test_utils from watcherclient.tests.functional.v1 import base @@ -79,6 +80,36 @@ class AuditTests(base.TestCase): assert int(audit_output['Interval']) == 2 +class AuditTestsV11(AuditTests): + """This class tests v1.1 of Watcher API""" + + api_version = 1.1 + + detailed_list_fields = AuditTests.list_fields + [ + 'Created At', 'Updated At', 'Deleted At', 'Parameters', 'Interval', + 'Audit Scope', 'Next Run Time', 'Hostname', 'Start Time', 'End Time'] + + def test_audit_detailed_list(self): + raw_output = self.watcher('audit list --detail') + self.assert_table_structure([raw_output], self.detailed_list_fields) + + def test_audit_show(self): + audit = self.watcher('audit show ' + self.audit_uuid) + self.assertIn(self.audit_uuid, audit) + self.assert_table_structure([audit], self.detailed_list_fields) + + def test_audit_update(self): + local_time = datetime.now(tz.tzlocal()) + local_time_str = local_time.strftime("%Y-%m-%dT%H:%M:%S") + utc_time = (local_time - local_time.utcoffset()) + utc_time_str = utc_time.strftime("%Y-%m-%dT%H:%M:%S") + audit_raw_output = self.watcher( + 'audit update {0} replace end_time="{1}"'.format(self.audit_uuid, + local_time_str)) + audit_output = self.parse_show_as_object(audit_raw_output) + assert audit_output['End Time'] == utc_time_str + + class AuditActiveTests(base.TestCase): list_fields = ['UUID', 'Name', 'Audit Type', 'State', 'Goal', 'Strategy']