cFouts c34e3b7d2c Nova Style API Version Support for Client
The Manila client needs the following changes to support microversions:

*   Maintain backwards compatibility with Kilo. When the client detects
    that the server doesn't support microversions it will fall back to
    using the v1 API.
*   The --os-share-api-version option supports overriding the version.
*   If 1.0 is specified as the version the client will load the v1
    client and use the server's v1 API.
*   The client will send a request for the server's API version and
    determine if the client's supported versions and the server's
    supported versions overlap. If not the client will display an error
    and quit. See diagram 1 below.
*   The client supports the @wraps annotation. The annotation is used
    with the v2/shell.py commands and any class that inherits from
    the Manager class in manilaclient/base.py.
*   If an appropriate command version isn't found for commands using
    @wraps then the client will display an error and quit.

following commit: ab49d645befd04c84272f0d24e1b604012d191dd.

Diagram 1:
  Client:
         2.5           2.8
          |-------------|

  Server1:
     2.0           2.5
      |-------------|
     Client uses version 2.5

  Server2:
               2.7           2.10
                |-------------|
               Client uses version 2.8

  Server3:
                  2.9           2.12
                   |-------------|
                  Client displays error and quits

  Server4:
     1.0 (Kilo Server)
      |-|
     Client detects pre-microversion server and loads v1 client

Example usage of wraps annotation:
*   Support 2.0 - 2.4: @api_versions.wraps("2.0", "2.4")
*   Support 2.5 - latest: @api_versions.wraps("2.5")

Implements: blueprint manila-client-advanced-microversion-support

Change-Id: I3733fe85424e39566addc070d42609e508259f19
2015-11-16 19:03:55 +00:00

174 lines
6.4 KiB
Python

# 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 re
import sys
import ddt
import fixtures
import mock
from six import moves
from testtools import matchers
import manilaclient
from manilaclient.common import constants
from manilaclient import exceptions
from manilaclient import shell
from manilaclient.tests.unit import utils
@ddt.ddt
class OpenstackManilaShellTest(utils.TestCase):
FAKE_ENV = {
'OS_USERNAME': 'username',
'OS_PASSWORD': 'password',
'OS_TENANT_NAME': 'tenant_name',
'OS_AUTH_URL': 'http://no.where',
}
# Patch os.environ to avoid required auth info.
def set_env_vars(self, env_vars):
for k, v in env_vars.items():
self.useFixture(fixtures.EnvironmentVariable(k, v))
def shell_discover_client(self,
current_client,
os_api_version,
os_endpoint_type,
os_service_type,
client_args):
return current_client, manilaclient.API_MAX_VERSION
def shell(self, argstr):
orig = sys.stdout
try:
sys.stdout = moves.StringIO()
_shell = shell.OpenStackManilaShell()
_shell._discover_client = self.shell_discover_client
_shell.main(argstr.split())
except SystemExit:
exc_type, exc_value, exc_traceback = sys.exc_info()
self.assertEqual(exc_value.code, 0)
finally:
out = sys.stdout.getvalue()
sys.stdout.close()
sys.stdout = orig
return out
@ddt.data(
{},
{'OS_AUTH_URL': 'http://foo.bar'},
{'OS_AUTH_URL': 'http://foo.bar', 'OS_USERNAME': 'foo'},
{'OS_AUTH_URL': 'http://foo.bar', 'OS_USERNAME': 'foo_user',
'OS_PASSWORD': 'foo_password'},
{'OS_TENANT_NAME': 'foo_tenant', 'OS_USERNAME': 'foo_user',
'OS_PASSWORD': 'foo_password'},
)
def test_main_failure(self, env_vars):
self.set_env_vars(env_vars)
with mock.patch.object(shell, 'client') as mock_client:
self.assertRaises(exceptions.CommandError, self.shell, 'list')
self.assertFalse(mock_client.Client.called)
def test_main_success(self):
env_vars = {
'OS_AUTH_URL': 'http://foo.bar',
'OS_USERNAME': 'foo_username',
'OS_USER_ID': 'foo_user_id',
'OS_PASSWORD': 'foo_password',
'OS_TENANT_NAME': 'foo_tenant',
'OS_TENANT_ID': 'foo_tenant_id',
'OS_PROJECT_NAME': 'foo_project',
'OS_PROJECT_ID': 'foo_project_id',
'OS_PROJECT_DOMAIN_ID': 'foo_project_domain_id',
'OS_PROJECT_DOMAIN_NAME': 'foo_project_domain_name',
'OS_PROJECT_DOMAIN_ID': 'foo_project_domain_id',
'OS_USER_DOMAIN_NAME': 'foo_user_domain_name',
'OS_USER_DOMAIN_ID': 'foo_user_domain_id',
'OS_CERT': 'foo_cert',
}
self.set_env_vars(env_vars)
with mock.patch.object(shell, 'client') as mock_client:
self.shell('list')
mock_client.Client.assert_called_with(
manilaclient.API_MAX_VERSION,
username=env_vars['OS_USERNAME'],
password=env_vars['OS_PASSWORD'],
project_name=env_vars['OS_PROJECT_NAME'],
auth_url=env_vars['OS_AUTH_URL'],
insecure=False,
region_name='',
tenant_id=env_vars['OS_PROJECT_ID'],
endpoint_type='publicURL',
extensions=mock.ANY,
service_type=constants.V1_SERVICE_TYPE,
service_name='',
retries=0,
http_log_debug=False,
cacert=None,
use_keyring=False,
force_new_token=False,
user_id=env_vars['OS_USER_ID'],
user_domain_id=env_vars['OS_USER_DOMAIN_ID'],
user_domain_name=env_vars['OS_USER_DOMAIN_NAME'],
project_domain_id=env_vars['OS_PROJECT_DOMAIN_ID'],
project_domain_name=env_vars['OS_PROJECT_DOMAIN_NAME'],
cert=env_vars['OS_CERT'],
)
def test_help_unknown_command(self):
self.assertRaises(exceptions.CommandError, self.shell, 'help foofoo')
def test_help(self):
required = [
'.*?^usage: ',
'.*?^\s+create\s+Creates a new share '
'\(NFS, CIFS, GlusterFS or HDFS\).',
'.*?(?m)^See "manila help COMMAND" for help '
'on a specific command.',
]
help_text = self.shell('help')
for r in required:
self.assertThat(help_text,
matchers.MatchesRegex(r, re.DOTALL | re.MULTILINE))
def test_help_on_subcommand(self):
required = [
'.*?^usage: manila list',
'.*?(?m)^List NAS shares with filters.',
]
help_text = self.shell('help list')
for r in required:
self.assertThat(help_text,
matchers.MatchesRegex(r, re.DOTALL | re.MULTILINE))
def test_common_args_in_help_message(self):
expected_args = (
'--version', '', '--debug', '--os-cache', '--os-reset-cache',
'--os-user-id', '--os-username', '--os-password',
'--os-tenant-name', '--os-project-name', '--os-tenant-id',
'--os-project-id', '--os-user-domain-id', '--os-user-domain-name',
'--os-project-domain-id', '--os-project-domain-name',
'--os-auth-url', '--os-region-name', '--service-type',
'--service-name', '--share-service-name', '--endpoint-type',
'--os-share-api-version', '--os-cacert', '--retries', '--os-cert',
)
help_text = self.shell('help')
for expected_arg in expected_args:
self.assertIn(expected_arg, help_text)