Search in sources :

Example 21 with ArtifactSummary

use of co.cask.cdap.api.artifact.ArtifactSummary in project cdap by caskdata.

the class AuthorizationTest method testPrograms.

@Test
public void testPrograms() throws Exception {
    createAuthNamespace();
    final ApplicationManager dummyAppManager = deployApplication(AUTH_NAMESPACE, DummyApp.class);
    ArtifactSummary dummyArtifactSummary = dummyAppManager.getInfo().getArtifact();
    ArtifactId dummyArtifact = AUTH_NAMESPACE.artifact(dummyArtifactSummary.getName(), dummyArtifactSummary.getVersion());
    ApplicationId appId = AUTH_NAMESPACE.app(DummyApp.class.getSimpleName());
    final ProgramId serviceId = appId.service(DummyApp.Greeting.SERVICE_NAME);
    DatasetId dsId = AUTH_NAMESPACE.dataset("whom");
    StreamId streamId = AUTH_NAMESPACE.stream("who");
    assertAllAccess(ALICE, AUTH_NAMESPACE, dummyArtifact, appId, serviceId, dsId, streamId);
    // alice should be able to start and stop programs in the app she deployed
    dummyAppManager.startProgram(serviceId.toId());
    Tasks.waitFor(true, new Callable<Boolean>() {

        @Override
        public Boolean call() throws Exception {
            return dummyAppManager.isRunning(serviceId.toId());
        }
    }, 5, TimeUnit.SECONDS);
    ServiceManager greetingService = dummyAppManager.getServiceManager(serviceId.getProgram());
    // alice should be able to set instances for the program
    greetingService.setInstances(2);
    Assert.assertEquals(2, greetingService.getProvisionedInstances());
    // alice should also be able to save runtime arguments for all future runs of the program
    Map<String, String> args = ImmutableMap.of("key", "value");
    greetingService.setRuntimeArgs(args);
    // Alice should be able to get runtime arguments as she has ADMIN on it
    Assert.assertEquals(args, greetingService.getRuntimeArgs());
    dummyAppManager.stopProgram(serviceId.toId());
    Tasks.waitFor(false, new Callable<Boolean>() {

        @Override
        public Boolean call() throws Exception {
            return dummyAppManager.isRunning(serviceId.toId());
        }
    }, 5, TimeUnit.SECONDS);
    // Bob should not be able to start programs in dummy app because he does not have privileges on it
    SecurityRequestContext.setUserId(BOB.getName());
    try {
        dummyAppManager.startProgram(serviceId.toId());
        Assert.fail("Bob should not be able to start the service because he does not have admin privileges on it.");
    } catch (RuntimeException expected) {
        //noinspection ThrowableResultOfMethodCallIgnored
        Assert.assertTrue(Throwables.getRootCause(expected) instanceof UnauthorizedException);
    }
    try {
        dummyAppManager.getInfo();
        Assert.fail("Bob should not be able to read the app info with out privileges");
    } catch (Exception expected) {
    // expected
    }
    // setting instances should fail because Bob does not have admin privileges on the program
    try {
        greetingService.setInstances(3);
        Assert.fail("Setting instances should have failed because bob does not have admin privileges on the service.");
    } catch (RuntimeException expected) {
        //noinspection ThrowableResultOfMethodCallIgnored
        Assert.assertTrue(Throwables.getRootCause(expected) instanceof UnauthorizedException);
    }
    try {
        greetingService.setRuntimeArgs(args);
        Assert.fail("Setting runtime arguments should have failed because bob does not have admin privileges on the " + "service");
    } catch (UnauthorizedException expected) {
    // expected
    }
    try {
        greetingService.getRuntimeArgs();
        Assert.fail("Getting runtime arguments should have failed because bob does not have READ privileges on the " + "service");
    } catch (UnauthorizedException expected) {
    // expected
    }
    SecurityRequestContext.setUserId(ALICE.getName());
    dummyAppManager.delete();
    assertNoAccess(appId);
}
Also used : ApplicationManager(co.cask.cdap.test.ApplicationManager) StreamId(co.cask.cdap.proto.id.StreamId) ArtifactId(co.cask.cdap.proto.id.ArtifactId) ProgramId(co.cask.cdap.proto.id.ProgramId) TimeoutException(java.util.concurrent.TimeoutException) UnauthorizedException(co.cask.cdap.security.spi.authorization.UnauthorizedException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) DatasetId(co.cask.cdap.proto.id.DatasetId) ArtifactSummary(co.cask.cdap.api.artifact.ArtifactSummary) ServiceManager(co.cask.cdap.test.ServiceManager) DummyApp(co.cask.cdap.test.app.DummyApp) UnauthorizedException(co.cask.cdap.security.spi.authorization.UnauthorizedException) ApplicationId(co.cask.cdap.proto.id.ApplicationId) Test(org.junit.Test)

Example 22 with ArtifactSummary

use of co.cask.cdap.api.artifact.ArtifactSummary in project cdap by caskdata.

the class TestFrameworkTestBase method deployWithArtifact.

/**
   * Deploys an {@link Application} using the given artifact jar with an optional config object.
   */
protected static <T> ApplicationManager deployWithArtifact(NamespaceId namespaceId, Class<? extends Application> appClass, File artifactJar, @Nullable T config) throws Exception {
    ArtifactId artifactId = new ArtifactId(namespaceId.getNamespace(), appClass.getSimpleName(), "1.0-SNAPSHOT");
    addArtifact(artifactId, artifactJar);
    AppRequest<T> appRequest = new AppRequest<>(new ArtifactSummary(artifactId.getArtifact(), artifactId.getVersion()), config);
    return deployApplication(namespaceId.app(appClass.getSimpleName()).toId(), appRequest);
}
Also used : ArtifactSummary(co.cask.cdap.api.artifact.ArtifactSummary) ArtifactId(co.cask.cdap.proto.id.ArtifactId) AppRequest(co.cask.cdap.proto.artifact.AppRequest)

Example 23 with ArtifactSummary

use of co.cask.cdap.api.artifact.ArtifactSummary in project cdap by caskdata.

the class TestFrameworkTestRun method testAppWithPlugin.

@Test
public void testAppWithPlugin() throws Exception {
    ArtifactId artifactId = NamespaceId.DEFAULT.artifact("app-with-plugin", "1.0.0-SNAPSHOT");
    addAppArtifact(artifactId, AppWithPlugin.class);
    ArtifactId pluginArtifactId = NamespaceId.DEFAULT.artifact("test-plugin", "1.0.0-SNAPSHOT");
    addPluginArtifact(pluginArtifactId, artifactId, ToStringPlugin.class);
    ApplicationId appId = NamespaceId.DEFAULT.app("AppWithPlugin");
    AppRequest createRequest = new AppRequest(new ArtifactSummary(artifactId.getArtifact(), artifactId.getVersion()));
    ApplicationManager appManager = deployApplication(appId, createRequest);
    final WorkerManager workerManager = appManager.getWorkerManager(AppWithPlugin.WORKER);
    workerManager.start();
    workerManager.waitForStatus(false, 5, 1);
    Tasks.waitFor(false, new Callable<Boolean>() {

        @Override
        public Boolean call() throws Exception {
            return workerManager.getHistory(ProgramRunStatus.COMPLETED).isEmpty();
        }
    }, 5, TimeUnit.SECONDS, 10, TimeUnit.MILLISECONDS);
    final ServiceManager serviceManager = appManager.getServiceManager(AppWithPlugin.SERVICE);
    serviceManager.start();
    serviceManager.waitForStatus(true, 1, 10);
    URL serviceURL = serviceManager.getServiceURL(5, TimeUnit.SECONDS);
    callServiceGet(serviceURL, "dummy");
    serviceManager.stop();
    serviceManager.waitForStatus(false, 1, 10);
    Tasks.waitFor(false, new Callable<Boolean>() {

        @Override
        public Boolean call() throws Exception {
            return serviceManager.getHistory(ProgramRunStatus.KILLED).isEmpty();
        }
    }, 5, TimeUnit.SECONDS, 10, TimeUnit.MILLISECONDS);
    WorkflowManager workflowManager = appManager.getWorkflowManager(AppWithPlugin.WORKFLOW);
    workflowManager.start();
    workflowManager.waitForRun(ProgramRunStatus.COMPLETED, 5, TimeUnit.MINUTES);
    List<RunRecord> runRecords = workflowManager.getHistory();
    Assert.assertNotEquals(ProgramRunStatus.FAILED, runRecords.get(0).getStatus());
    DataSetManager<KeyValueTable> workflowTableManager = getDataset(AppWithPlugin.WORKFLOW_TABLE);
    String value = Bytes.toString(workflowTableManager.get().read("val"));
    Assert.assertEquals(AppWithPlugin.TEST, value);
    Map<String, String> workflowTags = ImmutableMap.of(Constants.Metrics.Tag.NAMESPACE, NamespaceId.DEFAULT.getNamespace(), Constants.Metrics.Tag.APP, "AppWithPlugin", Constants.Metrics.Tag.WORKFLOW, AppWithPlugin.WORKFLOW, Constants.Metrics.Tag.RUN_ID, runRecords.get(0).getPid());
    getMetricsManager().waitForTotalMetricCount(workflowTags, String.format("user.destroy.%s", AppWithPlugin.WORKFLOW), 1, 60, TimeUnit.SECONDS);
    // Testing Spark Plugins. First send some data to stream for the Spark program to process
    StreamManager streamManager = getStreamManager(AppWithPlugin.SPARK_STREAM);
    for (int i = 0; i < 5; i++) {
        streamManager.send("Message " + i);
    }
    SparkManager sparkManager = appManager.getSparkManager(AppWithPlugin.SPARK).start();
    sparkManager.waitForRun(ProgramRunStatus.COMPLETED, 2, TimeUnit.MINUTES);
    // Verify the Spark result.
    DataSetManager<Table> dataSetManager = getDataset(AppWithPlugin.SPARK_TABLE);
    Table table = dataSetManager.get();
    try (Scanner scanner = table.scan(null, null)) {
        for (int i = 0; i < 5; i++) {
            Row row = scanner.next();
            Assert.assertNotNull(row);
            String expected = "Message " + i + " " + AppWithPlugin.TEST;
            Assert.assertEquals(expected, Bytes.toString(row.getRow()));
            Assert.assertEquals(expected, Bytes.toString(row.get(expected)));
        }
        // There shouldn't be any more rows in the table.
        Assert.assertNull(scanner.next());
    }
}
Also used : Scanner(co.cask.cdap.api.dataset.table.Scanner) ApplicationManager(co.cask.cdap.test.ApplicationManager) ArtifactId(co.cask.cdap.proto.id.ArtifactId) WorkflowManager(co.cask.cdap.test.WorkflowManager) URL(java.net.URL) ServiceManager(co.cask.cdap.test.ServiceManager) SparkManager(co.cask.cdap.test.SparkManager) KeyValueTable(co.cask.cdap.api.dataset.lib.KeyValueTable) Table(co.cask.cdap.api.dataset.table.Table) ConflictException(co.cask.cdap.common.ConflictException) IOException(java.io.IOException) TimeoutException(java.util.concurrent.TimeoutException) AppRequest(co.cask.cdap.proto.artifact.AppRequest) WorkerManager(co.cask.cdap.test.WorkerManager) RunRecord(co.cask.cdap.proto.RunRecord) ArtifactSummary(co.cask.cdap.api.artifact.ArtifactSummary) KeyValueTable(co.cask.cdap.api.dataset.lib.KeyValueTable) StreamManager(co.cask.cdap.test.StreamManager) Row(co.cask.cdap.api.dataset.table.Row) ApplicationId(co.cask.cdap.proto.id.ApplicationId) Test(org.junit.Test)

Example 24 with ArtifactSummary

use of co.cask.cdap.api.artifact.ArtifactSummary in project cdap by caskdata.

the class TestFrameworkTestRun method testAppFromArtifact.

@Test
public void testAppFromArtifact() throws Exception {
    ArtifactId artifactId = NamespaceId.DEFAULT.artifact("cfg-app", "1.0.0-SNAPSHOT");
    addAppArtifact(artifactId, ConfigTestApp.class);
    ApplicationId appId = NamespaceId.DEFAULT.app("AppFromArtifact");
    AppRequest<ConfigTestApp.ConfigClass> createRequest = new AppRequest<>(new ArtifactSummary(artifactId.getArtifact(), artifactId.getVersion()), new ConfigTestApp.ConfigClass("testStream", "testDataset"));
    ApplicationManager appManager = deployApplication(appId, createRequest);
    testAppConfig(appId.getApplication(), appManager, createRequest.getConfig());
}
Also used : ApplicationManager(co.cask.cdap.test.ApplicationManager) ArtifactSummary(co.cask.cdap.api.artifact.ArtifactSummary) ArtifactId(co.cask.cdap.proto.id.ArtifactId) ApplicationId(co.cask.cdap.proto.id.ApplicationId) ConfigTestApp(co.cask.cdap.ConfigTestApp) AppRequest(co.cask.cdap.proto.artifact.AppRequest) Test(org.junit.Test)

Example 25 with ArtifactSummary

use of co.cask.cdap.api.artifact.ArtifactSummary in project cdap by caskdata.

the class IntegrationTestBase method doClear.

private void doClear(NamespaceId namespace, boolean deleteNamespace) throws Exception {
    // stop all programs in the namespace
    getProgramClient().stopAll(namespace);
    if (deleteNamespace) {
        getNamespaceClient().delete(namespace);
        return;
    }
    // delete all apps in the namespace
    for (ApplicationRecord app : getApplicationClient().list(namespace)) {
        getApplicationClient().delete(namespace.app(app.getName(), app.getAppVersion()));
    }
    // delete all streams
    for (StreamDetail streamDetail : getStreamClient().list(namespace)) {
        getStreamClient().delete(namespace.stream(streamDetail.getName()));
    }
    // delete all dataset instances
    for (DatasetSpecificationSummary datasetSpecSummary : getDatasetClient().list(namespace)) {
        getDatasetClient().delete(namespace.dataset(datasetSpecSummary.getName()));
    }
    ArtifactClient artifactClient = new ArtifactClient(getClientConfig(), getRestClient());
    for (ArtifactSummary artifactSummary : artifactClient.list(namespace, ArtifactScope.USER)) {
        artifactClient.delete(namespace.artifact(artifactSummary.getName(), artifactSummary.getVersion()));
    }
    assertIsClear(namespace);
}
Also used : ArtifactSummary(co.cask.cdap.api.artifact.ArtifactSummary) StreamDetail(co.cask.cdap.proto.StreamDetail) ArtifactClient(co.cask.cdap.client.ArtifactClient) DatasetSpecificationSummary(co.cask.cdap.proto.DatasetSpecificationSummary) ApplicationRecord(co.cask.cdap.proto.ApplicationRecord)

Aggregations

ArtifactSummary (co.cask.cdap.api.artifact.ArtifactSummary)59 Test (org.junit.Test)34 ApplicationId (co.cask.cdap.proto.id.ApplicationId)31 AppRequest (co.cask.cdap.proto.artifact.AppRequest)28 Id (co.cask.cdap.proto.Id)21 NamespaceId (co.cask.cdap.proto.id.NamespaceId)21 ArtifactId (co.cask.cdap.proto.id.ArtifactId)18 ProgramId (co.cask.cdap.proto.id.ProgramId)13 IOException (java.io.IOException)10 ApplicationManager (co.cask.cdap.test.ApplicationManager)9 HashMap (java.util.HashMap)8 StreamId (co.cask.cdap.proto.id.StreamId)7 HashSet (java.util.HashSet)7 ArtifactRange (co.cask.cdap.api.artifact.ArtifactRange)6 ArtifactVersion (co.cask.cdap.api.artifact.ArtifactVersion)6 PluginInfo (co.cask.cdap.proto.artifact.PluginInfo)6 JsonObject (com.google.gson.JsonObject)6 File (java.io.File)6 URL (java.net.URL)6 NotFoundException (co.cask.cdap.common.NotFoundException)5