summaryrefslogtreecommitdiff
path: root/daemon/top_unix_test.go
blob: a663323b675491a7e0a1367cf2c08d607198c356 (plain)
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//go:build !windows
// +build !windows

package daemon // import "github.com/docker/docker/daemon"

import (
	"testing"

	"gotest.tools/v3/assert"
)

func TestContainerTopValidatePSArgs(t *testing.T) {
	tests := map[string]bool{
		"ae -o uid=PID":             true,
		"ae -o \"uid= PID\"":        true,  // ascii space (0x20)
		"ae -o \"uid= PID\"":        false, // unicode space (U+2003, 0xe2 0x80 0x83)
		"ae o uid=PID":              true,
		"aeo uid=PID":               true,
		"ae -O uid=PID":             true,
		"ae -o pid=PID2 -o uid=PID": true,
		"ae -o pid=PID":             false,
		"ae -o pid=PID -o uid=PIDX": true, // FIXME: we do not need to prohibit this
		"aeo pid=PID":               false,
		"ae":                        false,
		"":                          false,
	}
	for psArgs, errExpected := range tests {
		t.Run(psArgs, func(t *testing.T) {
			err := validatePSArgs(psArgs)
			if errExpected {
				assert.ErrorContains(t, err, "", "psArgs: %q", psArgs)
			} else {
				assert.NilError(t, err, "psArgs: %q", psArgs)
			}
		})
	}
}

func TestContainerTopParsePSOutput(t *testing.T) {
	tests := []struct {
		output      []byte
		pids        []uint32
		errExpected bool
	}{
		{
			output: []byte(`  PID COMMAND
   42 foo
   43 bar
		- -
  100 baz
`),
			pids:        []uint32{42, 43},
			errExpected: false,
		},
		{
			output: []byte(`  UID COMMAND
   42 foo
   43 bar
		- -
  100 baz
`),
			pids:        []uint32{42, 43},
			errExpected: true,
		},
		// unicode space (U+2003, 0xe2 0x80 0x83)
		{
			output: []byte(` PID COMMAND
   42 foo
   43 bar
		- -
  100 baz
`),
			pids:        []uint32{42, 43},
			errExpected: true,
		},
		// the first space is U+2003, the second one is ascii.
		{
			output: []byte(` PID COMMAND
   42 foo
   43 bar
  100 baz
`),
			pids:        []uint32{42, 43},
			errExpected: true,
		},
	}

	for _, tc := range tests {
		tc := tc
		t.Run(string(tc.output), func(t *testing.T) {
			_, err := parsePSOutput(tc.output, tc.pids)
			if tc.errExpected && err == nil {
				t.Fatalf("expected error, got %v (%q)", err, string(tc.output))
			}
			if !tc.errExpected && err != nil {
				t.Fatalf("expected nil, got %v (%q)", err, string(tc.output))
			}
		})
	}
}