问题描述
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"],scenarios: {
pod_green: {
tags: { my_custom_tag: "green" },env: { MYVAR: "green" },executor: "shared-iterations",pod_red: {
tags: { my_custom_tag: "red" },env: { MYVAR: "red" },staging: {
tags: { my_custom_tag: "staging" },env: { MYVAR: "staging" },};
然后我编辑了我的导出默认函数,我能够运行该脚本,但它可以在每个环境中运行。
解决方法
当您为字符串插值设置 url
变量以在模板文字中工作时,您需要使用反引号。见the documentation。
所以在你的情况下你应该有:
var url =
`https://${__ENV.TARGET_ENV}-api.mycompany.com/v1/managers/259999/properties`;