-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Unit Test for OSS, OBS and GCS #17985
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
205 changes: 205 additions & 0 deletions
205
dora/underfs/gcs/src/test/java/alluxio/underfs/gcs/GCSOutputStreamTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,205 @@ | ||
/* | ||
* 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.gcs; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.mockito.ArgumentMatchers.any; | ||
|
||
import alluxio.conf.AlluxioConfiguration; | ||
import alluxio.conf.Configuration; | ||
import alluxio.conf.PropertyKey; | ||
|
||
import org.jets3t.service.ServiceException; | ||
import org.jets3t.service.impl.rest.httpclient.GoogleStorageService; | ||
import org.jets3t.service.model.GSObject; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.ExpectedException; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mockito; | ||
import org.powermock.api.mockito.PowerMockito; | ||
import org.powermock.core.classloader.annotations.PrepareForTest; | ||
import org.powermock.modules.junit4.PowerMockRunner; | ||
|
||
import java.io.BufferedInputStream; | ||
import java.io.BufferedOutputStream; | ||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.security.DigestOutputStream; | ||
|
||
/** | ||
* Unit tests for the {@link GCSOutputStream}. | ||
*/ | ||
@RunWith(PowerMockRunner.class) | ||
public class GCSOutputStreamTest { | ||
private GoogleStorageService mClient; | ||
private File mFile; | ||
private BufferedOutputStream mLocalOutputStream; | ||
private static AlluxioConfiguration sConf = Configuration.global(); | ||
private GSObject mObject; | ||
private String mMd5Hash = "md5 hash"; | ||
|
||
/** | ||
* The exception expected to be thrown. | ||
*/ | ||
@Rule | ||
public final ExpectedException mThrown = ExpectedException.none(); | ||
|
||
/** | ||
* Sets the properties and configuration before each test runs. | ||
*/ | ||
@Before | ||
public void before() throws Exception { | ||
mClient = Mockito.mock(GoogleStorageService.class); | ||
mObject = Mockito.mock(GSObject.class); | ||
Mockito.when(mObject.getMd5HashAsBase64()).thenReturn(mMd5Hash); | ||
Mockito.when(mClient.putObject(Mockito.anyString(), Mockito.any(GSObject.class))) | ||
.thenReturn(mObject); | ||
mFile = Mockito.mock(File.class); | ||
mLocalOutputStream = Mockito.mock(BufferedOutputStream.class); | ||
} | ||
|
||
/** | ||
* Tests to ensure IOException is thrown if {@link FileOutputStream}() throws an IOException. | ||
*/ | ||
@Test | ||
@PrepareForTest(GCSOutputStream.class) | ||
public void testConstructor() throws Exception { | ||
PowerMockito.whenNew(File.class).withArguments(Mockito.anyString()).thenReturn(mFile); | ||
String errorMessage = "protocol doesn't support output"; | ||
PowerMockito.whenNew(FileOutputStream.class).withArguments(mFile) | ||
.thenThrow(new IOException(errorMessage)); | ||
mThrown.expect(IOException.class); | ||
mThrown.expectMessage(errorMessage); | ||
new GCSOutputStream("testBucketName", "testKey", mClient, | ||
sConf.getList(PropertyKey.TMP_DIRS)).close(); | ||
} | ||
|
||
/** | ||
* Tests to ensure {@link GCSOutputStream#write(int)} calls {@link OutputStream#write(int)}. | ||
*/ | ||
@Test | ||
@PrepareForTest(GCSOutputStream.class) | ||
public void testWrite1() throws Exception { | ||
PowerMockito.whenNew(BufferedOutputStream.class) | ||
.withArguments(any(DigestOutputStream.class)).thenReturn(mLocalOutputStream); | ||
PowerMockito.whenNew(BufferedOutputStream.class) | ||
.withArguments(any(FileOutputStream.class)).thenReturn(mLocalOutputStream); | ||
GCSOutputStream stream = new GCSOutputStream("testBucketName", "testKey", mClient, | ||
sConf.getList(PropertyKey.TMP_DIRS)); | ||
stream.write(1); | ||
stream.close(); | ||
assertEquals(mMd5Hash, stream.getContentHash().get()); | ||
Mockito.verify(mLocalOutputStream).write(1); | ||
} | ||
|
||
/** | ||
* Tests to ensure {@link GCSOutputStream#write(byte[], int, int)} calls | ||
* {@link OutputStream#write(byte[], int, int)} . | ||
*/ | ||
@Test | ||
@PrepareForTest(GCSOutputStream.class) | ||
public void testWrite2() throws Exception { | ||
PowerMockito.whenNew(BufferedOutputStream.class) | ||
.withArguments(any(DigestOutputStream.class)).thenReturn(mLocalOutputStream); | ||
PowerMockito.whenNew(BufferedOutputStream.class) | ||
.withArguments(any(FileOutputStream.class)).thenReturn(mLocalOutputStream); | ||
GCSOutputStream stream = new GCSOutputStream("testBucketName", "testKey", mClient, | ||
sConf.getList(PropertyKey.TMP_DIRS)); | ||
byte[] b = new byte[1]; | ||
stream.write(b, 0, 1); | ||
stream.close(); | ||
assertEquals(mMd5Hash, stream.getContentHash().get()); | ||
Mockito.verify(mLocalOutputStream).write(b, 0, 1); | ||
} | ||
|
||
/** | ||
* Tests to ensure {@link GCSOutputStream#write(byte[])} calls {@link OutputStream#write(byte[])}. | ||
*/ | ||
@Test | ||
@PrepareForTest(GCSOutputStream.class) | ||
public void testWrite3() throws Exception { | ||
PowerMockito.whenNew(BufferedOutputStream.class) | ||
.withArguments(any(DigestOutputStream.class)).thenReturn(mLocalOutputStream); | ||
PowerMockito.whenNew(BufferedOutputStream.class) | ||
.withArguments(any(FileOutputStream.class)).thenReturn(mLocalOutputStream); | ||
GCSOutputStream stream = new GCSOutputStream("testBucketName", "testKey", mClient, sConf | ||
.getList(PropertyKey.TMP_DIRS)); | ||
byte[] b = new byte[1]; | ||
stream.write(b); | ||
stream.close(); | ||
assertEquals(mMd5Hash, stream.getContentHash().get()); | ||
Mockito.verify(mLocalOutputStream).write(b, 0, 1); | ||
} | ||
|
||
/** | ||
* Tests to ensure IOException is thrown if | ||
* {@link GoogleStorageService#putObject(String, GSObject)} throws an | ||
* ServiceException. | ||
*/ | ||
@Test | ||
@PrepareForTest(GCSOutputStream.class) | ||
public void testCloseError() throws Exception { | ||
String errorMessage = "Invoke the createEmptyObject method error."; | ||
BufferedInputStream inputStream = PowerMockito.mock(BufferedInputStream.class); | ||
PowerMockito.whenNew(BufferedInputStream.class) | ||
.withArguments(any(FileInputStream.class)).thenReturn(inputStream); | ||
PowerMockito | ||
.when(mClient.putObject(Mockito.anyString(), Mockito.any(GSObject.class))) | ||
.thenThrow(new ServiceException(errorMessage)); | ||
GCSOutputStream stream = new GCSOutputStream("testBucketName", "testKey", mClient, sConf | ||
.getList(PropertyKey.TMP_DIRS)); | ||
mThrown.expect(IOException.class); | ||
mThrown.expectMessage(errorMessage); | ||
stream.close(); | ||
assertEquals(mMd5Hash, stream.getContentHash().get()); | ||
} | ||
|
||
/** | ||
* Tests to ensure {@link File#delete()} is called when close the stream. | ||
*/ | ||
@Test | ||
@PrepareForTest(GCSOutputStream.class) | ||
public void testCloseSuccess() throws Exception { | ||
PowerMockito.whenNew(File.class).withArguments(Mockito.anyString()).thenReturn(mFile); | ||
FileOutputStream outputStream = PowerMockito.mock(FileOutputStream.class); | ||
PowerMockito.whenNew(FileOutputStream.class).withArguments(mFile).thenReturn(outputStream); | ||
FileInputStream inputStream = PowerMockito.mock(FileInputStream.class); | ||
PowerMockito.whenNew(FileInputStream.class).withArguments(mFile).thenReturn(inputStream); | ||
|
||
GCSOutputStream stream = new GCSOutputStream("testBucketName", "testKey", mClient, sConf | ||
.getList(PropertyKey.TMP_DIRS)); | ||
stream.close(); | ||
assertEquals(mMd5Hash, stream.getContentHash().get()); | ||
Mockito.verify(mFile).delete(); | ||
} | ||
|
||
/** | ||
* Tests to ensure {@link GCSOutputStream#flush()} calls {@link OutputStream#flush()}. | ||
*/ | ||
@Test | ||
@PrepareForTest(GCSOutputStream.class) | ||
public void testFlush() throws Exception { | ||
PowerMockito.whenNew(BufferedOutputStream.class) | ||
.withArguments(any(DigestOutputStream.class)).thenReturn(mLocalOutputStream); | ||
GCSOutputStream stream = new GCSOutputStream("testBucketName", "testKey", mClient, sConf | ||
.getList(PropertyKey.TMP_DIRS)); | ||
stream.flush(); | ||
stream.close(); | ||
assertEquals(mMd5Hash, stream.getContentHash().get()); | ||
Mockito.verify(mLocalOutputStream).flush(); | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
dora/underfs/gcs/src/test/java/alluxio/underfs/gcs/GCSPositionReaderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* 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.gcs; | ||
|
||
import org.jets3t.service.ServiceException; | ||
import org.jets3t.service.impl.rest.httpclient.GoogleStorageService; | ||
import org.jets3t.service.model.GSObject; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mockito.ArgumentMatchers; | ||
import org.mockito.Mockito; | ||
|
||
import java.io.BufferedInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
public class GCSPositionReaderTest { | ||
/** | ||
* The GCS Position Reader. | ||
*/ | ||
private GCSPositionReader mPositionReader; | ||
/** | ||
* The COS Client. | ||
*/ | ||
private GoogleStorageService mClient; | ||
/** | ||
* The Bucket Name. | ||
*/ | ||
private final String mBucketName = "bucket"; | ||
/** | ||
* The Path (or the Key). | ||
*/ | ||
private final String mPath = "path"; | ||
/** | ||
* The File Length. | ||
*/ | ||
private final long mFileLength = 100L; | ||
|
||
@Before | ||
public void before() throws Exception { | ||
mClient = Mockito.mock(GoogleStorageService.class); | ||
mPositionReader = new GCSPositionReader(mClient, mBucketName, mPath, mFileLength); | ||
} | ||
|
||
/** | ||
* Test case for {@link GCSPositionReader#openObjectInputStream(long, int)}. | ||
*/ | ||
@Test | ||
public void openObjectInputStream() throws Exception { | ||
GSObject object = Mockito.mock(GSObject.class); | ||
BufferedInputStream objectInputStream = Mockito.mock(BufferedInputStream.class); | ||
Mockito.when(mClient.getObject(Mockito.anyString(), Mockito.anyString(), | ||
Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), | ||
Mockito.any(), Mockito.any())).thenReturn(object); | ||
Mockito.when(object.getDataInputStream()).thenReturn(objectInputStream); | ||
|
||
// test successful open object input stream | ||
long position = 0L; | ||
int bytesToRead = 10; | ||
InputStream inputStream = mPositionReader.openObjectInputStream(position, bytesToRead); | ||
Assert.assertTrue(inputStream instanceof BufferedInputStream); | ||
|
||
// test open object input stream with exception | ||
Mockito.when(mClient.getObject(ArgumentMatchers.anyString(), ArgumentMatchers.anyString())) | ||
.thenThrow(ServiceException.class); | ||
try { | ||
mPositionReader.openObjectInputStream(position, bytesToRead); | ||
} catch (Exception e) { | ||
Assert.assertTrue(e instanceof IOException); | ||
String errorMessage = String | ||
.format("Failed to get object: %s bucket: %s", mPath, mBucketName); | ||
Assert.assertEquals(errorMessage, e.getMessage()); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is not correct, we can't check whether this is set or not since only v1 would set access key and if we use v2 credential we would not be able to create ufs @JiamingMai @zhezhidashi
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I will fix this later.