Skip to content
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

Give up processing and throw ConfigException when "Auth fail" appears at the first time #33

Merged
merged 4 commits into from
Oct 5, 2018
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
14 changes: 6 additions & 8 deletions src/main/java/org/embulk/input/sftp/SftpFileInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,12 @@ else if (files.isFile()) {
@Override
public boolean isRetryableException(Exception exception)
{
if (exception.getCause() != null && exception.getCause().getCause() != null) {
Throwable cause = exception.getCause().getCause();
if (cause.getMessage() != null && cause.getMessage().contains("Auth fail")) {
throw new ConfigException(exception);
}
}
if (exception instanceof ConfigException) {
return false;
}
Expand All @@ -288,14 +294,6 @@ public void onRetry(Exception exception, int retryCount, int retryLimit, int ret
public void onGiveup(Exception firstException, Exception lastException)
throws RetryGiveupException
{
// Generally, Auth fail should be caught and throw ConfigException when first retry. But this library is a bit unstable.
// So we throw ConfigException after all retries are completed
if (lastException.getCause() != null && lastException.getCause().getCause() != null) {
Throwable cause = lastException.getCause().getCause();
if (cause.getMessage().contains("Auth fail")) {
throw new ConfigException(lastException);
}
}
}
});
}
Expand Down
31 changes: 31 additions & 0 deletions src/test/java/org/embulk/input/sftp/TestSftpFileInputPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

public class TestSftpFileInputPlugin
{
Expand Down Expand Up @@ -248,6 +249,36 @@ public List<TaskReport> run(TaskSource taskSource, int taskCount)
assertEquals(SftpFileInput.getRelativePath(task, Optional.of(expected.get(1).get(0))), configDiff.get(String.class, "last_path"));
}

@Test
public void testListFilesAuthFail() throws Exception
{
uploadFile(Resources.getResource("sample_01.csv").getPath(), REMOTE_DIRECTORY + "sample_01.csv", true);
uploadFile(Resources.getResource("sample_02.csv").getPath(), REMOTE_DIRECTORY + "sample_02.csv", true);

PluginTask task = config.deepCopy().set("user", "wrong_user").loadConfig(PluginTask.class);

plugin.transaction(config, new FileInputPlugin.Control() {
@Override
public List<TaskReport> run(TaskSource taskSource, int taskCount)
{
assertEquals(2, taskCount);
return emptyTaskReports(taskCount);
}
});

Method listFilesByPrefix = SftpFileInput.class.getDeclaredMethod("listFilesByPrefix", PluginTask.class);
listFilesByPrefix.setAccessible(true);
try {
listFilesByPrefix.invoke(plugin, task);
fail();
}
catch (Exception ex) {
Throwable cause = ex.getCause();
assertEquals(cause.getClass(), ConfigException.class);
assertEquals(cause.getCause().getCause().getCause().getMessage(), "Auth fail");
}
}

@Test
public void testListFilesWithPathPrefixPointToFile() throws Exception
{
Expand Down