def test_get_config_text_arc(self, create_file, mock_abspath): create_file.return_value = "relay_info_path" expected_lines_aad = [ "", "Host rg-vm", "\tHostName vm", "\tUser user", "\tCertificateFile \"cert_path\"", "\tIdentityFile \"priv_path\"", "\tProxyCommand \"proxy_path\" -r \"relay_info_path\"" ] expected_lines_local_user = [ "", "Host rg-vm-user", "\tHostName vm", "\tUser user", "\tCertificateFile \"cert_path\"", "\tIdentityFile \"priv_path\"", "\tProxyCommand \"proxy_path\" -r \"relay_info_path\"" ] mock_abspath.side_effect = ["config_path", "pub_path", "priv_path", "cert_path", "client_path", "cred_path"] session = ssh_info.ConfigSession("config", "rg", "vm", None, "pub", "priv", False, False, "user", "cert", None, "Microsoft.HybridCompute", "cred", None, "client/folder") session.proxy_path = "proxy_path" self.assertEqual(session.get_config_text(True), expected_lines_aad) self.assertEqual(session.get_config_text(False), expected_lines_local_user)
def test_config_session(self, mock_abspath): mock_abspath.side_effect = ["config_path", "pub_path", "priv_path", "cert_path", "client_path", "proxy_path", "cred_path"] expected_abspath_calls = [ mock.call("config"), mock.call("pub"), mock.call("priv"), mock.call("cert"), mock.call("client"), mock.call("proxy"), mock.call("cred") ] session = ssh_info.ConfigSession("config", "rg", "vm", "ip", "pub", "priv", False, False, "user", "cert", "port", "arc", "cred", "proxy", "client") mock_abspath.assert_has_calls(expected_abspath_calls) self.assertEqual(session.config_path, "config_path") self.assertEqual(session.resource_group_name, "rg") self.assertEqual(session.vm_name, "vm") self.assertEqual(session.ip, "ip") self.assertEqual(session.public_key_file, "pub_path") self.assertEqual(session.private_key_file, "priv_path") self.assertEqual(session.use_private_ip, False) self.assertEqual(session.overwrite, False) self.assertEqual(session.local_user, "user") self.assertEqual(session.port, "port") self.assertEqual(session.cert_file, "cert_path") self.assertEqual(session.ssh_client_folder, "client_path") self.assertEqual(session.resource_type, "arc") self.assertEqual(session.proxy_path, None) self.assertEqual(session.relay_info, None) self.assertEqual(session.relay_info_path, None) self.assertEqual(session.ssh_proxy_folder, "proxy_path") self.assertEqual(session.credentials_folder, "cred_path")
def test_write_ssh_config_arc_overwrite(self, mock_create_file, mock_abspath, mock_warning): op_info = ssh_info.ConfigSession("config", "rg", "vm", None, None, None, True, False, "user", None, "port", "Microsoft.HybridCompute", None, None, "client") op_info.config_path = "config" op_info.ssh_client_folder = "client" op_info.private_key_file = "priv" op_info.public_key_file = "pub" op_info.cert_file = "cert" op_info.proxy_path = "proxy" mock_create_file.return_value = "relay" expected_lines = [ "", "Host rg-vm", "\tHostName vm", "\tUser user", "\tCertificateFile \"cert\"", "\tIdentityFile \"priv\"", "\tProxyCommand \"proxy\" -r \"relay\" -p port" ] with mock.patch('builtins.open') as mock_open: mock_file = mock.Mock() mock_open.return_value.__enter__.return_value = mock_file ssh_utils.write_ssh_config(op_info, True, True) mock_warning.assert_called_once_with(True, True, True, "cert", "relay", "client") mock_open.assert_called_once_with("config", 'w', encoding='utf-8') mock_file.write.assert_called_once_with('\n'.join(expected_lines))
def test_get_arc_entry(self, mock_abspath): expected_lines_aad = [ "Host rg-vm", "\tHostName vm", "\tUser user", "\tCertificateFile \"cert_path\"", "\tIdentityFile \"priv_path\"", "\tProxyCommand \"proxy_path\" -r \"relay_info_path\" -p port" ] expected_lines_local_user = [ "Host rg-vm-user", "\tHostName vm", "\tUser user", "\tCertificateFile \"cert_path\"", "\tIdentityFile \"priv_path\"", "\tProxyCommand \"proxy_path\" -r \"relay_info_path\" -p port" ] mock_abspath.side_effect = ["config_path", "pub_path", "priv_path", "cert_path", "client_path", "cred_folder"] session = ssh_info.ConfigSession("config", "rg", "vm", None, "pub", "priv", False, False, "user", "cert", "port", "arc", "cred", None, "client/folder") session.proxy_path = "proxy_path" session.relay_info_path = "relay_info_path" self.assertEqual(session._get_arc_entry(True), expected_lines_aad) self.assertEqual(session._get_arc_entry(False), expected_lines_local_user)
def test_get_rg_and_vm_entry(self, mock_abspath): expected_lines_aad = [ "Host rg-vm", "\tUser user", "\tHostName ip", "\tCertificateFile \"cert_path\"", "\tIdentityFile \"priv_path\"", "\tPort port", ] expected_lines_local_user = [ "Host rg-vm-user", "\tUser user", "\tHostName ip", "\tCertificateFile \"cert_path\"", "\tIdentityFile \"priv_path\"", "\tPort port", ] mock_abspath.side_effect = [ "config_path", "pub_path", "priv_path", "cert_path", "client_path", "proxy_path", "cred_path" ] session = ssh_info.ConfigSession("config", "rg", "vm", "ip", "pub", "priv", False, False, "user", "cert", "port", "compute", "cred", "proxy", "client") self.assertEqual(session._get_rg_and_vm_entry(True), expected_lines_aad) self.assertEqual(session._get_rg_and_vm_entry(False), expected_lines_local_user)
def test_get_ip_entry(self, mock_abspath): expected_lines = [ "Host ip", "\tUser user", "\tCertificateFile \"cert_path\"", "\tIdentityFile \"priv_path\"" ] mock_abspath.side_effect = [ "config_path", "pub_path", "priv_path", "cert_path", "client_path" ] session = ssh_info.ConfigSession("config", "rg", "vm", "ip", "pub", "priv", False, False, "user", "cert", None, "client/folder") self.assertEqual(session._get_ip_entry(), expected_lines)
def test_do_ssh_op_local_user_compute(self, mock_ip, mock_check_files): cmd = mock.Mock() mock_op = mock.Mock() mock_ip.return_value = "1.2.3.4" op_info = ssh_info.ConfigSession("config", "rg", "vm", None, None, None, False, False, "username", None, None, "Microsoft.Compute", None, None, None) op_info.public_key_file = "publicfile" op_info.private_key_file = "privatefile" op_info.cert_file = "cert" op_info.ssh_client_folder = "/client/folder" custom._do_ssh_op(cmd, op_info, mock_op) mock_check_files.assert_not_called() mock_ip.assert_called_once_with(cmd, "rg", "vm", False) mock_op.assert_called_once_with(op_info, False, False)
def test_get_ip_entry(self, mock_abspath): expected_lines_aad = [ "Host ip", "\tUser user", "\tCertificateFile \"cert_path\"", "\tIdentityFile \"priv_path\"" ] expected_lines_local_user = [ "Host ip-user", "\tHostName ip", "\tUser user", "\tCertificateFile \"cert_path\"", "\tIdentityFile \"priv_path\"" ] mock_abspath.side_effect = ["config_path", "pub_path", "priv_path", "cert_path", "client_path", "cred_folder"] session = ssh_info.ConfigSession("config", "rg", "vm", "ip", "pub", "priv", False, False, "user", "cert", None, "compute", "cred", None, "client/folder") self.assertEqual(session._get_ip_entry(True), expected_lines_aad) self.assertEqual(session._get_ip_entry(False), expected_lines_local_user)
def test_get_config_text(self, mock_abspath): expected_lines = [ "", "Host rg-vm", "\tUser user", "\tHostName ip", "\tCertificateFile \"cert_path\"", "\tIdentityFile \"priv_path\"", "\tPort port", "Host ip", "\tUser user", "\tCertificateFile \"cert_path\"", "\tIdentityFile \"priv_path\"", "\tPort port", ] mock_abspath.side_effect = [ "config_path", "pub_path", "priv_path", "cert_path", "client_path" ] session = ssh_info.ConfigSession("config", "rg", "vm", "ip", "pub", "priv", False, False, "user", "cert", "port", "client/folder") self.assertEqual(session.get_config_text(), expected_lines)