Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@

package org.apache.iceberg.aws;

import java.util.List;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import software.amazon.awssdk.services.glue.GlueClient;
import software.amazon.awssdk.services.glue.model.DeleteDatabaseRequest;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.Delete;
import software.amazon.awssdk.services.s3.model.DeleteObjectsRequest;
Expand All @@ -29,6 +34,8 @@

public class AwsIntegTestUtil {

private static final Logger LOG = LoggerFactory.getLogger(AwsIntegTestUtil.class);

private AwsIntegTestUtil() {
}

Expand All @@ -55,4 +62,15 @@ public static void cleanS3Bucket(S3Client s3, String bucketName, String prefix)
}
}
}

public static void cleanGlueCatalog(GlueClient glue, List<String> namespaces) {
for (String namespace : namespaces) {
try {
// delete db also delete tables
glue.deleteDatabase(DeleteDatabaseRequest.builder().name(namespace).build());
} catch (Exception e) {
LOG.error("Cannot delete namespace {}", namespace, e);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*
* 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.iceberg.aws.glue;

import java.util.List;
import java.util.Map;
import org.apache.iceberg.AssertHelpers;
import org.apache.iceberg.catalog.Namespace;
import org.apache.iceberg.exceptions.AlreadyExistsException;
import org.apache.iceberg.exceptions.NamespaceNotEmptyException;
import org.apache.iceberg.exceptions.ValidationException;
import org.apache.iceberg.relocated.com.google.common.collect.Lists;
import org.apache.iceberg.relocated.com.google.common.collect.Maps;
import org.apache.iceberg.relocated.com.google.common.collect.Sets;
import org.junit.Assert;
import org.junit.Test;
import software.amazon.awssdk.services.glue.model.Database;
import software.amazon.awssdk.services.glue.model.EntityNotFoundException;
import software.amazon.awssdk.services.glue.model.GetDatabaseRequest;

public class GlueCatalogNamespaceTest extends GlueTestBase {

@Test
public void testCreateNamespace() {
String namespace = getRandomName();
namespaces.add(namespace);
AssertHelpers.assertThrows("namespace does not exist before create",
EntityNotFoundException.class,
"not found",
() -> glue.getDatabase(GetDatabaseRequest.builder().name(namespace).build()));
glueCatalog.createNamespace(Namespace.of(namespace));
Database database = glue.getDatabase(GetDatabaseRequest.builder().name(namespace).build()).database();
Assert.assertEquals("namespace must equal database name", namespace, database.name());
}

@Test
public void testCreateDuplicate() {
String namespace = createNamespace();
AssertHelpers.assertThrows("should not create namespace with the same name",
AlreadyExistsException.class,
"it already exists in Glue",
() -> glueCatalog.createNamespace(Namespace.of(namespace)));
}

@Test
public void testCreateBadName() {
List<Namespace> invalidNamespaces = Lists.newArrayList(
Namespace.of("db-1"),
Namespace.of("db", "db2")
);

for (Namespace namespace : invalidNamespaces) {
AssertHelpers.assertThrows("should not create namespace with invalid or nested names",
ValidationException.class,
"Cannot convert namespace",
() -> glueCatalog.createNamespace(namespace));
}
}

@Test
public void testNamespaceExists() {
String namespace = createNamespace();
Assert.assertTrue(glueCatalog.namespaceExists(Namespace.of(namespace)));
}

@Test
public void testListNamespace() {
String namespace = createNamespace();
List<Namespace> namespaceList = glueCatalog.listNamespaces();
Assert.assertTrue(namespaceList.size() > 0);
Assert.assertTrue(namespaceList.contains(Namespace.of(namespace)));
namespaceList = glueCatalog.listNamespaces(Namespace.of(namespace));
Assert.assertTrue(namespaceList.isEmpty());
}

@Test
public void testNamespaceProperties() {
String namespace = createNamespace();
// set properties
Map<String, String> properties = Maps.newHashMap();
properties.put("key", "val");
properties.put("key2", "val2");
glueCatalog.setProperties(Namespace.of(namespace), properties);
Database database = glue.getDatabase(GetDatabaseRequest.builder().name(namespace).build()).database();
Assert.assertTrue(database.parameters().containsKey("key"));
Assert.assertEquals("val", database.parameters().get("key"));
Assert.assertTrue(database.parameters().containsKey("key2"));
Assert.assertEquals("val2", database.parameters().get("key2"));
// remove properties
glueCatalog.removeProperties(Namespace.of(namespace), Sets.newHashSet("key"));
database = glue.getDatabase(GetDatabaseRequest.builder().name(namespace).build()).database();
Assert.assertFalse(database.parameters().containsKey("key"));
Assert.assertTrue(database.parameters().containsKey("key2"));
Assert.assertEquals("val2", database.parameters().get("key2"));
// add back
properties = Maps.newHashMap();
properties.put("key", "val");
glueCatalog.setProperties(Namespace.of(namespace), properties);
database = glue.getDatabase(GetDatabaseRequest.builder().name(namespace).build()).database();
Assert.assertTrue(database.parameters().containsKey("key"));
Assert.assertEquals("val", database.parameters().get("key"));
Assert.assertTrue(database.parameters().containsKey("key2"));
Assert.assertEquals("val2", database.parameters().get("key2"));
}

@Test
public void testDropNamespace() {
String namespace = createNamespace();
glueCatalog.dropNamespace(Namespace.of(namespace));
AssertHelpers.assertThrows("namespace should not exist after deletion",
EntityNotFoundException.class,
"not found",
() -> glue.getDatabase(GetDatabaseRequest.builder().name(namespace).build()));
}

@Test
public void testDropNamespaceNonEmpty() {
String namespace = createNamespace();
createTable(namespace);
AssertHelpers.assertThrows("namespace should not be dropped when still has table",
NamespaceNotEmptyException.class,
"it is not empty",
() -> glueCatalog.dropNamespace(Namespace.of(namespace)));
}

}
Loading