Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion systemvm/debian/opt/cloud/bin/cs/CsStaticRoutes.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def __update(self, route):
logging.info("Deleted static route %s via %s from PBR table %s" % (network, gateway, table_name))
else:
# Add to main table (existing logic)
command = "ip route show | grep '^%s' | awk '{print $1, $3}'" % network
command = "ip route show | grep '^%s' | grep -v 'proto bgp' | awk '{print $1, $3}'" % network
result = CsHelper.execute(command)
if not result:
route_command = "ip route add %s via %s" % (network, gateway)
Expand Down
76 changes: 76 additions & 0 deletions systemvm/test/TestCsStaticRoutes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT 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 subprocess
import unittest
try:
import mock
except ImportError:
from unittest import mock
from cs.CsStaticRoutes import CsStaticRoutes
import merge


class TestCsStaticRoutes(unittest.TestCase):

def setUp(self):
merge.DataBag.DPATH = "."
self.csstaticroutes = CsStaticRoutes("staticroutes", {})
self.csstaticroutes.config = None
self.commands = []
self.main_table = []

def _execute(self, command):
"""Record the command. Run the route lookup pipeline against self.main_table in place of the router."""
self.commands.append(command)
if command.startswith("ip route show |"):
routes = "".join(line + "\n" for line in self.main_table)
pipeline = command.replace("ip route show", "cat", 1)
return subprocess.run(pipeline, shell=True, input=routes, capture_output=True, text=True).stdout.splitlines()
return []

def _add(self, network, gateway):
self.csstaticroutes.dbag = {'id': 'staticroutes',
network: {'network': network, 'gateway': gateway, 'revoke': False}}
with mock.patch('cs.CsStaticRoutes.CsHelper.execute', side_effect=self._execute), \
mock.patch('cs.CsStaticRoutes.CsHelper.find_device_for_gateway', return_value=None):
self.csstaticroutes.process()

def test_route_added_to_empty_table(self):
self._add("172.16.0.0/16", "192.0.2.6")
self.assertIn("ip route add 172.16.0.0/16 via 192.0.2.6", self.commands)

def test_route_not_added_twice(self):
self.main_table = ["172.16.0.0/16 via 192.0.2.6 dev eth4"]
self._add("172.16.0.0/16", "192.0.2.6")
self.assertNotIn("ip route add 172.16.0.0/16 via 192.0.2.6", self.commands)

def test_route_added_over_bgp_route_for_same_prefix(self):
self.main_table = ["172.16.0.0/16 nhid 20 via 192.0.2.2 dev eth3 proto bgp metric 20"]
self._add("172.16.0.0/16", "192.0.2.6")
self.assertIn("ip route add 172.16.0.0/16 via 192.0.2.6", self.commands)

def test_route_added_over_bgp_multipath_route_for_same_prefix(self):
self.main_table = ["172.16.0.0/16 nhid 24 proto bgp metric 20",
"\tnexthop via 192.0.2.2 dev eth3 weight 1",
"\tnexthop via 192.0.2.6 dev eth4 weight 1"]
self._add("172.16.0.0/16", "192.0.2.6")
self.assertIn("ip route add 172.16.0.0/16 via 192.0.2.6", self.commands)


if __name__ == '__main__':
unittest.main()