Search in sources :

Example 51 with NamespaceMeta

use of co.cask.cdap.proto.NamespaceMeta in project cdap by caskdata.

the class CLIMainTest method testNamespacesOutput.

private static void testNamespacesOutput(CLI cli, String command, final List<NamespaceMeta> expected) throws Exception {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    PrintStream output = new PrintStream(outputStream);
    Table table = Table.builder().setHeader("name", "description", "config").setRows(expected, new RowMaker<NamespaceMeta>() {

        @Override
        public List<?> makeRow(NamespaceMeta object) {
            return Lists.newArrayList(object.getName(), object.getDescription(), NamespaceCommandUtils.prettyPrintNamespaceConfigCLI(object.getConfig()));
        }
    }).build();
    cliMain.getTableRenderer().render(cliConfig, output, table);
    final String expectedOutput = outputStream.toString();
    testCommand(cli, command, new Function<String, Void>() {

        @Nullable
        @Override
        public Void apply(@Nullable String output) {
            Assert.assertNotNull(output);
            Assert.assertEquals(expectedOutput, output);
            return null;
        }
    });
}
Also used : PrintStream(java.io.PrintStream) Table(co.cask.cdap.cli.util.table.Table) RowMaker(co.cask.cdap.cli.util.RowMaker) NamespaceMeta(co.cask.cdap.proto.NamespaceMeta) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Nullable(javax.annotation.Nullable)

Example 52 with NamespaceMeta

use of co.cask.cdap.proto.NamespaceMeta in project cdap by caskdata.

the class CLIMainTest method testNamespaces.

@Test
public void testNamespaces() throws Exception {
    final String name = PREFIX + "testNamespace";
    final String description = "testDescription";
    final String keytab = "keytab";
    final String principal = "principal";
    final String group = "group";
    final String hbaseNamespace = "hbase";
    final String hiveDatabase = "hiveDB";
    final String schedulerQueueName = "queue";
    File rootdir = TEMPORARY_FOLDER.newFolder("rootdir");
    final String rootDirectory = rootdir.getAbsolutePath();
    final String defaultFields = PREFIX + "defaultFields";
    final String doesNotExist = "doesNotExist";
    createHiveDB(hiveDatabase);
    // initially only default namespace should be present
    NamespaceMeta defaultNs = new NamespaceMeta.Builder().setName("default").setDescription("Default Namespace").build();
    List<NamespaceMeta> expectedNamespaces = Lists.newArrayList(defaultNs);
    testNamespacesOutput(cli, "list namespaces", expectedNamespaces);
    // describe non-existing namespace
    testCommandOutputContains(cli, String.format("describe namespace %s", doesNotExist), String.format("Error: 'namespace:%s' was not found", doesNotExist));
    // delete non-existing namespace
    // TODO: uncomment when fixed - this makes build hang since it requires confirmation from user
    //    testCommandOutputContains(cli, String.format("delete namespace %s", doesNotExist),
    //                              String.format("Error: namespace '%s' was not found", doesNotExist));
    // create a namespace
    String command = String.format("create namespace %s description %s principal %s group-name %s keytab-URI %s " + "hbase-namespace %s hive-database %s root-directory %s %s %s %s %s", name, description, principal, group, keytab, hbaseNamespace, hiveDatabase, rootDirectory, ArgumentName.NAMESPACE_SCHEDULER_QUEUENAME, schedulerQueueName, ArgumentName.NAMESPACE_EXPLORE_AS_PRINCIPAL, false);
    testCommandOutputContains(cli, command, String.format("Namespace '%s' created successfully.", name));
    NamespaceMeta expected = new NamespaceMeta.Builder().setName(name).setDescription(description).setPrincipal(principal).setGroupName(group).setKeytabURI(keytab).setHBaseNamespace(hbaseNamespace).setSchedulerQueueName(schedulerQueueName).setHiveDatabase(hiveDatabase).setRootDirectory(rootDirectory).setExploreAsPrincipal(false).build();
    expectedNamespaces = Lists.newArrayList(defaultNs, expected);
    // list namespaces and verify
    testNamespacesOutput(cli, "list namespaces", expectedNamespaces);
    // get namespace details and verify
    expectedNamespaces = Lists.newArrayList(expected);
    command = String.format("describe namespace %s", name);
    testNamespacesOutput(cli, command, expectedNamespaces);
    // try creating a namespace with existing id
    command = String.format("create namespace %s", name);
    testCommandOutputContains(cli, command, String.format("Error: 'namespace:%s' already exists\n", name));
    // create a namespace with default name and description
    command = String.format("create namespace %s", defaultFields);
    testCommandOutputContains(cli, command, String.format("Namespace '%s' created successfully.", defaultFields));
    NamespaceMeta namespaceDefaultFields = new NamespaceMeta.Builder().setName(defaultFields).setDescription("").build();
    // test that there are 3 namespaces including default
    expectedNamespaces = Lists.newArrayList(defaultNs, namespaceDefaultFields, expected);
    testNamespacesOutput(cli, "list namespaces", expectedNamespaces);
    // describe namespace with default fields
    expectedNamespaces = Lists.newArrayList(namespaceDefaultFields);
    testNamespacesOutput(cli, String.format("describe namespace %s", defaultFields), expectedNamespaces);
    // delete namespace and verify
    // TODO: uncomment when fixed - this makes build hang since it requires confirmation from user
    //    command = String.format("delete namespace %s", name);
    //    testCommandOutputContains(cli, command, String.format("Namespace '%s' deleted successfully.", name));
    dropHiveDb(hiveDatabase);
}
Also used : NamespaceMeta(co.cask.cdap.proto.NamespaceMeta) File(java.io.File) Test(org.junit.Test)

Example 53 with NamespaceMeta

use of co.cask.cdap.proto.NamespaceMeta in project cdap by caskdata.

the class BaseHiveExploreServiceTest method createNamespace.

/**
   * Create a namespace because app fabric is not started in explore tests.
   */
protected static void createNamespace(NamespaceId namespaceId) throws Exception {
    namespacedLocationFactory.get(namespaceId).mkdirs();
    NamespaceMeta namespaceMeta = new NamespaceMeta.Builder().setName(namespaceId.toId()).build();
    namespaceAdmin.create(namespaceMeta);
    if (!NamespaceId.DEFAULT.equals(namespaceId)) {
        exploreService.createNamespace(namespaceMeta);
    }
}
Also used : NamespaceMeta(co.cask.cdap.proto.NamespaceMeta) GsonBuilder(com.google.gson.GsonBuilder)

Example 54 with NamespaceMeta

use of co.cask.cdap.proto.NamespaceMeta in project cdap by caskdata.

the class ExploreDisabledTest method start.

@BeforeClass
public static void start() throws Exception {
    Injector injector = Guice.createInjector(createInMemoryModules(CConfiguration.create(), new Configuration()));
    transactionManager = injector.getInstance(TransactionManager.class);
    transactionManager.startAndWait();
    dsOpExecutor = injector.getInstance(DatasetOpExecutor.class);
    dsOpExecutor.startAndWait();
    datasetService = injector.getInstance(DatasetService.class);
    datasetService.startAndWait();
    exploreClient = injector.getInstance(DiscoveryExploreClient.class);
    try {
        exploreClient.ping();
        Assert.fail("Expected not to be able to ping explore client.");
    } catch (ServiceUnavailableException e) {
    // expected
    }
    datasetFramework = injector.getInstance(DatasetFramework.class);
    namespaceAdmin = injector.getInstance(NamespaceAdmin.class);
    NamespacedLocationFactory namespacedLocationFactory = injector.getInstance(NamespacedLocationFactory.class);
    NamespaceMeta namespaceMeta = new NamespaceMeta.Builder().setName(namespaceId).build();
    namespaceAdmin.create(namespaceMeta);
    // This happens when you create a namespace via REST APIs. However, since we do not start AppFabricServer in
    // Explore tests, simulating that scenario by explicitly calling DatasetFramework APIs.
    namespacedLocationFactory.get(namespaceId).mkdirs();
    exploreClient.addNamespace(namespaceMeta);
}
Also used : DatasetFramework(co.cask.cdap.data2.dataset2.DatasetFramework) DiscoveryExploreClient(co.cask.cdap.explore.client.DiscoveryExploreClient) CConfiguration(co.cask.cdap.common.conf.CConfiguration) Configuration(org.apache.hadoop.conf.Configuration) Injector(com.google.inject.Injector) TransactionManager(org.apache.tephra.TransactionManager) NamespaceMeta(co.cask.cdap.proto.NamespaceMeta) NamespaceAdmin(co.cask.cdap.common.namespace.NamespaceAdmin) DatasetService(co.cask.cdap.data2.datafabric.dataset.service.DatasetService) DatasetOpExecutor(co.cask.cdap.data2.datafabric.dataset.service.executor.DatasetOpExecutor) ServiceUnavailableException(co.cask.cdap.common.ServiceUnavailableException) NamespacedLocationFactory(co.cask.cdap.common.namespace.NamespacedLocationFactory) BeforeClass(org.junit.BeforeClass)

Example 55 with NamespaceMeta

use of co.cask.cdap.proto.NamespaceMeta in project cdap by caskdata.

the class ApplicationLifecycleServiceTest method testOwnerInImpersonatedNS.

@Test
public void testOwnerInImpersonatedNS() throws Exception {
    // create a namespace with principal
    String nsPrincipal = "nsCreator/somehost.net@somekdc.net";
    String nsKeytabURI = "some/path";
    NamespaceMeta impNsMeta = new NamespaceMeta.Builder().setName("impNs").setPrincipal(nsPrincipal).setKeytabURI(nsKeytabURI).build();
    createNamespace(GSON.toJson(impNsMeta), impNsMeta.getName());
    // deploy an app without owner
    HttpResponse response = deploy(WordCountApp.class, Constants.Gateway.API_VERSION_3_TOKEN, impNsMeta.getName());
    Assert.assertEquals(HttpResponseStatus.OK.getCode(), response.getStatusLine().getStatusCode());
    // try to redeploy with some owner different than namespace principal it should fail
    response = deploy(WordCountApp.class, Constants.Gateway.API_VERSION_3_TOKEN, impNsMeta.getName(), "bob/somehost.net@somekdc.net");
    Assert.assertEquals(HttpResponseStatus.FORBIDDEN.getCode(), response.getStatusLine().getStatusCode());
    // although trying to re-deploy the app with namespace principal should work
    response = deploy(WordCountApp.class, Constants.Gateway.API_VERSION_3_TOKEN, impNsMeta.getName(), nsPrincipal);
    Assert.assertEquals(HttpResponseStatus.OK.getCode(), response.getStatusLine().getStatusCode());
    // re-deploy without any owner should also work
    response = deploy(WordCountApp.class, Constants.Gateway.API_VERSION_3_TOKEN, impNsMeta.getName());
    Assert.assertEquals(HttpResponseStatus.OK.getCode(), response.getStatusLine().getStatusCode());
    // cleanup
    deleteNamespace(impNsMeta.getName());
    Assert.assertEquals(HttpResponseCodes.SC_NOT_FOUND, getNamespace(impNsMeta.getName()).getStatusLine().getStatusCode());
    // create an impersonated ns again
    createNamespace(GSON.toJson(impNsMeta), impNsMeta.getName());
    // deploy an app with an owner
    response = deploy(WordCountApp.class, Constants.Gateway.API_VERSION_3_TOKEN, impNsMeta.getName(), "bob/somehost.net@somekdc.net");
    Assert.assertEquals(HttpResponseStatus.OK.getCode(), response.getStatusLine().getStatusCode());
    // re-deploy with namespace principal should fail
    response = deploy(WordCountApp.class, Constants.Gateway.API_VERSION_3_TOKEN, impNsMeta.getName(), impNsMeta.getConfig().getPrincipal());
    Assert.assertEquals(HttpResponseStatus.FORBIDDEN.getCode(), response.getStatusLine().getStatusCode());
    // although redeploy with same app principal should work
    response = deploy(WordCountApp.class, Constants.Gateway.API_VERSION_3_TOKEN, impNsMeta.getName(), "bob/somehost.net@somekdc.net");
    Assert.assertEquals(HttpResponseStatus.OK.getCode(), response.getStatusLine().getStatusCode());
}
Also used : NamespaceMeta(co.cask.cdap.proto.NamespaceMeta) HttpResponse(org.apache.http.HttpResponse) WordCountApp(co.cask.cdap.WordCountApp) Test(org.junit.Test)

Aggregations

NamespaceMeta (co.cask.cdap.proto.NamespaceMeta)58 NamespaceId (co.cask.cdap.proto.id.NamespaceId)26 Test (org.junit.Test)22 IOException (java.io.IOException)12 Location (org.apache.twill.filesystem.Location)6 KeyValueTable (co.cask.cdap.api.dataset.lib.KeyValueTable)5 BadRequestException (co.cask.cdap.common.BadRequestException)5 NamespaceNotFoundException (co.cask.cdap.common.NamespaceNotFoundException)5 ApplicationManager (co.cask.cdap.test.ApplicationManager)5 ExecutionException (java.util.concurrent.ExecutionException)5 NamespaceConfig (co.cask.cdap.proto.NamespaceConfig)4 UnauthorizedException (co.cask.cdap.security.spi.authorization.UnauthorizedException)4 DatasetManagementException (co.cask.cdap.api.dataset.DatasetManagementException)3 RowMaker (co.cask.cdap.cli.util.RowMaker)3 Table (co.cask.cdap.cli.util.table.Table)3 NotFoundException (co.cask.cdap.common.NotFoundException)3 CConfiguration (co.cask.cdap.common.conf.CConfiguration)3 NamespaceAdmin (co.cask.cdap.common.namespace.NamespaceAdmin)3 Id (co.cask.cdap.proto.Id)3 ApplicationId (co.cask.cdap.proto.id.ApplicationId)3