How to access/edit the exec.test.options.scenarios.default.stages[0]

Hi friends.
I am requiering to edit the STAGES in the OPTIONS as I am sending the target and duration values as an argument in the runner of my configmap, as follow:

apiVersion: k6.io/v1alpha1
kind: K6
metadata:
  name: k6-sample
  namespace: k6-operator-system
spec:
  parallelism: 2
  script:
    configMap:
      name: buyer-simulation
      file: archive.tar
  arguments: --out statsd --env K6_STATSD_ADDR=newrelic-statsd.newrelic:80 --env K6_STATSD_ENABLE_TAGS=true
  runner:
    env:
      - name: CLIENTID
        value: "client_id"
      - name: CLIENTSECRET
        value: "client_secret"
      - name: RPS
        value: "5"
      - name: DURATION
        value: "60s"

On another side, in my main.js I capture the values as follow:

const inputStages = {

    target : `${__ENV.RPS}`,
    duration : `'${__ENV.DURATION}'`

  }
stage = JSON.parse(inputStages)
export const options = {
  setupTimeout: '120s',
  scenarios: {
    post_something: {
      executor: 'ramping-arrival-rate',
      startTime: '1s',
      startRate: 1,
      timeUnit: '1s',
      stages: stage,

such solution did not work, throwing the error when I launch the command “k6 archive main.js”, which is totally neccesary to wrap or zip all the files imported on my main.js :

syntaxError: invalid character 'o' looking for beginning of value
        at parse (native)

But I think that a good approach is being able to edit the options with “exec” library imported from k6/execution

exec.test.options.scenarios.post_something.stages[0].target

But as many of you know, this is read only objetc.

¿what can I do?
PD: My purpose is to allow the developers be able to build their own target and duration, by a tool that I already have set named Rundeck, which is very useful to create basic forms and pass the values through a python script that overrides the parameters into my configmap , ready to be used by my k6 k8s operator :wink:
It’s fancy.

thank you all!

The problem here is that you are trying to JSON.parse() something that is not JSON. inputStages is already a JS object that you can directly use in the options, no need to parse it or transform it in any way.

Hi ned, thanks for answering!
Well, you are right. I can omit the JSON.parse. BTW the issue persist, as ‘k6 archive main.js’ breaks when it finds an empty assignation of env vars in
target : ${__ENV.RPS}, duration : ‘${__ENV.DURATION}’``

it’s very weird or I am not able to understand the reason hehe.
¿could you please explain to me why occurs that when k6 tries to archive the file?
Otherwise, if you have a good approach under the hat to allow the developer to define the target and duration by CLI (in my case by a configmap with k6 operator) as well as the archiving ends up completly, it would be great.

tks

The k6 archive bundle is a combination of a test script, all of its imports and dependencies, and its options. You cannot have empty or invalid options in a script .tar archive.

And while you can overwrite whatever options were present in the archive directly (with k6 run --vus X --iterations Y archive.tar), you can’t overwrite the options with --env environment variables like how you are trying to do it, sorry.

@olha, do you have some ideas on how to decouple complicated script options (e.g. scenarios) with k6 archives in the context of k6-operator? Can users pass a config.json file and run the test essentially with k6 run --config /path/to/their/custom/config.json archive.tar? :thinking:

1 Like

Thanks Ned,

Let me say that I run my python script (orchestrated by Rundeck) that overrides whatever word/number in a file, and it worked fine replacing values in archive.tar So, I won’t be using the _ENV vars anymore.

Cheers