From 1b3e0c664332274bdc20aa51cbd73cb78f7b126c Mon Sep 17 00:00:00 2001 From: Lomanic Date: Sun, 30 Aug 2020 01:31:13 +0200 Subject: [PATCH] [linux] Fix #900, skip or fix failing tests in docker TestGetProcInodesAll: create a server so there are some opened inodes TestUsers: skip if Users is empty, because of an empty /var/run/utmp Test_Process_Groups: skip if Groups is empty TestConnectionsMax: skip on CI, not only CircleCI --- host/host_test.go | 2 +- net/net_linux_test.go | 27 +++++++++++++++++++++++---- process/process_test.go | 5 ++++- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/host/host_test.go b/host/host_test.go index b4db399..759f24e 100644 --- a/host/host_test.go +++ b/host/host_test.go @@ -67,7 +67,7 @@ func TestUsers(t *testing.T) { } empty := UserStat{} if len(v) == 0 { - t.Fatal("Users is empty") + t.Skip("Users is empty") } for _, u := range v { if u == empty { diff --git a/net/net_linux_test.go b/net/net_linux_test.go index cf19fcd..f5c2a11 100644 --- a/net/net_linux_test.go +++ b/net/net_linux_test.go @@ -3,6 +3,7 @@ package net import ( "fmt" "io/ioutil" + "net" "os" "strings" "syscall" @@ -77,9 +78,27 @@ func TestIOCountersByFileParsing(t *testing.T) { } func TestGetProcInodesAll(t *testing.T) { - if os.Getenv("CIRCLECI") == "true" { - t.Skip("Skip CI") - } + waitForServer := make(chan bool) + go func() { // TCP listening goroutine to have some opened inodes even in CI + addr, err := net.ResolveTCPAddr("tcp", "localhost:0") // dynamically get a random open port from OS + if err != nil { + t.Skip("unable to resolve localhost:", err) + } + l, err := net.ListenTCP(addr.Network(), addr) + if err != nil { + t.Skip(fmt.Sprintf("unable to listen on %v: %v", addr, err)) + } + defer l.Close() + waitForServer <- true + for { + conn, err := l.Accept() + if err != nil { + t.Skip("unable to accept connection:", err) + } + defer conn.Close() + } + }() + <-waitForServer root := common.HostProc("") v, err := getProcInodesAll(root, 0) @@ -88,7 +107,7 @@ func TestGetProcInodesAll(t *testing.T) { } func TestConnectionsMax(t *testing.T) { - if os.Getenv("CIRCLECI") == "true" { + if os.Getenv("CI") != "" { t.Skip("Skip CI") } diff --git a/process/process_test.go b/process/process_test.go index 60b134c..e7a3b2b 100644 --- a/process/process_test.go +++ b/process/process_test.go @@ -253,7 +253,10 @@ func Test_Process_Groups(t *testing.T) { if err != nil { t.Errorf("getting groups error %v", err) } - if len(v) <= 0 || v[0] < 0 { + if len(v) == 0 { + t.Skip("Groups is empty") + } + if v[0] < 0 { t.Errorf("invalid Groups: %v", v) } }