From 84117e57ba6adc581223920fd1c5944e4315a09e Mon Sep 17 00:00:00 2001 From: Arnaud <32064012+arn-alt@users.noreply.github.com> Date: Mon, 20 Nov 2017 11:09:16 +0100 Subject: [PATCH] nxos: 32 bits AS in as-dot format not recognized by regexp asn_regex (#30569) * added test for 32 bits AS * Lint not happy. --- lib/ansible/modules/network/nxos/nxos_bgp.py | 2 +- .../modules/network/nxos/nxos_bgp_af.py | 2 +- .../modules/network/nxos/nxos_bgp_neighbor.py | 2 +- .../network/nxos/nxos_bgp_neighbor_af.py | 2 +- .../fixtures/nxos_bgp/config_32_bits_as.cfg | 6 ++++ .../modules/network/nxos/test_nxos_bgp.py | 36 +++++++++++++++++++ 6 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 test/units/modules/network/nxos/fixtures/nxos_bgp/config_32_bits_as.cfg diff --git a/lib/ansible/modules/network/nxos/nxos_bgp.py b/lib/ansible/modules/network/nxos/nxos_bgp.py index 2dd8db5ab4..e2b1b58c66 100644 --- a/lib/ansible/modules/network/nxos/nxos_bgp.py +++ b/lib/ansible/modules/network/nxos/nxos_bgp.py @@ -461,7 +461,7 @@ def get_existing(module, args, warnings): existing = {} netcfg = CustomNetworkConfig(indent=2, contents=get_config(module, flags=['bgp all'])) - asn_re = re.compile(r'.*router\sbgp\s(?P\d+).*', re.S) + asn_re = re.compile(r'.*router\sbgp\s(?P\d+(\.\d+)?).*', re.S) asn_match = asn_re.match(str(netcfg)) if asn_match: diff --git a/lib/ansible/modules/network/nxos/nxos_bgp_af.py b/lib/ansible/modules/network/nxos/nxos_bgp_af.py index fec265ed02..7ebe018cff 100644 --- a/lib/ansible/modules/network/nxos/nxos_bgp_af.py +++ b/lib/ansible/modules/network/nxos/nxos_bgp_af.py @@ -449,7 +449,7 @@ def get_existing(module, args, warnings): existing = {} netcfg = CustomNetworkConfig(indent=2, contents=get_config(module)) - asn_regex = re.compile(r'.*router\sbgp\s(?P\d+).*', re.DOTALL) + asn_regex = re.compile(r'.*router\sbgp\s(?P\d+(\.\d+)?).*', re.DOTALL) match_asn = asn_regex.match(str(netcfg)) if match_asn: diff --git a/lib/ansible/modules/network/nxos/nxos_bgp_neighbor.py b/lib/ansible/modules/network/nxos/nxos_bgp_neighbor.py index 0e6d894d22..5eec02d3d8 100644 --- a/lib/ansible/modules/network/nxos/nxos_bgp_neighbor.py +++ b/lib/ansible/modules/network/nxos/nxos_bgp_neighbor.py @@ -308,7 +308,7 @@ def get_existing(module, args, warnings): existing = {} netcfg = CustomNetworkConfig(indent=2, contents=get_config(module)) - asn_regex = re.compile(r'.*router\sbgp\s(?P\d+).*', re.S) + asn_regex = re.compile(r'.*router\sbgp\s(?P\d+(\.\d+)?).*', re.S) match_asn = asn_regex.match(str(netcfg)) if match_asn: diff --git a/lib/ansible/modules/network/nxos/nxos_bgp_neighbor_af.py b/lib/ansible/modules/network/nxos/nxos_bgp_neighbor_af.py index d819cb2384..e8b159ae3a 100644 --- a/lib/ansible/modules/network/nxos/nxos_bgp_neighbor_af.py +++ b/lib/ansible/modules/network/nxos/nxos_bgp_neighbor_af.py @@ -446,7 +446,7 @@ def get_existing(module, args, warnings): existing = {} netcfg = CustomNetworkConfig(indent=2, contents=get_config(module)) - asn_regex = re.compile(r'.*router\sbgp\s(?P\d+).*', re.S) + asn_regex = re.compile(r'.*router\sbgp\s(?P\d+(\.\d+)?).*', re.S) match_asn = asn_regex.match(str(netcfg)) if match_asn: diff --git a/test/units/modules/network/nxos/fixtures/nxos_bgp/config_32_bits_as.cfg b/test/units/modules/network/nxos/fixtures/nxos_bgp/config_32_bits_as.cfg new file mode 100644 index 0000000000..03db9936fd --- /dev/null +++ b/test/units/modules/network/nxos/fixtures/nxos_bgp/config_32_bits_as.cfg @@ -0,0 +1,6 @@ +feature bgp + +router bgp 65535.65535 + router-id 192.168.1.1 + vrf test + address-family ipv4 unicast diff --git a/test/units/modules/network/nxos/test_nxos_bgp.py b/test/units/modules/network/nxos/test_nxos_bgp.py index b39508be0b..ba0dedb508 100644 --- a/test/units/modules/network/nxos/test_nxos_bgp.py +++ b/test/units/modules/network/nxos/test_nxos_bgp.py @@ -96,3 +96,39 @@ class TestNxosBgpModule(TestNxosModule): changed=True, commands=['router bgp 65535', 'graceful-restart restart-time 120'] ) + + +class TestNxosBgp32BitsAS(TestNxosModule): + + module = nxos_bgp + + def setUp(self): + super(TestNxosBgp32BitsAS, self).setUp() + + self.mock_load_config = patch('ansible.modules.network.nxos.nxos_bgp.load_config') + self.load_config = self.mock_load_config.start() + + self.mock_get_config = patch('ansible.modules.network.nxos.nxos_bgp.get_config') + self.get_config = self.mock_get_config.start() + + def tearDown(self): + super(TestNxosBgp32BitsAS, self).tearDown() + self.mock_load_config.stop() + self.mock_get_config.stop() + + def load_fixtures(self, commands=None, device=''): + self.get_config.return_value = load_fixture('nxos_bgp', 'config_32_bits_as.cfg') + self.load_config.return_value = [] + + def test_nxos_bgp_change_nothing(self): + set_module_args(dict(asn='65535.65535', router_id='192.168.1.1')) + self.execute_module(changed=False) + + def test_nxos_bgp_wrong_asn(self): + set_module_args(dict(asn='65535.10', router_id='192.168.1.1')) + result = self.execute_module(failed=True) + self.assertEqual(result['msg'], 'Another BGP ASN already exists.') + + def test_nxos_bgp_remove(self): + set_module_args(dict(asn='65535.65535', state='absent')) + self.execute_module(changed=True, commands=['no router bgp 65535.65535'])