def test_sftp_chmod(self, from_transport, ssh_client): ssh = SSHRetry("host", "user", "passwd", read_file_as_string("../files/privatekey.pem")) client = MagicMock() from_transport.return_value = client res = ssh.sftp_chmod("some_file", 0644) self.assertTrue(res)
def test_sftp_chmod(self, from_transport, ssh_client): ssh = SSHRetry("host", "user", "passwd", read_file_as_string("../files/privatekey.pem")) client = MagicMock() from_transport.return_value = client res = ssh.sftp_chmod("some_file", 0o644) self.assertTrue(res)
def test_getcwd(self, from_transport, ssh_client): ssh = SSHRetry("host", "user", "passwd", read_file_as_string("../files/privatekey.pem")) client = MagicMock() from_transport.return_value = client client.getcwd.return_value = "some_dir" res = ssh.getcwd() self.assertEqual(res, "some_dir")
def test_sftp_list_attr(self, from_transport, ssh_client): ssh = SSHRetry("host", "user", "passwd", read_file_as_string("../files/privatekey.pem")) client = MagicMock() from_transport.return_value = client client.listdir_attr.return_value = ["file"] res = ssh.sftp_list_attr("/some_dir") self.assertEqual(res, ["file"])
def test_execute_timeout(self, ssh_client): ssh = SSHRetry("host", "user", "passwd", read_file_as_string("../files/privatekey.pem")) client = MagicMock() ssh_client.return_value = client client.exec_command.return_value = "", ["out"], ["err"] res_stdout, res_stderr = ssh.execute_timeout("ls", 5) self.assertEqual(res_stdout, "out") self.assertEqual(res_stderr, "err")
def test_test_connectivity_proxy(self, ssh_client): proxy = SSH("proxy", "userp", "passwdp") ssh = SSHRetry("host", "user", "passwd", read_file_as_string("../files/privatekey.pem"), proxy_host=proxy) client = MagicMock() ssh_client.return_value = client success = ssh.test_connectivity(5) self.assertNotEqual(client.connect.call_args_list[1][1]['sock'], None) self.assertTrue(success)
def get_ssh(self, retry=False, auto_close=True): """ Get SSH object to connect with this VM """ proxy_host = None with self._lock: (user, passwd, _, private_key) = self.getCredentialValues() ip = self.getPublicIP() if ip is None: ip = self.getPrivateIP() if ip and self.getProxyHost(): proxy_host = self.getProxyHost() if ip is None: self.log_warn( "VM ID %s does not have IP. Do not return SSH Object." % self.im_id) return None if retry: return SSHRetry(ip, user, passwd, private_key, self.getSSHPort(), proxy_host, auto_close=auto_close) else: return SSH(ip, user, passwd, private_key, self.getSSHPort(), proxy_host, auto_close=auto_close)
def get_master_ssh(self, general_conf_data): ctxt_vm = None for vm in general_conf_data['vms']: if vm['master']: ctxt_vm = vm break if not ctxt_vm: self.logger.error('Not VM master found to get ssh.') return None cred_used = self.wait_ssh_access(ctxt_vm, 2, 10, True) passwd = ctxt_vm['passwd'] if cred_used == 'new': passwd = ctxt_vm['new_passwd'] private_key = ctxt_vm['private_key'] if cred_used == "pk_file": private_key = CtxtAgentBase.PK_FILE vm_ip = ctxt_vm['ip'] if 'ctxt_ip' in ctxt_vm: vm_ip = ctxt_vm['ctxt_ip'] return SSHRetry(vm_ip, ctxt_vm['user'], passwd, private_key, ctxt_vm['remote_port'])
def test_execute(self, ssh_client): ssh = SSHRetry("host", "user", "passwd", read_file_as_string("../files/privatekey.pem")) client = MagicMock() ssh_client.return_value = client transport = MagicMock() channel = MagicMock() client.get_transport.return_value = transport transport.open_session.return_value = channel channel.makefile.return_value = ["out"] channel.makefile_stderr.return_value = ["err"] channel.recv_exit_status.return_value = 0 (res_stdout, res_stderr, exit_status) = ssh.execute("ls") self.assertEqual(res_stdout, "out") self.assertEqual(res_stderr, "err") self.assertEqual(exit_status, 0)
def test_execute_timeout(self, ssh_client): ssh = SSHRetry("host", "user", "passwd", read_file_as_string("../files/privatekey.pem")) client = MagicMock() ssh_client.return_value = client tansport = MagicMock() client.get_transport.return_value = tansport channel = MagicMock() tansport.open_session.return_value = channel channel.makefile.return_value = "out" channel.makefile_stderr.return_value = "err" channel.recv_exit_status.return_value = 0 res_stdout, res_stderr, code = ssh.execute_timeout("ls", 5) self.assertEqual(res_stdout, "out") self.assertEqual(res_stderr, "err") self.assertEqual(code, 0)
def get_ssh_ansible_master(self): ansible_host = None if self.requested_radl.ansible_hosts: ansible_host = self.requested_radl.ansible_hosts[0] if self.requested_radl.systems[0].getValue("ansible_host"): ansible_host = self.requested_radl.get_ansible_by_id( self.getValue("ansible_host")) if ansible_host: (user, passwd, private_key) = ansible_host.getCredentialValues() return SSHRetry(ansible_host.getHost(), user, passwd, private_key) else: return self.inf.vm_master.get_ssh(retry=True)
def get_ssh_ansible_master(self, retry=True): ansible_host = self.get_ansible_host() if ansible_host: (user, passwd, private_key) = ansible_host.getCredentialValues() if retry: return SSHRetry(ansible_host.getHost(), user, passwd, private_key) else: return SSH(ansible_host.getHost(), user, passwd, private_key) else: if self.inf.vm_master: return self.inf.vm_master.get_ssh(retry=retry) else: return None
def get_ssh(self, retry=False): """ Get SSH object to connect with this VM """ (user, passwd, _, private_key) = self.getCredentialValues() ip = self.getPublicIP() if ip is None: ip = self.getPrivateIP() if ip is None: return None if retry: return SSHRetry(ip, user, passwd, private_key, self.getSSHPort()) else: return SSH(ip, user, passwd, private_key, self.getSSHPort())
def get_ssh(ctxt_vm, changed_pass, pk_file): passwd = ctxt_vm['passwd'] if 'new_passwd' in ctxt_vm and ctxt_vm['new_passwd'] and changed_pass: passwd = ctxt_vm['new_passwd'] private_key = ctxt_vm['private_key'] if pk_file: private_key = pk_file vm_ip = ctxt_vm['ip'] if 'ctxt_ip' in ctxt_vm: vm_ip = ctxt_vm['ctxt_ip'] return SSHRetry(vm_ip, ctxt_vm['user'], passwd, private_key, ctxt_vm['remote_port'])
def get_ssh_ansible_master(self, retry=True): ansible_host = self.get_ansible_host() if ansible_host: (user, passwd, private_key) = ansible_host.getCredentialValues() if retry: return SSHRetry(ansible_host.getHost(), user, passwd, private_key) else: return SSH(ansible_host.getHost(), user, passwd, private_key) else: if self.inf.vm_master: return self.inf.vm_master.get_ssh(retry=retry) else: self.log_warn( "There is not master VM. Do not return SSH object.") return None
def get_ssh(vm, pk_file, changed_pass=None): passwd = vm['passwd'] if 'new_passwd' in vm and vm['new_passwd'] and changed_pass: passwd = vm['new_passwd'] private_key = vm['private_key'] if pk_file: private_key = pk_file vm_ip = vm['ip'] remote_port = vm['remote_port'] if 'ctxt_ip' in vm: vm_ip = vm['ctxt_ip'] if 'ctxt_port' in vm: remote_port = vm['ctxt_port'] return SSHRetry(vm_ip, vm['user'], passwd, private_key, remote_port)
def test_sftp_put_dir(self, sftp_client, ssh_client): ssh = SSHRetry("host", "user", "passwd", read_file_as_string("../files/privatekey.pem")) ssh.sftp_put_dir("/tmp", "/tmp")
def test_sftp_put_files(self, sftp_client, ssh_client): ssh = SSHRetry("host", "user", "passwd", read_file_as_string("../files/privatekey.pem")) ssh.sftp_put_files([("some_file", "some_file")])
def test_test_connectivity(self, ssh_client): ssh = SSHRetry("host", "user", "passwd", read_file_as_string("../files/privatekey.pem")) success = ssh.test_connectivity(5) self.assertTrue(success)
def test_sftp_get_files(self, sftp_client, ssh_client): ssh = SSHRetry("host", "user", "passwd", read_file_as_string("../files/privatekey.pem")) ssh.sftp_get(["some_file"], ["some_file"])
def test_sftp_put_content(self, sftp_client, ssh_client): ssh = SSHRetry("host", "user", "passwd", read_file_as_string("../files/privatekey.pem")) ssh.sftp_put_content("some_file", "some_content")