尝试 k6 运行 一个 js 文件时获得权限被拒绝

get permission denied when trying to k6 run a js file

我正在尝试学习 k6,我从 its official documents. I tried the sample code in run document 开始,同时使用 docker 图像和命令行。在 docker 版本中它工作正常:

zeinab@ZiZi:~$ docker run -i loadimpact/k6 run - <k6_test.js

      /\      |‾‾|  /‾‾/  /‾/   
     /\  /  \     |  |_/  /  / /    
    /  \/    \    |      |  /  ‾‾\  
   /          \   |  |‾\  \ | (_) | 
  / __________ \  |__|  \__\ \___/ .io

  execution: local--------------------------------------------------]   servertor
     output: -
     script: -

    duration: -, iterations: 1
     vus: 1, max: 1

time="2020-02-18T11:14:26Z" level=info msg=Running i=0 t=971.124809ms starting
...
time="2020-02-18T11:14:55Z" level=info msg=Running i=0 t=29.971104099s
time="2020-02-18T11:14:55Z" level=warning msg="Request Failed" error="Get http://test.loadimpact.com: proxyconnect tcp: dial tcp 192.168.14.109:1087: i/o timeout"
time="2020-02-18T11:14:56Z" level=info msg=Running i=0 t=30.971060995s
time="2020-02-18T11:14:56Z" level=info msg="Test finished" i=1 t=31.000143344s

    data_received..............: 0 B 0 B/s
    data_sent..................: 0 B 0 B/s
    http_req_blocked...........: avg=0s  min=0s  med=0s  max=0s  p(90)=0s  p(95)=0s 
    http_req_connecting........: avg=0s  min=0s  med=0s  max=0s  p(90)=0s  p(95)=0s 
    http_req_duration..........: avg=0s  min=0s  med=0s  max=0s  p(90)=0s  p(95)=0s 
    http_req_receiving.........: avg=0s  min=0s  med=0s  max=0s  p(90)=0s  p(95)=0s 
    http_req_sending...........: avg=0s  min=0s  med=0s  max=0s  p(90)=0s  p(95)=0s 
    http_req_tls_handshaking...: avg=0s  min=0s  med=0s  max=0s  p(90)=0s  p(95)=0s 
    http_req_waiting...........: avg=0s  min=0s  med=0s  max=0s  p(90)=0s  p(95)=0s 
    http_reqs..................: 1   0.032258/s
    iteration_duration.........: avg=31s min=31s med=31s max=31s p(90)=31s p(95)=31s
    iterations.................: 1   0.032258/s
    vus........................: 1   min=1 max=1
    vus_max....................: 1   min=1 max=1

但是当从命令行 运行 时,我得到:

zeinab@ZiZi:~$ sudo k6 run ./k6_test.js

          /\      |‾‾|  /‾‾/  /‾/   
     /\  /  \     |  |_/  /  / /    
    /  \/    \    |      |  /  ‾‾\  
   /          \   |  |‾\  \ | (_) | 
  / __________ \  |__|  \__\ \___/ .io

ERRO[0000] open /home/zeinab/k6_test.js: permission denied 

编辑 1:

当 运行 当前用户:

时我得到同样的错误
k6 run ./k6_test.js

编辑 2:

我安装了 k6 使用 snap:

zeinab@ZiZi:~$ snap install k6 
k6 v0.23.1 from Null Box (nullboxorg) installed

鉴于您使用 snap 来安装它,这意味着来自 snap 内部的 k6 也无法访问您的整个 fs(就像 docker 没有)。

我不太了解 snap,但考虑到为 snap hasn't updated it in the last year I would recommend just getting the binary 和 运行 构建 k6 的人;)。

原回答:

正如上面所说:您的 root 用户无法打开 /home/zeinab/k6_test.js,因为他们没有权限。我猜他们要么无法读取您的主目录 (/home/zeinab),要么无法读取文件本身。

你真的不需要使用 sudo 所以要么删除它要么修复脚本的权限,尽管再次 运行 root 不是必需的并且无论哪种方式都是不好的做法:D

作为旁注,docker 起作用是因为无论 shell(可能 bash)你 运行 是读取文件并将其发送到标准输入的人docker 过程。因此,即使 dockersudo docker 的别名,它仍然有效。