def test_libssh_connect(self, mocked_super, mock_session): pc = PlayContext() pc.remote_addr = "localhost" pc.password = "******" pc.port = 8080 pc.timeout = 60 pc.remote_user = "******" conn = connection_loader.get( "ansible.netcommon.libssh", pc, "/dev/null" ) conn.ssh = mock_session mock_connect = MagicMock() conn.ssh.connect = mock_connect conn._connect() conn.ssh.connect.assert_called_with( host="localhost", host_key_checking=False, look_for_keys=True, password="******", port=8080, timeout=60, user="******", )
def test_netconf_proxy_command(proxy_command, proxy_response): pc = PlayContext() pc.remote_addr = "example.com" conn = connection_loader.get("ansible.netcommon.netconf", pc, "/dev/null") conn.set_option("proxy_command", proxy_command) response = conn._get_proxy_command() if proxy_command is None: assert response is proxy_response else: assert response.cmd == proxy_response
def test_libssh_fetch_file(self, mocked_super, mock_session): pc = PlayContext() pc.remote_addr = "localhost" conn = connection_loader.get("ansible.netcommon.libssh", pc, "/dev/null") conn.ssh = mock_session mock_connect = MagicMock() conn.ssh.connect = mock_connect file_path = "test_libssh.py" conn.fetch_file(in_path=file_path, out_path=file_path) conn.sftp.get.assert_called_with(to_bytes(file_path), to_bytes(file_path))
def get_play_context(self): play_context = PlayContext(play=None, options=None, passwords=self._passwords) play_context.remote_addr = self.addr play_context.port = self.port play_context.remote_user = self.user play_context.ssh_executable = 'ssh' play_context.timeout = 10 play_context.connection = 'ssh' play_context.become = self.sudo play_context.become_method = 'sudo' play_context.become_user = '******' return play_context
def _run_sub_task( self, task_data: Dict[str, Any] = None, hostname: str = None, action_name: str = 'normal' ) -> Dict[str, Any]: task_vars, task, host = self._make_module_task_vars(task_data=task_data, hostname=hostname) try: ansible_host = host.address # get_name()#task_vars['ansible_host']#:'localhost' connection_name = task_vars.get('ansible_connection') # :'local' if not connection_name and is_localhost(host): self._display.warning(f"supposing ansible_collection=local for {host}") connection_name = "local" # TODO What about become and username play: Play = Play.load({ "hosts": hostname}, variable_manager=task.get_variable_manager(), loader=task.get_loader()) play_context = PlayContext(play=play) if not play_context.remote_addr: play_context.remote_addr = ansible_host # cmd_task_vars['ansible_delegated_vars'][spire_server_host][] # ...'ansible_host': 'localhost' # ...'inventory_hostname':'spire_server' # ...'inventory_hostname_short':'spire_server' connection: ConnectionBase = self._shared_loader_obj.connection_loader.get(connection_name, play_context, os.devnull) normal_action = self._shared_loader_obj.action_loader.get(action_name, task=task, connection=connection, play_context=play_context, loader=task.get_loader(), templar=self._templar, shared_loader_obj=self._shared_loader_obj) sub_task_ret: Dict[str, Any] = normal_action.run(task_vars=task_vars) finally: # self._task = original_task pass return sub_task_ret
def test_network_cli_send(self, mocked_connect, mocked_terminal_re): pc = PlayContext() pc.network_os = "ios" pc.remote_addr = "localhost" conn = connection_loader.get( "ansible.netcommon.network_cli", pc, "/dev/null" ) mock__terminal = MagicMock() mocked_terminal_re.side_effect = [ [re.compile(b"^ERROR")], [re.compile(b"device#")], ] conn._terminal = mock__terminal mock__shell = MagicMock() conn._ssh_shell = mock__shell response = b"""device#command command response device# """ mock__shell.recv.side_effect = [response, None] conn.send(b"command") mock__shell.sendall.assert_called_with(b"command\r") self.assertEqual(to_text(conn._command_response), "command response") mock__shell.reset_mock() mock__shell.recv.side_effect = [b"ERROR: error message device#"] mocked_terminal_re.side_effect = [ [re.compile(b"^ERROR")], [re.compile(b"device#")], ] with self.assertRaises(AnsibleConnectionFailure) as exc: conn.send(b"command") self.assertEqual(str(exc.exception), "ERROR: error message device#")