From 9a6a7664c2709023390e49447545799d5c7ca805 Mon Sep 17 00:00:00 2001 From: Ikuo Kumagai Date: Fri, 21 Nov 2014 13:47:30 +0000 Subject: [PATCH] Change the unsuitable item caching for completion This fix changes the item that cached for completion from "human_id" to "name". A string for completion shoud not be changed in any way, because changed keyword can not use as command line parameter. But the "human_id" means "human readable id" that is changed from "name" by method "to_slug". "to_slug" is meant to create a valid path name from a string. The tools/nova.bash_completion take the completion string from the file ".novaclient/*/*-cache". The file is created when client call "list" command. For example , "nova list","nova image-list", and the others. Currently, items that are written to the cache file is the "id" and "human_id". Closes-Bug: #1193049 Change-Id: I241ec8b7c8729274ee43db6e360141fd381b265e --- novaclient/client.py | 4 ++-- novaclient/tests/unit/test_client.py | 29 ++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/novaclient/client.py b/novaclient/client.py index 200d75c73..b2a59474f 100644 --- a/novaclient/client.py +++ b/novaclient/client.py @@ -90,9 +90,9 @@ class CompletionCache(object): ~/.novaclient/ / -id-cache - -human-id-cache + -name-cache """ - def __init__(self, username, auth_url, attributes=('id', 'human_id')): + def __init__(self, username, auth_url, attributes=('id', 'name')): self.directory = self._make_directory_name(username, auth_url) self.attributes = attributes diff --git a/novaclient/tests/unit/test_client.py b/novaclient/tests/unit/test_client.py index 2c7f7eaf7..b7628b956 100644 --- a/novaclient/tests/unit/test_client.py +++ b/novaclient/tests/unit/test_client.py @@ -16,6 +16,7 @@ import json import logging +import os import fixtures import mock @@ -366,3 +367,31 @@ class ClientTest(utils.TestCase): self.assertIn('RESP BODY: {"access": {"token": {"id":' ' "{SHA1}4fc49c6a671ce889078ff6b250f7066cf6d2ada2"}}}', output) + + @mock.patch('novaclient.client.HTTPClient') + def test_completion_cache(self, instance): + cp_cache = novaclient.client.CompletionCache('user', + "server/v2") + + client = novaclient.v2.client.Client("user", + "password", "project_id", + auth_url="server/v2", + completion_cache=cp_cache) + + instance.id = "v1c49c6a671ce889078ff6b250f7066cf6d2ada2" + instance.name = "foo.bar.baz v1_1" + client.write_object_to_completion_cache(instance) + self.assertTrue(os.path.isdir(cp_cache.directory)) + + for file_name in os.listdir(cp_cache.directory): + file_path = os.path.join(cp_cache.directory, file_name) + f = open(file_path, 'r') + for line in f: + line = line.rstrip() + if '-id-' in file_name: + self.assertEqual(instance.id, line) + elif '-name-' in file_name: + self.assertEqual(instance.name, line) + f.close() + os.remove(file_path) + os.rmdir(cp_cache.directory)