From a23dc9a9b794fdb8ff08717c9b8bbc32c517a7ee Mon Sep 17 00:00:00 2001 From: Piotr Skamruk Date: Tue, 29 Jan 2019 11:43:34 +0100 Subject: [PATCH] Fix flake in streamer utils test --- pkg/stream/utils_test.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/pkg/stream/utils_test.go b/pkg/stream/utils_test.go index 467fe4fe6..a75204c3a 100644 --- a/pkg/stream/utils_test.go +++ b/pkg/stream/utils_test.go @@ -17,6 +17,7 @@ limitations under the License. package stream import ( + "fmt" "io/ioutil" "net" "os" @@ -66,6 +67,7 @@ func TestGetProcessEnvironment(t *testing.T) { "BAR=asd", }) defer tc.Stop() + waitForChildExec(t, tc.Pid()) env, err := getProcessEnvironment(int32(tc.Pid())) if err != nil { t.Error(err) @@ -80,3 +82,25 @@ func TestGetProcessEnvironment(t *testing.T) { } } } + +// waitForChildExec waits for a child process to perform exec() +// by means of waiting when its `/proc/PID/cmdline` becomes different from the +// cmdline of the current process +func waitForChildExec(t *testing.T, pid int) { + ownPid := os.Getpid() + ownCmdline, err := ioutil.ReadFile(fmt.Sprintf("/proc/%d/cmdline", ownPid)) + if err != nil { + t.Fatalf("cannot read own cmdline: %v", err) + } + processCmdlineLocation := fmt.Sprintf("/proc/%d/cmdline", pid) + for { + processCmdline, err := ioutil.ReadFile(processCmdlineLocation) + if err != nil { + t.Fatalf("cannot read cmdline for pid %q: %v", pid, err) + } + if string(processCmdline) != string(ownCmdline) { + break + } + time.Sleep(100 * time.Millisecond) + } +}