-
Notifications
You must be signed in to change notification settings - Fork 370
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Extract SFTP tests to a separate module for readability * Add an SFTP test where slice mode is activated * Don't mask exception coming from the FTP backend * add `retrieveFileStream` in `SFTPClient` Co-authored-by: Jehan Bruggeman <[email protected]>
- Loading branch information
Showing
5 changed files
with
279 additions
and
170 deletions.
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
80 changes: 80 additions & 0 deletions
80
...p/src/it/scala/com/datamountaineer/streamreactor/connect/ftp/source/SftpBySliceTest.scala
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,80 @@ | ||
package com.datamountaineer.streamreactor.connect.ftp.source | ||
|
||
import com.datamountaineer.streamreactor.connect.ftp.source.EndToEnd.DummyOffsetStorage | ||
import com.github.stefanbirkner.fakesftpserver.lambda.FakeSftpServer.withSftpServer | ||
import com.typesafe.scalalogging.StrictLogging | ||
import org.apache.kafka.connect.source.SourceRecord | ||
import org.scalatest.BeforeAndAfter | ||
import org.scalatest.concurrent.Eventually.eventually | ||
import org.scalatest.funsuite.AnyFunSuite | ||
import org.scalatest.matchers.should.Matchers | ||
|
||
import scala.jdk.CollectionConverters.MapHasAsJava | ||
|
||
class SftpBySliceTest extends AnyFunSuite with Matchers with BeforeAndAfter with StrictLogging { | ||
|
||
val lineSep = System.getProperty("line.separator") | ||
val fileContent1 = (0 to 10000).map(index => s"line_${index}${lineSep}").mkString.getBytes | ||
|
||
test( | ||
"Sftp:Same content mode by slices mode with SimpleFileConverter") { | ||
|
||
withSftpServer { server => | ||
server.addUser("demo", "password") | ||
|
||
val offsets = new DummyOffsetStorage | ||
val configMap = Map() | ||
.updated(FtpSourceConfig.Address, s"localhost:${server.getPort}") | ||
.updated(FtpSourceConfig.protocol, "sftp") | ||
.updated(FtpSourceConfig.User, "demo") | ||
.updated(FtpSourceConfig.Password, "password") | ||
.updated(FtpSourceConfig.RefreshRate, "PT1S") | ||
.updated(FtpSourceConfig.MonitorTail, "/directory/:sftp_update_slice") | ||
.updated(FtpSourceConfig.MonitorSliceSize, "20480") | ||
.updated(FtpSourceConfig.FileMaxAge, "PT952302H53M5.962S") | ||
.updated(FtpSourceConfig.KeyStyle, "struct") | ||
.updated(FtpSourceConfig.fileFilter, ".*") | ||
|
||
val cfg = new FtpSourceConfig(configMap.asJava) | ||
|
||
val poller = new FtpSourcePoller(cfg, offsets) | ||
|
||
//push file | ||
server.putFile("/directory/file1.txt", fileContent1) | ||
|
||
val allReadBytes: Array[Byte] = sftpPollUntilEnd(poller) | ||
|
||
allReadBytes.length shouldBe fileContent1.size | ||
allReadBytes shouldBe fileContent1 | ||
() | ||
} | ||
} | ||
|
||
def sftpPollUntilEnd(poller: FtpSourcePoller): Array[Byte] = { | ||
var cnt = 0 | ||
logger.info("--------------------poll" + cnt + "-------------------------") | ||
var slice = waitForSlice(poller) | ||
var allReadBytes = new Array[Byte](0) | ||
while (slice.lengthCompare(1) == 0) { | ||
slice.head.topic shouldBe "sftp_update_slice" | ||
val bytes = slice.head.value().asInstanceOf[Array[Byte]] | ||
allReadBytes ++= bytes | ||
logger.info(s"bytes.size=${bytes.length}") | ||
cnt += 1 | ||
logger.info(s"--------------------poll$cnt-------------------------") | ||
slice = waitForSlice(poller) | ||
} | ||
allReadBytes | ||
} | ||
|
||
private def waitForSlice(poller: FtpSourcePoller): Seq[SourceRecord] = { | ||
var slice: Seq[SourceRecord] = poller.poll() | ||
eventually { | ||
Thread.sleep(100) | ||
slice = poller.poll() | ||
println(slice) | ||
slice should not be null | ||
} | ||
slice | ||
} | ||
} |
Oops, something went wrong.