Merge "Increase Test coverage"

This commit is contained in:
Jenkins 2017-03-20 18:35:21 +00:00 committed by Gerrit Code Review
commit 67976c4826
2 changed files with 21 additions and 121 deletions

View File

@ -24,7 +24,6 @@ Command-line interface to the OpenStack Magnum API.
from __future__ import print_function from __future__ import print_function
import argparse import argparse
import getpass
import logging import logging
import os import os
import sys import sys
@ -80,126 +79,6 @@ def positive_non_zero_float(text):
return value return value
class SecretsHelper(object):
def __init__(self, args, client):
self.args = args
self.client = client
self.key = None
def _validate_string(self, text):
if text is None or len(text) == 0:
return False
return True
def _make_key(self):
if self.key is not None:
return self.key
keys = [
self.client.auth_url,
self.client.projectid,
self.client.user,
self.client.region_name,
self.client.endpoint_type,
self.client.service_type,
self.client.service_name,
self.client.volume_service_name,
]
for (index, key) in enumerate(keys):
if key is None:
keys[index] = '?'
else:
keys[index] = str(keys[index])
self.key = "/".join(keys)
return self.key
def _prompt_password(self, verify=True):
pw = None
if hasattr(sys.stdin, 'isatty') and sys.stdin.isatty():
# Check for Ctl-D
try:
while True:
pw1 = getpass.getpass('OS Password: ')
if verify:
pw2 = getpass.getpass('Please verify: ')
else:
pw2 = pw1
if pw1 == pw2 and self._validate_string(pw1):
pw = pw1
break
except EOFError:
pass
return pw
def save(self, auth_token, management_url, tenant_id):
if not HAS_KEYRING or not self.args.os_cache:
return
if (auth_token == self.auth_token and
management_url == self.management_url):
# Nothing changed....
return
if not all([management_url, auth_token, tenant_id]):
raise ValueError("Unable to save empty management url/auth token")
value = "|".join([str(auth_token),
str(management_url),
str(tenant_id)])
keyring.set_password("magnumclient_auth", self._make_key(), value)
@property
def password(self):
if self._validate_string(self.args.os_password):
return self.args.os_password
verify_pass = (
strutils.bool_from_string(cliutils.env("OS_VERIFY_PASSWORD"))
)
return self._prompt_password(verify_pass)
@property
def management_url(self):
if not HAS_KEYRING or not self.args.os_cache:
return None
management_url = None
try:
block = keyring.get_password('magnumclient_auth',
self._make_key())
if block:
_token, management_url, _tenant_id = block.split('|', 2)
except all_errors:
pass
return management_url
@property
def auth_token(self):
# Now is where it gets complicated since we
# want to look into the keyring module, if it
# exists and see if anything was provided in that
# file that we can use.
if not HAS_KEYRING or not self.args.os_cache:
return None
token = None
try:
block = keyring.get_password('magnumclient_auth',
self._make_key())
if block:
token, _management_url, _tenant_id = block.split('|', 2)
except all_errors:
pass
return token
@property
def tenant_id(self):
if not HAS_KEYRING or not self.args.os_cache:
return None
tenant_id = None
try:
block = keyring.get_password('magnumclient_auth',
self._make_key())
if block:
_token, _management_url, tenant_id = block.split('|', 2)
except all_errors:
pass
return tenant_id
class MagnumClientArgumentParser(argparse.ArgumentParser): class MagnumClientArgumentParser(argparse.ArgumentParser):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):

View File

@ -15,6 +15,7 @@
import re import re
import sys import sys
import argparse
import fixtures import fixtures
from keystoneauth1 import fixture from keystoneauth1 import fixture
import mock import mock
@ -82,6 +83,26 @@ class ShellTest(utils.TestCase):
'magnumclient.common.cliutils.isunauthenticated').start() 'magnumclient.common.cliutils.isunauthenticated').start()
self.nc_util.return_value = False self.nc_util.return_value = False
def test_positive_non_zero_float(self):
output = magnumclient.shell.positive_non_zero_float(None)
self.assertEqual(None, output)
output = magnumclient.shell.positive_non_zero_float(5)
self.assertEqual(5, output)
output = magnumclient.shell.positive_non_zero_float(5.0)
self.assertEqual(5.0, output)
self.assertRaises(argparse.ArgumentTypeError,
magnumclient.shell.positive_non_zero_float,
"Invalid")
self.assertRaises(argparse.ArgumentTypeError,
magnumclient.shell.positive_non_zero_float, -1)
self.assertRaises(argparse.ArgumentTypeError,
magnumclient.shell.positive_non_zero_float, 0)
def test_help_unknown_command(self): def test_help_unknown_command(self):
self.assertRaises(exceptions.CommandError, self.shell, 'help foofoo') self.assertRaises(exceptions.CommandError, self.shell, 'help foofoo')