【问题标题】:k6 environmental variables results in GO error "invalid character"k6环境变量导致GO错误“invalid character”
【发布时间】:2021-03-26 16:00:12
【问题描述】:

k6 文档使使用环境变量变得非常简单,我尝试按照他们的说明进行操作,但是当我尝试运行它时出现 GO 错误:

ERRO[0000] GoError: parse https://${__ENV.TARGET_ENV}-api.mycompany.com/v1/managers/259999/properties": invalid character "{" in host name".

我在任何地方都没有看到额外的括号。当我的 url 为 https://green-api.mycompany.com/v1/managers/259999/properties 时,此脚本运行良好。我是否可能在某处缺少导入或依赖项?我要做的就是让它工作到当我键入 k6 run --env TARGET_ENV=green propertiesScript.js 时,它会针对http://green-api.mycompany.com/v1/managers/259999/properties 执行。这是文件:

import { check } from "k6";

export let options = {
  thresholds: {
    http_req_duration: ["p(90)<300"], // 95% of requests should be below 200ms
    Errors: ["count<100"],
  },
};

export default function () {
  var url =
    "https://${__ENV.TARGET_ENV}-api.mycompany.com/v1/managers/259999/properties";

  const params = {
    headers: {
      "X-App-Token": "<our app token>",
      "X-Auth-Token":
        "<our auth token>",
      accept: "application/json",
    },
  };

  let res = http.get(url, params);
  console.log(res.body);
  console.log(JSON.stringify(res.headers));

  check(res, {
    "status is 200": (r) => r.status === 200,
  });
}

我还尝试添加场景并调整文件的正文。这些是场景:

  thresholds: {
    http_req_duration: ["p(90)<300"], // 95% of requests should be below 200ms
    Errors: ["count<100"],
  },
  scenarios: {
    pod_green: {
      tags: { my_custom_tag: "green" },
      env: { MYVAR: "green" },
      executor: "shared-iterations",
    },
    pod_red: {
      tags: { my_custom_tag: "red" },
      env: { MYVAR: "red" },
      executor: "shared-iterations",
    },
    staging: {
      tags: { my_custom_tag: "staging" },
      env: { MYVAR: "staging" },
      executor: "shared-iterations",
    },
  },
};

然后我编辑了我的导出默认函数,我能够让该脚本运行,但它针对每个单独的环境运行。

【问题讨论】:

    标签: k6


    【解决方案1】:

    您需要在设置url 变量以使字符串插值在模板文字中起作用时使用反引号。见the documentation

    所以在你的情况下你应该有:

    var url =
        `https://${__ENV.TARGET_ENV}-api.mycompany.com/v1/managers/259999/properties`;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-22
      • 2014-06-29
      • 1970-01-01
      • 1970-01-01
      • 2016-11-04
      • 1970-01-01
      • 2023-04-02
      相关资源
      最近更新 更多