conftest.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #!/usr/bin/env python
  2. """py.test fixtures to be used in netmiko test suite."""
  3. from os import path
  4. import os
  5. import pytest
  6. from netmiko import ConnectHandler, FileTransfer, InLineTransfer, SSHDetect
  7. from tests.test_utils import parse_yaml
  8. PWD = path.dirname(path.realpath(__file__))
  9. def pytest_addoption(parser):
  10. """Add test_device option to py.test invocations."""
  11. parser.addoption("--test_device", action="store", dest="test_device", type=str,
  12. help="Specify the platform type to test on")
  13. @pytest.fixture(scope='module')
  14. def net_connect(request):
  15. """
  16. Create the SSH connection to the remote device
  17. Return the netmiko connection object
  18. """
  19. device_under_test = request.config.getoption('test_device')
  20. test_devices = parse_yaml(PWD + "/etc/test_devices.yml")
  21. device = test_devices[device_under_test]
  22. device['verbose'] = False
  23. conn = ConnectHandler(**device)
  24. return conn
  25. @pytest.fixture()
  26. def net_connect_cm(request):
  27. """
  28. Create the SSH connection to the remote device using a context manager
  29. retrieve the find_prompt() data and close the connection.
  30. """
  31. device_under_test = request.config.getoption('test_device')
  32. test_devices = parse_yaml(PWD + "/etc/test_devices.yml")
  33. device = test_devices[device_under_test]
  34. device['verbose'] = False
  35. my_prompt = ""
  36. with ConnectHandler(**device) as conn:
  37. my_prompt = conn.find_prompt()
  38. return my_prompt
  39. @pytest.fixture(scope='module')
  40. def expected_responses(request):
  41. '''
  42. Parse the responses.yml file to get a responses dictionary
  43. '''
  44. device_under_test = request.config.getoption('test_device')
  45. responses = parse_yaml(PWD + "/etc/responses.yml")
  46. return responses[device_under_test]
  47. @pytest.fixture(scope='module')
  48. def commands(request):
  49. '''
  50. Parse the commands.yml file to get a commands dictionary
  51. '''
  52. device_under_test = request.config.getoption('test_device')
  53. test_devices = parse_yaml(PWD + "/etc/test_devices.yml")
  54. device = test_devices[device_under_test]
  55. test_platform = device['device_type']
  56. commands_yml = parse_yaml(PWD + "/etc/commands.yml")
  57. return commands_yml[test_platform]
  58. def delete_file_ios(ssh_conn, dest_file_system, dest_file):
  59. """Delete a remote file for a Cisco IOS device."""
  60. if not dest_file_system:
  61. raise ValueError("Invalid file system specified")
  62. if not dest_file:
  63. raise ValueError("Invalid dest file specified")
  64. # Check if the dest_file already exists
  65. full_file_name = "{0}/{1}".format(dest_file_system, dest_file)
  66. cmd = "delete {0}".format(full_file_name)
  67. output = ssh_conn.send_command_timing(cmd)
  68. if 'Delete' in output and dest_file in output:
  69. output += ssh_conn.send_command_timing("\n")
  70. if 'Delete' in output and full_file_name in output and 'confirm' in output:
  71. output += ssh_conn.send_command_timing("y")
  72. return output
  73. else:
  74. output += ssh_conn.send_command_timing("n")
  75. raise ValueError("An error happened deleting file on Cisco IOS")
  76. @pytest.fixture(scope='module')
  77. def scp_fixture(request):
  78. """
  79. Create an FileTransfer object.
  80. Return a tuple (ssh_conn, scp_handle)
  81. """
  82. device_under_test = request.config.getoption('test_device')
  83. test_devices = parse_yaml(PWD + "/etc/test_devices.yml")
  84. device = test_devices[device_under_test]
  85. device['verbose'] = False
  86. ssh_conn = ConnectHandler(**device)
  87. dest_file_system = 'flash:'
  88. source_file = 'test9.txt'
  89. dest_file = 'test9.txt'
  90. local_file = 'testx.txt'
  91. direction = 'put'
  92. scp_transfer = FileTransfer(ssh_conn, source_file=source_file, dest_file=dest_file,
  93. file_system=dest_file_system, direction=direction)
  94. scp_transfer.establish_scp_conn()
  95. # Make sure SCP is enabled
  96. scp_transfer.enable_scp()
  97. # Delete the test transfer files
  98. if scp_transfer.check_file_exists():
  99. delete_file_ios(ssh_conn, dest_file_system, dest_file)
  100. if os.path.exists(local_file):
  101. os.remove(local_file)
  102. return (ssh_conn, scp_transfer)
  103. @pytest.fixture(scope='module')
  104. def scp_fixture_get(request):
  105. """
  106. Create an FileTransfer object (direction=get)
  107. Return a tuple (ssh_conn, scp_handle)
  108. """
  109. device_under_test = request.config.getoption('test_device')
  110. test_devices = parse_yaml(PWD + "/etc/test_devices.yml")
  111. device = test_devices[device_under_test]
  112. device['verbose'] = False
  113. ssh_conn = ConnectHandler(**device)
  114. dest_file_system = 'flash:'
  115. source_file = 'test9.txt'
  116. local_file = 'testx.txt'
  117. dest_file = local_file
  118. direction = 'get'
  119. scp_transfer = FileTransfer(ssh_conn, source_file=source_file, dest_file=dest_file,
  120. file_system=dest_file_system, direction=direction)
  121. scp_transfer.establish_scp_conn()
  122. # Make sure SCP is enabled
  123. scp_transfer.enable_scp()
  124. # Delete the test transfer files
  125. if os.path.exists(local_file):
  126. os.remove(local_file)
  127. return (ssh_conn, scp_transfer)
  128. @pytest.fixture(scope='module')
  129. def tcl_fixture(request):
  130. """
  131. Create an InLineTransfer object.
  132. Return a tuple (ssh_conn, tcl_handle)
  133. """
  134. device_under_test = request.config.getoption('test_device')
  135. test_devices = parse_yaml(PWD + "/etc/test_devices.yml")
  136. device = test_devices[device_under_test]
  137. device['verbose'] = False
  138. ssh_conn = ConnectHandler(**device)
  139. dest_file_system = 'flash:'
  140. source_file = 'test9.txt'
  141. dest_file = 'test9.txt'
  142. local_file = 'testx.txt'
  143. direction = 'put'
  144. tcl_transfer = InLineTransfer(ssh_conn, source_file=source_file, dest_file=dest_file,
  145. file_system=dest_file_system, direction=direction)
  146. # Delete the test transfer files
  147. if tcl_transfer.check_file_exists():
  148. delete_file_ios(ssh_conn, dest_file_system, dest_file)
  149. if os.path.exists(local_file):
  150. os.remove(local_file)
  151. return (ssh_conn, tcl_transfer)
  152. @pytest.fixture(scope='module')
  153. def ssh_autodetect(request):
  154. """Create an SSH autodetect object.
  155. return (ssh_conn, real_device_type)
  156. """
  157. device_under_test = request.config.getoption('test_device')
  158. test_devices = parse_yaml(PWD + "/etc/test_devices.yml")
  159. device = test_devices[device_under_test]
  160. device['verbose'] = False
  161. my_device_type = device.pop('device_type')
  162. device['device_type'] = 'autodetect'
  163. conn = SSHDetect(**device)
  164. return (conn, my_device_type)