0

How to get the list of files and folders from a path? And is it possible to apply RegEx when searching?

eSPiYa
  • 882
  • 1
  • 12
  • 29
  • What are you trying to do exactly? What is your use-case? I don't think it is possible, but this question is lacking detail to be answered properly. – knittl Jul 31 '22 at 12:16
  • @knittl as straight-forward as wanting to check a directory for certain format of filenames. Will use those files for testing out different payloads as actual scenarios for integration/business rules testing instead of just load-testing. – eSPiYa Aug 01 '22 at 02:17
  • This sounds like something that should be done outside of k6. k6 will only execute a single script file and does not have builtin support to scan your local file system. – knittl Aug 01 '22 at 21:29

2 Answers2

0

By default, you can't reach the filesystem from k6 scripts, this is because of design choice (security). If those files are js files, you can import them or bundle them with a webpack.

If you really need to read files you can write a k6 extension. For example xk6-file is k6 extension for writing to files. You can start with this.

Umut Gerçek
  • 630
  • 6
  • 9
0

The way I've solved this is:

  1. Having a list of files to read. I coded an script (python in my case) that will read the directory with the files I want to use and create a json file containing an array of files: `["file1.ext", "file2.ext", "fileN.ext"]
  2. In the test, I used k6's SharedArray for obtaining the previous list, iterate it, and create a final array with the content of all previous files.

The code looks pretty similar to this:

const data = new SharedArray('reqs', function () {
  const file = open('/requests_list.json');
  const list = JSON.parse(file);

  const requests = [];

  list.forEach(filePath => {
    requests.push(open(`/requests/${filePath}`))
  });

  return requests;
});

export default function () {
  const body = data[Math.floor(Math.random() * data.length)];
  const res = http.post(url, body, params);
  check(res, { 'status was 200': (r) => r.status == 200 });
  sleep(1);
}

Another option I thought (but haven't implemented) is to use xk6 exec extension for through bash obtain the list and process it. The benefit of this is that you don't require an external script for creating the list file.

Javier Seixas
  • 319
  • 3
  • 11