2014-07-04 18:56:07 +09:00
|
|
|
# Copyright (C) 2013 Yahoo! Inc.
|
|
|
|
# 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.
|
|
|
|
|
|
|
|
import argparse
|
2017-03-18 06:12:33 +00:00
|
|
|
import logging
|
2014-07-04 18:56:07 +09:00
|
|
|
import os
|
|
|
|
import re
|
2016-08-11 16:38:08 +08:00
|
|
|
import six
|
2014-07-04 18:56:07 +09:00
|
|
|
import sys
|
|
|
|
|
|
|
|
import fixtures
|
|
|
|
import mox
|
|
|
|
import testtools
|
|
|
|
from testtools import matchers
|
|
|
|
|
2014-06-27 18:06:02 +09:00
|
|
|
from tackerclient.common import clientmanager
|
|
|
|
from tackerclient import shell as openstack_shell
|
2014-07-04 18:56:07 +09:00
|
|
|
|
|
|
|
|
|
|
|
DEFAULT_USERNAME = 'username'
|
|
|
|
DEFAULT_PASSWORD = 'password'
|
|
|
|
DEFAULT_TENANT_ID = 'tenant_id'
|
|
|
|
DEFAULT_TENANT_NAME = 'tenant_name'
|
2014-06-27 18:06:02 +09:00
|
|
|
DEFAULT_AUTH_URL = 'http://127.0.0.1:5000/v1.0/'
|
2014-07-04 18:56:07 +09:00
|
|
|
DEFAULT_TOKEN = '3bcc3d3a03f44e3d8377f9247b0ad155'
|
2016-06-10 17:02:53 +09:00
|
|
|
DEFAULT_URL = 'http://tacker.example.org:9890/'
|
2014-07-04 18:56:07 +09:00
|
|
|
|
|
|
|
|
|
|
|
class ShellTest(testtools.TestCase):
|
|
|
|
|
|
|
|
FAKE_ENV = {
|
|
|
|
'OS_USERNAME': DEFAULT_USERNAME,
|
|
|
|
'OS_PASSWORD': DEFAULT_PASSWORD,
|
|
|
|
'OS_TENANT_ID': DEFAULT_TENANT_ID,
|
|
|
|
'OS_TENANT_NAME': DEFAULT_TENANT_NAME,
|
|
|
|
'OS_AUTH_URL': DEFAULT_AUTH_URL}
|
|
|
|
|
|
|
|
# Patch os.environ to avoid required auth info.
|
|
|
|
def setUp(self):
|
|
|
|
super(ShellTest, self).setUp()
|
|
|
|
self.mox = mox.Mox()
|
|
|
|
for var in self.FAKE_ENV:
|
|
|
|
self.useFixture(
|
|
|
|
fixtures.EnvironmentVariable(
|
|
|
|
var, self.FAKE_ENV[var]))
|
|
|
|
|
|
|
|
def shell(self, argstr, check=False):
|
|
|
|
orig = (sys.stdout, sys.stderr)
|
|
|
|
clean_env = {}
|
|
|
|
_old_env, os.environ = os.environ, clean_env.copy()
|
|
|
|
try:
|
2016-08-11 16:38:08 +08:00
|
|
|
sys.stdout = six.StringIO()
|
|
|
|
sys.stderr = six.StringIO()
|
2014-06-27 18:06:02 +09:00
|
|
|
_shell = openstack_shell.TackerShell('1.0')
|
2014-07-04 18:56:07 +09:00
|
|
|
_shell.run(argstr.split())
|
|
|
|
except SystemExit:
|
|
|
|
exc_type, exc_value, exc_traceback = sys.exc_info()
|
|
|
|
self.assertEqual(exc_value.code, 0)
|
|
|
|
finally:
|
|
|
|
stdout = sys.stdout.getvalue()
|
|
|
|
stderr = sys.stderr.getvalue()
|
|
|
|
sys.stdout.close()
|
|
|
|
sys.stderr.close()
|
|
|
|
sys.stdout, sys.stderr = orig
|
|
|
|
os.environ = _old_env
|
|
|
|
return stdout, stderr
|
|
|
|
|
|
|
|
def test_run_unknown_command(self):
|
|
|
|
self.useFixture(fixtures.FakeLogger(level=logging.DEBUG))
|
|
|
|
stdout, stderr = self.shell('fake', check=True)
|
|
|
|
self.assertFalse(stdout)
|
|
|
|
self.assertEqual("Unknown command ['fake']", stderr.strip())
|
|
|
|
|
|
|
|
def test_help(self):
|
|
|
|
required = 'usage:'
|
|
|
|
help_text, stderr = self.shell('help')
|
|
|
|
self.assertThat(
|
|
|
|
help_text,
|
|
|
|
matchers.MatchesRegex(required))
|
|
|
|
self.assertFalse(stderr)
|
|
|
|
|
|
|
|
def test_help_on_subcommand(self):
|
|
|
|
required = [
|
2016-03-02 00:27:42 +05:30
|
|
|
'.*?^usage: .* vnfd-list']
|
|
|
|
stdout, stderr = self.shell('help vnfd-list')
|
2014-07-04 18:56:07 +09:00
|
|
|
for r in required:
|
|
|
|
self.assertThat(
|
|
|
|
stdout,
|
|
|
|
matchers.MatchesRegex(r, re.DOTALL | re.MULTILINE))
|
|
|
|
self.assertFalse(stderr)
|
|
|
|
|
|
|
|
def test_help_command(self):
|
|
|
|
required = 'usage:'
|
|
|
|
help_text, stderr = self.shell('help network-create')
|
|
|
|
self.assertThat(
|
|
|
|
help_text,
|
|
|
|
matchers.MatchesRegex(required))
|
|
|
|
self.assertFalse(stderr)
|
|
|
|
|
|
|
|
def test_unknown_auth_strategy(self):
|
|
|
|
self.useFixture(fixtures.FakeLogger(level=logging.DEBUG))
|
2014-06-27 18:06:02 +09:00
|
|
|
stdout, stderr = self.shell('--os-auth-strategy fake '
|
2016-03-02 00:27:42 +05:30
|
|
|
'vnfd-list')
|
2014-07-04 18:56:07 +09:00
|
|
|
self.assertFalse(stdout)
|
|
|
|
self.assertEqual('You must provide a service URL via '
|
|
|
|
'either --os-url or env[OS_URL]', stderr.strip())
|
|
|
|
|
|
|
|
def test_auth(self):
|
2014-06-27 18:06:02 +09:00
|
|
|
tacker_shell = openstack_shell.TackerShell('1.0')
|
2014-07-04 18:56:07 +09:00
|
|
|
self.addCleanup(self.mox.UnsetStubs)
|
|
|
|
self.mox.StubOutWithMock(clientmanager.ClientManager, '__init__')
|
2014-06-27 18:06:02 +09:00
|
|
|
self.mox.StubOutWithMock(tacker_shell, 'run_subcommand')
|
2014-07-04 18:56:07 +09:00
|
|
|
clientmanager.ClientManager.__init__(
|
|
|
|
token='', url='', auth_url='http://127.0.0.1:5000/',
|
|
|
|
tenant_name='test', tenant_id='tenant_id',
|
|
|
|
username='test', user_id='',
|
2015-12-17 22:06:20 -08:00
|
|
|
password='test', region_name='',
|
|
|
|
api_version={'nfv-orchestration': '1.0'},
|
|
|
|
auth_strategy='keystone',
|
|
|
|
service_type='nfv-orchestration',
|
2014-07-04 18:56:07 +09:00
|
|
|
endpoint_type='publicURL', insecure=False, ca_cert=None,
|
|
|
|
log_credentials=True)
|
2016-03-02 00:27:42 +05:30
|
|
|
tacker_shell.run_subcommand(['vnfd-list'])
|
2014-07-04 18:56:07 +09:00
|
|
|
self.mox.ReplayAll()
|
|
|
|
cmdline = ('--os-username test '
|
|
|
|
'--os-password test '
|
|
|
|
'--os-tenant-name test '
|
|
|
|
'--os-auth-url http://127.0.0.1:5000/ '
|
2016-03-02 00:27:42 +05:30
|
|
|
'--os-auth-strategy keystone vnfd-list')
|
2014-06-27 18:06:02 +09:00
|
|
|
tacker_shell.run(cmdline.split())
|
2014-07-04 18:56:07 +09:00
|
|
|
self.mox.VerifyAll()
|
|
|
|
|
|
|
|
def test_build_option_parser(self):
|
2014-06-27 18:06:02 +09:00
|
|
|
tacker_shell = openstack_shell.TackerShell('1.0')
|
|
|
|
result = tacker_shell.build_option_parser('descr', '1.0')
|
2016-09-28 10:30:59 +07:00
|
|
|
self.assertIsInstance(result, argparse.ArgumentParser)
|
2014-07-04 18:56:07 +09:00
|
|
|
|
|
|
|
def test_main_with_unicode(self):
|
2014-06-27 18:06:02 +09:00
|
|
|
self.mox.StubOutClassWithMocks(openstack_shell, 'TackerShell')
|
|
|
|
qshell_mock = openstack_shell.TackerShell('1.0')
|
2014-07-04 18:56:07 +09:00
|
|
|
unicode_text = u'\u7f51\u7edc'
|
|
|
|
argv = ['net-list', unicode_text, unicode_text.encode('utf-8')]
|
|
|
|
qshell_mock.run([u'net-list', unicode_text,
|
|
|
|
unicode_text]).AndReturn(0)
|
|
|
|
self.mox.ReplayAll()
|
|
|
|
ret = openstack_shell.main(argv=argv)
|
|
|
|
self.mox.VerifyAll()
|
|
|
|
self.mox.UnsetStubs()
|
|
|
|
self.assertEqual(ret, 0)
|
|
|
|
|
|
|
|
def test_endpoint_option(self):
|
2014-06-27 18:06:02 +09:00
|
|
|
shell = openstack_shell.TackerShell('1.0')
|
|
|
|
parser = shell.build_option_parser('descr', '1.0')
|
2014-07-04 18:56:07 +09:00
|
|
|
|
|
|
|
# Neither $OS_ENDPOINT_TYPE nor --endpoint-type
|
|
|
|
namespace = parser.parse_args([])
|
|
|
|
self.assertEqual('publicURL', namespace.endpoint_type)
|
|
|
|
|
|
|
|
# --endpoint-type but not $OS_ENDPOINT_TYPE
|
|
|
|
namespace = parser.parse_args(['--endpoint-type=admin'])
|
|
|
|
self.assertEqual('admin', namespace.endpoint_type)
|
|
|
|
|
|
|
|
def test_endpoint_environment_variable(self):
|
|
|
|
fixture = fixtures.EnvironmentVariable("OS_ENDPOINT_TYPE",
|
|
|
|
"public")
|
|
|
|
self.useFixture(fixture)
|
|
|
|
|
2014-06-27 18:06:02 +09:00
|
|
|
shell = openstack_shell.TackerShell('1.0')
|
|
|
|
parser = shell.build_option_parser('descr', '1.0')
|
2014-07-04 18:56:07 +09:00
|
|
|
|
|
|
|
# $OS_ENDPOINT_TYPE but not --endpoint-type
|
|
|
|
namespace = parser.parse_args([])
|
|
|
|
self.assertEqual("public", namespace.endpoint_type)
|
|
|
|
|
|
|
|
# --endpoint-type and $OS_ENDPOINT_TYPE
|
|
|
|
namespace = parser.parse_args(['--endpoint-type=admin'])
|
|
|
|
self.assertEqual('admin', namespace.endpoint_type)
|