示例#1
0
    def test_start_ssh_connection(self, mock_print_error, mock_start, mock_dirname, mock_host, mock_build, mock_call, mock_path, mock_join):
        mock_path.return_value = "ssh"
        mock_join.return_value = "/log/file/path"
        mock_build.return_value = ['-i', 'file', '-o', 'option']
        mock_host.return_value = "user@ip"
        mock_dirname.return_value = "dirname"
        mock_call.return_value = 0

        expected_command = ["ssh", "user@ip", "-i", "file", "-o", "option", "-E", "/log/file/path", "-v"]

        op_info = mock.Mock()
        op_info.ip = "ip"
        op_info.port = "port"
        op_info.local_user = "******"
        op_info.private_key_file = "private"
        op_info.public_key_file = "public"
        op_info.cert_file = "cert"
        op_info.ssh_args = None
        op_info.ssh_client_folder = "client/folder"
        op_info.build_args = mock_build
        op_info.get_host = mock_host

        ssh_utils.start_ssh_connection(op_info, True, True)
        mock_start.assert_called_once()
        mock_print_error.assert_called_once_with("/log/file/path", 0)
        mock_path.assert_called_once_with('ssh', 'client/folder')
        mock_call.assert_called_once_with(expected_command, shell=platform.system() == 'Windows')
    def test_start_ssh_connection_with_args(self, mock_call, mock_host, mock_path):
        mock_path.return_value = "ssh"
        mock_host.return_value = "user@ip"

        expected_command = ["ssh", "user@ip", "-i", "private", "-o", "CertificateFile=cert", "-p", "2222", "--thing", "-vv"]

        ssh_utils.start_ssh_connection("2222", ["--thing", "-vv"], "ip", "user", "cert", "private", True, True)

        mock_path.assert_called_once_with()
        mock_host.assert_called_once_with("user", "ip")
        mock_call.assert_called_once_with(expected_command, shell=platform.system() == 'Windows')
示例#3
0
    def test_start_ssh_connection(self, mock_call, mock_build, mock_host,
                                  mock_path):
        mock_path.return_value = "ssh"
        mock_host.return_value = "user@ip"
        mock_build.return_value = ['-i', 'file', '-o', 'option']
        expected_command = ["ssh", "user@ip", "-i", "file", "-o", "option"]

        ssh_utils.start_ssh_connection("ip", "user", "cert", "private")

        mock_path.assert_called_once_with()
        mock_host.assert_called_once_with("user", "ip")
        mock_build.assert_called_once_with("cert", "private")
        mock_call.assert_called_once_with(expected_command, shell=True)
    def test_start_ssh_connection(self, mock_call, mock_build, mock_host, mock_path, mock_join):
        mock_path.return_value = "ssh"
        mock_host.return_value = "user@ip"
        mock_build.return_value = ['-i', 'file', '-o', 'option']
        mock_join.return_value = "/log/file/path"

        expected_command = ["ssh", "user@ip", "-i", "file", "-o", "option", "-E", "/log/file/path", "-v"]

        ssh_utils.start_ssh_connection("port", None, "ip", "user", "cert", "private", True, True)

        mock_path.assert_called_once_with()
        mock_host.assert_called_once_with("user", "ip")
        mock_build.assert_called_once_with("cert", "private", "port")
        mock_call.assert_called_once_with(expected_command, shell=platform.system() == 'Windows')
    def test_start_ssh_connection_arc(self, mock_system, mock_relay_str,
                                      mock_call, mock_path, mock_copy_env,
                                      mock_terminatecleanup):

        op_info = ssh_info.SSHSession("rg", "vm", None, None, None, False,
                                      "user", None, "port", None, ['arg1'],
                                      False, "Microsoft.HybridCompute", None,
                                      None, False)
        op_info.public_key_file = "pub"
        op_info.private_key_file = "priv"
        op_info.cert_file = "cert"
        op_info.ssh_client_folder = "client"
        op_info.proxy_path = "proxy"
        op_info.relay_info = "relay"

        mock_system.return_value = 'Linux'
        mock_call.return_value = 0
        mock_relay_str.return_value = 'relay_string'
        mock_copy_env.return_value = {
            'var1': 'value1',
            'var2': 'value2',
            'var3': 'value3'
        }
        mock_path.return_value = 'ssh'
        expected_command = [
            'ssh', 'user@vm', '-o', 'ProxyCommand=\"proxy\" -p port', '-i',
            'priv', '-o', 'CertificateFile=\"cert\"', 'arg1'
        ]
        expected_env = {
            'var1': 'value1',
            'var2': 'value2',
            'var3': 'value3',
            'SSHPROXY_RELAY_INFO': 'relay_string'
        }

        ssh_utils.start_ssh_connection(op_info, False, False)

        mock_relay_str.assert_called_once_with('relay')
        mock_path.assert_called_once_with('ssh', 'client')
        mock_call.assert_called_once_with(expected_command,
                                          shell=False,
                                          env=expected_env,
                                          stderr=mock.ANY,
                                          encoding='utf-8')
        mock_terminatecleanup.assert_called_once_with(False, False, False,
                                                      None, 'cert', 'priv',
                                                      'pub', None, 0)
    def test_start_ssh_connection_compute(self, mock_system, mock_copy_env,
                                          mock_call, mock_path,
                                          mock_terminatecleanup,
                                          mock_startcleanup):

        op_info = ssh_info.SSHSession("rg", "vm", "ip", None, None, False,
                                      "user", None, "port", None,
                                      ['arg1', 'arg2', 'arg3'], False,
                                      "Microsof.Compute", None, None, False)
        op_info.public_key_file = "pub"
        op_info.private_key_file = "priv"
        op_info.cert_file = "cert"
        op_info.ssh_client_folder = "client"

        mock_system.return_value = 'Windows'
        mock_call.return_value = 0
        mock_path.return_value = 'ssh'
        mock_copy_env.return_value = {
            'var1': 'value1',
            'var2': 'value2',
            'var3': 'value3'
        }
        mock_startcleanup.return_value = 'log', [
            'arg1', 'arg2', 'arg3', '-E', 'log', '-v'
        ], 'cleanup process'
        expected_command = [
            'ssh', 'user@ip', '-i', 'priv', '-o', 'CertificateFile=\"cert\"',
            '-p', 'port', 'arg1', 'arg2', 'arg3', '-E', 'log', '-v'
        ]
        expected_env = {'var1': 'value1', 'var2': 'value2', 'var3': 'value3'}

        ssh_utils.start_ssh_connection(op_info, True, True)

        mock_path.assert_called_once_with('ssh', 'client')
        mock_startcleanup.assert_called_with('cert', 'priv', 'pub', False,
                                             True, True,
                                             ['arg1', 'arg2', 'arg3'])
        mock_call.assert_called_once_with(expected_command,
                                          shell=True,
                                          env=expected_env,
                                          stderr=mock.ANY,
                                          encoding='utf-8')
        mock_terminatecleanup.assert_called_once_with(True, True, False,
                                                      'cleanup process',
                                                      'cert', 'priv', 'pub',
                                                      'log', 0)
示例#7
0
    def test_start_ssh_connection_with_args(self, mock_host, mock_build, mock_call, mock_path):
        mock_path.return_value = "ssh"
        mock_host.return_value = "user@ip"
        mock_build.return_value = ["-i", "private", "-o", "CertificateFile=cert", "-p", "2222"]

        expected_command = ["ssh", "user@ip", "-i", "private", "-o", "CertificateFile=cert", "-p", "2222", "--thing", "-vv"]

        op_info = mock.Mock()
        op_info.ip = "ip"
        op_info.port = "2222"
        op_info.local_user = "******"
        op_info.private_key_file = "private"
        op_info.public_key_file = "public"
        op_info.cert_file = "cert"
        op_info.ssh_args = ["--thing", "-vv"]
        op_info.ssh_client_folder = "client/folder"
        op_info.build_args = mock_build
        op_info.get_host = mock_host

        ssh_utils.start_ssh_connection(op_info, True, True)

        mock_path.assert_called_once_with('ssh', 'client/folder')
        mock_call.assert_called_once_with(expected_command, shell=platform.system() == 'Windows')