Skip to content

Commit

Permalink
Create TOSUnderFileSystemFactoryTest.java
Browse files Browse the repository at this point in the history
  • Loading branch information
thu-david committed Jul 3, 2024
1 parent e55bf07 commit 695321a
Show file tree
Hide file tree
Showing 2 changed files with 236 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* The Alluxio Open Foundation licenses this work under the Apache License, version 2.0
* (the "License"). You may not use this work except in compliance with the License, which is
* available at www.apache.org/licenses/LICENSE-2.0
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied, as more fully set forth in the License.
*
* See the NOTICE file distributed with this work for information regarding copyright ownership.
*/

package alluxio.underfs.tos;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;

import alluxio.conf.InstancedConfiguration;
import alluxio.conf.PropertyKey;
import alluxio.underfs.UnderFileSystemConfiguration;
import alluxio.underfs.UnderFileSystemFactory;
import alluxio.underfs.UnderFileSystemFactoryRegistry;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;

public class TOSUnderFileSystemFactoryTest {
private final String mTOSPath = "tos://tos-alluxio-test/";
private InstancedConfiguration mAlluxioConf;
private UnderFileSystemConfiguration mConf;
private UnderFileSystemFactory mFactory1;

@Before
public void setUp() {
mAlluxioConf = Mockito.mock(InstancedConfiguration.class);
Mockito.when(mAlluxioConf.get(PropertyKey.MASTER_MOUNT_TABLE_ROOT_UFS))
.thenReturn("tos://tos-alluxio-test/");
Mockito.when(mAlluxioConf.get(PropertyKey.TOS_ACCESS_KEY))
.thenReturn("mockAccessKey");
Mockito.when(mAlluxioConf.get(PropertyKey.TOS_SECRET_KEY))
.thenReturn("mockSecretKey");
Mockito.when(mAlluxioConf.get(PropertyKey.TOS_ENDPOINT_KEY))
.thenReturn("tos-cn-beijing.volces.com");
Mockito.when(mAlluxioConf.get(PropertyKey.TOS_REGION))
.thenReturn("cn-beijing");

mConf = UnderFileSystemConfiguration.defaults(mAlluxioConf);
mFactory1 = UnderFileSystemFactoryRegistry.find(mTOSPath, mAlluxioConf);
}

@Test
public void factory() {
assertNotNull(mFactory1);
}

@Test
public void createInstanceWithNullPath() {
Exception e = assertThrows(NullPointerException.class, () -> mFactory1.create(null, mConf));
assertTrue(e.getMessage().contains("path"));
}

@Test
public void supportsPath() {
assertTrue(mFactory1.supportsPath(mTOSPath));
assertFalse(mFactory1.supportsPath("s3a://test-bucket/path"));
assertFalse(mFactory1.supportsPath("InvalidPath"));
assertFalse(mFactory1.supportsPath(null));
}

@Test
public void createInstanceWithMissingCredentials() {
Mockito.when(mAlluxioConf.get(PropertyKey.TOS_ACCESS_KEY)).thenReturn(null);
Mockito.when(mAlluxioConf.get(PropertyKey.TOS_SECRET_KEY)).thenReturn(null);
mConf = UnderFileSystemConfiguration.defaults(mAlluxioConf);

Exception e = assertThrows(RuntimeException.class, () -> mFactory1.create(mTOSPath, mConf));
assertTrue(e.getMessage().contains("TOS Credentials not available"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/*
* The Alluxio Open Foundation licenses this work under the Apache License, version 2.0
* (the "License"). You may not use this work except in compliance with the License, which is
* available at www.apache.org/licenses/LICENSE-2.0
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied, as more fully set forth in the License.
*
* See the NOTICE file distributed with this work for information regarding copyright ownership.
*/

package alluxio.underfs.tos;

import alluxio.AlluxioURI;
import alluxio.conf.Configuration;
import alluxio.conf.InstancedConfiguration;
import alluxio.conf.PropertyKey;
import alluxio.underfs.UnderFileSystemConfiguration;
import alluxio.underfs.options.DeleteOptions;

import com.volcengine.tos.TOSV2;
import com.volcengine.tos.TosClientException;
import com.volcengine.tos.TosServerException;
import com.volcengine.tos.model.object.HeadObjectV2Input;
import com.volcengine.tos.model.object.ListObjectsType2Input;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.ArgumentMatchers;
import org.mockito.Mockito;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
* Unit tests for the {@link TOSUnderFileSystem}.
*/
public class TOSUnderFileSystemTest {
private static final String PATH = "path";
private static final String SRC = "src";
private static final String DST = "dst";
private static final InstancedConfiguration CONF = Configuration.copyGlobal();

private static final String BUCKET_NAME = "bucket";

private TOSUnderFileSystem mTOSUnderFileSystem;
private TOSV2 mClient;

@Rule
public final ExpectedException mThrown = ExpectedException.none();

@Before
public void setUp() {
mClient = Mockito.mock(TOSV2.class);
mTOSUnderFileSystem = new TOSUnderFileSystem(new AlluxioURI("tos://" + BUCKET_NAME),
mClient, BUCKET_NAME, UnderFileSystemConfiguration.defaults(CONF));
}

/**
* Test case for {@link TOSUnderFileSystem#deleteDirectory(String, DeleteOptions)}.
*/
@Test
public void deleteNonRecursiveOnTosClientException() throws IOException {
Mockito.when(mClient.listObjectsType2(ArgumentMatchers.any(ListObjectsType2Input.class)))
.thenThrow(TosClientException.class);

boolean ret =
mTOSUnderFileSystem.deleteDirectory(PATH, DeleteOptions.defaults().setRecursive(false));
Assert.assertFalse(ret);
}

/**
* Test case for {@link TOSUnderFileSystem#deleteDirectory(String, DeleteOptions)}.
*/
@Test
public void deleteRecursiveOnTosClientException() throws IOException {
Mockito.when(mClient.listObjectsType2(ArgumentMatchers.any(ListObjectsType2Input.class)))
.thenThrow(TosClientException.class);

boolean ret =
mTOSUnderFileSystem.deleteDirectory(PATH, DeleteOptions.defaults().setRecursive(true));
Assert.assertFalse(ret);
}

@Test
public void isFile404() throws IOException {
TosServerException e = new TosServerException(404);
Mockito.when(
mClient.headObject(ArgumentMatchers.any(HeadObjectV2Input.class)))
.thenThrow(e);

Assert.assertFalse(mTOSUnderFileSystem.isFile(SRC));
}

@Test
public void isFileException() throws IOException {
TosServerException e = new TosServerException(403);
Mockito.when(
mClient.headObject(ArgumentMatchers.any(HeadObjectV2Input.class)))
.thenThrow(e);

mThrown.expect(AlluxioTosException.class);
mTOSUnderFileSystem.isFile(SRC);
}

/**
* Test case for {@link TOSUnderFileSystem#renameFile(String, String)}.
*/
@Test
public void renameOnTosClientException() throws IOException {
Mockito.when(
mClient.headObject(ArgumentMatchers.any(HeadObjectV2Input.class)))
.thenThrow(TosClientException.class);

mThrown.expect(AlluxioTosException.class);
mTOSUnderFileSystem.renameFile(SRC, DST);
}

@Test
public void createCredentialsFromConf() {
// Assume appropriate methods to set configuration and create credentials provider
Map<PropertyKey, Object> conf = new HashMap<>();
conf.put(PropertyKey.TOS_ACCESS_KEY, "key1");
conf.put(PropertyKey.TOS_SECRET_KEY, "key2");

UnderFileSystemConfiguration ufsConf = UnderFileSystemConfiguration.defaults(CONF);
// Implement the method to create a credentials provider from the configuration
// TOSCredentialsProvider credentialsProvider =
// TOSUnderFileSystem.createTosCredentialsProvider(ufsConf);
// Assert.assertEquals("key1", credentialsProvider.getAccessKeyId());
// Assert.assertEquals("key2", credentialsProvider.getSecretAccessKey());
}

@Test
public void stripPrefixIfPresent() {
Assert.assertEquals("", mTOSUnderFileSystem.stripPrefixIfPresent("tos://" + BUCKET_NAME));
Assert.assertEquals("", mTOSUnderFileSystem.stripPrefixIfPresent("tos://" + BUCKET_NAME + "/"));
Assert.assertEquals("test/",
mTOSUnderFileSystem.stripPrefixIfPresent("tos://" + BUCKET_NAME + "/test/"));
Assert.assertEquals("test", mTOSUnderFileSystem.stripPrefixIfPresent("test"));
Assert.assertEquals("test/", mTOSUnderFileSystem.stripPrefixIfPresent("test/"));
Assert.assertEquals("test/", mTOSUnderFileSystem.stripPrefixIfPresent("/test/"));
Assert.assertEquals("test", mTOSUnderFileSystem.stripPrefixIfPresent("/test"));
Assert.assertEquals("", mTOSUnderFileSystem.stripPrefixIfPresent(""));
Assert.assertEquals("", mTOSUnderFileSystem.stripPrefixIfPresent("/"));
}

@Test
public void getFolderSuffix() {
Assert.assertEquals("/", mTOSUnderFileSystem.getFolderSuffix());
}
}

0 comments on commit 695321a

Please sign in to comment.