use of io.cdap.cdap.proto.id.ServiceId in project cdap by caskdata.
the class PreferencesClientTestRun method testPreferences.
@Test
public void testPreferences() throws Exception {
NamespaceId invalidNamespace = new NamespaceId("invalid");
namespaceClient.create(new NamespaceMeta.Builder().setName(invalidNamespace).build());
Map<String, String> propMap = client.getInstancePreferences();
Assert.assertEquals(ImmutableMap.<String, String>of(), propMap);
propMap.put("k1", "instance");
client.setInstancePreferences(propMap);
Assert.assertEquals(propMap, client.getInstancePreferences());
File jarFile = createAppJarFile(FakeApp.class);
appClient.deploy(NamespaceId.DEFAULT, jarFile);
try {
propMap.put("k1", "namespace");
client.setNamespacePreferences(NamespaceId.DEFAULT, propMap);
Assert.assertEquals(propMap, client.getNamespacePreferences(NamespaceId.DEFAULT, true));
Assert.assertEquals(propMap, client.getNamespacePreferences(NamespaceId.DEFAULT, false));
Assert.assertTrue(client.getNamespacePreferences(invalidNamespace, false).isEmpty());
Assert.assertEquals("instance", client.getNamespacePreferences(invalidNamespace, true).get("k1"));
client.deleteNamespacePreferences(NamespaceId.DEFAULT);
propMap.put("k1", "instance");
Assert.assertEquals(propMap, client.getNamespacePreferences(NamespaceId.DEFAULT, true));
Assert.assertEquals(ImmutableMap.<String, String>of(), client.getNamespacePreferences(NamespaceId.DEFAULT, false));
propMap.put("k1", "namespace");
client.setNamespacePreferences(NamespaceId.DEFAULT, propMap);
Assert.assertEquals(propMap, client.getNamespacePreferences(NamespaceId.DEFAULT, true));
Assert.assertEquals(propMap, client.getNamespacePreferences(NamespaceId.DEFAULT, false));
propMap.put("k1", "application");
client.setApplicationPreferences(FAKE_APP_ID, propMap);
Assert.assertEquals(propMap, client.getApplicationPreferences(FAKE_APP_ID, true));
Assert.assertEquals(propMap, client.getApplicationPreferences(FAKE_APP_ID, false));
propMap.put("k1", "program");
ServiceId service = FAKE_APP_ID.service(FakeApp.SERVICES.get(0));
client.setProgramPreferences(service, propMap);
Assert.assertEquals(propMap, client.getProgramPreferences(service, true));
Assert.assertEquals(propMap, client.getProgramPreferences(service, false));
client.deleteProgramPreferences(service);
propMap.put("k1", "application");
Assert.assertTrue(client.getProgramPreferences(service, false).isEmpty());
Assert.assertEquals(propMap, client.getProgramPreferences(service, true));
client.deleteApplicationPreferences(FAKE_APP_ID);
propMap.put("k1", "namespace");
Assert.assertTrue(client.getApplicationPreferences(FAKE_APP_ID, false).isEmpty());
Assert.assertEquals(propMap, client.getApplicationPreferences(FAKE_APP_ID, true));
Assert.assertEquals(propMap, client.getProgramPreferences(service, true));
client.deleteNamespacePreferences(NamespaceId.DEFAULT);
propMap.put("k1", "instance");
Assert.assertTrue(client.getNamespacePreferences(NamespaceId.DEFAULT, false).isEmpty());
Assert.assertEquals(propMap, client.getNamespacePreferences(NamespaceId.DEFAULT, true));
Assert.assertEquals(propMap, client.getApplicationPreferences(FAKE_APP_ID, true));
Assert.assertEquals(propMap, client.getProgramPreferences(service, true));
client.deleteInstancePreferences();
propMap.clear();
Assert.assertEquals(propMap, client.getInstancePreferences());
Assert.assertEquals(propMap, client.getNamespacePreferences(NamespaceId.DEFAULT, true));
Assert.assertEquals(propMap, client.getNamespacePreferences(NamespaceId.DEFAULT, true));
Assert.assertEquals(propMap, client.getApplicationPreferences(FAKE_APP_ID, true));
Assert.assertEquals(propMap, client.getProgramPreferences(service, true));
// Test Deleting Application
propMap.put("k1", "application");
client.setApplicationPreferences(FAKE_APP_ID, propMap);
Assert.assertEquals(propMap, client.getApplicationPreferences(FAKE_APP_ID, false));
propMap.put("k1", "program");
client.setProgramPreferences(service, propMap);
Assert.assertEquals(propMap, client.getProgramPreferences(service, false));
appClient.delete(FAKE_APP_ID);
// deleting the app should have deleted the preferences that were stored. so deploy the app and check
// if the preferences are empty. we need to deploy the app again since getting preferences of non-existent apps
// is not allowed by the API.
appClient.deploy(NamespaceId.DEFAULT, jarFile);
propMap.clear();
Assert.assertEquals(propMap, client.getApplicationPreferences(FAKE_APP_ID, false));
Assert.assertEquals(propMap, client.getProgramPreferences(service, false));
} finally {
try {
appClient.delete(FAKE_APP_ID);
} catch (ApplicationNotFoundException e) {
// ok if this happens, means its already deleted.
}
namespaceClient.delete(invalidNamespace);
}
}
use of io.cdap.cdap.proto.id.ServiceId in project cdap by caskdata.
the class MetricsClientTestRun method testAll.
@Test
public void testAll() throws Exception {
appClient.deploy(NamespaceId.DEFAULT, createAppJarFile(FakeApp.class));
ApplicationId app = NamespaceId.DEFAULT.app(FakeApp.NAME);
ServiceId service = app.service(PingService.NAME);
try {
programClient.start(service);
programClient.waitForStatus(service, ProgramStatus.RUNNING, 15, TimeUnit.SECONDS);
URL serviceURL = serviceClient.getServiceURL(service);
URL pingURL = new URL(serviceURL, "ping");
HttpResponse response = HttpRequests.execute(HttpRequest.get(pingURL).build(), new DefaultHttpRequestConfig(false));
Assert.assertEquals(200, response.getResponseCode());
Tasks.waitFor(true, () -> metricsClient.query(MetricsTags.service(service), Collections.singletonList(Constants.Metrics.Name.Service.SERVICE_INPUT), Collections.emptyList(), ImmutableMap.of("start", "now-20s", "end", "now")).getSeries().length > 0, 10, TimeUnit.SECONDS);
MetricQueryResult result = metricsClient.query(MetricsTags.service(service), Constants.Metrics.Name.Service.SERVICE_INPUT);
Assert.assertEquals(1, result.getSeries()[0].getData()[0].getValue());
result = metricsClient.query(MetricsTags.service(service), Collections.singletonList(Constants.Metrics.Name.Service.SERVICE_INPUT), Collections.emptyList(), Collections.singletonMap("aggregate", "true"));
Assert.assertEquals(1, result.getSeries()[0].getData()[0].getValue());
result = metricsClient.query(MetricsTags.service(service), Collections.singletonList(Constants.Metrics.Name.Service.SERVICE_INPUT), Collections.emptyList(), ImmutableMap.of("start", "now-20s", "end", "now"));
Assert.assertEquals(1, result.getSeries()[0].getData()[0].getValue());
List<MetricTagValue> tags = metricsClient.searchTags(MetricsTags.service(service));
Assert.assertEquals(1, tags.size());
Assert.assertEquals("run", tags.get(0).getName());
List<String> metrics = metricsClient.searchMetrics(MetricsTags.service(service));
Assert.assertTrue(metrics.contains(Constants.Metrics.Name.Service.SERVICE_INPUT));
} finally {
programClient.stop(service);
assertProgramRuns(programClient, service, ProgramRunStatus.KILLED, 1, 10);
appClient.delete(app);
}
}
use of io.cdap.cdap.proto.id.ServiceId in project cdap by cdapio.
the class ProgramLifecycleHttpHandlerTest method testBatchStatus.
@Test
public void testBatchStatus() throws Exception {
final String statusUrl1 = getVersionedAPIPath("status", Constants.Gateway.API_VERSION_3_TOKEN, TEST_NAMESPACE1);
final String statusUrl2 = getVersionedAPIPath("status", Constants.Gateway.API_VERSION_3_TOKEN, TEST_NAMESPACE2);
// invalid json must return 400
Assert.assertEquals(400, doPost(statusUrl1, "").getResponseCode());
Assert.assertEquals(400, doPost(statusUrl2, "").getResponseCode());
// empty array is valid args
Assert.assertEquals(200, doPost(statusUrl1, EMPTY_ARRAY_JSON).getResponseCode());
Assert.assertEquals(200, doPost(statusUrl2, EMPTY_ARRAY_JSON).getResponseCode());
// deploy an app in namespace1
deploy(AllProgramsApp.class, 200, Constants.Gateway.API_VERSION_3_TOKEN, TEST_NAMESPACE1);
// deploy another app in namespace2
deploy(AppWithServices.class, 200, Constants.Gateway.API_VERSION_3_TOKEN, TEST_NAMESPACE2);
Gson gson = new Gson();
// data requires appId, programId, and programType. Test missing fields/invalid programType
List<Map<String, String>> request = Collections.singletonList(ImmutableMap.of("appId", AllProgramsApp.NAME, "programType", "Service"));
Assert.assertEquals(400, doPost(statusUrl1, gson.toJson(request)).getResponseCode());
request = Collections.singletonList(ImmutableMap.of("appId", AllProgramsApp.NAME, "programId", AllProgramsApp.NoOpService.NAME));
Assert.assertEquals(400, doPost(statusUrl1, gson.toJson(request)).getResponseCode());
request = Arrays.asList(ImmutableMap.of("programType", "Service", "programId", AllProgramsApp.NoOpService.NAME), ImmutableMap.of("appId", AppWithServices.NAME, "programType", "service", "programId", AppWithServices.SERVICE_NAME));
Assert.assertEquals(400, doPost(statusUrl1, gson.toJson(request)).getResponseCode());
request = Collections.singletonList(ImmutableMap.of("appId", AllProgramsApp.NAME, "programType", "XXX"));
Assert.assertEquals(400, doPost(statusUrl1, gson.toJson(request)).getResponseCode());
// Test missing app, programType, etc
request = Collections.singletonList(ImmutableMap.of("appId", "NotExist", "programType", ProgramType.SERVICE.getPrettyName(), "programId", "Service"));
List<JsonObject> returnedBody = readResponse(doPost(statusUrl1, gson.toJson(request)), LIST_OF_JSONOBJECT_TYPE);
Assert.assertEquals(HttpURLConnection.HTTP_NOT_FOUND, returnedBody.get(0).get("statusCode").getAsInt());
request = Arrays.asList(ImmutableMap.of("appId", AllProgramsApp.NAME, "programType", ProgramType.SERVICE.getPrettyName(), "programId", "NotExist"), ImmutableMap.of("appId", AllProgramsApp.NAME, "programType", ProgramType.SERVICE.getPrettyName(), "programId", AllProgramsApp.NoOpService.NAME));
returnedBody = readResponse(doPost(statusUrl1, gson.toJson(request)), LIST_OF_JSONOBJECT_TYPE);
Assert.assertEquals(HttpURLConnection.HTTP_NOT_FOUND, returnedBody.get(0).get("statusCode").getAsInt());
// The programType should be consistent. Second object should have proper status
Assert.assertEquals("Service", returnedBody.get(1).get("programType").getAsString());
Assert.assertEquals(STOPPED, returnedBody.get(1).get("status").getAsString());
// test valid cases for namespace1
request = Arrays.asList(ImmutableMap.of("appId", AllProgramsApp.NAME, "programType", "Service", "programId", AllProgramsApp.NoOpService.NAME), ImmutableMap.of("appId", AllProgramsApp.NAME, "programType", "Worker", "programId", AllProgramsApp.NoOpWorker.NAME));
HttpResponse response = doPost(statusUrl1, gson.toJson(request));
verifyInitialBatchStatusOutput(response);
// test valid cases for namespace2
request = Collections.singletonList(ImmutableMap.of("appId", AppWithServices.NAME, "programType", "Service", "programId", AppWithServices.SERVICE_NAME));
response = doPost(statusUrl2, gson.toJson(request));
verifyInitialBatchStatusOutput(response);
// start the service in ns1
ServiceId serviceId1 = new ServiceId(TEST_NAMESPACE1, AllProgramsApp.NAME, AllProgramsApp.NoOpService.NAME);
ServiceId serviceId2 = new ServiceId(TEST_NAMESPACE2, AppWithServices.NAME, AppWithServices.SERVICE_NAME);
startProgram(serviceId1);
waitState(serviceId1, RUNNING);
// test status API after starting the service
request = Arrays.asList(ImmutableMap.of("appId", AllProgramsApp.NAME, "programType", "Service", "programId", AllProgramsApp.NoOpService.NAME), ImmutableMap.of("appId", AllProgramsApp.NAME, "programType", "MapReduce", "programId", AllProgramsApp.NoOpMR.NAME));
response = doPost(statusUrl1, gson.toJson(request));
Assert.assertEquals(200, response.getResponseCode());
returnedBody = readResponse(response, LIST_OF_JSONOBJECT_TYPE);
Assert.assertEquals(ProgramRunStatus.RUNNING.toString(), returnedBody.get(0).get("status").getAsString());
Assert.assertEquals(STOPPED, returnedBody.get(1).get("status").getAsString());
// start the service in ns2
startProgram(serviceId2);
waitState(serviceId2, RUNNING);
// test status API after starting the service
request = Collections.singletonList(ImmutableMap.of("appId", AppWithServices.NAME, "programType", "Service", "programId", AppWithServices.SERVICE_NAME));
response = doPost(statusUrl2, gson.toJson(request));
Assert.assertEquals(200, response.getResponseCode());
returnedBody = readResponse(response, LIST_OF_JSONOBJECT_TYPE);
Assert.assertEquals(ProgramRunStatus.RUNNING.toString(), returnedBody.get(0).get("status").getAsString());
// stop both services
stopProgram(serviceId1);
stopProgram(serviceId2);
waitState(serviceId1, STOPPED);
waitState(serviceId2, STOPPED);
// try posting a status request with namespace2 for apps in namespace1
request = Arrays.asList(ImmutableMap.of("appId", AllProgramsApp.NAME, "programType", "Worker", "programId", AllProgramsApp.NoOpWorker.NAME), ImmutableMap.of("appId", AllProgramsApp.NAME, "programType", "Service", "programId", AllProgramsApp.NoOpService.NAME), ImmutableMap.of("appId", AllProgramsApp.NAME, "programType", "Mapreduce", "programId", AllProgramsApp.NoOpMR.NAME));
response = doPost(statusUrl2, gson.toJson(request));
returnedBody = readResponse(response, LIST_OF_JSONOBJECT_TYPE);
Assert.assertEquals(HttpURLConnection.HTTP_NOT_FOUND, returnedBody.get(0).get("statusCode").getAsInt());
Assert.assertEquals(HttpURLConnection.HTTP_NOT_FOUND, returnedBody.get(1).get("statusCode").getAsInt());
Assert.assertEquals(HttpURLConnection.HTTP_NOT_FOUND, returnedBody.get(2).get("statusCode").getAsInt());
}
use of io.cdap.cdap.proto.id.ServiceId in project cdap by cdapio.
the class ProgramLifecycleHttpHandlerTest method testBatchInstances.
@Test
public void testBatchInstances() throws Exception {
String instancesUrl1 = getVersionedAPIPath("instances", Constants.Gateway.API_VERSION_3_TOKEN, TEST_NAMESPACE1);
Assert.assertEquals(400, doPost(instancesUrl1, "").getResponseCode());
// empty array is valid args
Assert.assertEquals(200, doPost(instancesUrl1, "[]").getResponseCode());
deploy(AllProgramsApp.class, 200, Constants.Gateway.API_VERSION_3_TOKEN, TEST_NAMESPACE1);
Gson gson = new Gson();
// data requires appId, programId, and programType. Test missing fields/invalid programType
List<Map<String, String>> request = Collections.singletonList(ImmutableMap.of("appId", AllProgramsApp.NAME, "programType", "Service"));
Assert.assertEquals(400, doPost(instancesUrl1, gson.toJson(request)).getResponseCode());
request = Collections.singletonList(ImmutableMap.of("appId", AllProgramsApp.NAME, "programId", AllProgramsApp.NoOpService.NAME));
Assert.assertEquals(400, doPost(instancesUrl1, gson.toJson(request)).getResponseCode());
request = Arrays.asList(ImmutableMap.of("programType", "Service", "programId", AllProgramsApp.NoOpService.NAME), ImmutableMap.of("appId", AllProgramsApp.NAME, "programType", "Service", "programId", AllProgramsApp.NoOpService.NAME));
Assert.assertEquals(400, doPost(instancesUrl1, gson.toJson(request)).getResponseCode());
request = Collections.singletonList(ImmutableMap.of("appId", AllProgramsApp.NAME, "programType", "XXX", "programId", AllProgramsApp.NoOpService.NAME));
Assert.assertEquals(400, doPost(instancesUrl1, gson.toJson(request)).getResponseCode());
// Test malformed json
request = Collections.singletonList(ImmutableMap.of("appId", AllProgramsApp.NAME, "programType", "Service", "programId", AllProgramsApp.NoOpService.NAME));
Assert.assertEquals(400, doPost(instancesUrl1, gson.toJson(request) + "]]").getResponseCode());
// Test missing app
request = Collections.singletonList(ImmutableMap.of("appId", "NotExist", "programType", "Service", "programId", AllProgramsApp.NoOpService.NAME));
List<JsonObject> returnedBody = readResponse(doPost(instancesUrl1, gson.toJson(request)), LIST_OF_JSONOBJECT_TYPE);
Assert.assertEquals(404, returnedBody.get(0).get("statusCode").getAsInt());
// valid test
request = Arrays.asList(ImmutableMap.of("appId", AllProgramsApp.NAME, "programType", "Service", "programId", AllProgramsApp.NoOpService.NAME), ImmutableMap.of("appId", AllProgramsApp.NAME, "programType", "Worker", "programId", AllProgramsApp.NoOpWorker.NAME));
HttpResponse response = doPost(instancesUrl1, gson.toJson(request));
verifyInitialBatchInstanceOutput(response);
// start the service
ServiceId serviceId = new ServiceId(TEST_NAMESPACE1, AllProgramsApp.NAME, AllProgramsApp.NoOpService.NAME);
startProgram(serviceId);
waitState(serviceId, RUNNING);
request = Collections.singletonList(ImmutableMap.of("appId", AllProgramsApp.NAME, "programType", "Service", "programId", AllProgramsApp.NoOpService.NAME));
response = doPost(instancesUrl1, gson.toJson(request));
returnedBody = readResponse(response, LIST_OF_JSONOBJECT_TYPE);
Assert.assertEquals(1, returnedBody.get(0).get("provisioned").getAsInt());
// Increase service instances to 2
String setInstanceUrl = getVersionedAPIPath(String.format("apps/%s/services/%s/instances", AllProgramsApp.NAME, AllProgramsApp.NoOpService.NAME), Constants.Gateway.API_VERSION_3_TOKEN, TEST_NAMESPACE1);
Assert.assertEquals(200, doPut(setInstanceUrl, gson.toJson(new Instances(2))).getResponseCode());
// Validate there are 2 instances
Tasks.waitFor(2, () -> {
List<Map<String, String>> request1 = Collections.singletonList(ImmutableMap.of("appId", AllProgramsApp.NAME, "programType", "Service", "programId", AllProgramsApp.NoOpService.NAME));
HttpResponse response1 = doPost(instancesUrl1, gson.toJson(request1));
List<JsonObject> returnedBody1 = readResponse(response1, LIST_OF_JSONOBJECT_TYPE);
return returnedBody1.get(0).get("provisioned").getAsInt();
}, 5, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
stopProgram(serviceId);
waitState(serviceId, STOPPED);
}
use of io.cdap.cdap.proto.id.ServiceId in project cdap by cdapio.
the class CLITestBase method testService.
@Test
public void testService() throws Exception {
ProgramClient programClient = getProgramClient();
ServiceId service = FAKE_APP_ID.service(PrefixedEchoHandler.NAME);
ServiceId serviceV1 = FAKE_APP_ID_V_1.service(PrefixedEchoHandler.NAME);
String serviceName = String.format("%s.%s", FakeApp.NAME, PrefixedEchoHandler.NAME);
String serviceArgument = String.format("%s version %s", serviceName, ApplicationId.DEFAULT_VERSION);
String serviceV1Argument = String.format("%s version %s", serviceName, V1_SNAPSHOT);
try {
// Test service commands with no optional version argument
testCommandOutputContains("start service " + serviceName, "Successfully started service");
assertProgramStatus(programClient, service, ProgramStatus.RUNNING.name());
testCommandOutputContains("get endpoints service " + serviceName, "POST");
testCommandOutputContains("get endpoints service " + serviceName, "/echo");
testCommandOutputContains("check service availability " + serviceName, "Service is available");
testCommandOutputContains("call service " + serviceName + " POST /echo body \"testBody\"", ":testBody");
testCommandOutputContains("stop service " + serviceName, "Successfully stopped service");
assertProgramStatus(programClient, service, ProgramStatus.STOPPED.name());
// Test service commands with version argument when two versions of the service are running
testCommandOutputContains("start service " + serviceArgument, "Successfully started service");
testCommandOutputContains("start service " + serviceV1Argument, "Successfully started service");
assertProgramStatus(programClient, service, ProgramStatus.RUNNING.name());
assertProgramStatus(programClient, serviceV1, ProgramStatus.RUNNING.name());
testCommandOutputContains("get endpoints service " + serviceArgument, "POST");
testCommandOutputContains("get endpoints service " + serviceV1Argument, "POST");
testCommandOutputContains("get endpoints service " + serviceArgument, "/echo");
testCommandOutputContains("get endpoints service " + serviceV1Argument, "/echo");
testCommandOutputContains("check service availability " + serviceArgument, "Service is available");
testCommandOutputContains("check service availability " + serviceV1Argument, "Service is available");
testCommandOutputContains("call service " + serviceArgument + " POST /echo body \"testBody\"", ":testBody");
testCommandOutputContains("call service " + serviceV1Argument + " POST /echo body \"testBody\"", ":testBody");
testCommandOutputContains("get service logs " + serviceName, "Starting HTTP server for Service " + service);
} finally {
// Stop all running services
programClient.stopAll(NamespaceId.DEFAULT);
}
}
Aggregations