Fix the argument check for account-generator

Concurrency parameter for account-generator command was accepting
negative values and zero.

This patch makes sure that when the account-generator is passed
a negative value or zero, it ends with an error.

Closes-Bug: #1811349
Partially-Implements: blueprint tempest-cli-unit-test-coverage
Change-Id: I4d7de89b2ad3ee91d113da3746fe393d8cce2aa2
This commit is contained in:
Lukas Piwowarski 2020-06-03 12:28:53 +00:00
parent 257f3b009f
commit 58b0538670
3 changed files with 38 additions and 1 deletions

View File

@ -0,0 +1,7 @@
---
fixes:
- |
Concurrency parameter for account-generator command was accepting
negative values and zero. The concurrency parameter now accepts only
positive numbers. When a negative value or zero is passed to the
program then the program ends and help is displayed.

View File

@ -97,6 +97,7 @@ To see help on specific argument, please do: ``tempest account-generator
[OPTIONS] <accounts_file.yaml> -h``. [OPTIONS] <accounts_file.yaml> -h``.
""" """
import argparse
import os import os
import traceback import traceback
@ -199,6 +200,14 @@ def dump_accounts(resources, identity_version, account_file):
LOG.info('%s generated successfully!', account_file) LOG.info('%s generated successfully!', account_file)
def positive_int(number):
number = int(number)
if number <= 0:
raise argparse.ArgumentTypeError("Concurrency value should be a "
"positive number")
return number
def _parser_add_args(parser): def _parser_add_args(parser):
parser.add_argument('-c', '--config-file', parser.add_argument('-c', '--config-file',
metavar='/etc/tempest.conf', metavar='/etc/tempest.conf',
@ -228,7 +237,7 @@ def _parser_add_args(parser):
help='Resources tag') help='Resources tag')
parser.add_argument('-r', '--concurrency', parser.add_argument('-r', '--concurrency',
default=1, default=1,
type=int, type=positive_int,
required=False, required=False,
dest='concurrency', dest='concurrency',
help='Concurrency count') help='Concurrency count')

View File

@ -337,3 +337,24 @@ class TestDumpAccountsV3(TestDumpAccountsV2):
def setUp(self): def setUp(self):
self.mock_domains() self.mock_domains()
super(TestDumpAccountsV3, self).setUp() super(TestDumpAccountsV3, self).setUp()
class TestAccountGeneratorCliCheck(base.TestCase):
def setUp(self):
super(TestAccountGeneratorCliCheck, self).setUp()
self.account_generator = account_generator.TempestAccountGenerator(
app=mock.Mock(), app_args=mock.Mock())
self.parser = self.account_generator.get_parser("generator")
def test_account_generator_zero_concurrency(self):
error = self.assertRaises(
SystemExit, lambda: self.parser.parse_args(
['-r', '0', 'accounts_file.yaml']))
self.assertTrue(error.code != 0)
def test_account_generator_negative_concurrency(self):
error = self.assertRaises(
SystemExit, lambda: self.parser.parse_args(
['-r', '-1', 'accounts_file.yaml']))
self.assertTrue(error.code != 0)