Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c7dcef156fe74c414070914f4e51851ccbf5c18d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*******************************************************************************
 * Copyright (c) 2013, 2018 IBM Corporation.
 *
 * This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License 2.0
 * which is available at https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - Rodrigo Fraxino De Araujo <rfaraujo@br.ibm.com>
 *******************************************************************************/
package org.eclipse.linuxtools.profiling.tests;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeTrue;

import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.filesystem.IFileStore;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Platform;
import org.eclipse.remote.core.IRemoteConnection;
import org.eclipse.remote.core.IRemoteConnectionType;
import org.eclipse.remote.core.IRemoteConnectionWorkingCopy;
import org.eclipse.remote.core.IRemoteFileService;
import org.eclipse.remote.core.IRemoteServicesManager;
import org.eclipse.remote.core.exception.RemoteConnectionException;
import org.eclipse.remote.internal.jsch.core.JSchConnection;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;

@SuppressWarnings("restriction")
public abstract class AbstractRemoteTest extends AbstractTest {
	private static final String PLUGIN_ID="org.eclipse.linuxtools.profiling.tests";
    public static final String REMOTE_NATURE_ID = "org.eclipse.ptp.rdt.core.remoteNature"; //$NON-NLS-1$
    public static final String REMOTE_SERVICES = "org.eclipse.ptp.remote.RemoteTools"; //$NON-NLS-1$
    public static final String REMOTE_MAKE_NATURE = "org.eclipse.ptp.rdt.core.remoteMakeNature"; //$NON-NLS-1$
    public static final String REMOTE_MAKE_BUILDER = "org.eclipse.ptp.rdt.core.remoteMakeBuilder"; //$NON-NLS-1$
    public static final String BUILD_SERVICE = "org.eclipse.ptp.rdt.core.BuildService"; //$NON-NLS-1$
    public static final String CINDEX_SERVICE = "org.eclipse.ptp.rdt.core.CIndexingService"; //$NON-NLS-1$
    public static final String RDT_CINDEX_SERVICE = "org.eclipse.ptp.rdt.server.dstore.RemoteToolsCIndexServiceProvider"; //$NON-NLS-1$
    public static final String TOOLCHAIN_ID = "org.eclipse.ptp.rdt.managedbuild.toolchain.gnu.base"; //$NON-NLS-1$
    public static final String PTP_EXE = "org.eclipse.ptp.rdt.managedbuild.target.gnu.exe"; //$NON-NLS-1$
    public static final String DEBUG = "Debug"; //$NON-NLS-1$
    public static String USERNAME = ""; //$NON-NLS-1$
    private static String PASSWORD = ""; //$NON-NLS-1$
    public static final String CONNECTION_TYPE_JSCH = "org.eclipse.remote.JSch";
    public static final String SYNC_SERVICE_GIT = "org.eclipse.ptp.rdt.sync.git.core.synchronizeService";

    // Sets localhost as default connection if no remote host is given
    protected static String HOST = ""; //$NON-NLS-1$
    public static String CONNECTION_NAME = "localhost"; //$NON-NLS-1$
    public static final String RESOURCES_DIR = "resources/"; //$NON-NLS-1$

    // Skip tests if there is not suitable connection details
    public static void checkConnectionInfo() {
        String host = System.getenv("TEST_HOST");
        if (host != null) {
            HOST = host;
        }
        assumeTrue("Skip remote tests due lack of host information", !HOST.isEmpty());

        String username = System.getenv("TEST_USERNAME");
        if (username != null) {
            USERNAME = username;
        }
        assumeTrue("Skip remote tests due lack of an username for connection", !USERNAME.isEmpty());

        String password = System.getenv("TEST_PASSWORD");
        if (password != null) {
            PASSWORD = password;
        }
        assumeTrue("Skip remote tests due lack of an password for connection", !PASSWORD.isEmpty());
    }

     protected void deleteResource (String directory) {
                IRemoteServicesManager sm = getServicesManager();
                IRemoteConnection conn = sm.getConnectionType("ssh").getConnection(CONNECTION_NAME);
                assertNotNull(conn);
                IRemoteFileService fileManager = conn.getService(IRemoteFileService.class);
                assertNotNull(fileManager);
                final IFileStore dstFileStore = fileManager.getResource(directory);
                try {
                    dstFileStore.delete(EFS.NONE, null);
                } catch (CoreException e) {
                }
            }
    /**
     * Create a new connection. Save the connection working copy before return.
     *
     * @param connName Connection Name
     * @param connTypeId The connection type identifier
     * @return The created remote connection
     * @throws RemoteConnectionException
     */
    protected static IRemoteConnection createJSchConnection(String connName, String connTypeId) throws RemoteConnectionException {
        checkConnectionInfo();
        IRemoteServicesManager manager = getServicesManager();
        IRemoteConnectionType ct = manager.getConnectionType(connTypeId);
        assertNotNull(ct);
        IRemoteConnectionWorkingCopy wc = ct.newConnection(connName);
        wc.setAttribute(JSchConnection.ADDRESS_ATTR, HOST);
        wc.setAttribute(JSchConnection.USERNAME_ATTR, USERNAME);
        wc.setSecureAttribute(JSchConnection.PASSWORD_ATTR, PASSWORD);
        IRemoteConnection conn = wc.save();
        assertNotNull(conn);
        conn.open(new NullProgressMonitor());
        assertTrue(conn.isOpen());
        return conn;
    }

    /**
     * Delete connection
     *
     * @param conn The connection
     * @throws RemoteConnectionException
     */
    protected static void deleteConnection(IRemoteConnection conn) throws RemoteConnectionException {
        IRemoteConnectionType ct = conn.getConnectionType();
        ct.removeConnection(conn);
    }

    private static IRemoteServicesManager getServicesManager() {
		BundleContext context = Platform.getBundle(PLUGIN_ID).getBundleContext();
		ServiceReference<IRemoteServicesManager> ref = context.getServiceReference(IRemoteServicesManager.class);
		assertNotNull(ref);
		return context.getService(ref);
    }

}

Back to the top