aboutsummaryrefslogtreecommitdiff
path: root/test/com/sun/tools/attach/StartManagementAgent.java
blob: da8289558ae05b11c7651c93aeb458f8a3fe807b (plain)
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
 * Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

import com.sun.tools.attach.AttachOperationFailedException;
import com.sun.tools.attach.VirtualMachine;

import java.io.File;
import java.io.FileWriter;
import java.util.Properties;
import java.util.HashMap;

import javax.management.remote.JMXServiceURL;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;

import jdk.testlibrary.ProcessThread;
import jdk.testlibrary.Utils;

/*
 * @test
 * @summary Test for VirtualMachine.startManagementAgent and VirtualMachine.startLocalManagementAgent
 * @library /lib/testlibrary
 * @run build Application Shutdown SimpleProvider jdk.testlibrary.*
 * @run main StartManagementAgent
 */

/*
 * This test is not meant to test all possible configuration parameters to
 * the JMX agent, there are other tests for that. This test makes sure it is
 * possible to start the agent via attach.
 */
public class StartManagementAgent {
    public static void main(String[] args) throws Throwable {
        final String pidFile = "StartManagementAgent.Application.pid";
        ProcessThread processThread = null;
        RunnerUtil.ProcessInfo info = null;
        try {
            processThread = RunnerUtil.startApplication(pidFile);
            info = RunnerUtil.readProcessInfo(pidFile);
            runTests(info.pid);
        } catch (Throwable t) {
            System.out.println("StartManagementAgent got unexpected exception: " + t);
            t.printStackTrace();
            throw t;
        } finally {
            // Make sure the Application process is stopped.
            RunnerUtil.stopApplication(info.shutdownPort, processThread);
        }
    }

    private static void basicTests(VirtualMachine vm) throws Exception {

        // Try calling with null argument
        boolean exception = false;
        try {
            vm.startManagementAgent(null);
        } catch (NullPointerException e) {
            exception = true;
        }
        if (!exception) {
            throw new Exception("startManagementAgent(null) should throw NPE");
        }

        // Try calling with a property value with a space in it
        Properties p = new Properties();
        File f = new File("file with space");
        try (FileWriter fw = new FileWriter(f)) {
            fw.write("com.sun.management.jmxremote.port=apa");
        }
        p.put("com.sun.management.config.file", f.getAbsolutePath());
        try {
            vm.startManagementAgent(p);
        } catch(AttachOperationFailedException ex) {
            // We expect parsing of "apa" above to fail, but if the file path
            // can't be read we get a different exception message
            if (!ex.getMessage().contains("Invalid com.sun.management.jmxremote.port number")) {
                throw ex;
            }
        }
    }

    private static final String LOCAL_CONNECTOR_ADDRESS_PROP =
        "com.sun.management.jmxremote.localConnectorAddress";

    private static final int MAX_RETRIES = 10;

    public static void runTests(int pid) throws Exception {
        VirtualMachine vm = VirtualMachine.attach(""+pid);
        try {

            basicTests(vm);

            testLocalAgent(vm);

            // we retry the remote case several times in case the error
            // was caused by a port conflict
            int i = 0;
            boolean success = false;
            do {
                try {
                    System.err.println("Trying remote agent. Try #" + i);
                    testRemoteAgent(vm);
                    success = true;
                } catch(Exception ex) {
                    System.err.println("testRemoteAgent failed with exception:");
                    ex.printStackTrace();
                    System.err.println("Retrying.");
                }
                i++;
            } while(!success && i < MAX_RETRIES);
            if (!success) {
                throw new Exception("testRemoteAgent failed after " + MAX_RETRIES + " tries");
            }
        } finally {
            vm.detach();
        }
    }

    public static void testLocalAgent(VirtualMachine vm) throws Exception {
        Properties agentProps = vm.getAgentProperties();
        String address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);
        if (address != null) {
            throw new Exception("Local management agent already started");
        }

        String result = vm.startLocalManagementAgent();

        // try to parse the return value as a JMXServiceURL
        new JMXServiceURL(result);

        agentProps = vm.getAgentProperties();
        address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);
        if (address == null) {
            throw new Exception("Local management agent could not be started");
        }
    }

    public static void testRemoteAgent(VirtualMachine vm) throws Exception {
        int port = Utils.getFreePort();

        // try to connect - should fail
        tryConnect(port, false);

        // start agent
        System.out.println("Starting agent on port: " + port);
        Properties mgmtProps = new Properties();
        mgmtProps.put("com.sun.management.jmxremote.port", port);
        mgmtProps.put("com.sun.management.jmxremote.authenticate", "false");
        mgmtProps.put("com.sun.management.jmxremote.ssl", "false");
        vm.startManagementAgent(mgmtProps);

        // try to connect - should work
        tryConnect(port, true);

        // try to start again - should fail
        boolean exception = false;
        try {
            vm.startManagementAgent(mgmtProps);
        } catch(AttachOperationFailedException ex) {
            // expected
            exception = true;
        }
        if (!exception) {
            throw new Exception("Expected the second call to vm.startManagementAgent() to fail");
        }
    }

    private static void tryConnect(int port, boolean shouldSucceed) throws Exception {
        String jmxUrlStr =
            String.format(
                "service:jmx:rmi:///jndi/rmi://localhost:%d/jmxrmi",
                port);
        JMXServiceURL url = new JMXServiceURL(jmxUrlStr);
        HashMap<String, ?> env = new HashMap<>();

        boolean succeeded;
        try {
            JMXConnector c = JMXConnectorFactory.connect(url, env);
            c.getMBeanServerConnection();
            succeeded = true;
        } catch(Exception ex) {
            succeeded = false;
        }
        if (succeeded && !shouldSucceed) {
            throw new Exception("Could connect to agent, but should not have been possible");
        }
        if (!succeeded && shouldSucceed) {
            throw new Exception("Could not connect to agent");
        }
    }
}