2013-03-01 17:39:42 +02: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
|
2014-03-23 05:45:35 +09:00
|
|
|
import logging
|
2013-03-01 17:39:42 +02:00
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import sys
|
|
|
|
|
|
|
|
import fixtures
|
2014-02-13 11:13:12 +01:00
|
|
|
from mox3 import mox
|
2014-07-17 12:30:11 +04:00
|
|
|
import six
|
2013-03-01 17:39:42 +02:00
|
|
|
import testtools
|
|
|
|
from testtools import matchers
|
|
|
|
|
2013-07-02 18:44:42 -04:00
|
|
|
from neutronclient import shell as openstack_shell
|
2013-03-01 17:39:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
DEFAULT_USERNAME = 'username'
|
|
|
|
DEFAULT_PASSWORD = 'password'
|
|
|
|
DEFAULT_TENANT_ID = 'tenant_id'
|
|
|
|
DEFAULT_TENANT_NAME = 'tenant_name'
|
|
|
|
DEFAULT_AUTH_URL = 'http://127.0.0.1:5000/v2.0/'
|
|
|
|
DEFAULT_TOKEN = '3bcc3d3a03f44e3d8377f9247b0ad155'
|
|
|
|
DEFAULT_URL = 'http://quantum.example.org:9696/'
|
|
|
|
|
|
|
|
|
|
|
|
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,
|
2014-08-13 10:08:17 +10:00
|
|
|
'OS_AUTH_URL': DEFAULT_AUTH_URL,
|
|
|
|
'OS_REGION_NAME': None,
|
|
|
|
'HTTP_PROXY': None,
|
|
|
|
'http_proxy': None,
|
|
|
|
}
|
2013-03-01 17:39:42 +02:00
|
|
|
|
|
|
|
# Patch os.environ to avoid required auth info.
|
|
|
|
def setUp(self):
|
|
|
|
super(ShellTest, self).setUp()
|
2013-04-01 13:27:28 +08:00
|
|
|
self.mox = mox.Mox()
|
2013-03-01 17:39:42 +02:00
|
|
|
for var in self.FAKE_ENV:
|
|
|
|
self.useFixture(
|
|
|
|
fixtures.EnvironmentVariable(
|
|
|
|
var, self.FAKE_ENV[var]))
|
|
|
|
|
2015-09-17 12:11:59 +05:30
|
|
|
def shell(self, argstr, check=False, expected_val=0):
|
|
|
|
# expected_val is the expected return value after executing
|
|
|
|
# the command in NeutronShell
|
2014-03-23 05:45:35 +09:00
|
|
|
orig = (sys.stdout, sys.stderr)
|
2013-03-01 17:39:42 +02:00
|
|
|
clean_env = {}
|
|
|
|
_old_env, os.environ = os.environ, clean_env.copy()
|
|
|
|
try:
|
2014-02-17 14:34:45 +08:00
|
|
|
sys.stdout = six.moves.cStringIO()
|
|
|
|
sys.stderr = six.moves.cStringIO()
|
2013-07-02 18:44:42 -04:00
|
|
|
_shell = openstack_shell.NeutronShell('2.0')
|
2013-03-01 17:39:42 +02:00
|
|
|
_shell.run(argstr.split())
|
|
|
|
except SystemExit:
|
|
|
|
exc_type, exc_value, exc_traceback = sys.exc_info()
|
2015-09-17 12:11:59 +05:30
|
|
|
self.assertEqual(expected_val, exc_value.code)
|
2013-03-01 17:39:42 +02:00
|
|
|
finally:
|
2014-03-23 05:45:35 +09:00
|
|
|
stdout = sys.stdout.getvalue()
|
|
|
|
stderr = sys.stderr.getvalue()
|
2013-03-01 17:39:42 +02:00
|
|
|
sys.stdout.close()
|
2014-03-23 05:45:35 +09:00
|
|
|
sys.stderr.close()
|
|
|
|
sys.stdout, sys.stderr = orig
|
2013-03-01 17:39:42 +02:00
|
|
|
os.environ = _old_env
|
2014-03-23 05:45:35 +09:00
|
|
|
return stdout, stderr
|
2013-03-01 17:39:42 +02:00
|
|
|
|
|
|
|
def test_run_unknown_command(self):
|
2014-03-23 05:45:35 +09:00
|
|
|
self.useFixture(fixtures.FakeLogger(level=logging.DEBUG))
|
|
|
|
stdout, stderr = self.shell('fake', check=True)
|
|
|
|
self.assertFalse(stdout)
|
|
|
|
self.assertEqual("Unknown command ['fake']", stderr.strip())
|
2013-03-01 17:39:42 +02:00
|
|
|
|
|
|
|
def test_help(self):
|
|
|
|
required = 'usage:'
|
2014-03-23 05:45:35 +09:00
|
|
|
help_text, stderr = self.shell('help')
|
2013-03-01 17:39:42 +02:00
|
|
|
self.assertThat(
|
|
|
|
help_text,
|
|
|
|
matchers.MatchesRegex(required))
|
2014-03-23 05:45:35 +09:00
|
|
|
self.assertFalse(stderr)
|
2013-03-01 17:39:42 +02:00
|
|
|
|
2014-05-05 15:00:03 -04:00
|
|
|
def test_bash_completion(self):
|
|
|
|
required = '.*os_user_domain_id.*'
|
|
|
|
bash_completion, stderr = self.shell('bash-completion')
|
|
|
|
self.assertThat(
|
|
|
|
bash_completion,
|
|
|
|
matchers.MatchesRegex(required))
|
|
|
|
self.assertFalse(stderr)
|
|
|
|
|
2013-03-01 17:39:42 +02:00
|
|
|
def test_help_on_subcommand(self):
|
|
|
|
required = [
|
|
|
|
'.*?^usage: .* quota-list']
|
2014-03-23 05:45:35 +09:00
|
|
|
stdout, stderr = self.shell('help quota-list')
|
2013-03-01 17:39:42 +02:00
|
|
|
for r in required:
|
|
|
|
self.assertThat(
|
|
|
|
stdout,
|
|
|
|
matchers.MatchesRegex(r, re.DOTALL | re.MULTILINE))
|
2014-03-23 05:45:35 +09:00
|
|
|
self.assertFalse(stderr)
|
2013-03-01 17:39:42 +02:00
|
|
|
|
|
|
|
def test_help_command(self):
|
|
|
|
required = 'usage:'
|
2014-03-23 05:45:35 +09:00
|
|
|
help_text, stderr = self.shell('help network-create')
|
2013-03-01 17:39:42 +02:00
|
|
|
self.assertThat(
|
|
|
|
help_text,
|
|
|
|
matchers.MatchesRegex(required))
|
2014-03-23 05:45:35 +09:00
|
|
|
self.assertFalse(stderr)
|
2013-03-01 17:39:42 +02:00
|
|
|
|
2014-07-16 11:06:50 +08:00
|
|
|
def test_bash_completion_in_outputs_of_help_command(self):
|
|
|
|
help_text, stderr = self.shell('help')
|
|
|
|
self.assertFalse(stderr)
|
|
|
|
completion_cmd = "bash-completion"
|
|
|
|
completion_help_str = ("Prints all of the commands and options "
|
|
|
|
"for bash-completion.")
|
|
|
|
self.assertIn(completion_cmd, help_text)
|
|
|
|
self.assertIn(completion_help_str, help_text)
|
|
|
|
|
|
|
|
def test_bash_completion_command(self):
|
|
|
|
# just check we have some output
|
|
|
|
required = [
|
|
|
|
'.*--tenant_id',
|
|
|
|
'.*--client-certificate',
|
|
|
|
'.*help',
|
|
|
|
'.*gateway-device-create',
|
|
|
|
'.*--dns-nameserver']
|
|
|
|
help_text, stderr = self.shell('neutron bash-completion')
|
|
|
|
self.assertFalse(stderr)
|
|
|
|
for r in required:
|
|
|
|
self.assertThat(help_text,
|
|
|
|
matchers.MatchesRegex(r, re.DOTALL | re.MULTILINE))
|
|
|
|
|
2013-03-01 17:39:42 +02:00
|
|
|
def test_build_option_parser(self):
|
2013-07-02 18:44:42 -04:00
|
|
|
neutron_shell = openstack_shell.NeutronShell('2.0')
|
|
|
|
result = neutron_shell.build_option_parser('descr', '2.0')
|
2015-10-07 13:35:44 +05:30
|
|
|
self.assertIsInstance(result, argparse.ArgumentParser)
|
2013-04-01 13:27:28 +08:00
|
|
|
|
|
|
|
def test_main_with_unicode(self):
|
2013-07-02 18:44:42 -04:00
|
|
|
self.mox.StubOutClassWithMocks(openstack_shell, 'NeutronShell')
|
|
|
|
qshell_mock = openstack_shell.NeutronShell('2.0')
|
2013-04-01 13:27:28 +08:00
|
|
|
unicode_text = u'\u7f51\u7edc'
|
2014-08-26 15:36:04 +04:00
|
|
|
argv = ['net-list', unicode_text, unicode_text]
|
2013-04-01 13:27:28 +08:00
|
|
|
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()
|
2015-10-07 13:35:44 +05:30
|
|
|
self.assertEqual(0, ret)
|
2013-04-30 15:20:10 -06:00
|
|
|
|
|
|
|
def test_endpoint_option(self):
|
2013-07-02 18:44:42 -04:00
|
|
|
shell = openstack_shell.NeutronShell('2.0')
|
2013-04-30 15:20:10 -06:00
|
|
|
parser = shell.build_option_parser('descr', '2.0')
|
|
|
|
|
2014-05-05 15:00:03 -04:00
|
|
|
# Neither $OS_ENDPOINT_TYPE nor --os-endpoint-type
|
2013-04-30 15:20:10 -06:00
|
|
|
namespace = parser.parse_args([])
|
2015-10-17 17:57:32 -04:00
|
|
|
self.assertEqual('public', namespace.os_endpoint_type)
|
2013-04-30 15:20:10 -06:00
|
|
|
|
|
|
|
# --endpoint-type but not $OS_ENDPOINT_TYPE
|
2014-05-05 15:00:03 -04:00
|
|
|
namespace = parser.parse_args(['--os-endpoint-type=admin'])
|
|
|
|
self.assertEqual('admin', namespace.os_endpoint_type)
|
2013-04-30 15:20:10 -06:00
|
|
|
|
|
|
|
def test_endpoint_environment_variable(self):
|
|
|
|
fixture = fixtures.EnvironmentVariable("OS_ENDPOINT_TYPE",
|
|
|
|
"public")
|
|
|
|
self.useFixture(fixture)
|
|
|
|
|
2013-07-02 18:44:42 -04:00
|
|
|
shell = openstack_shell.NeutronShell('2.0')
|
2013-04-30 15:20:10 -06:00
|
|
|
parser = shell.build_option_parser('descr', '2.0')
|
|
|
|
|
|
|
|
# $OS_ENDPOINT_TYPE but not --endpoint-type
|
|
|
|
namespace = parser.parse_args([])
|
2014-05-05 15:00:03 -04:00
|
|
|
self.assertEqual("public", namespace.os_endpoint_type)
|
2013-04-30 15:20:10 -06:00
|
|
|
|
|
|
|
# --endpoint-type and $OS_ENDPOINT_TYPE
|
|
|
|
namespace = parser.parse_args(['--endpoint-type=admin'])
|
|
|
|
self.assertEqual('admin', namespace.endpoint_type)
|
2014-07-08 01:13:43 -07:00
|
|
|
|
|
|
|
def test_timeout_option(self):
|
|
|
|
shell = openstack_shell.NeutronShell('2.0')
|
|
|
|
parser = shell.build_option_parser('descr', '2.0')
|
|
|
|
|
|
|
|
# Neither $OS_ENDPOINT_TYPE nor --endpoint-type
|
|
|
|
namespace = parser.parse_args([])
|
2014-08-18 13:54:01 -07:00
|
|
|
self.assertIsNone(namespace.http_timeout)
|
2014-07-08 01:13:43 -07:00
|
|
|
|
|
|
|
# --endpoint-type but not $OS_ENDPOINT_TYPE
|
2014-08-18 13:54:01 -07:00
|
|
|
namespace = parser.parse_args(['--http-timeout=50'])
|
|
|
|
self.assertEqual(50, namespace.http_timeout)
|
2014-07-08 01:13:43 -07:00
|
|
|
|
|
|
|
def test_timeout_environment_variable(self):
|
|
|
|
fixture = fixtures.EnvironmentVariable("OS_NETWORK_TIMEOUT",
|
|
|
|
"50")
|
|
|
|
self.useFixture(fixture)
|
|
|
|
|
|
|
|
shell = openstack_shell.NeutronShell('2.0')
|
|
|
|
parser = shell.build_option_parser('descr', '2.0')
|
|
|
|
|
|
|
|
namespace = parser.parse_args([])
|
2014-08-18 13:54:01 -07:00
|
|
|
self.assertEqual(50, namespace.http_timeout)
|
2015-09-17 12:11:59 +05:30
|
|
|
|
|
|
|
def test_run_incomplete_command(self):
|
|
|
|
self.useFixture(fixtures.FakeLogger(level=logging.DEBUG))
|
|
|
|
cmd = (
|
|
|
|
'--os-username test --os-password test --os-project-id test '
|
|
|
|
'--os-auth-strategy keystone --os-auth-url '
|
|
|
|
'%s port-create' %
|
|
|
|
DEFAULT_AUTH_URL)
|
|
|
|
stdout, stderr = self.shell(cmd, check=True, expected_val=2)
|
|
|
|
search_str = "Try 'neutron help port-create' for more information"
|
|
|
|
self.assertTrue(any(search_str in string for string
|
|
|
|
in stderr.split('\n')))
|