Fix network segment range "_get_ranges" function

This function should return an ordered set of ranges based on an
unordered list of numbers (int or str).

Change-Id: I918c8befc51236cc33d96a5c88fb6eafdd143e9c
Story: 2007341
Task: 38878
This commit is contained in:
Rodolfo Alonso Hernandez 2020-02-26 12:24:52 +00:00 committed by Akihiro Motoki
parent e07324e30f
commit 27da238da2
2 changed files with 15 additions and 2 deletions
openstackclient

@ -23,7 +23,6 @@ from osc_lib.cli import format_columns
from osc_lib.command import command
from osc_lib import exceptions
from osc_lib import utils
import six
from openstackclient.i18n import _
from openstackclient.identity import common as identity_common
@ -42,7 +41,7 @@ def _get_columns(item):
def _get_ranges(item):
item = [int(i) if isinstance(i, six.string_types) else i for i in item]
item = sorted([int(i) for i in item])
for a, b in itertools.groupby(enumerate(item), lambda xy: xy[1] - xy[0]):
b = list(b)
yield "%s-%s" % (b[0][1], b[-1][1]) if b[0][1] != b[-1][1] else \

@ -24,6 +24,20 @@ from openstackclient.tests.unit.network.v2 import fakes as network_fakes
from openstackclient.tests.unit import utils as tests_utils
class TestAuxiliaryFunctions(tests_utils.TestCase):
def test__get_ranges(self):
input_reference = [
([1, 2, 3, 4, 5, 6, 7], ['1-7']),
([1, 2, 5, 4, 3, 6, 7], ['1-7']),
([1, 2, 4, 3, 7, 6], ['1-4', '6-7']),
([1, 2, 4, 3, '13', 12, '7', '6'], ['1-4', '6-7', '12-13'])
]
for input, reference in input_reference:
self.assertEqual(reference,
list(network_segment_range._get_ranges(input)))
class TestNetworkSegmentRange(network_fakes.TestNetworkV2):
def setUp(self):