apideck-libraries/postman-to-k6

postman variables that reference broader scoped postman variables are not replaced

betzrhodes opened this issue · 10 comments

I have postman collection variables that reference postman environment variables and postman environment variables that reference postman global variables. Here is the output after running postman-to-k6 script.

postman[Symbol.for("initial")]({
  options,
  global: {
    local_auth_token: "my_local_api_auth_token",
    dev_auth_token: "my_dev_api_auth_token",
    stage_auth_token: "my_stage_api_auth_token",
    prod_auth_token: "my_prod_api_auth_token"
  },
  collection: {
    collection_name: "automated_package_details_by_uuid",
    product: "{{product_zulu}}"
  },
  environment: {
    base_url: "https://stage.api.azul.com/metadata/v1",
    auth_token: "{{stage_auth_token}}",
    product_zulu: "zulu"
  }
});

When exporting environment files from Postman variables are copied verbatim, leaving the variable references in double curly brackets. Unfortunately the postman-to-k6 script doesn't replace variables with the broader scoped variable values, so all variables that reference other variables do not end up with their expected values.

hi @betzrhodes

Are {{}} variables like "{{stage_auth_token}}" updated during the Postman execution?

No these are all unchanged during test runs. I use global variables mainly to pass auth tokens into ci pipelines and keep them out of source control. The less protected env variables are stored in postman environment json and are stored in version control.

@betzrhodes It is a bit difficult to understand the setup you are using, would you mind sharing your Postman files & Postman ENV files and the postman-to-k6 command you are using?

This will allow me to reproduce and see for options to solve this.

postman json exported files can be found here:
https://gist.github.com/betzrhodes/97b3fef6afb046254ad11e18f7629a6d

Here is a screenshot of the project directory
Screen Shot 2022-06-28 at 10 40 57 AM

This is the command I'm running from the postman-k6 directory
npx @apideck/postman-to-k6 automated_package_details_by_uuid.postman_collection.json --environment stage.postman_environment.json --global metadata_api.postman_globals.json -s -o k6-script.js

One more thing to note, when running locally with -s option, all files in the requests directory do not end up with the correct import relative path. I've been adjusting import statements after the running the script. Looks to be the same issue reported here

@betzrhodes The -s issue and import relative path is fixed (but not yet released).

I want to tackle your bug before releasing a new version.

I've reproduced the situation with your collection, env & global vars.
I struggle to understand what is the expected outcome and what goes wrong with the variables & the scopes?

Could you explain with an example what would be the desired variables?

Does this describe it?

CURRENTLY:

postman[Symbol.for("initial")]({
  options,
  global: {
    local_auth_token: "my_local_api_auth_token",
    dev_auth_token: "my_dev_api_auth_token",
    stage_auth_token: "my_stage_api_auth_token",
    prod_auth_token: "my_prod_api_auth_token"
  },
  collection: {
    collection_name: "automated_package_details_by_uuid",
    product: "{{product_zulu}}"
  },
  environment: {
    base_url: "https://stage.api.azul.com/metadata/v1",
    auth_token: "{{stage_auth_token}}",
    product_zulu: "zulu"
  }
});

EXPECTED

postman[Symbol.for("initial")]({
  options,
  global: {
    local_auth_token: "my_local_api_auth_token",
    dev_auth_token: "my_dev_api_auth_token",
    stage_auth_token: "my_stage_api_auth_token",
    prod_auth_token: "my_prod_api_auth_token"
  },
  collection: {
    collection_name: "automated_package_details_by_uuid",
    product: "zulu"
  },
  environment: {
    base_url: "https://stage.api.azul.com/metadata/v1",
    auth_token: "my_stage_api_auth_token",
    product_zulu: "zulu"
  }
});

Yes that is exactly the issue.

@betzrhodes I have a PR ready, just need to figure out a way to add additional tests.

The enhancement will be part of the next postman-to-k6 release.

Closing the issue, since the latest postman-to-k6 version (1.8.5) contains this enhancement.