Using proxy for getting remote HTTP(S) modules?

Hi. My test uses remote modules like this:

import { randomItem } from "https://jslib.k6.io/k6-utils/1.2.0/index.js";
import { htmlReport } from "https://raw.githubusercontent.com/benc-uk/k6-reporter/main/dist/bundle.js";

It works fine locally, but fails on CI with the following errors:

time="2022-04-25T12:44:36Z" level=error msg="Module specifier \"https://jslib.k6.io/k6-utils/1.2.0/index.js\" was tried to be loaded as remote module by prepending \"https://\" to it, which didn't work. If you are trying to import a nodejs module, this is not supported as k6 is _not_ nodejs based. Please read https://k6.io/docs/using-k6/modules for more information. Remote resolution error: \"Get \"https://jslib.k6.io/k6-utils/1.2.0/index.js\": dial tcp xxx.xxx.xxx.xxx:443: i/o timeout\"\n\tat go.k6.io/k6/js.(*InitContext).Require-fm (native)\n\tat file:///workspace/1/s/k6/assignees-filter-test.js:4:0(60)\n" hint="script exception"

I know, my CI node is behind the proxy, so an expected solution would be to set appropriate env variables (http(s)_proxy). I’ve tried them all (including uppercased ones), but k6 keeps failing with above error. I’am sure that my proxy configuration should work, because when I add proxy-related env-variables, wget -qO- https://jslib.k6.io/k6-utils/1.2.0/index.js starts working (but nor k6, unfortunately).

What options do I have to resolve this issue? How to teach k6 to use proxy for resolving http(s) modules?

Which k6 version are you using? Setting https_proxy=IP:port or HTTPS_PROXY=IP:port seems to work on my machine with the latest k6 version :confused: Mind you, that will also make k6 proxy all HTTP requests from the script thought the given proxy, but it should also cause it to load any remote modules though it as well…

Hi @ned. I am using the latest version of grafana/k6 container with k6 v0.37.0 (2022-03-15T09:43:26+0000/67657f4, go1.17.8, linux/amd64) inside.

The value for proxy env variables is http://some-domain:8080. As I said, wget starts working when I provide proxy settings, but k6 gives me the above error.

How exactly are you executing the docker container? If I run this:

docker run -i --rm --env https_proxy=127.0.0.1 grafana/k6 run - <<< 'import { randomItem } from "https://jslib.k6.io/k6-utils/1.2.0/index.js"; export default function() {};'

I get this error:

time="2022-04-27T05:31:07Z" level=error msg="Module specifier \"https://jslib.k6.io/k6-utils/1.2.0/index.js\" was tried to be loaded as remote module by prepending \"https://\" to it, which didn't work. If you are trying to import a nodejs module, this is not supported as k6 is _not_ nodejs based. Please read https://k6.io/docs/using-k6/modules for more information. Remote resolution error: \"Get \"https://jslib.k6.io/k6-utils/1.2.0/index.js\": proxyconnect tcp: dial tcp 127.0.0.1:80: connect: connection refused\"\n\tat go.k6.io/k6/js.(*InitContext).Require-fm (native)\n\tat file:///-:1:0(17)\n" hint="script exception"

That’s because I don’t have a convenient HTTPs proxy to test with, but it’s at least showing that k6 is trying to use the given proxy to load the import-ed file. If I remove --env https_proxy=127.0.0.1 from the command, the script runs without an error.

Hello I’m trying to unblock the same issue. I’m running a test script in a docker container on Jenkins and I didn’t quite understand your solution. It seems to run fine locally, but in Jenkins I get:

time=“2022-06-01T01:06:42Z” level=error msg=“Module specifier "[404 - resource not found](404 - resource not found)” was tried to be loaded as remote module by prepending "https://" to it, which didn’t work. If you are trying to import a nodejs module, this is not supported as k6 is not nodejs based. Please read Modules for more information. Remote resolution error: "Get "[404 - resource not found](404 - resource not found)“: dial tcp: lookup jslib.k6.io on 10.12.0.2:53: read udp 172.17.0.2:51754->10.12.0.2:53: i/o timeout"\n\tat go.k6.io/k6/js.(*InitContext).Require-fm (native)\n\tat [file:///-:3:0(37)\n](file:///-:3:0(37)/n)” hint=“script exception”

The command I’m using in my Jenkinsfile is:

                sh 'docker pull grafana/k6'
                sh 'docker run -i -rm grafana/k6 run - <schedule.js'

I saw your comment about this command working when you removed the --env stuff:

docker run -i --rm grafana/k6 run - <<< 'import { randomItem } from "https://jslib.k6.io/k6-utils/1.2.0/index.js"; export default function() {};'

I’m guessing this command makes the imported randomItem available to the default function in my test script but where do I specify the test script I want to run? Do I insert “schedule.js” into this command somewhere?

Also, here’s what I’m importing in my schedule.js file:

import { group, sleep } from 'k6'
import http from 'k6/http'
import papaparse from 'https://jslib.k6.io/papaparse/5.1.1/index.js';
import { SharedArray } from 'k6/data';
import { randomItem, randomString } from 'https://jslib.k6.io/k6-utils/1.2.0/index.js';
import { Httpx, Get } from 'https://jslib.k6.io/httpx/0.0.2/index.js';

const session = new Httpx({ baseURL: 'https://api.staging.parsable.net' });

BTW I’m LOVING k6, and I’m in the process of switching all of our load tests over from Artillery

OK I got this running in a docker-in-docker kind of set up on Jenkins.