Reputation: 643
I need to iterate over a number of files within a directory in order to perform a file upload with each of them. The directory resides in src/test/resources
.
I understand Gatling’s file feeders, but I’m not seeing any that allow me to look up files from a directory (the names of the files are arbitrary and shouldn’t be hard-codes in the test if at all possible).
What’s the best way to go about this?
Upvotes: 1
Views: 1114
Reputation: 710
First you need feeder with files. That's an Array
made of Map
s. This maps need to have String
as a key and each file will have it's own map with things we need.
Let's say we need just a name, so something like that should work:
def getFilePropsFromDir(dir: String): Array[Map[String, String]] = {
val d = new File(dir)
if (d.exists && d.isDirectory) {
d.listFiles.filter(_.isFile).map(x => Map("path" -> x.toString))
} else {
Array()
}
}
val feederWithFiles = getFilePropsFromDir("src/test/resources/dir_with_files/")
Then you need scenario like that (I don't upload anything)
val sut = scenario("Just feed files and query google")
.feed(feederWithFiles)
.exec(session => {
val path = session("path").as[String] // get values by key from map - we had only "path" there
println(path)
val fileToUpload = getFileToUpload(path) // dummy function
session.setAll(
// prepare data for using later. Also k -> v
("fileToUpload", fileToUpload),
// another entry to illustrate how to use session elements
("googleAddress","http://google.com")
)
}
)
.exec(
exec(
http("Should do something that uploads, but I just GET google")
.get("${googleAddress}") // access key "googleAddress" from session
)
)
setUp(
sut.inject(rampUsers(1).during(1.seconds))
).protocols(http)
def getFileToUpload(path: String): String = {
path
}
I created 2 files and GET was executed 2 times. Now you need to figure out how to do upload.
Imports that I have:
import io.gatling.core.Predef._
import io.gatling.core.body.StringBody
import io.gatling.core.structure.ChainBuilder
import io.gatling.http.Predef.http
import java.io.File
import scala.concurrent.duration._
import scala.io.Source
Upvotes: 2