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

Allow listing of files in the init context #1005

Open
na-- opened this issue Apr 22, 2019 · 1 comment
Open

Allow listing of files in the init context #1005

na-- opened this issue Apr 22, 2019 · 1 comment
Labels
evaluation needed proposal needs to be validated or tested before fully implementing it in k6 feature ux

Comments

@na--
Copy link
Member

na-- commented Apr 22, 2019

Currently, users can open() files if they know their filenames, but they can't traverse the filesystem and open files dynamically. A few functions to navigate around the filesystem would help with the UX of writing file-heavy tests, as described in this forum post.

This should probably happen after we have a reasonable way to not read the contents of every file in every VU, as open() currently does. So, as a quick fix, having the __VU variable available in the init context would do, but longer-term, something like #592 and #532 would be ideal.

And an open question is whether we should try to emulate even parts of the fs node API, or if we should do something much simpler and narrower in scope. For now I'm definitely leaning toward the latter option...

Current workarounds:

Describe desired files in a .json file:

If I have a testfile.json with an array of file names (or something more complicated with additional metadata), I can just open it, and then iterate over its contents to open all of the other files:

let fileContents = [];
let filePaths = JSON.parse(open("filenames.json"));

filePaths.forEach(fileName => fileContents.push({
    name: fileName,
    contents: open(fileName, "b"),
}));


export default function () {
    // Do something with fileContents
    // ...
}

Pass a list of file as an environment variable

You could run k6 run --env filelist="file1;file2;file3" script.js and then use __ENV.filelist in the k6 script's init context to open all of the supplied files. Of course, the filelist value could also be dynamically generated (e.g. --env filelist="$(find ./some/folder -type f -print0 | tr '\0' ';')"), as long as we pay attention that the filenames don't contain the chosen separator. And, for the moment, that the separator isn't a comma 🪲 ...

Load the files from an HTTP server

As the forum post suggested, the files could be located on a simple HTTP server. This way they can be retrieved in the setup() function or in the future per-VU init function.

@na-- na-- added ux feature evaluation needed proposal needs to be validated or tested before fully implementing it in k6 labels Apr 22, 2019
@oleiade
Copy link
Member

oleiade commented Dec 4, 2023

ref #2977
ref #3141
ref #557

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
evaluation needed proposal needs to be validated or tested before fully implementing it in k6 feature ux
Projects
None yet
Development

No branches or pull requests

2 participants