test_netmiko_config.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/usr/bin/env python
  2. """
  3. This module runs tests against Cisco IOS devices.
  4. setup_module: setup variables for later use.
  5. test_ssh_connect: verify ssh connectivity
  6. test_enable_mode: verify enter enable mode
  7. test_config_mode: verify enter config mode
  8. test_exit_config_mode: verify exit config mode
  9. test_command_set: verify sending a set of config commands
  10. test_commands_from_file: verify sending a set of config commands from a file
  11. test_disconnect: cleanly disconnect the SSH session
  12. """
  13. from __future__ import print_function
  14. from __future__ import unicode_literals
  15. def test_ssh_connect(net_connect, commands, expected_responses):
  16. '''
  17. Verify the connection was established successfully
  18. '''
  19. show_version = net_connect.send_command(commands["version"])
  20. assert expected_responses["version_banner"] in show_version
  21. def test_enable_mode(net_connect, commands, expected_responses):
  22. '''
  23. Test entering enable mode
  24. Catch exception for devices that don't support enable
  25. '''
  26. try:
  27. net_connect.enable()
  28. enable_prompt = net_connect.find_prompt()
  29. assert enable_prompt == expected_responses['enable_prompt']
  30. except AttributeError:
  31. assert True == True
  32. def test_config_mode(net_connect, commands, expected_responses):
  33. '''
  34. Test enter config mode
  35. '''
  36. net_connect.config_mode()
  37. assert net_connect.check_config_mode() == True
  38. def test_exit_config_mode(net_connect, commands, expected_responses):
  39. '''
  40. Test exit config mode
  41. '''
  42. net_connect.exit_config_mode()
  43. assert net_connect.check_config_mode() == False
  44. def test_command_set(net_connect, commands, expected_responses):
  45. """Test sending configuration commands."""
  46. config_commands = commands['config']
  47. support_commit = commands.get('support_commit')
  48. config_verify = commands['config_verification']
  49. # Set to initial value and testing sending command as a string
  50. net_connect.send_config_set(config_commands[0])
  51. if support_commit:
  52. net_connect.commit()
  53. cmd_response = expected_responses.get('cmd_response_init')
  54. config_commands_output = net_connect.send_command(config_verify)
  55. print(config_verify)
  56. print(config_commands_output)
  57. if cmd_response:
  58. assert cmd_response in config_commands_output
  59. else:
  60. assert config_commands[0] in config_commands_output
  61. net_connect.send_config_set(config_commands)
  62. if support_commit:
  63. net_connect.commit()
  64. cmd_response = expected_responses.get('cmd_response_final')
  65. config_commands_output = net_connect.send_command_expect(config_verify)
  66. if cmd_response:
  67. assert cmd_response in config_commands_output
  68. else:
  69. assert config_commands[-1] in config_commands_output
  70. def test_commands_from_file(net_connect, commands, expected_responses):
  71. '''
  72. Test sending configuration commands from a file
  73. '''
  74. config_file = commands.get('config_file')
  75. config_verify = commands['config_verification']
  76. if config_file is not None:
  77. net_connect.send_config_from_file(config_file)
  78. config_commands_output = net_connect.send_command_expect(config_verify)
  79. assert expected_responses["file_check_cmd"] in config_commands_output
  80. else:
  81. print("Skipping test (no file specified)...",)
  82. def test_disconnect(net_connect, commands, expected_responses):
  83. '''
  84. Terminate the SSH session
  85. '''
  86. net_connect.disconnect()