cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
235
Views
0
Helpful
1
Replies

unittest - test parse vlan - issue search type

Netmart
Level 1
Level 1

Hello,

I was trying to run the following test parser and it seems that there is an issue with the search type under the ConfigParserVLAN.

Please see details below.

 

Thanks.

 

python3 -m unittest test_parser_customer_vlan.py
ConfigurationParser
Accessed ConfigParser Module
E
======================================================================
ERROR: test_parse_cust_vlan (test_parser_customer_vlan.TestParse)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/cisco/test_parser_customer_vlan.py", line 11, in test_parse_cust_vlan
parsed_vlan = cp.parseCustomerVlan(customer_name)
File "/cisco/ConfigParserVLAN.py", line 12, in parseCustomerVlan
allCustomerSubInterfaces = re.search(intPattern, self.deviceConfig)
File "/usr/lib/python3.10/re.py", line 200, in search
return _compile(pattern, flags).search(string)
TypeError: expected string or bytes-like object

----------------------------------------------------------------------
Ran 1 test in 0.001s

FAILED (errors=1)

 

%%%%%%%%%%%%%%%%%%%%%%%%%%

test_parser_customer_vlan.py

 

import unittest
import re
from ConfigParserVLAN import ConfigurationParserVLAN

class TestParse (unittest.TestCase
  def test_parse_cust_vlan(self
     print ("ConfigurationParser")
     cp = ConfigurationParserVLAN()
     customer_name = "CUSTOMER_A"
     expected_vlan = 100
     parsed_vlan = cp.parseCustomerVlan(customer_name)
     #self.assertEqual(list, type(parsed_vlan))
     self.assertEqual(expected_vlan, parsed_vlan)


 
if __name__ == '__main__':
    unittest.main()
 
 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 ConfigParserVLAN.py
#Regular Expression Module re
import re
class ConfigurationParserVLAN:
 with open("config.txt") as deviceConfig:
  def parseCustomerVlan(self, customerName
      print ("Accessed ConfigParser Module")
      intPattern = (
          r"interface GigabitEthernet0\/0\. ([0-9]+)\n encapsulation "
          r"dot1Q [0-9]+\n ip vrf forwarding %s"
          % (customerName)
      )
      allCustomerSubInterfaces = re.search(intPattern, self.deviceConfig)
      return int(allCustomerSubInterfaces.group(1))
 

 

1 Reply 1

@Netmart looks like you need to read the contents of the config.txt file into a string before calling re.search. Try using the open() function's read() method.

Hope this helps 

Please mark this as helpful or solution accepted to help others
Connect with me https://bigevilbeard.github.io