Skip to content

Commit

Permalink
Update config mirror_session CLI to support heximal gre type value (#…
Browse files Browse the repository at this point in the history
…2095)

* Update config CLI to support heximal gre type

Signed-off-by: bingwang <bingwang@microsoft.com>
  • Loading branch information
bingwang-ms authored Mar 21, 2022
1 parent b25f1e1 commit 2a982a1
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
17 changes: 15 additions & 2 deletions config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1024,6 +1024,19 @@ def validate_ipv4_address(ctx, param, ip_addr):
except ValueError as e:
raise click.UsageError(str(e))

def validate_gre_type(ctx, _, value):
"""A validator for validating input gre_type
"""
try:
base = 10
if value.lower().startswith('0x'):
base = 16
gre_type_value = int(value, base)
if gre_type_value < GRE_TYPE_RANGE.min or gre_type_value > GRE_TYPE_RANGE.max:
raise click.UsageError("{} is not a valid GRE type".format(value))
return gre_type_value
except ValueError:
raise click.UsageError("{} is not a valid GRE type".format(value))

# This is our main entrypoint - the main 'config' command
@click.group(cls=clicommon.AbbreviationGroup, context_settings=CONTEXT_SETTINGS)
Expand Down Expand Up @@ -1893,7 +1906,7 @@ def mirror_session():
@click.argument('dst_ip', metavar='<dst_ip>', callback=validate_ipv4_address, required=True)
@click.argument('dscp', metavar='<dscp>', type=DSCP_RANGE, required=True)
@click.argument('ttl', metavar='<ttl>', type=TTL_RANGE, required=True)
@click.argument('gre_type', metavar='[gre_type]', type=GRE_TYPE_RANGE, required=False)
@click.argument('gre_type', metavar='[gre_type]', callback=validate_gre_type, required=False)
@click.argument('queue', metavar='[queue]', type=QUEUE_RANGE, required=False)
@click.option('--policer')
def add(session_name, src_ip, dst_ip, dscp, ttl, gre_type, queue, policer):
Expand All @@ -1917,7 +1930,7 @@ def erspan(ctx):
@click.argument('dst_ip', metavar='<dst_ip>', callback=validate_ipv4_address,required=True)
@click.argument('dscp', metavar='<dscp>', type=DSCP_RANGE, required=True)
@click.argument('ttl', metavar='<ttl>', type=TTL_RANGE, required=True)
@click.argument('gre_type', metavar='[gre_type]', type=GRE_TYPE_RANGE, required=False)
@click.argument('gre_type', metavar='[gre_type]', callback=validate_gre_type, required=False)
@click.argument('queue', metavar='[queue]', type=QUEUE_RANGE, required=False)
@click.argument('src_port', metavar='[src_port]', required=False)
@click.argument('direction', metavar='[direction]', required=False)
Expand Down
28 changes: 26 additions & 2 deletions tests/config_mirror_session_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

ERR_MSG_IP_FAILURE = "does not appear to be an IPv4 or IPv6 network"
ERR_MSG_IP_VERSION_FAILURE = "not a valid IPv4 address"
ERR_MSG_GRE_TYPE_FAILURE = "not a valid GRE type"
ERR_MSG_VALUE_FAILURE = "Invalid value for"

def test_mirror_session_add():
Expand Down Expand Up @@ -50,7 +51,13 @@ def test_mirror_session_add():
config.config.commands["mirror_session"].commands["add"],
["test_session", "1.1.1.1", "2.2.2.2", "6", "63", "65536", "100"])
assert result.exit_code != 0
assert ERR_MSG_VALUE_FAILURE in result.stdout
assert ERR_MSG_GRE_TYPE_FAILURE in result.stdout

result = runner.invoke(
config.config.commands["mirror_session"].commands["add"],
["test_session", "1.1.1.1", "2.2.2.2", "6", "63", "abcd", "100"])
assert result.exit_code != 0
assert ERR_MSG_GRE_TYPE_FAILURE in result.stdout

# Verify invalid queue
result = runner.invoke(
Expand All @@ -67,6 +74,11 @@ def test_mirror_session_add():

mocked.assert_called_with("test_session", "100.1.1.1", "2.2.2.2", 8, 63, 10, 100, None)

result = runner.invoke(
config.config.commands["mirror_session"].commands["add"],
["test_session", "100.1.1.1", "2.2.2.2", "8", "63", "0X1234", "100"])

mocked.assert_called_with("test_session", "100.1.1.1", "2.2.2.2", 8, 63, 0x1234, 100, None)


def test_mirror_session_erspan_add():
Expand Down Expand Up @@ -112,7 +124,13 @@ def test_mirror_session_erspan_add():
config.config.commands["mirror_session"].commands["erspan"].commands["add"],
["test_session", "1.1.1.1", "2.2.2.2", "6", "63", "65536", "100"])
assert result.exit_code != 0
assert ERR_MSG_VALUE_FAILURE in result.stdout
assert ERR_MSG_GRE_TYPE_FAILURE in result.stdout

result = runner.invoke(
config.config.commands["mirror_session"].commands["erspan"].commands["add"],
["test_session", "1.1.1.1", "2.2.2.2", "6", "63", "abcd", "100"])
assert result.exit_code != 0
assert ERR_MSG_GRE_TYPE_FAILURE in result.stdout

# Verify invalid queue
result = runner.invoke(
Expand All @@ -129,6 +147,12 @@ def test_mirror_session_erspan_add():

mocked.assert_called_with("test_session", "100.1.1.1", "2.2.2.2", 8, 63, 10, 100, None, None, None)

result = runner.invoke(
config.config.commands["mirror_session"].commands["erspan"].commands["add"],
["test_session", "100.1.1.1", "2.2.2.2", "8", "63", "0x1234", "100"])

mocked.assert_called_with("test_session", "100.1.1.1", "2.2.2.2", 8, 63, 0x1234, 100, None, None, None)


def test_mirror_session_span_add():
runner = CliRunner()
Expand Down

0 comments on commit 2a982a1

Please sign in to comment.