Repository: cilium/ebpf
Pull request #82 (2020-03-19 10:15:11)

Fix test build on non-linux
Use wrappers from github.com/cilium/ebpf/internal/unix to avoid the
following build error when building the tests on non-linux:

```
./prog_test.go:131:14: undefined: "golang.org/x/sys/unix".Gettid
./prog_test.go:148:10: undefined: "golang.org/x/sys/unix".Tgkill
```

Signed-off-by: Tobias Klauser <tklauser@distanz.ch>

tklauser: FWIW, the tests won't pass on non-linux but it's still nice to be able to build them.
lmb: Strange, I thought that https://github.com/cilium/ebpf/blob/master/.semaphore/semaphore.yml#L25-L26 should catch issues like these. What OS are you on?
tklauser: > Strange, I thought that https://github.com/cilium/ebpf/blob/master/.semaphore/semaphore.yml#L25-L26 should catch issues like these. What OS are you on?

This was done on macOS, using `go test -c`. It seems the semaphore CI job only does `go build`. Will add an additional commit to this PR updating `.semaphore.yml` to also to `go test -c` on non-linux.
tklauser: > > Strange, I thought that https://github.com/cilium/ebpf/blob/master/.semaphore/semaphore.yml#L25-L26 should catch issues like these. What OS are you on?
> 
> This was done on macOS, using `go test -c`. It seems the semaphore CI job only does `go build`. Will add an additional commit to this PR updating `.semaphore.yml` to also to `go test -c` on non-linux.

Seems like it isn't easily possible to run all tests of the repo using `go test -c ./...`, see golang/go#15513 The suggested workaround is to use `go test -run xxxxx ./...` which will achieve the same. However, this leads to some tests still being run when on linux :confused: 

```
% GOOS=darwin go build ./... && GOOS=darwin go test -run xxxxxx ./...
# github.com/cilium/ebpf/perf [github.com/cilium/ebpf/perf.test]
perf/reader_test.go:25:8: undefined: unix.RLIM_INFINITY
perf/reader_test.go:26:8: undefined: unix.RLIM_INFINITY
fork/exec /tmp/go-build228086715/b001/ebpf.test: exec format error
FAIL	github.com/cilium/ebpf	0.003s
fork/exec /tmp/go-build228086715/b073/asm.test: exec format error
FAIL	github.com/cilium/ebpf/asm	0.009s
fork/exec /tmp/go-build228086715/b077/internal.test: exec format error
FAIL	github.com/cilium/ebpf/internal	0.005s
fork/exec /tmp/go-build228086715/b080/btf.test: exec format error
FAIL	github.com/cilium/ebpf/internal/btf	0.003s
?   	github.com/cilium/ebpf/internal/testutils	[no test files]
?   	github.com/cilium/ebpf/internal/unix	[no test files]
FAIL	github.com/cilium/ebpf/perf [build failed]
FAIL
```
```
tklauser: `for p in go list ./... ; do GOOS=darwin go test -c $p ; done` seems to work, added in 
668e6f5 

Patch:
diff --git a/.semaphore/semaphore.yml b/.semaphore/semaphore.yml
index 9eb3746..fc4d8f3 100644
--- a/.semaphore/semaphore.yml
+++ b/.semaphore/semaphore.yml
@@ -22,9 +22,9 @@ blocks:
       jobs:
       - name: Test building on other OS and arch
         commands:
-          - GOOS=darwin go build ./...
-          - GOARCH=arm GOARM=6 go build ./...
-          - GOARCH=arm64 go build ./...
+          - GOOS=darwin go build ./... && for p in $(go list ./...) ; do GOOS=darwin go test -c $p ; done
+          - GOARCH=arm GOARM=6 go build ./... && for p in $(go list ./...) ; do GOARCH=arm GOARM=6 go test -c $p ; done
+          - GOARCH=arm64 go build ./... && for p in $(go list ./...) ; do GOARCH=arm64 go test -c $p ; done
       - name: Run unit tests
         matrix:
           - env_var: GO_VERSION
diff --git a/internal/unix/types_other.go b/internal/unix/types_other.go
index d85fdcf..7961b35 100644
--- a/internal/unix/types_other.go
+++ b/internal/unix/types_other.go
@@ -37,6 +37,7 @@ const (
 	PerfBitWatermark         = 0x4000
 	PERF_SAMPLE_RAW          = 0x400
 	PERF_FLAG_FD_CLOEXEC     = 0x8
+	RLIM_INFINITY            = 0x7fffffffffffffff
 )
 
 // Statfs_t is a wrapper


Test patch:
diff --git a/prog_test.go b/prog_test.go
index faef237..463d0f5 100644
--- a/prog_test.go
+++ b/prog_test.go
@@ -16,7 +16,7 @@ import (
 	"github.com/cilium/ebpf/asm"
 	"github.com/cilium/ebpf/internal"
 	"github.com/cilium/ebpf/internal/testutils"
-	"golang.org/x/sys/unix"
+	"github.com/cilium/ebpf/internal/unix"
 	"golang.org/x/xerrors"
 )