blob: d966566f92dca61d5b49b556095e25b17d31b929 [file] [log] [blame]
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.oozie.servlet;
import io.prometheus.client.exporter.common.TextFormat;
import org.apache.commons.io.IOUtils;
import org.apache.oozie.client.rest.JsonTags;
import org.apache.oozie.client.rest.RestConstants;
import org.apache.oozie.service.Services;
import org.apache.oozie.BuildInfo;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;
import org.apache.hadoop.util.Shell;
public class TestAdminServlet extends DagServletTestCase {
static {
new V0AdminServlet();
new V0JobServlet();
}
private static final boolean IS_SECURITY_ENABLED = false;
protected void setUp() throws Exception {
super.setUp();
}
public void testStatus() throws Exception {
runTest("/v0/admin/*", V0AdminServlet.class, IS_SECURITY_ENABLED, new Callable<Void>() {
public Void call() throws Exception {
URL url = createURL(RestConstants.ADMIN_STATUS_RESOURCE, Collections.EMPTY_MAP);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
assertEquals(HttpServletResponse.SC_OK, conn.getResponseCode());
assertTrue(conn.getHeaderField("content-type").startsWith(RestConstants.JSON_CONTENT_TYPE));
JSONObject json = (JSONObject) JSONValue.parse(new InputStreamReader(conn.getInputStream(),
StandardCharsets.UTF_8));
assertEquals(false, json.get(JsonTags.OOZIE_SAFE_MODE));
return null;
}
});
}
public void testOsEnv() throws Exception {
runTest("/v0/admin/*", V0AdminServlet.class, IS_SECURITY_ENABLED, new Callable<Void>() {
public Void call() throws Exception {
URL url = createURL(RestConstants.ADMIN_OS_ENV_RESOURCE, Collections.EMPTY_MAP);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
assertEquals(HttpServletResponse.SC_OK, conn.getResponseCode());
assertTrue(conn.getHeaderField("content-type").startsWith(RestConstants.JSON_CONTENT_TYPE));
JSONObject json = (JSONObject) JSONValue.parse(new InputStreamReader(conn.getInputStream(),
StandardCharsets.UTF_8));
assertTrue("USERNAME, USER or HOME property not found in json",
json.containsKey(Shell.WINDOWS ? "USERNAME" : "USER") ||
json.containsKey("HOME")
);
return null;
}
});
}
public void testJavaSysProps() throws Exception {
runTest("/v0/admin/*", V0AdminServlet.class, IS_SECURITY_ENABLED, new Callable<Void>() {
public Void call() throws Exception {
URL url = createURL(RestConstants.ADMIN_JAVA_SYS_PROPS_RESOURCE, Collections.EMPTY_MAP);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
assertEquals(HttpServletResponse.SC_OK, conn.getResponseCode());
assertTrue(conn.getHeaderField("content-type").startsWith(RestConstants.JSON_CONTENT_TYPE));
JSONObject json = (JSONObject) JSONValue.parse(new InputStreamReader(conn.getInputStream(),
StandardCharsets.UTF_8));
assertTrue(json.containsKey("java.version"));
return null;
}
});
}
public void testConfiguration() throws Exception {
runTest("/v0/admin/*", V0AdminServlet.class, IS_SECURITY_ENABLED, new Callable<Void>() {
public Void call() throws Exception {
URL url = createURL(RestConstants.ADMIN_CONFIG_RESOURCE, Collections.EMPTY_MAP);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
assertEquals(HttpServletResponse.SC_OK, conn.getResponseCode());
assertTrue(conn.getHeaderField("content-type").startsWith(RestConstants.JSON_CONTENT_TYPE));
JSONObject json = (JSONObject) JSONValue.parse(new InputStreamReader(conn.getInputStream(),
StandardCharsets.UTF_8));
assertTrue(json.containsKey(Services.CONF_SERVICE_CLASSES));
return null;
}
});
}
public void testMetrics() throws Exception {
runTest("/v2/admin/*", V2AdminServlet.class, IS_SECURITY_ENABLED, new Callable<Void>() {
public Void call() throws Exception {
URL url = createURL(RestConstants.ADMIN_METRICS_RESOURCE, Collections.EMPTY_MAP);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
assertEquals(HttpServletResponse.SC_OK, conn.getResponseCode());
assertTrue(conn.getHeaderField("content-type").startsWith(RestConstants.JSON_CONTENT_TYPE));
JSONObject json = (JSONObject) JSONValue.parse(new InputStreamReader(conn.getInputStream(),
StandardCharsets.UTF_8));
assertTrue(json.containsKey(JsonTags.INSTR_COUNTERS));
return null;
}
});
}
public void testPrometheus() throws Exception {
runTest("/v2/admin/*", V2AdminServlet.class, IS_SECURITY_ENABLED, new Callable<Void>() {
public Void call() throws Exception {
URL url = createURL(RestConstants.ADMIN_PROMETHEUS_RESOURCE, Collections.EMPTY_MAP);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
assertEquals(HttpServletResponse.SC_OK, conn.getResponseCode());
assertEquals(conn.getHeaderField("content-type"), TextFormat.CONTENT_TYPE_004);
String string = IOUtils.toString(conn.getInputStream(), StandardCharsets.UTF_8);
assertTrue("Prometheus metrics does not show up", string.contains("jobstatus_SUCCEEDED"));
return null;
}
});
}
public void testSafeMode() throws Exception {
runTest(new String[]{"/v0/admin/*", "/v0/job/*"}, new Class[]{V0AdminServlet.class, V0JobServlet.class},
IS_SECURITY_ENABLED, new Callable<Void>() {
public Void call() throws Exception {
MockDagEngineService.reset();
Map<String, String> params = new HashMap<String, String>();
params.put(RestConstants.ACTION_PARAM, RestConstants.JOB_ACTION_START);
URL url = createURL("/v0/job/*", MockDagEngineService.JOB_ID + 1, params);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("PUT");
assertEquals(HttpServletResponse.SC_OK, conn.getResponseCode());
MockDagEngineService.reset();
url = createURL("/v0/admin/*", RestConstants.ADMIN_STATUS_RESOURCE, Collections.EMPTY_MAP);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
assertEquals(HttpServletResponse.SC_OK, conn.getResponseCode());
assertTrue(conn.getHeaderField("content-type").startsWith(RestConstants.JSON_CONTENT_TYPE));
JSONObject json = (JSONObject) JSONValue.parse(new InputStreamReader(conn.getInputStream(),
StandardCharsets.UTF_8));
assertTrue(json.containsKey(JsonTags.OOZIE_SAFE_MODE));
assertFalse((Boolean) json.get(JsonTags.OOZIE_SAFE_MODE));
MockDagEngineService.reset();
params = new HashMap<String, String>();
params.put(RestConstants.ADMIN_SAFE_MODE_PARAM, "true");
url = createURL("/v0/admin/*", RestConstants.ADMIN_STATUS_RESOURCE, params);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("PUT");
assertEquals(HttpServletResponse.SC_OK, conn.getResponseCode());
MockDagEngineService.reset();
url = createURL("/v0/admin/*", RestConstants.ADMIN_STATUS_RESOURCE, Collections.EMPTY_MAP);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
assertEquals(HttpServletResponse.SC_OK, conn.getResponseCode());
assertTrue(conn.getHeaderField("content-type").startsWith(RestConstants.JSON_CONTENT_TYPE));
json = (JSONObject) JSONValue.parse(new InputStreamReader(conn.getInputStream(),
StandardCharsets.UTF_8));
assertTrue(json.containsKey(JsonTags.OOZIE_SAFE_MODE));
assertTrue((Boolean) json.get(JsonTags.OOZIE_SAFE_MODE));
MockDagEngineService.reset();
params = new HashMap<String, String>();
params.put(RestConstants.ACTION_PARAM, RestConstants.JOB_ACTION_START);
url = createURL("/v0/job/*", MockDagEngineService.JOB_ID + 1, params);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("PUT");
assertEquals(HttpServletResponse.SC_SERVICE_UNAVAILABLE, conn.getResponseCode());
MockDagEngineService.reset();
params = new HashMap<String, String>();
params.put(RestConstants.ADMIN_SAFE_MODE_PARAM, "false");
url = createURL("/v0/admin/*", RestConstants.ADMIN_STATUS_RESOURCE, params);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("PUT");
assertEquals(HttpServletResponse.SC_OK, conn.getResponseCode());
MockDagEngineService.reset();
url = createURL("/v0/admin/*", RestConstants.ADMIN_STATUS_RESOURCE, Collections.EMPTY_MAP);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
assertEquals(HttpServletResponse.SC_OK, conn.getResponseCode());
assertTrue(conn.getHeaderField("content-type").startsWith(RestConstants.JSON_CONTENT_TYPE));
json = (JSONObject) JSONValue.parse(new InputStreamReader(conn.getInputStream(),
StandardCharsets.UTF_8));
assertTrue(json.containsKey(JsonTags.OOZIE_SAFE_MODE));
assertFalse((Boolean) json.get(JsonTags.OOZIE_SAFE_MODE));
MockDagEngineService.reset();
params = new HashMap<String, String>();
params.put(RestConstants.ACTION_PARAM, RestConstants.JOB_ACTION_START);
url = createURL("/v0/job/*", MockDagEngineService.JOB_ID + 1, params);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("PUT");
assertEquals(HttpServletResponse.SC_OK, conn.getResponseCode());
return null;
}
});
}
public void testVersion() throws Exception {
runTest("/v0/admin/*", V0AdminServlet.class, IS_SECURITY_ENABLED, new Callable<Void>() {
public Void call() throws Exception {
URL url = createURL(RestConstants.ADMIN_BUILD_VERSION_RESOURCE, Collections.EMPTY_MAP);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
assertEquals(HttpServletResponse.SC_OK, conn.getResponseCode());
assertTrue(conn.getHeaderField("content-type").startsWith(RestConstants.JSON_CONTENT_TYPE));
final String response = IOUtils.toString(conn.getInputStream());
JSONObject json = (JSONObject) JSONValue.parse(new StringReader(response));
assertEquals(BuildInfo.getBuildInfo().getProperty(BuildInfo.BUILD_VERSION), json.get(JsonTags.BUILD_VERSION));
JSONObject buildInfo = (JSONObject) json.get(JsonTags.BUILD_INFO);
for (String buildInfoKey : BuildInfo.getBuildInfo().stringPropertyNames()) {
assertEquals("Build value difference in key " + buildInfoKey,
BuildInfo.getBuildInfo().getProperty(buildInfoKey), buildInfo.get(buildInfoKey));
}
return null;
}
});
}
public void testShareLibUpdate() throws Exception {
runTest("/v2/admin/*", V2AdminServlet.class, IS_SECURITY_ENABLED, new Callable<Void>() {
public Void call() throws Exception {
URL url = createURL(RestConstants.ADMIN_UPDATE_SHARELIB, Collections.EMPTY_MAP);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
assertEquals(HttpServletResponse.SC_OK, conn.getResponseCode());
assertTrue(conn.getHeaderField("content-type").startsWith(RestConstants.JSON_CONTENT_TYPE));
JSONObject json = (JSONObject) JSONValue.parse(new InputStreamReader(conn.getInputStream(),
StandardCharsets.UTF_8));
assertEquals(1, json.entrySet().size());
return null;
}
});
}
public void testShareLib() throws Exception {
runTest("/v2/admin/*", V2AdminServlet.class, IS_SECURITY_ENABLED, new Callable<Void>() {
public Void call() throws Exception {
URL url = createURL(RestConstants.ADMIN_LIST_SHARELIB, Collections.EMPTY_MAP);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
assertEquals(HttpServletResponse.SC_OK, conn.getResponseCode());
assertTrue(conn.getHeaderField("content-type").startsWith(RestConstants.JSON_CONTENT_TYPE));
JSONObject json = (JSONObject) JSONValue.parse(new InputStreamReader(conn.getInputStream(),
StandardCharsets.UTF_8));
assertEquals(null, json.get(JsonTags.SHARELIB_LIB));
return null;
}
});
}
public void testShareLib_withKey() throws Exception {
runTest("/v2/admin/*", V2AdminServlet.class, IS_SECURITY_ENABLED, new Callable<Void>() {
public Void call() throws Exception {
Map<String, String> map = new HashMap<String, String>();
map.put(RestConstants.SHARE_LIB_REQUEST_KEY, "pig");
URL url = createURL(RestConstants.ADMIN_LIST_SHARELIB, map);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
assertEquals(HttpServletResponse.SC_OK, conn.getResponseCode());
assertTrue(conn.getHeaderField("content-type").startsWith(RestConstants.JSON_CONTENT_TYPE));
JSONObject json = (JSONObject) JSONValue.parse(new InputStreamReader(conn.getInputStream(),
StandardCharsets.UTF_8));
assertEquals(null, json.get(JsonTags.SHARELIB_LIB));
return null;
}
});
}
public void testPurgeServiceV2() throws Exception {
runTest("/v2/admin/*", V2AdminServlet.class, IS_SECURITY_ENABLED, new Callable<Void>() {
@Override
public Void call() throws Exception {
URL url = createURL(RestConstants.ADMIN_PURGE, Collections.EMPTY_MAP);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("PUT");
assertEquals(HttpServletResponse.SC_OK, connection.getResponseCode());
return null;
}
});
}
public void testPurgeServiceV2Negative() throws Exception {
runTest("/v2/admin/*", V2AdminServlet.class, IS_SECURITY_ENABLED, new Callable<Void>() {
@Override
public Void call() throws Exception {
Map<String, String> params = new HashMap<>();
params.put(RestConstants.PURGE_WF_AGE, "30");
params.put(RestConstants.PURGE_COORD_AGE, "7");
params.put(RestConstants.PURGE_BUNDLE_AGE, "-7");
URL url = createURL(RestConstants.ADMIN_PURGE, params);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("PUT");
assertEquals(HttpServletResponse.SC_BAD_REQUEST, connection.getResponseCode());
return null;
}
});
}
}