File: process_testing.go

package info (click to toggle)
mender-client 3.4.0%2Bds1-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 2,084 kB
  • sloc: sh: 1,134; makefile: 277; python: 172; ansic: 125; xml: 27
file content (78 lines) | stat: -rw-r--r-- 2,462 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright 2022 Northern.tech AS
//
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
//
//        http://www.apache.org/licenses/LICENSE-2.0
//
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.
package testing

import (
	"fmt"
	"os"
	"os/exec"
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

// This won't run automatically because of the filename of this file, but it is
// exported and tests from other packages are meant to call it directly.
func TestZombieProcessLeaking(t *testing.T) {
	// Test that we don't leave behind any zombies after a test run.

	// Perform a sanity check first. Make a zombie process so that we can
	// verify that our detection works.
	cmd := exec.Command("true")
	require.NoError(t, cmd.Start())
	require.Eventually(t, func() bool {
		_, _, result := zombieProcessesPresent()
		return result
	},
		10*time.Second,
		100*time.Millisecond,
		"Zombie detection doesn't work. Perhaps ps works differently on this platform?",
	)
	require.NoError(t, cmd.Wait())

	// This is not primarily to enable parallalism, but to move the test to
	// the end of the run, since Go executes all parallel tests at the
	// end. This doesn't guarantee that it's the last one though, since we
	// can execute at the same time as the others.
	t.Parallel()

	var output string
	var err error
	// Use Eventually(), since we aren't guaranteed to be the last test.
	assert.Eventuallyf(t, func() bool {
		var result bool
		output, err, result = zombieProcessesPresent()
		return !result
	},
		60*time.Second,
		1*time.Second,
		"Zombie processes are still present! Missing Wait()?",
	)
	if t.Failed() {
		t.Logf("ps output: %s, error: %v", output, err)
	}
}

func zombieProcessesPresent() (string, error, bool) {
	cmdStr := fmt.Sprintf("ps -ef | grep %d | grep '<defunct>' | grep -v grep", os.Getpid())
	cmd := exec.Command("/bin/sh", "-c", cmdStr)
	output, err := cmd.Output()
	if err == nil {
		return string(output), err, true
	} else {
		return string(output), err, false
	}
}