示例#1
0
    def test_parse_range(self):
        """
        Test that the parse_range method returns the correct list of ints
        """
        # test comma-delimited
        one_three_five_str = '1,3,5'
        one_three_five_lst = [1, 3, 5]
        result = TimeWindow.parse_range(one_three_five_str)
        self.assertEqual(one_three_five_lst,
                         result,
                         msg='Could not parse comma-delimeted str.')

        # test range
        one_to_three_str = '1-3'
        one_to_three_lst = [1, 2, 3]
        result = TimeWindow.parse_range(one_to_three_str)
        self.assertEqual(one_to_three_lst,
                         result,
                         msg='Could not parse range str.')

        # test mix of commas and ranges
        one_three_four_to_six_str = '1,3,4-6'
        one_three_four_to_six_lst = [1, 3, 4, 5, 6]
        result = TimeWindow.parse_range(one_three_four_to_six_str)
        self.assertEqual(
            one_three_four_to_six_lst,
            result,
            msg='Could not parse mix of comma-delimited and range str.')

        # test invalid range
        zero_to_10_str = '0-10'
        zero_to_6_lst = [0, 1, 2, 3, 4, 5, 6]
        result = TimeWindow.parse_range(zero_to_10_str)
        self.assertEqual(zero_to_6_lst, result)

        # test invalid single
        eleven_str = '11'
        empty_lst = []
        result = TimeWindow.parse_range(eleven_str)
        self.assertEqual(empty_lst, result)
示例#2
0
    def test_parse_range(self):
        """
        Test that the parse_range method returns the correct list of ints
        """
        # test comma-delimited
        one_three_five_str = '1,3,5'
        one_three_five_lst = [1, 3, 5]
        result = TimeWindow.parse_range(one_three_five_str)
        self.assertEqual(one_three_five_lst, result,
                         msg='Could not parse comma-delimeted str.')

        # test range
        one_to_three_str = '1-3'
        one_to_three_lst = [1, 2, 3]
        result = TimeWindow.parse_range(one_to_three_str)
        self.assertEqual(one_to_three_lst, result,
                         msg='Could not parse range str.')

        # test mix of commas and ranges
        one_three_four_to_six_str = '1,3,4-6'
        one_three_four_to_six_lst = [1, 3, 4, 5, 6]
        result = TimeWindow.parse_range(one_three_four_to_six_str)
        self.assertEqual(one_three_four_to_six_lst, result,
                         msg='Could not parse mix of comma-delimited and range str.')

        # test invalid range
        zero_to_10_str = '0-10'
        zero_to_6_lst = [0, 1, 2, 3, 4, 5, 6]
        result = TimeWindow.parse_range(zero_to_10_str)
        self.assertEqual(zero_to_6_lst, result)

        # test invalid single
        eleven_str = '11'
        empty_lst = []
        result = TimeWindow.parse_range(eleven_str)
        self.assertEqual(empty_lst, result)
    def _parse_dependencies(self, action):
        """
        Parse dependencies out of XML
        :type action: xml.etree.ElementTree.Element
        :rtype: list
        """
        # TODO: rename 'path' when it really isn't a path. this is a hack...
        # prev_was_not keeps track of whether the outer class was 'not'
        dependencies = []
        prev_was_not = False
        for predicate in action.iter('Predicate'):
            pred_type = predicate.get('type').lower()
            pred_path = predicate.get('path', None)
            # pred_oper = predicate.get('operational', False)
            pred_oper = bool(
                verify_attribute(predicate, 'operational', none_allowed=True))
            if pred_type == PredicateType.ZOOKEEPERHASCHILDREN:
                dependencies.append({
                    'type': pred_type,
                    'path': pred_path,
                    'operational': pred_oper
                })
                prev_was_not = False
            elif pred_type == PredicateType.ZOOKEEPERHASGRANDCHILDREN:
                dependencies.append({
                    'type': pred_type,
                    'path': pred_path,
                    'operational': pred_oper
                })
                prev_was_not = False
            elif pred_type == PredicateType.ZOOKEEPERGOODUNTILTIME:
                if len(pred_path.split('gut/')) > 1:
                    dependencies.append({
                        'type':
                        pred_type,
                        'path': ("I should be up between: {0}".format(
                            pred_path.split("gut/")[1])),
                        'operational':
                        pred_oper
                    })
                else:
                    logging.debug('Invalid GUT path: {0}'.format(pred_path))
                prev_was_not = False
            elif pred_type == PredicateType.HOLIDAY:
                dependencies.append({
                    'type':
                    pred_type,
                    'path': ("Does NOT run on holidays"
                             if prev_was_not else "Runs on holidays"),
                    'operational':
                    pred_oper
                })
                prev_was_not = False
            elif pred_type == PredicateType.WEEKEND:
                dependencies.append({
                    'type':
                    pred_type,
                    'path': ("Does NOT run on weekends"
                             if prev_was_not else "Runs on weekends"),
                    'operational':
                    pred_oper
                })
                prev_was_not = False
            elif pred_type == PredicateType.TIMEWINDOW:
                begin = predicate.get('begin', None)
                end = predicate.get('end', None)
                weekdays = predicate.get('weekdays', None)
                msg = 'I should be up '
                if begin is not None:
                    msg += 'after: {0} '.format(begin)
                if end is not None:
                    msg += 'until: {0}'.format(end)
                # only send dependency if there is something to send
                if begin is not None or end is not None:
                    dependencies.append({
                        'type': pred_type,
                        'path': msg,
                        'operational': pred_oper
                    })

                # pretend this is a weekend predicate for convenience
                if weekdays is not None:
                    day_range = TimeWindow.parse_range(weekdays)
                    if Weekdays.SATURDAY in day_range or Weekdays.SUNDAY in day_range:
                        wk_msg = 'Runs on weekends'
                    else:
                        wk_msg = 'Does NOT run on weekends'

                    dependencies.append({
                        'type': PredicateType.WEEKEND,
                        'path': wk_msg,
                        'operational': pred_oper
                    })

            elif pred_type == PredicateType.NOT:
                prev_was_not = True

        return dependencies