Increase Test coverage
Increase Test coverage from 80% to 84%. Removed unused code of SecretsHelper. Change-Id: I7bf81f44defde984c8201423447d4071682a8c99 Partially-Implements: magnumclient-ut-coverage
This commit is contained in:
parent
44ae7822a7
commit
278232620d
@ -24,7 +24,6 @@ Command-line interface to the OpenStack Magnum API.
|
||||
|
||||
from __future__ import print_function
|
||||
import argparse
|
||||
import getpass
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
@ -80,126 +79,6 @@ def positive_non_zero_float(text):
|
||||
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):
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
|
@ -15,6 +15,7 @@
|
||||
import re
|
||||
import sys
|
||||
|
||||
import argparse
|
||||
import fixtures
|
||||
from keystoneauth1 import fixture
|
||||
import mock
|
||||
@ -82,6 +83,26 @@ class ShellTest(utils.TestCase):
|
||||
'magnumclient.common.cliutils.isunauthenticated').start()
|
||||
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):
|
||||
self.assertRaises(exceptions.CommandError, self.shell, 'help foofoo')
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user