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
5 changes: 5 additions & 0 deletions hadoop-ozone/client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@
</dependency>

<!-- Test dependencies -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.ozone</groupId>
<artifactId>hdds-client</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* 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.hadoop.ozone.client;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;

import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.io.IOUtils;

/** Utilities for tests using Ozone client. */
public final class OzoneClientTestUtils {

/** Verify contents of a key.
* @return key details for convenience (further checks) */
public static OzoneKeyDetails assertKeyContent(
OzoneBucket bucket,
String keyName,
String expected
) throws IOException {
return assertKeyContent(bucket, keyName, expected.getBytes(UTF_8));
}

/** Verify contents of a key.
* @return key details for convenience (further checks) */
public static OzoneKeyDetails assertKeyContent(
OzoneBucket bucket,
String keyName,
byte[] expected
) throws IOException {
try (InputStream is = bucket.readKey(keyName)) {
assertArrayEquals(expected, IOUtils.readFully(is, expected.length));
}
return bucket.getKey(keyName);
}

private OzoneClientTestUtils() {
// no instances
}
}
6 changes: 6 additions & 0 deletions hadoop-ozone/integration-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,12 @@
<artifactId>ozone-client</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.ozone</groupId>
<artifactId>ozone-client</artifactId>
<type>test-jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.ozone</groupId>
<artifactId>ozone-common</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import static org.apache.hadoop.ozone.OzoneConsts.GB;
import static org.apache.hadoop.ozone.OzoneConsts.MD5_HASH;
import static org.apache.hadoop.ozone.OzoneConsts.OZONE_URI_DELIMITER;
import static org.apache.hadoop.ozone.client.OzoneClientTestUtils.assertKeyContent;
import static org.apache.hadoop.ozone.om.OMConfigKeys.OZONE_DIR_DELETING_SERVICE_INTERVAL;
import static org.apache.hadoop.ozone.om.exceptions.OMException.ResultCodes.KEY_NOT_FOUND;
import static org.apache.hadoop.ozone.om.exceptions.OMException.ResultCodes.NO_SUCH_MULTIPART_UPLOAD_ERROR;
Expand Down Expand Up @@ -1405,20 +1406,6 @@ private static void rewriteKey(
}
}

private static OzoneKeyDetails assertKeyContent(
OzoneBucket bucket, String keyName, byte[] expectedContent
) throws IOException {
OzoneKeyDetails updatedKeyDetails = bucket.getKey(keyName);

try (OzoneInputStream is = bucket.readKey(keyName)) {
byte[] fileContent = new byte[expectedContent.length];
IOUtils.readFully(is, fileContent);
assertArrayEquals(expectedContent, fileContent);
}

return updatedKeyDetails;
}

private OzoneBucket createBucket(BucketLayout layout) throws IOException {
String volumeName = UUID.randomUUID().toString();
String bucketName = UUID.randomUUID().toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,13 @@

import static org.apache.hadoop.hdds.protocol.proto.HddsProtos.ReplicationFactor.THREE;
import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_CLIENT_KEY_LATEST_VERSION_LOCATION;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.apache.hadoop.ozone.client.OzoneClientTestUtils.assertKeyContent;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.UUID;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.RandomUtils;
import org.apache.hadoop.hdds.client.RatisReplicationConfig;
import org.apache.hadoop.hdds.client.ReplicationConfig;
Expand Down Expand Up @@ -107,13 +105,6 @@ private static void writeKey(OzoneBucket bucket, String key, byte[] content,
TestDataUtil.createKey(bucket, key, replication, content);
}

public static void assertKeyContent(OzoneBucket bucket, String key,
byte[] expected) throws Exception {
try (InputStream in = bucket.readKey(key)) {
assertArrayEquals(expected, IOUtils.readFully(in, expected.length));
}
}

private void assertListStatus(OzoneBucket bucket, String keyName,
int expectedVersionCount) throws Exception {
List<OzoneFileStatus> files = bucket.listStatus(keyName, false, "", 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package org.apache.hadoop.ozone.client.rpc;

import static org.apache.hadoop.hdds.protocol.proto.HddsProtos.ReplicationFactor.THREE;
import static org.apache.hadoop.ozone.client.rpc.TestOzoneRpcClientWithKeyLatestVersion.assertKeyContent;
import static org.apache.hadoop.ozone.client.OzoneClientTestUtils.assertKeyContent;
import static org.apache.hadoop.ozone.om.request.OMRequestTestUtils.configureFSOptimizedPaths;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
Expand Down
6 changes: 6 additions & 0 deletions hadoop-ozone/s3gateway/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,12 @@
<artifactId>weld-servlet-shaded</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.ozone</groupId>
<artifactId>ozone-client</artifactId>
<type>test-jar</type>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* 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.hadoop.ozone.s3.endpoint;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import javax.ws.rs.core.Response;
import org.apache.hadoop.ozone.s3.exception.OS3Exception;
import org.apache.http.HttpStatus;
import org.apache.ratis.util.function.CheckedSupplier;

/** Utilities for unit-testing S3 endpoints. */
public final class EndpointTestUtils {

/** Put without content. */
public static Response putDir(
ObjectEndpoint subject,
String bucket,
String key
) throws IOException, OS3Exception {
return put(subject, bucket, key, 0, null, null);
}

/** Put with content. */
public static Response put(
ObjectEndpoint subject,
String bucket,
String key,
String content
) throws IOException, OS3Exception {
return put(subject, bucket, key, 0, null, content);
}

/** Put with content, part number, upload ID. */
public static Response put(
ObjectEndpoint subject,
String bucket,
String key,
int partNumber,
String uploadID,
String content
) throws IOException, OS3Exception {
if (content == null) {
return subject.put(bucket, key, 0, partNumber, uploadID, null, null, null);
} else {
final long length = content.length();
try (ByteArrayInputStream body = new ByteArrayInputStream(content.getBytes(UTF_8))) {
return subject.put(bucket, key, length, partNumber, uploadID, null, null, body);
}
}
}

/** Verify response is success for {@code request}. */
public static <E extends Exception> void assertSucceeds(CheckedSupplier<Response, E> request) throws E {
try (Response response = request.get()) {
assertEquals(HttpStatus.SC_OK, response.getStatus());
}
}

/** Verify error response for {@code request} matching {@code expected} {@link OS3Exception}. */
public static OS3Exception assertErrorResponse(OS3Exception expected, CheckedSupplier<Response, ?> request) {
OS3Exception actual = assertThrows(OS3Exception.class, () -> request.get().close());
assertEquals(expected.getCode(), actual.getCode());
assertEquals(expected.getHttpCode(), actual.getHttpCode());
return actual;
}

private EndpointTestUtils() {
// no instances
}
}
Loading