diff --git a/systemvm/debian/opt/cloud/bin/cs/CsStaticRoutes.py b/systemvm/debian/opt/cloud/bin/cs/CsStaticRoutes.py index d15ea66e164e..6a45bde538bd 100755 --- a/systemvm/debian/opt/cloud/bin/cs/CsStaticRoutes.py +++ b/systemvm/debian/opt/cloud/bin/cs/CsStaticRoutes.py @@ -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) diff --git a/systemvm/test/TestCsStaticRoutes.py b/systemvm/test/TestCsStaticRoutes.py new file mode 100644 index 000000000000..4bd3380b8610 --- /dev/null +++ b/systemvm/test/TestCsStaticRoutes.py @@ -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()