Merge "Fix aodh client fails when command with the arg --time-constraint"
This commit is contained in:
@@ -129,7 +129,7 @@ class AodhClientTest(base.ClientTestBase):
|
||||
# CREATE
|
||||
result = self.aodh(u'alarm',
|
||||
params=(u"create --type threshold --name alarm1 "
|
||||
" -m meter_name --threshold 5 "
|
||||
"-m meter_name --threshold 5 "
|
||||
"--project-id %s" % PROJECT_ID))
|
||||
alarm = self.details_multiple(result)[0]
|
||||
ALARM_ID = alarm['alarm_id']
|
||||
@@ -137,6 +137,22 @@ class AodhClientTest(base.ClientTestBase):
|
||||
self.assertEqual('meter_name', alarm['meter_name'])
|
||||
self.assertEqual('5.0', alarm['threshold'])
|
||||
|
||||
# CREATE WITH --TIME-CONSTRAINT
|
||||
result = self.aodh(
|
||||
u'alarm',
|
||||
params=(u"create --type threshold --name alarm_tc "
|
||||
"-m meter_name --threshold 5 "
|
||||
"--time-constraint "
|
||||
"name=cons1;start='0 11 * * *';duration=300 "
|
||||
"--time-constraint "
|
||||
"name=cons2;start='0 23 * * *';duration=600 "
|
||||
"--project-id %s" % PROJECT_ID))
|
||||
alarm = self.details_multiple(result)[0]
|
||||
self.assertEqual('alarm_tc', alarm['name'])
|
||||
self.assertEqual('meter_name', alarm['meter_name'])
|
||||
self.assertEqual('5.0', alarm['threshold'])
|
||||
self.assertIsNotNone(alarm['time_constraints'])
|
||||
|
||||
# CREATE FAIL
|
||||
result = self.aodh(u'alarm',
|
||||
params=(u"create --type threshold --name alarm1 "
|
||||
|
||||
@@ -116,7 +116,8 @@ class CliAlarmCreateTest(testtools.TestCase):
|
||||
'--repeat-action', 'True',
|
||||
'--insufficient-data-action',
|
||||
'http://something/insufficient',
|
||||
'--time-constraint', '',
|
||||
'--time-constraint',
|
||||
'name=cons1;start="0 11 * * *";duration=300;description=desc1',
|
||||
'--meter-name', 'cpu',
|
||||
'--period', '60',
|
||||
'--evaluation-periods', '60',
|
||||
@@ -145,7 +146,10 @@ class CliAlarmCreateTest(testtools.TestCase):
|
||||
'ok_actions': ['http://something/ok'],
|
||||
'insufficient_data_actions':
|
||||
['http://something/insufficient'],
|
||||
'time_constraints': [''],
|
||||
'time_constraints': [{'description': 'desc1',
|
||||
'duration': '300',
|
||||
'name': 'cons1',
|
||||
'start': '0 11 * * *'}],
|
||||
'repeat_actions': True,
|
||||
'threshold_rule': {
|
||||
'meter_name': 'cpu',
|
||||
@@ -192,3 +196,21 @@ class CliAlarmCreateTest(testtools.TestCase):
|
||||
}
|
||||
alarm_rep = self.cli_alarm_create._alarm_from_args(test_parsed_args)
|
||||
self.assertEqual(alarm, alarm_rep)
|
||||
|
||||
def test_validate_time_constraint(self):
|
||||
starts = ['0 11 * * *', ' 0 11 * * * ',
|
||||
'"0 11 * * *"', '\'0 11 * * *\'']
|
||||
for start in starts:
|
||||
string = 'name=const1;start=%s;duration=1' % start
|
||||
expected = dict(name='const1',
|
||||
start='0 11 * * *',
|
||||
duration='1')
|
||||
self.assertEqual(
|
||||
expected,
|
||||
self.cli_alarm_create.validate_time_constraint(string))
|
||||
|
||||
def test_validate_time_constraint_with_bad_format(self):
|
||||
string = 'name=const2;start="0 11 * * *";duration:2'
|
||||
self.assertRaises(argparse.ArgumentTypeError,
|
||||
self.cli_alarm_create.validate_time_constraint,
|
||||
string)
|
||||
|
||||
@@ -10,6 +10,9 @@
|
||||
# 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
|
||||
|
||||
from cliff import command
|
||||
from cliff import lister
|
||||
from cliff import show
|
||||
@@ -134,6 +137,7 @@ class CliAlarmCreate(show.ShowOne):
|
||||
parser.add_argument(
|
||||
'--time-constraint', dest='time_constraints',
|
||||
metavar='<Time Constraint>', action='append',
|
||||
type=self.validate_time_constraint,
|
||||
help=('Only evaluate the alarm if the time at evaluation '
|
||||
'is within this time constraint. Start point(s) of '
|
||||
'the constraint are specified with a cron expression'
|
||||
@@ -231,6 +235,19 @@ class CliAlarmCreate(show.ShowOne):
|
||||
self.parser = parser
|
||||
return parser
|
||||
|
||||
def validate_time_constraint(self, values_to_convert):
|
||||
"""Converts 'a=1;b=2' to {a:1,b:2}."""
|
||||
|
||||
try:
|
||||
return dict((item.strip(" \"'")
|
||||
for item in kv.split("=", 1))
|
||||
for kv in values_to_convert.split(";"))
|
||||
except ValueError:
|
||||
msg = ('must be a list of '
|
||||
'key1=value1;key2=value2;... not %s'
|
||||
% values_to_convert)
|
||||
raise argparse.ArgumentTypeError(msg)
|
||||
|
||||
def _validate_args(self, parsed_args):
|
||||
if (parsed_args.type == 'threshold' and
|
||||
not (parsed_args.meter_name and parsed_args.threshold)):
|
||||
|
||||
Reference in New Issue
Block a user