Change defaults for --url

Defaults for --url param has been changed to env[REFSTACK_URL] if
it is set and 'http://api.refstack.net' if env[REFSTACK_URL] is not set.
It will be helpull during development and for upload to local refstack
server.

Change-Id: I8de8dfba1f6741133585815ace5cfd2ca2780fa2
This commit is contained in:
sslypushenko 2015-05-07 21:10:10 +03:00
parent a5059305a9
commit 31cd05d8e1
2 changed files with 42 additions and 8 deletions
refstack_client

@ -163,6 +163,17 @@ class RefstackClient:
file.write(json.dumps(results, indent=4, separators=(',', ': ')))
file.close()
def _user_query(self, q):
"""Ask user a query. Return true if user agreed (yes/y)"""
if self.args.quiet:
return True
try:
inp = six.moves.input(q + ' (yes/y): ')
except KeyboardInterrupt:
return
else:
return inp.lower() in ('yes', 'y')
def get_passed_tests(self, result_file):
'''Get a list of tests IDs that passed Tempest from a subunit file.'''
subunit_processor = SubunitProcessor(result_file)
@ -278,8 +289,10 @@ class RefstackClient:
json_file = open(self.upload_file)
json_data = json.load(json_file)
json_file.close()
self.post_results(self.args.url, json_data,
sign_with=self.args.priv_key)
if self._user_query('Test results will be uploaded to %s. '
'Ok?' % self.args.url):
self.post_results(self.args.url, json_data,
sign_with=self.args.priv_key)
def yield_results(self, url, start_page=1,
start_date='', end_date='', cpid=''):
@ -339,9 +352,12 @@ def parse_cli_args(args=None):
shared_args.add_argument('--url',
action='store',
required=False,
default='http://api.refstack.net',
default=os.environ.get(
'REFSTACK_URL', 'http://api.refstack.net'),
type=str,
help='Refstack API URL to upload results to '
help='Refstack API URL to upload results to. '
'Defaults to env[REFSTACK_URL] or '
'http://api.refstack.net if it is not set '
'(--url http://localhost:8000).')
shared_args.add_argument('-i', '--sign',
@ -351,6 +367,11 @@ def parse_cli_args(args=None):
help='Private RSA key. '
'OpenSSH RSA keys format supported ('
'-i ~/.ssh/id-rsa)')
shared_args.add_argument('-y',
action='store_true',
dest='quiet',
required=False,
help='Assume Yes to all prompt queries')
# Upload command
parser_upload = subparsers.add_parser(

@ -52,7 +52,7 @@ class TestRefstackClient(unittest.TestCase):
:return: argv
"""
argv = [command,
'--url', 'http://127.0.0.1']
'--url', 'http://127.0.0.1', '-y']
if command == 'test':
argv.extend(
('-c', kwargs.get('conf_file_name', self.conf_file_name)))
@ -228,6 +228,19 @@ class TestRefstackClient(unittest.TestCase):
]
self.assertEqual(expected, results)
@mock.patch('six.moves.input')
def test_user_query(self, mock_input):
client = rc.RefstackClient(rc.parse_cli_args(self.mock_argv()))
self.assertTrue(client._user_query('42?'))
mock_input.return_value = 'n'
cli_args = self.mock_argv()
cli_args.remove('-y')
client = rc.RefstackClient(rc.parse_cli_args(cli_args))
self.assertFalse(client._user_query('42?'))
mock_input.return_value = 'yes'
self.assertTrue(client._user_query('42?'))
def test_post_results(self):
"""
Test the post_results method, ensuring a requests call is made.
@ -435,8 +448,8 @@ class TestRefstackClient(unittest.TestCase):
Test that the upload command runs as expected.
"""
upload_file_path = self.test_path + "/.testrepository/0.json"
args = rc.parse_cli_args(['upload', upload_file_path,
'--url', 'http://api.test.org'])
args = rc.parse_cli_args(
self.mock_argv(command='upload') + [upload_file_path])
client = rc.RefstackClient(args)
client.post_results = MagicMock()
@ -450,7 +463,7 @@ class TestRefstackClient(unittest.TestCase):
'uuid': '0146f675-ffbd-4208-b3a4-60eb628dbc5e'}
]
}
client.post_results.assert_called_with('http://api.test.org',
client.post_results.assert_called_with('http://127.0.0.1',
expected_json,
sign_with=None)