在 week2 的 meeting 中。Bartek 展示了如何使用 e2e test 进行端到端测试。通过在测试中加 time.Sleep(10 * time.Minute)来在某个映射端口进行测试。但是使用 WSL + GoLand 进行 setup 的过程中发现了很多问题,有关 WSL2 的资料又不是很多,走了很多弯路推到重来,所以在这里做一下记录。以下以 Thanos 的 Docker 以及本地运行开发为例。

Go 环境配置

配置共享目录

为了方便地在 Windows 和 WSL 之间拷贝文件,以及防止之后运行中出现权限问题,先设置共享目录。在 Windows Terminal 中输入 wsl,进入 WSL 系统,可以看到当前的目录为 mnt/c/ ,这里就是 WSL 对应于 windows 下的 [C:\](https://thanos.io/tip/contributing/community.md/) 的挂载分区。

  1. 在 Windows 中新建文件夹 public,我们之后将把 public 作为我们的 GOPATH。
  2. 在 WSL 中添加软连接。ln -s /mnt/c/public ~/public

WSL 上安装 Go

  1. wsl 安装:这个 gist 可以参考

    除此之外我还根据 Thanos 的 contributing.md 设置了 GOBIN GOPATH GOPROXY

    1
    2
    3
    4
    export GOBIN="$HOME/public/Repos/thanos/.bin"
    export GOPATH="$HOME/public/Repos/thanosgopath"
    export GOPROXY="https://proxy.golang.org"
    export PATH="$GOBIN:$PATH"
  2. windows 端安装 Go (不确定这个是否必须,但我之前就装了)

GOPATH 配置

  1. Windows:在环境变量配置中将用户变量中的 GOPATH 改为 C:\public\Repos\thanosgopath
  2. WSL:GOPATH="$HOME/public/Repos/thanosgopath"

项目下载和 WSL 中 build 运行

  1. 参考 Thanos 的 doc

GoLand 的安装和配置

参考:

安装 GoLand

  • 由于我们这里使用 WSL2,为了使用 GoLand 的 Run Target 的新特性,需要安装 GoLand 2021.1 Beta 及以上的版本。

打开 Windows 路径下的项目

在控制变量之后发现,当打开的是 WSL 目录下的项目,之后编译可能会出现无法操作 \\wsl$ 目录的问题。

配置 GO 设置

  • File - Setting - Go
    • GOROOT :添加 \\wsl$\Ubuntu\usr\local\go WSL 的 GOROOT。
    • GOPATH :选中 Use GOPATH that's defined in system environment ,这将使用 Windows 系统设置中的变量。

添加 Target:

  • Run - Manage Targets - Add Target On - WSL
  • Add language runtime。
    • Go Executable : WSL 中 Go 的可执行文件的路径,为了找到这个路径,在 WSL 中使用 whereis go ,可以看到
      • go: /usr/local/go /mnt/c/Program Files/Go/bin/go.exe /usr/local/go/bin/go
      • 第一个是 GOROOT, 第二个是 Windows 所在可执行文件的位置,第三个是 WSL 可执行文件的位置,/usr/local/go/bin/go 是要填在这里的。
    • GOPATH:使用 go env 进行查询填入。
    • Version:可以缺省

Run/Debug Configurations - Edit Configurations

  • Add - Go Build

    • Run on: 之前的 target
    • check:Build on remote target

耐心等待

Thanos 依赖较多,GoLand 会自动识别到 go.mod 文件,下载和 indexing 的时间都比较久,在下载依赖和和 indexing 的过程中,可能会出现 cannot find package 的问题,这并不是配置问题,只是需要等待。完成下载和 indexing 之后,可以看到项目的 Go Modules。

End to End Test

GoLand 会运行识别到可独立运行的 test 函数和文件,点击在行号旁边的小箭头运行 test。要注意 run configuration 中的配置,默认的配置是 Run on local ,也没有 check build on remote target。

TestRulesAPI_Fanout 为例,会在 Run 中看到如下提示。如果 GOROOT 出现了其他的设置,或者可执行文件使用的是 go.exe,都需要检查是配置是否出现了问题。

1
2
3
4
5
6
GOROOT=null #gosetup
GOPATH=/mnt/c/public/Repos/thanosgopath #gosetup
/usr/local/go/bin/go test -c -o /mnt/c/Userst ....
/usr/local/go/bin/go tool test2json -t /mnt/c/Users ...
=== RUN TestRulesAPI_Fanout
=== PAUSE TestRulesAPI_Fanout

Docker + WSL2

为了之后开发的方便,希望能够使用 docker Thanos。此处以make dockermake e2e-test 执行成功为例,来说一些碰到的的问题以及可行的解决方法。 WSL2 上的 docker 使用也会遇到一些之前没有碰到过的问题。

Docker 安装流程:

  1. https://zhuanlan.zhihu.com/p/143156163

出现的问题

apt-get update failed

参考: https://stackoverflow.com/questions/60269422/windows10-wsl2-ubuntu-debian-apt-get-update-failed-no-network

  • sudo unlink /etc/resolv.conf
  • 新建一个 /etc/resolv.conf
  • 写入 10.8.0.1 (使用了 vpn,常规的 8.8.8.8 也无法解决问题)

docker pull 找不到 Thanos:latest 的 image

  • 去 dockerhub 找到最新的 Thanos image,复制 pull command docker pull thanosio/thanos:main-2021-06-23-d8a794ba

无法启动 docker

报错信息:Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

  • 在一个 terminal 运行 sudo dockered ,这是对我来说在 WSL2 上唯一有用的方法。
  • 新开一个 terminal 进行想要做的操作。

make - Permission denied error 127

在 make 的过程中

  • 权限问题: 尝试 sudo 或者 chmod 777 -R finename
  • 其他:定位到报错的行,查看是否是有些文件是否真的存在,有可能是没有正确安装或者环境变量配置错误。