Skip to content

Commit

Permalink
Add S3StorageIT and initiate StorageController @startup (#86)
Browse files Browse the repository at this point in the history
* Add S3StorageIT and initiate StorageController @startup

* Add testFileExist by setting storage.physicalFileExistenceCheck to true

* Add config for gcBatchSize
  • Loading branch information
ruhan1 authored Apr 12, 2024
1 parent 9f11e36 commit 9c65d7c
Show file tree
Hide file tree
Showing 7 changed files with 206 additions and 4 deletions.
23 changes: 23 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<toolchains-plugin.version>3.0.0</toolchains-plugin.version>
<cassandra-maven-plugin.version>3.8</cassandra-maven-plugin.version>
<quarkus.package.type>uber-jar</quarkus.package.type>
<testcontainers.version>1.19.7</testcontainers.version>
<skipTests>false</skipTests>
</properties>

Expand All @@ -58,6 +59,13 @@
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers-bom</artifactId>
<version>${testcontainers.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
Expand Down Expand Up @@ -173,6 +181,21 @@
<artifactId>quarkus-junit5-mockito</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>localstack</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ public interface StorageServiceConfig
@WithName( "removableFilesystemPattern" )
String removableFilesystemPattern();

@WithName( "physicalFileExistenceCheck" )
@WithDefault("false")
boolean physicalFileExistenceCheck();

@WithName( "gcBatchSize" )
@WithDefault("100")
int gcBatchSize();

@WithName( "type" )
@WithDefault( STORAGE_NFS )
String type();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.commonjava.service.storage.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.quarkus.runtime.Startup;
import org.apache.commons.io.IOUtils;
import org.commonjava.service.storage.dto.*;
import org.commonjava.storage.pathmapped.core.PathMappedFileManager;
Expand Down Expand Up @@ -45,6 +46,7 @@
import static org.commonjava.service.storage.util.Utils.sort;
import static org.commonjava.storage.pathmapped.util.PathMapUtils.ROOT_DIR;

@Startup
@ApplicationScoped
public class StorageController
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ public PathMappedFileManager getFileManager()

DefaultPathMappedStorageConfig config = new DefaultPathMappedStorageConfig( props );
config.setGcBatchSize( STORAGE_GC_BATCH_SIZE );

config.setPhysicalFileExistenceCheckEnabled( storageConfig.physicalFileExistenceCheck() );
config.setGcBatchSize( storageConfig.gcBatchSize() );

PathDB pathDB = new CassandraPathDB( config );
PhysicalStore physicalStore;
String storageType = storageConfig.type();
Expand Down
3 changes: 0 additions & 3 deletions src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@ quarkus:
max-backup-index: 5
max-file-size: 10M

devservices:
enabled: false

swagger-ui:
always-include: true

Expand Down
94 changes: 94 additions & 0 deletions src/test/java/org/commonjava/service/storage/S3StorageIT.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/**
* Copyright (C) 2021 Red Hat, Inc. (https://github.com/Commonjava/service-parent)
*
* Licensed 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.commonjava.service.storage;

import io.quarkus.test.common.QuarkusTestResource;
import io.quarkus.test.junit.QuarkusTest;
import jakarta.inject.Inject;
import org.commonjava.service.storage.util.LocalStackTestResource;
import org.junit.jupiter.api.Test;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.ListObjectsRequest;
import software.amazon.awssdk.services.s3.model.ListObjectsResponse;
import software.amazon.awssdk.services.s3.model.S3Object;

import java.util.List;

import static io.restassured.RestAssured.given;
import static org.commonjava.service.storage.jaxrs.StorageResource.API_BASE;
import static org.junit.jupiter.api.Assertions.assertTrue;

@QuarkusTest
@QuarkusTestResource(LocalStackTestResource.class)
public class S3StorageIT
extends StorageIT
{
@Inject
S3Client s3Client;

@Test
public void testGetFile()
{
given().pathParam( "filesystem", filesystem )
.pathParam( "path", PATH)
.when()
.get( API_BASE + "/content/{filesystem}/{path}" )
.then()
.statusCode( 200 );
}

/**
* We set "storage.physicalFileExistenceCheck" to "true" in LocalStackTestResource so than it checks
* both pathDB and physical store.
*/
@Test
public void testFileExist()
{
given().pathParam( "filesystem", filesystem )
.pathParam( "path", PATH )
.when()
.head( API_BASE + "/content/{filesystem}/{path}" )
.then()
.statusCode( 200 );
}

@Test
public void testPutFile()
{
given().pathParam( "filesystem", filesystem )
.pathParam( "path", PATH)
.when()
.put( API_BASE + "/content/{filesystem}/{path}" )
.then()
.statusCode( 200 );

verifyBucket();
}

private void verifyBucket()
{
ListObjectsRequest lor = ListObjectsRequest.builder().bucket("test").build();
ListObjectsResponse response = s3Client.listObjects(lor);
List<S3Object> contents = response.contents();

assertTrue(contents.size() > 0 );
for (S3Object et: contents)
{
//System.out.println( ">>> " + et.key());
assertTrue(et.key().contains(PATH));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* Copyright (C) 2021 Red Hat, Inc. (https://github.com/Commonjava/service-parent)
*
* Licensed 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.commonjava.service.storage.util;

import io.quarkus.test.common.QuarkusTestResourceLifecycleManager;
import org.testcontainers.containers.localstack.LocalStackContainer;
import org.testcontainers.utility.DockerImageName;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.CreateBucketRequest;

import java.util.HashMap;
import java.util.Map;

import static org.testcontainers.containers.localstack.LocalStackContainer.Service.S3;

public class LocalStackTestResource implements QuarkusTestResourceLifecycleManager {

static DockerImageName dockerImageName = DockerImageName.parse("localstack/localstack:3.2.0");
static LocalStackContainer localStackContainer = new LocalStackContainer(dockerImageName)
.withServices(S3);

@Override
public Map<String, String> start() {
localStackContainer.start();
prepareBucket();

HashMap<String, String> map = new HashMap<>();
map.put("storage.type", "s3");
map.put("storage.physicalFileExistenceCheck", "true");
map.put("storage.bucket.name", "test");
map.put("quarkus.s3.endpoint-override", localStackContainer.getEndpointOverride(S3).toString());
map.put("quarkus.s3.aws.region", localStackContainer.getRegion());
map.put("quarkus.s3.aws.credentials.type", "static");
map.put("quarkus.s3.aws.credentials.static-provider.access-key-id", localStackContainer.getAccessKey());
map.put("quarkus.s3.aws.credentials.static-provider.secret-access-key", localStackContainer.getSecretKey());
return map;
}

private void prepareBucket()
{
S3Client s3 = S3Client
.builder()
.endpointOverride(localStackContainer.getEndpoint())
.credentialsProvider(
StaticCredentialsProvider.create(
AwsBasicCredentials.create(localStackContainer.getAccessKey(), localStackContainer.getSecretKey())
)
)
.region(Region.of(localStackContainer.getRegion()))
.build();

s3.createBucket(CreateBucketRequest.builder().bucket("test").build());
}

@Override
public void stop() {
localStackContainer.stop();
}

}

0 comments on commit 9c65d7c

Please sign in to comment.