def test_init(self, mock_conn, mock_set): sshclient.SSHClient('ip', 'username', 'password') mock_conn.assert_called_with('ip', username='******', password='******', pkey=None, key_filename=None, look_for_keys=False, allow_agent=False)
def test_scp(self, mock_open, mock_conn, mock_set): mock_log = mock.Mock() mock_sftp = mock.Mock() mock_open.return_value = mock_sftp client = sshclient.SSHClient('ip', 'username', password='******', log=mock_log) client.scp('source_file', 'dest_file') mock_log.info.assert_called() mock_sftp.put.assert_called_with('source_file', 'dest_file')
def test_ssh_except(self, mock_exec, mock_conn, mock_set): mock_log = mock.Mock() mock_channel = mock.Mock() mock_exec.return_value = (fake_channel_file(['input']), fake_channel_file(['info'], mock_channel), fake_channel_file(['err'])) mock_channel.recv_exit_status.return_value = -1 client = sshclient.SSHClient('ip', 'username', password='******', log=mock_log) self.assertRaises(sshclient.SshExecCmdFailure, client.ssh, 'fake_command')
def test_ssh_allow_error_return(self, mock_exec, mock_conn, mock_set): mock_log = mock.Mock() mock_channel = mock.Mock() mock_exec.return_value = (fake_channel_file(['input']), fake_channel_file(['info'], mock_channel), fake_channel_file(['err'])) mock_channel.recv_exit_status.return_value = 1 client = sshclient.SSHClient('ip', 'username', password='******', log=mock_log) return_code, out, err = client.ssh('fake_command', allowed_return_codes=[0, 1]) mock_exec.assert_called_once_with('fake_command', get_pty=True) mock_channel.recv_exit_status.assert_called_once() self.assertEqual(return_code, 1)
def test_ssh(self, mock_exec, mock_conn, mock_set): mock_log = mock.Mock() mock_channel = mock.Mock() mock_exec.return_value = (fake_channel_file(['input']), fake_channel_file(['out_line1', 'out_line2'], mock_channel), fake_channel_file(['err_line1', 'err_line2'])) mock_channel.recv_exit_status.return_value = 0 client = sshclient.SSHClient('ip', 'username', password='******', log=mock_log) return_code, out, err = client.ssh('fake_command') mock_log.debug.assert_called() mock_exec.assert_called() mock_log.info.assert_called_with('out_line1\nout_line2') mock_log.error.assert_called_with('err_line1\nerr_line2') mock_channel.recv_exit_status.assert_called_with() self.assertEqual(out, 'out_line1\nout_line2') self.assertEqual(err, 'err_line1\nerr_line2')
return common_function.detailed_execute( *command, allowed_return_codes=allowed_return_codes) def config_iptables(client, forwarding_interfaces=None): """This function is used to configure iptables on a XenServer compute node. :param client: session client with Dom0 :param forwarding_interfaces: network interface list which user want to forward HIMN packages. """ if forwarding_interfaces: configure_himn_forwards(forwarding_interfaces, client.ip) configure_dom0_iptables(client) if __name__ == '__main__': if len(sys.argv) != 5: exit_with_error("Wrong parameters input.") dom0_himn_ip, user_name, password, forwarding_interfaces = sys.argv[1:] forwarding_interfaces = forwarding_interfaces.split() try: client = sshclient.SSHClient(dom0_himn_ip, user_name, password) except Exception: exit_with_error("Create connection failed, ip: %(dom0_himn_ip)s," " user_name: %(user_name)s" % { 'dom0_himn_ip': dom0_himn_ip, 'user_name': user_name }) config_iptables(client, forwarding_interfaces)
:returns: a dict which contains all facts gathered. """ facts = {} # get dom0's hostname facts['dom0_hostname'] = common_function.get_remote_hostname(dom0_client) # get dom0's IPs facts['dom0_ipv4s'] = common_function.get_host_ipv4s(dom0_client) # get domU's eth and ip which are connected to HIMN. eth = himn.get_local_himn_eth(dom0_client.ip) ip_addr = common_function.get_eth_ipaddr(eth) facts['domu_himn_eth'] = eth facts['domu_himn_ip'] = ip_addr # get domU eths' vif data facts['domu_vifs'] = common_function.get_domu_vifs_by_eth(dom0_client) return facts if __name__ == '__main__': # Run in domU which has an interface connected to HIMN # argv[1]: dom0's IP address # argv[2]: user name # argv[3]: user passwd ssh_client = sshclient.SSHClient(sys.argv[1], sys.argv[2], sys.argv[3]) print('Got XenAPI facts as:\n%s' % json.dumps(get_xenapi_facts(ssh_client), indent=4, sort_keys=True))