test_netmiko_show.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #!/usr/bin/env python
  2. """
  3. setup_module: setup variables for later use.
  4. test_disable_paging: disable paging
  5. test_ssh_connect: verify ssh connectivity
  6. test_send_command: send a command
  7. test_send_command_expect: send a command
  8. test_base_prompt: test the base prompt
  9. test_strip_prompt: test removing the prompt
  10. test_strip_command: test stripping extraneous info after sending a command
  11. test_normalize_linefeeds: ensure \n is the only line termination character in output
  12. test_clear_buffer: clear text buffer
  13. test_enable_mode: verify enter enable mode
  14. test_disconnect: cleanly disconnect the SSH session
  15. """
  16. from __future__ import print_function
  17. from __future__ import unicode_literals
  18. import time
  19. def test_disable_paging(net_connect, commands, expected_responses):
  20. """Verify paging is disabled by looking for string after when paging would normally occur."""
  21. if net_connect.device_type == 'arista_eos':
  22. # Arista logging buffer gets enormous
  23. net_connect.send_command_expect('clear logging')
  24. multiple_line_output = net_connect.send_command_expect(commands["extended_output"])
  25. assert expected_responses["multiple_line_output"] in multiple_line_output
  26. if net_connect.device_type == 'arista_eos':
  27. # Arista output is slow and has router-name in output
  28. time.sleep(5)
  29. net_connect.clear_buffer()
  30. net_connect.send_command_expect('clear logging', expect_string='#')
  31. def test_ssh_connect(net_connect, commands, expected_responses):
  32. """Verify the connection was established successfully."""
  33. show_version = net_connect.send_command_expect(commands["version"])
  34. assert expected_responses["version_banner"] in show_version
  35. def test_ssh_connect_cm(net_connect_cm, commands, expected_responses):
  36. """Test the context manager."""
  37. prompt_str = net_connect_cm
  38. assert expected_responses['base_prompt'] in prompt_str
  39. def test_send_command_timing(net_connect, commands, expected_responses):
  40. """Verify a command can be sent down the channel successfully."""
  41. time.sleep(1)
  42. net_connect.clear_buffer()
  43. show_ip = net_connect.send_command_timing(commands["basic"])
  44. assert expected_responses['interface_ip'] in show_ip
  45. def test_send_command_expect(net_connect, commands, expected_responses):
  46. """Verify a command can be sent down the channel successfully using _expect method."""
  47. time.sleep(1)
  48. net_connect.clear_buffer()
  49. show_ip_alt = net_connect.send_command_expect(commands["basic"])
  50. assert expected_responses['interface_ip'] in show_ip_alt
  51. def test_base_prompt(net_connect, commands, expected_responses):
  52. """Verify the router prompt is detected correctly."""
  53. assert net_connect.base_prompt == expected_responses['base_prompt']
  54. def test_strip_prompt(net_connect, commands, expected_responses):
  55. """Ensure the router prompt is not in the command output."""
  56. show_ip = net_connect.send_command_timing(commands["basic"])
  57. show_ip_alt = net_connect.send_command_expect(commands["basic"])
  58. assert expected_responses['base_prompt'] not in show_ip
  59. assert expected_responses['base_prompt'] not in show_ip_alt
  60. def test_strip_command(net_connect, commands, expected_responses):
  61. """Ensure that the command that was executed does not show up in the command output."""
  62. show_ip = net_connect.send_command_timing(commands["basic"])
  63. show_ip_alt = net_connect.send_command_expect(commands["basic"])
  64. assert commands['basic'] not in show_ip
  65. assert commands['basic'] not in show_ip_alt
  66. def test_normalize_linefeeds(net_connect, commands, expected_responses):
  67. """Ensure no '\r\n' sequences."""
  68. show_version = net_connect.send_command_timing(commands["version"])
  69. show_version_alt = net_connect.send_command_expect(commands["version"])
  70. assert not '\r\n' in show_version
  71. assert not '\r\n' in show_version_alt
  72. def test_clear_buffer(net_connect, commands, expected_responses):
  73. """Test that clearing the buffer works."""
  74. # Manually send a command down the channel so that data needs read.
  75. net_connect.write_channel(commands["basic"] + '\n')
  76. time.sleep(4)
  77. net_connect.clear_buffer()
  78. # Should not be anything there on the second pass
  79. clear_buffer_check = net_connect.clear_buffer()
  80. assert clear_buffer_check is None
  81. def test_enable_mode(net_connect, commands, expected_responses):
  82. '''
  83. Test entering enable mode
  84. Catch exception for devices that don't support enable
  85. '''
  86. try:
  87. net_connect.enable()
  88. enable_prompt = net_connect.find_prompt()
  89. assert enable_prompt == expected_responses['enable_prompt']
  90. except AttributeError:
  91. assert True == True
  92. def test_disconnect(net_connect, commands, expected_responses):
  93. """Terminate the SSH session."""
  94. net_connect.disconnect()